blob: 4ceed6e0f1bebf983299a9304d54213101b55b0f [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
142static int debug_level;
143module_param(debug_level, int, 0);
144MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
145
146static int ale_ageout = 10;
147module_param(ale_ageout, int, 0);
148MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
149
150static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
151module_param(rx_packet_max, int, 0);
152MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
153
Richard Cochran996a5c22012-10-29 08:45:12 +0000154struct cpsw_wr_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000155 u32 id_ver;
156 u32 soft_reset;
157 u32 control;
158 u32 int_control;
159 u32 rx_thresh_en;
160 u32 rx_en;
161 u32 tx_en;
162 u32 misc_en;
163};
164
Richard Cochran996a5c22012-10-29 08:45:12 +0000165struct cpsw_ss_regs {
Mugunthan V Ndf828592012-03-18 20:17:54 +0000166 u32 id_ver;
167 u32 control;
168 u32 soft_reset;
169 u32 stat_port_en;
170 u32 ptype;
Richard Cochranbd357af2012-10-29 08:45:13 +0000171 u32 soft_idle;
172 u32 thru_rate;
173 u32 gap_thresh;
174 u32 tx_start_wds;
175 u32 flow_control;
176 u32 vlan_ltype;
177 u32 ts_ltype;
178 u32 dlr_ltype;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000179};
180
Richard Cochran9750a3a2012-10-29 08:45:15 +0000181/* CPSW_PORT_V1 */
182#define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
183#define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
184#define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
185#define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
186#define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
187#define CPSW1_TS_CTL 0x14 /* Time Sync Control */
188#define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
189#define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
190
191/* CPSW_PORT_V2 */
192#define CPSW2_CONTROL 0x00 /* Control Register */
193#define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
194#define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
195#define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
196#define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
197#define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
198#define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
199
200/* CPSW_PORT_V1 and V2 */
201#define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
202#define SA_HI 0x24 /* CPGMAC_SL Source Address High */
203#define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
204
205/* CPSW_PORT_V2 only */
206#define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
207#define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
208#define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
209#define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
210#define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
211#define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
212#define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
213#define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
214
215/* Bit definitions for the CPSW2_CONTROL register */
216#define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
217#define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
218#define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
219#define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
220#define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
221#define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
222#define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
223#define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
224#define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
225#define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
226#define TS_BIT8 (1<<8) /* ts_ttl_nonzero? */
227#define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
228#define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
229#define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
230#define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
231#define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
232
233#define CTRL_TS_BITS \
234 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 | TS_BIT8 | \
235 TS_ANNEX_D_EN | TS_LTYPE1_EN)
236
237#define CTRL_ALL_TS_MASK (CTRL_TS_BITS | TS_TX_EN | TS_RX_EN)
238#define CTRL_TX_TS_BITS (CTRL_TS_BITS | TS_TX_EN)
239#define CTRL_RX_TS_BITS (CTRL_TS_BITS | TS_RX_EN)
240
241/* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
242#define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
243#define TS_SEQ_ID_OFFSET_MASK (0x3f)
244#define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
245#define TS_MSG_TYPE_EN_MASK (0xffff)
246
247/* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
248#define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
Mugunthan V Ndf828592012-03-18 20:17:54 +0000249
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000250/* Bit definitions for the CPSW1_TS_CTL register */
251#define CPSW_V1_TS_RX_EN BIT(0)
252#define CPSW_V1_TS_TX_EN BIT(4)
253#define CPSW_V1_MSG_TYPE_OFS 16
254
255/* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
256#define CPSW_V1_SEQ_ID_OFS_SHIFT 16
257
Mugunthan V Ndf828592012-03-18 20:17:54 +0000258struct cpsw_host_regs {
259 u32 max_blks;
260 u32 blk_cnt;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000261 u32 tx_in_ctl;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000262 u32 port_vlan;
263 u32 tx_pri_map;
264 u32 cpdma_tx_pri_map;
265 u32 cpdma_rx_chan_map;
266};
267
268struct cpsw_sliver_regs {
269 u32 id_ver;
270 u32 mac_control;
271 u32 mac_status;
272 u32 soft_reset;
273 u32 rx_maxlen;
274 u32 __reserved_0;
275 u32 rx_pause;
276 u32 tx_pause;
277 u32 __reserved_1;
278 u32 rx_pri_map;
279};
280
281struct cpsw_slave {
Richard Cochran9750a3a2012-10-29 08:45:15 +0000282 void __iomem *regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000283 struct cpsw_sliver_regs __iomem *sliver;
284 int slave_num;
285 u32 mac_control;
286 struct cpsw_slave_data *data;
287 struct phy_device *phy;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000288 struct net_device *ndev;
289 u32 port_vlan;
290 u32 open_stat;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000291};
292
Richard Cochran9750a3a2012-10-29 08:45:15 +0000293static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
294{
295 return __raw_readl(slave->regs + offset);
296}
297
298static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
299{
300 __raw_writel(val, slave->regs + offset);
301}
302
Mugunthan V Ndf828592012-03-18 20:17:54 +0000303struct cpsw_priv {
304 spinlock_t lock;
305 struct platform_device *pdev;
306 struct net_device *ndev;
307 struct resource *cpsw_res;
Richard Cochrana65dd5b2012-11-02 22:25:29 +0000308 struct resource *cpsw_wr_res;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000309 struct napi_struct napi;
310 struct device *dev;
311 struct cpsw_platform_data data;
Richard Cochran996a5c22012-10-29 08:45:12 +0000312 struct cpsw_ss_regs __iomem *regs;
313 struct cpsw_wr_regs __iomem *wr_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000314 struct cpsw_host_regs __iomem *host_port_regs;
315 u32 msg_enable;
Richard Cochrane90cfac2012-10-29 08:45:14 +0000316 u32 version;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000317 struct net_device_stats stats;
318 int rx_packet_max;
319 int host_port;
320 struct clk *clk;
321 u8 mac_addr[ETH_ALEN];
322 struct cpsw_slave *slaves;
323 struct cpdma_ctlr *dma;
324 struct cpdma_chan *txch, *rxch;
325 struct cpsw_ale *ale;
326 /* snapshot of IRQ numbers */
327 u32 irqs_table[4];
328 u32 num_irqs;
Mugunthan V N9232b162013-02-11 09:52:19 +0000329 struct cpts *cpts;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000330 u32 emac_port;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000331};
332
333#define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000334#define for_each_slave(priv, func, arg...) \
335 do { \
336 int idx; \
337 if (priv->data.dual_emac) \
338 (func)((priv)->slaves + priv->emac_port, ##arg);\
339 else \
340 for (idx = 0; idx < (priv)->data.slaves; idx++) \
341 (func)((priv)->slaves + idx, ##arg); \
Mugunthan V Ndf828592012-03-18 20:17:54 +0000342 } while (0)
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000343#define cpsw_get_slave_ndev(priv, __slave_no__) \
344 (priv->slaves[__slave_no__].ndev)
345#define cpsw_get_slave_priv(priv, __slave_no__) \
346 ((priv->slaves[__slave_no__].ndev) ? \
347 netdev_priv(priv->slaves[__slave_no__].ndev) : NULL) \
348
349#define cpsw_dual_emac_src_port_detect(status, priv, ndev, skb) \
350 do { \
351 if (!priv->data.dual_emac) \
352 break; \
353 if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
354 ndev = cpsw_get_slave_ndev(priv, 0); \
355 priv = netdev_priv(ndev); \
356 skb->dev = ndev; \
357 } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
358 ndev = cpsw_get_slave_ndev(priv, 1); \
359 priv = netdev_priv(ndev); \
360 skb->dev = ndev; \
361 } \
362 } while (0)
363#define cpsw_add_mcast(priv, addr) \
364 do { \
365 if (priv->data.dual_emac) { \
366 struct cpsw_slave *slave = priv->slaves + \
367 priv->emac_port; \
368 int slave_port = cpsw_get_slave_port(priv, \
369 slave->slave_num); \
370 cpsw_ale_add_mcast(priv->ale, addr, \
371 1 << slave_port | 1 << priv->host_port, \
372 ALE_VLAN, slave->port_vlan, 0); \
373 } else { \
374 cpsw_ale_add_mcast(priv->ale, addr, \
375 ALE_ALL_PORTS << priv->host_port, \
376 0, 0, 0); \
377 } \
378 } while (0)
379
380static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
381{
382 if (priv->host_port == 0)
383 return slave_num + 1;
384 else
385 return slave_num;
386}
Mugunthan V Ndf828592012-03-18 20:17:54 +0000387
Mugunthan V N5c50a852012-10-29 08:45:11 +0000388static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
389{
390 struct cpsw_priv *priv = netdev_priv(ndev);
391
392 if (ndev->flags & IFF_PROMISC) {
393 /* Enable promiscuous mode */
394 dev_err(priv->dev, "Ignoring Promiscuous mode\n");
395 return;
396 }
397
398 /* Clear all mcast from ALE */
399 cpsw_ale_flush_multicast(priv->ale, ALE_ALL_PORTS << priv->host_port);
400
401 if (!netdev_mc_empty(ndev)) {
402 struct netdev_hw_addr *ha;
403
404 /* program multicast address list into ALE register */
405 netdev_for_each_mc_addr(ha, ndev) {
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000406 cpsw_add_mcast(priv, (u8 *)ha->addr);
Mugunthan V N5c50a852012-10-29 08:45:11 +0000407 }
408 }
409}
410
Mugunthan V Ndf828592012-03-18 20:17:54 +0000411static void cpsw_intr_enable(struct cpsw_priv *priv)
412{
Richard Cochran996a5c22012-10-29 08:45:12 +0000413 __raw_writel(0xFF, &priv->wr_regs->tx_en);
414 __raw_writel(0xFF, &priv->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000415
416 cpdma_ctlr_int_ctrl(priv->dma, true);
417 return;
418}
419
420static void cpsw_intr_disable(struct cpsw_priv *priv)
421{
Richard Cochran996a5c22012-10-29 08:45:12 +0000422 __raw_writel(0, &priv->wr_regs->tx_en);
423 __raw_writel(0, &priv->wr_regs->rx_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000424
425 cpdma_ctlr_int_ctrl(priv->dma, false);
426 return;
427}
428
429void cpsw_tx_handler(void *token, int len, int status)
430{
431 struct sk_buff *skb = token;
432 struct net_device *ndev = skb->dev;
433 struct cpsw_priv *priv = netdev_priv(ndev);
434
Mugunthan V Nfae50822013-01-17 06:31:34 +0000435 /* Check whether the queue is stopped due to stalled tx dma, if the
436 * queue is stopped then start the queue as we have free desc for tx
437 */
Mugunthan V Ndf828592012-03-18 20:17:54 +0000438 if (unlikely(netif_queue_stopped(ndev)))
439 netif_start_queue(ndev);
Mugunthan V N9232b162013-02-11 09:52:19 +0000440 cpts_tx_timestamp(priv->cpts, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000441 priv->stats.tx_packets++;
442 priv->stats.tx_bytes += len;
443 dev_kfree_skb_any(skb);
444}
445
446void cpsw_rx_handler(void *token, int len, int status)
447{
448 struct sk_buff *skb = token;
449 struct net_device *ndev = skb->dev;
450 struct cpsw_priv *priv = netdev_priv(ndev);
451 int ret = 0;
452
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000453 cpsw_dual_emac_src_port_detect(status, priv, ndev, skb);
454
Mugunthan V Ndf828592012-03-18 20:17:54 +0000455 /* free and bail if we are shutting down */
456 if (unlikely(!netif_running(ndev)) ||
457 unlikely(!netif_carrier_ok(ndev))) {
458 dev_kfree_skb_any(skb);
459 return;
460 }
461 if (likely(status >= 0)) {
462 skb_put(skb, len);
Mugunthan V N9232b162013-02-11 09:52:19 +0000463 cpts_rx_timestamp(priv->cpts, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000464 skb->protocol = eth_type_trans(skb, ndev);
465 netif_receive_skb(skb);
466 priv->stats.rx_bytes += len;
467 priv->stats.rx_packets++;
468 skb = NULL;
469 }
470
471 if (unlikely(!netif_running(ndev))) {
472 if (skb)
473 dev_kfree_skb_any(skb);
474 return;
475 }
476
477 if (likely(!skb)) {
478 skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
479 if (WARN_ON(!skb))
480 return;
481
482 ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
Mugunthan V Nf6e135c2013-02-11 09:52:18 +0000483 skb_tailroom(skb), 0, GFP_KERNEL);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000484 }
485 WARN_ON(ret < 0);
486}
487
488static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
489{
490 struct cpsw_priv *priv = dev_id;
491
492 if (likely(netif_running(priv->ndev))) {
493 cpsw_intr_disable(priv);
494 cpsw_disable_irq(priv);
495 napi_schedule(&priv->napi);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000496 } else {
497 priv = cpsw_get_slave_priv(priv, 1);
498 if (likely(priv) && likely(netif_running(priv->ndev))) {
499 cpsw_intr_disable(priv);
500 cpsw_disable_irq(priv);
501 napi_schedule(&priv->napi);
502 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000503 }
504 return IRQ_HANDLED;
505}
506
Mugunthan V Ndf828592012-03-18 20:17:54 +0000507static int cpsw_poll(struct napi_struct *napi, int budget)
508{
509 struct cpsw_priv *priv = napi_to_priv(napi);
510 int num_tx, num_rx;
511
512 num_tx = cpdma_chan_process(priv->txch, 128);
513 num_rx = cpdma_chan_process(priv->rxch, budget);
514
515 if (num_rx || num_tx)
516 cpsw_dbg(priv, intr, "poll %d rx, %d tx pkts\n",
517 num_rx, num_tx);
518
519 if (num_rx < budget) {
520 napi_complete(napi);
521 cpsw_intr_enable(priv);
522 cpdma_ctlr_eoi(priv->dma);
523 cpsw_enable_irq(priv);
524 }
525
526 return num_rx;
527}
528
529static inline void soft_reset(const char *module, void __iomem *reg)
530{
531 unsigned long timeout = jiffies + HZ;
532
533 __raw_writel(1, reg);
534 do {
535 cpu_relax();
536 } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
537
538 WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
539}
540
541#define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
542 ((mac)[2] << 16) | ((mac)[3] << 24))
543#define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
544
545static void cpsw_set_slave_mac(struct cpsw_slave *slave,
546 struct cpsw_priv *priv)
547{
Richard Cochran9750a3a2012-10-29 08:45:15 +0000548 slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
549 slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000550}
551
552static void _cpsw_adjust_link(struct cpsw_slave *slave,
553 struct cpsw_priv *priv, bool *link)
554{
555 struct phy_device *phy = slave->phy;
556 u32 mac_control = 0;
557 u32 slave_port;
558
559 if (!phy)
560 return;
561
562 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
563
564 if (phy->link) {
565 mac_control = priv->data.mac_control;
566
567 /* enable forwarding */
568 cpsw_ale_control_set(priv->ale, slave_port,
569 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
570
571 if (phy->speed == 1000)
572 mac_control |= BIT(7); /* GIGABITEN */
573 if (phy->duplex)
574 mac_control |= BIT(0); /* FULLDUPLEXEN */
Daniel Mack342b7b72012-09-27 09:19:34 +0000575
576 /* set speed_in input in case RMII mode is used in 100Mbps */
577 if (phy->speed == 100)
578 mac_control |= BIT(15);
579
Mugunthan V Ndf828592012-03-18 20:17:54 +0000580 *link = true;
581 } else {
582 mac_control = 0;
583 /* disable forwarding */
584 cpsw_ale_control_set(priv->ale, slave_port,
585 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
586 }
587
588 if (mac_control != slave->mac_control) {
589 phy_print_status(phy);
590 __raw_writel(mac_control, &slave->sliver->mac_control);
591 }
592
593 slave->mac_control = mac_control;
594}
595
596static void cpsw_adjust_link(struct net_device *ndev)
597{
598 struct cpsw_priv *priv = netdev_priv(ndev);
599 bool link = false;
600
601 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
602
603 if (link) {
604 netif_carrier_on(ndev);
605 if (netif_running(ndev))
606 netif_wake_queue(ndev);
607 } else {
608 netif_carrier_off(ndev);
609 netif_stop_queue(ndev);
610 }
611}
612
613static inline int __show_stat(char *buf, int maxlen, const char *name, u32 val)
614{
615 static char *leader = "........................................";
616
617 if (!val)
618 return 0;
619 else
620 return snprintf(buf, maxlen, "%s %s %10d\n", name,
621 leader + strlen(name), val);
622}
623
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000624static int cpsw_common_res_usage_state(struct cpsw_priv *priv)
625{
626 u32 i;
627 u32 usage_count = 0;
628
629 if (!priv->data.dual_emac)
630 return 0;
631
632 for (i = 0; i < priv->data.slaves; i++)
633 if (priv->slaves[i].open_stat)
634 usage_count++;
635
636 return usage_count;
637}
638
639static inline int cpsw_tx_packet_submit(struct net_device *ndev,
640 struct cpsw_priv *priv, struct sk_buff *skb)
641{
642 if (!priv->data.dual_emac)
643 return cpdma_chan_submit(priv->txch, skb, skb->data,
644 skb->len, 0, GFP_KERNEL);
645
646 if (ndev == cpsw_get_slave_ndev(priv, 0))
647 return cpdma_chan_submit(priv->txch, skb, skb->data,
648 skb->len, 1, GFP_KERNEL);
649 else
650 return cpdma_chan_submit(priv->txch, skb, skb->data,
651 skb->len, 2, GFP_KERNEL);
652}
653
654static inline void cpsw_add_dual_emac_def_ale_entries(
655 struct cpsw_priv *priv, struct cpsw_slave *slave,
656 u32 slave_port)
657{
658 u32 port_mask = 1 << slave_port | 1 << priv->host_port;
659
660 if (priv->version == CPSW_VERSION_1)
661 slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
662 else
663 slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
664 cpsw_ale_add_vlan(priv->ale, slave->port_vlan, port_mask,
665 port_mask, port_mask, 0);
666 cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
667 port_mask, ALE_VLAN, slave->port_vlan, 0);
668 cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
669 priv->host_port, ALE_VLAN, slave->port_vlan);
670}
671
Mugunthan V Ndf828592012-03-18 20:17:54 +0000672static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
673{
674 char name[32];
675 u32 slave_port;
676
677 sprintf(name, "slave-%d", slave->slave_num);
678
679 soft_reset(name, &slave->sliver->soft_reset);
680
681 /* setup priority mapping */
682 __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
Richard Cochran9750a3a2012-10-29 08:45:15 +0000683
684 switch (priv->version) {
685 case CPSW_VERSION_1:
686 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
687 break;
688 case CPSW_VERSION_2:
689 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
690 break;
691 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000692
693 /* setup max packet size, and mac address */
694 __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
695 cpsw_set_slave_mac(slave, priv);
696
697 slave->mac_control = 0; /* no link yet */
698
699 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
700
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000701 if (priv->data.dual_emac)
702 cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
703 else
704 cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
705 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000706
707 slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
Florian Fainellif9a8f832013-01-14 00:52:52 +0000708 &cpsw_adjust_link, slave->data->phy_if);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000709 if (IS_ERR(slave->phy)) {
710 dev_err(priv->dev, "phy %s not found on slave %d\n",
711 slave->data->phy_id, slave->slave_num);
712 slave->phy = NULL;
713 } else {
714 dev_info(priv->dev, "phy found : id is : 0x%x\n",
715 slave->phy->phy_id);
716 phy_start(slave->phy);
717 }
718}
719
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000720static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
721{
722 const int vlan = priv->data.default_vlan;
723 const int port = priv->host_port;
724 u32 reg;
725 int i;
726
727 reg = (priv->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
728 CPSW2_PORT_VLAN;
729
730 writel(vlan, &priv->host_port_regs->port_vlan);
731
732 for (i = 0; i < 2; i++)
733 slave_write(priv->slaves + i, vlan, reg);
734
735 cpsw_ale_add_vlan(priv->ale, vlan, ALE_ALL_PORTS << port,
736 ALE_ALL_PORTS << port, ALE_ALL_PORTS << port,
737 (ALE_PORT_1 | ALE_PORT_2) << port);
738}
739
Mugunthan V Ndf828592012-03-18 20:17:54 +0000740static void cpsw_init_host_port(struct cpsw_priv *priv)
741{
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000742 u32 control_reg;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000743 u32 fifo_mode;
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000744
Mugunthan V Ndf828592012-03-18 20:17:54 +0000745 /* soft reset the controller and initialize ale */
746 soft_reset("cpsw", &priv->regs->soft_reset);
747 cpsw_ale_start(priv->ale);
748
749 /* switch to vlan unaware mode */
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000750 cpsw_ale_control_set(priv->ale, priv->host_port, ALE_VLAN_AWARE,
751 CPSW_ALE_VLAN_AWARE);
752 control_reg = readl(&priv->regs->control);
753 control_reg |= CPSW_VLAN_AWARE;
754 writel(control_reg, &priv->regs->control);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000755 fifo_mode = (priv->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
756 CPSW_FIFO_NORMAL_MODE;
757 writel(fifo_mode, &priv->host_port_regs->tx_in_ctl);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000758
759 /* setup host port priority mapping */
760 __raw_writel(CPDMA_TX_PRIORITY_MAP,
761 &priv->host_port_regs->cpdma_tx_pri_map);
762 __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
763
764 cpsw_ale_control_set(priv->ale, priv->host_port,
765 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
766
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000767 if (!priv->data.dual_emac) {
768 cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port,
769 0, 0);
770 cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
771 1 << priv->host_port, 0, 0, ALE_MCAST_FWD_2);
772 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000773}
774
775static int cpsw_ndo_open(struct net_device *ndev)
776{
777 struct cpsw_priv *priv = netdev_priv(ndev);
778 int i, ret;
779 u32 reg;
780
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000781 if (!cpsw_common_res_usage_state(priv))
782 cpsw_intr_disable(priv);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000783 netif_carrier_off(ndev);
784
Mugunthan V Nf150bd72012-07-17 08:09:50 +0000785 pm_runtime_get_sync(&priv->pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000786
Richard Cochran549985e2012-11-14 09:07:56 +0000787 reg = priv->version;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000788
789 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
790 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
791 CPSW_RTL_VERSION(reg));
792
793 /* initialize host and slave ports */
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000794 if (!cpsw_common_res_usage_state(priv))
795 cpsw_init_host_port(priv);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000796 for_each_slave(priv, cpsw_slave_open, priv);
797
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000798 /* Add default VLAN */
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000799 if (!priv->data.dual_emac)
800 cpsw_add_default_vlan(priv);
Mugunthan V N3b72c2f2013-02-05 08:26:48 +0000801
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000802 if (!cpsw_common_res_usage_state(priv)) {
803 /* setup tx dma to fixed prio and zero offset */
804 cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
805 cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000806
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000807 /* disable priority elevation */
808 __raw_writel(0, &priv->regs->ptype);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000809
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000810 /* enable statistics collection only on all ports */
811 __raw_writel(0x7, &priv->regs->stat_port_en);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000812
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000813 if (WARN_ON(!priv->data.rx_descs))
814 priv->data.rx_descs = 128;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000815
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000816 for (i = 0; i < priv->data.rx_descs; i++) {
817 struct sk_buff *skb;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000818
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000819 ret = -ENOMEM;
820 skb = netdev_alloc_skb_ip_align(priv->ndev,
821 priv->rx_packet_max);
822 if (!skb)
823 break;
824 ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
Mugunthan V Nf6e135c2013-02-11 09:52:18 +0000825 skb_tailroom(skb), 0, GFP_KERNEL);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000826 if (WARN_ON(ret < 0))
827 break;
828 }
829 /* continue even if we didn't manage to submit all
830 * receive descs
831 */
832 cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000833 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000834
835 cpdma_ctlr_start(priv->dma);
836 cpsw_intr_enable(priv);
837 napi_enable(&priv->napi);
838 cpdma_ctlr_eoi(priv->dma);
839
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000840 if (priv->data.dual_emac)
841 priv->slaves[priv->emac_port].open_stat = true;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000842 return 0;
843}
844
845static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
846{
847 if (!slave->phy)
848 return;
849 phy_stop(slave->phy);
850 phy_disconnect(slave->phy);
851 slave->phy = NULL;
852}
853
854static int cpsw_ndo_stop(struct net_device *ndev)
855{
856 struct cpsw_priv *priv = netdev_priv(ndev);
857
858 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
Mugunthan V Ndf828592012-03-18 20:17:54 +0000859 netif_stop_queue(priv->ndev);
860 napi_disable(&priv->napi);
861 netif_carrier_off(priv->ndev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000862
863 if (cpsw_common_res_usage_state(priv) <= 1) {
864 cpsw_intr_disable(priv);
865 cpdma_ctlr_int_ctrl(priv->dma, false);
866 cpdma_ctlr_stop(priv->dma);
867 cpsw_ale_stop(priv->ale);
868 }
Mugunthan V Ndf828592012-03-18 20:17:54 +0000869 for_each_slave(priv, cpsw_slave_stop, priv);
Mugunthan V Nf150bd72012-07-17 08:09:50 +0000870 pm_runtime_put_sync(&priv->pdev->dev);
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000871 if (priv->data.dual_emac)
872 priv->slaves[priv->emac_port].open_stat = false;
Mugunthan V Ndf828592012-03-18 20:17:54 +0000873 return 0;
874}
875
876static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
877 struct net_device *ndev)
878{
879 struct cpsw_priv *priv = netdev_priv(ndev);
880 int ret;
881
882 ndev->trans_start = jiffies;
883
884 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
885 cpsw_err(priv, tx_err, "packet pad failed\n");
886 priv->stats.tx_dropped++;
887 return NETDEV_TX_OK;
888 }
889
Mugunthan V N9232b162013-02-11 09:52:19 +0000890 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
891 priv->cpts->tx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000892 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
893
894 skb_tx_timestamp(skb);
895
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000896 ret = cpsw_tx_packet_submit(ndev, priv, skb);
Mugunthan V Ndf828592012-03-18 20:17:54 +0000897 if (unlikely(ret != 0)) {
898 cpsw_err(priv, tx_err, "desc submit failed\n");
899 goto fail;
900 }
901
Mugunthan V Nfae50822013-01-17 06:31:34 +0000902 /* If there is no more tx desc left free then we need to
903 * tell the kernel to stop sending us tx frames.
904 */
905 if (unlikely(cpdma_check_free_tx_desc(priv->txch)))
906 netif_stop_queue(ndev);
907
Mugunthan V Ndf828592012-03-18 20:17:54 +0000908 return NETDEV_TX_OK;
909fail:
910 priv->stats.tx_dropped++;
911 netif_stop_queue(ndev);
912 return NETDEV_TX_BUSY;
913}
914
915static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags)
916{
917 /*
918 * The switch cannot operate in promiscuous mode without substantial
919 * headache. For promiscuous mode to work, we would need to put the
920 * ALE in bypass mode and route all traffic to the host port.
921 * Subsequently, the host will need to operate as a "bridge", learn,
922 * and flood as needed. For now, we simply complain here and
923 * do nothing about it :-)
924 */
925 if ((flags & IFF_PROMISC) && (ndev->flags & IFF_PROMISC))
926 dev_err(&ndev->dev, "promiscuity ignored!\n");
927
928 /*
929 * The switch cannot filter multicast traffic unless it is configured
930 * in "VLAN Aware" mode. Unfortunately, VLAN awareness requires a
931 * whole bunch of additional logic that this driver does not implement
932 * at present.
933 */
934 if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI))
935 dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n");
936}
937
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000938#ifdef CONFIG_TI_CPTS
939
940static void cpsw_hwtstamp_v1(struct cpsw_priv *priv)
941{
942 struct cpsw_slave *slave = &priv->slaves[priv->data.cpts_active_slave];
943 u32 ts_en, seq_id;
944
Mugunthan V N9232b162013-02-11 09:52:19 +0000945 if (!priv->cpts->tx_enable && !priv->cpts->rx_enable) {
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000946 slave_write(slave, 0, CPSW1_TS_CTL);
947 return;
948 }
949
950 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
951 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
952
Mugunthan V N9232b162013-02-11 09:52:19 +0000953 if (priv->cpts->tx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000954 ts_en |= CPSW_V1_TS_TX_EN;
955
Mugunthan V N9232b162013-02-11 09:52:19 +0000956 if (priv->cpts->rx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000957 ts_en |= CPSW_V1_TS_RX_EN;
958
959 slave_write(slave, ts_en, CPSW1_TS_CTL);
960 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
961}
962
963static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
964{
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000965 struct cpsw_slave *slave;
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000966 u32 ctrl, mtype;
967
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +0000968 if (priv->data.dual_emac)
969 slave = &priv->slaves[priv->emac_port];
970 else
971 slave = &priv->slaves[priv->data.cpts_active_slave];
972
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000973 ctrl = slave_read(slave, CPSW2_CONTROL);
974 ctrl &= ~CTRL_ALL_TS_MASK;
975
Mugunthan V N9232b162013-02-11 09:52:19 +0000976 if (priv->cpts->tx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000977 ctrl |= CTRL_TX_TS_BITS;
978
Mugunthan V N9232b162013-02-11 09:52:19 +0000979 if (priv->cpts->rx_enable)
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000980 ctrl |= CTRL_RX_TS_BITS;
981
982 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
983
984 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
985 slave_write(slave, ctrl, CPSW2_CONTROL);
986 __raw_writel(ETH_P_1588, &priv->regs->ts_ltype);
987}
988
Mugunthan V N3177bf62012-11-27 07:53:40 +0000989static int cpsw_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000990{
Mugunthan V N3177bf62012-11-27 07:53:40 +0000991 struct cpsw_priv *priv = netdev_priv(dev);
Mugunthan V N9232b162013-02-11 09:52:19 +0000992 struct cpts *cpts = priv->cpts;
Richard Cochran2e5b38a2012-10-29 08:45:20 +0000993 struct hwtstamp_config cfg;
994
995 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
996 return -EFAULT;
997
998 /* reserved for future extensions */
999 if (cfg.flags)
1000 return -EINVAL;
1001
1002 switch (cfg.tx_type) {
1003 case HWTSTAMP_TX_OFF:
1004 cpts->tx_enable = 0;
1005 break;
1006 case HWTSTAMP_TX_ON:
1007 cpts->tx_enable = 1;
1008 break;
1009 default:
1010 return -ERANGE;
1011 }
1012
1013 switch (cfg.rx_filter) {
1014 case HWTSTAMP_FILTER_NONE:
1015 cpts->rx_enable = 0;
1016 break;
1017 case HWTSTAMP_FILTER_ALL:
1018 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1019 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1020 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1021 return -ERANGE;
1022 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1023 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1024 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1025 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1026 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1027 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1028 case HWTSTAMP_FILTER_PTP_V2_EVENT:
1029 case HWTSTAMP_FILTER_PTP_V2_SYNC:
1030 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1031 cpts->rx_enable = 1;
1032 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1033 break;
1034 default:
1035 return -ERANGE;
1036 }
1037
1038 switch (priv->version) {
1039 case CPSW_VERSION_1:
1040 cpsw_hwtstamp_v1(priv);
1041 break;
1042 case CPSW_VERSION_2:
1043 cpsw_hwtstamp_v2(priv);
1044 break;
1045 default:
1046 return -ENOTSUPP;
1047 }
1048
1049 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1050}
1051
1052#endif /*CONFIG_TI_CPTS*/
1053
1054static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
1055{
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001056 if (!netif_running(dev))
1057 return -EINVAL;
1058
1059#ifdef CONFIG_TI_CPTS
1060 if (cmd == SIOCSHWTSTAMP)
Mugunthan V N3177bf62012-11-27 07:53:40 +00001061 return cpsw_hwtstamp_ioctl(dev, req);
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001062#endif
1063 return -ENOTSUPP;
1064}
1065
Mugunthan V Ndf828592012-03-18 20:17:54 +00001066static void cpsw_ndo_tx_timeout(struct net_device *ndev)
1067{
1068 struct cpsw_priv *priv = netdev_priv(ndev);
1069
1070 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
1071 priv->stats.tx_errors++;
1072 cpsw_intr_disable(priv);
1073 cpdma_ctlr_int_ctrl(priv->dma, false);
1074 cpdma_chan_stop(priv->txch);
1075 cpdma_chan_start(priv->txch);
1076 cpdma_ctlr_int_ctrl(priv->dma, true);
1077 cpsw_intr_enable(priv);
1078 cpdma_ctlr_eoi(priv->dma);
1079}
1080
1081static struct net_device_stats *cpsw_ndo_get_stats(struct net_device *ndev)
1082{
1083 struct cpsw_priv *priv = netdev_priv(ndev);
1084 return &priv->stats;
1085}
1086
1087#ifdef CONFIG_NET_POLL_CONTROLLER
1088static void cpsw_ndo_poll_controller(struct net_device *ndev)
1089{
1090 struct cpsw_priv *priv = netdev_priv(ndev);
1091
1092 cpsw_intr_disable(priv);
1093 cpdma_ctlr_int_ctrl(priv->dma, false);
1094 cpsw_interrupt(ndev->irq, priv);
1095 cpdma_ctlr_int_ctrl(priv->dma, true);
1096 cpsw_intr_enable(priv);
1097 cpdma_ctlr_eoi(priv->dma);
1098}
1099#endif
1100
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001101static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
1102 unsigned short vid)
1103{
1104 int ret;
1105
1106 ret = cpsw_ale_add_vlan(priv->ale, vid,
1107 ALE_ALL_PORTS << priv->host_port,
1108 0, ALE_ALL_PORTS << priv->host_port,
1109 (ALE_PORT_1 | ALE_PORT_2) << priv->host_port);
1110 if (ret != 0)
1111 return ret;
1112
1113 ret = cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
1114 priv->host_port, ALE_VLAN, vid);
1115 if (ret != 0)
1116 goto clean_vid;
1117
1118 ret = cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
1119 ALE_ALL_PORTS << priv->host_port,
1120 ALE_VLAN, vid, 0);
1121 if (ret != 0)
1122 goto clean_vlan_ucast;
1123 return 0;
1124
1125clean_vlan_ucast:
1126 cpsw_ale_del_ucast(priv->ale, priv->mac_addr,
1127 priv->host_port, ALE_VLAN, vid);
1128clean_vid:
1129 cpsw_ale_del_vlan(priv->ale, vid, 0);
1130 return ret;
1131}
1132
1133static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
1134 unsigned short vid)
1135{
1136 struct cpsw_priv *priv = netdev_priv(ndev);
1137
1138 if (vid == priv->data.default_vlan)
1139 return 0;
1140
1141 dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
1142 return cpsw_add_vlan_ale_entry(priv, vid);
1143}
1144
1145static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
1146 unsigned short vid)
1147{
1148 struct cpsw_priv *priv = netdev_priv(ndev);
1149 int ret;
1150
1151 if (vid == priv->data.default_vlan)
1152 return 0;
1153
1154 dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
1155 ret = cpsw_ale_del_vlan(priv->ale, vid, 0);
1156 if (ret != 0)
1157 return ret;
1158
1159 ret = cpsw_ale_del_ucast(priv->ale, priv->mac_addr,
1160 priv->host_port, ALE_VLAN, vid);
1161 if (ret != 0)
1162 return ret;
1163
1164 return cpsw_ale_del_mcast(priv->ale, priv->ndev->broadcast,
1165 0, ALE_VLAN, vid);
1166}
1167
Mugunthan V Ndf828592012-03-18 20:17:54 +00001168static const struct net_device_ops cpsw_netdev_ops = {
1169 .ndo_open = cpsw_ndo_open,
1170 .ndo_stop = cpsw_ndo_stop,
1171 .ndo_start_xmit = cpsw_ndo_start_xmit,
1172 .ndo_change_rx_flags = cpsw_ndo_change_rx_flags,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001173 .ndo_do_ioctl = cpsw_ndo_ioctl,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001174 .ndo_validate_addr = eth_validate_addr,
David S. Miller5c473ed2012-03-20 00:33:59 -04001175 .ndo_change_mtu = eth_change_mtu,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001176 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
1177 .ndo_get_stats = cpsw_ndo_get_stats,
Mugunthan V N5c50a852012-10-29 08:45:11 +00001178 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001179#ifdef CONFIG_NET_POLL_CONTROLLER
1180 .ndo_poll_controller = cpsw_ndo_poll_controller,
1181#endif
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001182 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
1183 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001184};
1185
1186static void cpsw_get_drvinfo(struct net_device *ndev,
1187 struct ethtool_drvinfo *info)
1188{
1189 struct cpsw_priv *priv = netdev_priv(ndev);
Jiri Pirko7826d432013-01-06 00:44:26 +00001190
1191 strlcpy(info->driver, "TI CPSW Driver v1.0", sizeof(info->driver));
1192 strlcpy(info->version, "1.0", sizeof(info->version));
1193 strlcpy(info->bus_info, priv->pdev->name, sizeof(info->bus_info));
Mugunthan V Ndf828592012-03-18 20:17:54 +00001194}
1195
1196static u32 cpsw_get_msglevel(struct net_device *ndev)
1197{
1198 struct cpsw_priv *priv = netdev_priv(ndev);
1199 return priv->msg_enable;
1200}
1201
1202static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
1203{
1204 struct cpsw_priv *priv = netdev_priv(ndev);
1205 priv->msg_enable = value;
1206}
1207
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001208static int cpsw_get_ts_info(struct net_device *ndev,
1209 struct ethtool_ts_info *info)
1210{
1211#ifdef CONFIG_TI_CPTS
1212 struct cpsw_priv *priv = netdev_priv(ndev);
1213
1214 info->so_timestamping =
1215 SOF_TIMESTAMPING_TX_HARDWARE |
1216 SOF_TIMESTAMPING_TX_SOFTWARE |
1217 SOF_TIMESTAMPING_RX_HARDWARE |
1218 SOF_TIMESTAMPING_RX_SOFTWARE |
1219 SOF_TIMESTAMPING_SOFTWARE |
1220 SOF_TIMESTAMPING_RAW_HARDWARE;
Mugunthan V N9232b162013-02-11 09:52:19 +00001221 info->phc_index = priv->cpts->phc_index;
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001222 info->tx_types =
1223 (1 << HWTSTAMP_TX_OFF) |
1224 (1 << HWTSTAMP_TX_ON);
1225 info->rx_filters =
1226 (1 << HWTSTAMP_FILTER_NONE) |
1227 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
1228#else
1229 info->so_timestamping =
1230 SOF_TIMESTAMPING_TX_SOFTWARE |
1231 SOF_TIMESTAMPING_RX_SOFTWARE |
1232 SOF_TIMESTAMPING_SOFTWARE;
1233 info->phc_index = -1;
1234 info->tx_types = 0;
1235 info->rx_filters = 0;
1236#endif
1237 return 0;
1238}
1239
Mugunthan V Ndf828592012-03-18 20:17:54 +00001240static const struct ethtool_ops cpsw_ethtool_ops = {
1241 .get_drvinfo = cpsw_get_drvinfo,
1242 .get_msglevel = cpsw_get_msglevel,
1243 .set_msglevel = cpsw_set_msglevel,
1244 .get_link = ethtool_op_get_link,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001245 .get_ts_info = cpsw_get_ts_info,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001246};
1247
Richard Cochran549985e2012-11-14 09:07:56 +00001248static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv,
1249 u32 slave_reg_ofs, u32 sliver_reg_ofs)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001250{
1251 void __iomem *regs = priv->regs;
1252 int slave_num = slave->slave_num;
1253 struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
1254
1255 slave->data = data;
Richard Cochran549985e2012-11-14 09:07:56 +00001256 slave->regs = regs + slave_reg_ofs;
1257 slave->sliver = regs + sliver_reg_ofs;
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001258 slave->port_vlan = data->dual_emac_res_vlan;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001259}
1260
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001261static int cpsw_probe_dt(struct cpsw_platform_data *data,
1262 struct platform_device *pdev)
1263{
1264 struct device_node *node = pdev->dev.of_node;
1265 struct device_node *slave_node;
1266 int i = 0, ret;
1267 u32 prop;
1268
1269 if (!node)
1270 return -EINVAL;
1271
1272 if (of_property_read_u32(node, "slaves", &prop)) {
1273 pr_err("Missing slaves property in the DT.\n");
1274 return -EINVAL;
1275 }
1276 data->slaves = prop;
1277
Richard Cochran78ca0b22012-10-29 08:45:18 +00001278 if (of_property_read_u32(node, "cpts_active_slave", &prop)) {
1279 pr_err("Missing cpts_active_slave property in the DT.\n");
1280 ret = -EINVAL;
1281 goto error_ret;
1282 }
1283 data->cpts_active_slave = prop;
1284
Richard Cochran00ab94e2012-10-29 08:45:19 +00001285 if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
1286 pr_err("Missing cpts_clock_mult property in the DT.\n");
1287 ret = -EINVAL;
1288 goto error_ret;
1289 }
1290 data->cpts_clock_mult = prop;
1291
1292 if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
1293 pr_err("Missing cpts_clock_shift property in the DT.\n");
1294 ret = -EINVAL;
1295 goto error_ret;
1296 }
1297 data->cpts_clock_shift = prop;
1298
Joe Perchesb2adaca2013-02-03 17:43:58 +00001299 data->slave_data = kcalloc(data->slaves, sizeof(struct cpsw_slave_data),
1300 GFP_KERNEL);
1301 if (!data->slave_data)
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001302 return -EINVAL;
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001303
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001304 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
1305 pr_err("Missing cpdma_channels property in the DT.\n");
1306 ret = -EINVAL;
1307 goto error_ret;
1308 }
1309 data->channels = prop;
1310
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001311 if (of_property_read_u32(node, "ale_entries", &prop)) {
1312 pr_err("Missing ale_entries property in the DT.\n");
1313 ret = -EINVAL;
1314 goto error_ret;
1315 }
1316 data->ale_entries = prop;
1317
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001318 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
1319 pr_err("Missing bd_ram_size property in the DT.\n");
1320 ret = -EINVAL;
1321 goto error_ret;
1322 }
1323 data->bd_ram_size = prop;
1324
1325 if (of_property_read_u32(node, "rx_descs", &prop)) {
1326 pr_err("Missing rx_descs property in the DT.\n");
1327 ret = -EINVAL;
1328 goto error_ret;
1329 }
1330 data->rx_descs = prop;
1331
1332 if (of_property_read_u32(node, "mac_control", &prop)) {
1333 pr_err("Missing mac_control property in the DT.\n");
1334 ret = -EINVAL;
1335 goto error_ret;
1336 }
1337 data->mac_control = prop;
1338
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001339 if (!of_property_read_u32(node, "dual_emac", &prop))
1340 data->dual_emac = prop;
1341
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00001342 /*
1343 * Populate all the child nodes here...
1344 */
1345 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
1346 /* We do not want to force this, as in some cases may not have child */
1347 if (ret)
1348 pr_warn("Doesn't have any child node\n");
1349
Richard Cochran549985e2012-11-14 09:07:56 +00001350 for_each_node_by_name(slave_node, "slave") {
1351 struct cpsw_slave_data *slave_data = data->slave_data + i;
1352 const void *mac_addr = NULL;
1353 u32 phyid;
1354 int lenp;
1355 const __be32 *parp;
1356 struct device_node *mdio_node;
1357 struct platform_device *mdio;
1358
1359 parp = of_get_property(slave_node, "phy_id", &lenp);
1360 if ((parp == NULL) && (lenp != (sizeof(void *) * 2))) {
1361 pr_err("Missing slave[%d] phy_id property\n", i);
1362 ret = -EINVAL;
1363 goto error_ret;
1364 }
1365 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
1366 phyid = be32_to_cpup(parp+1);
1367 mdio = of_find_device_by_node(mdio_node);
1368 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
1369 PHY_ID_FMT, mdio->name, phyid);
1370
1371 mac_addr = of_get_mac_address(slave_node);
1372 if (mac_addr)
1373 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
1374
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001375 if (data->dual_emac) {
1376 if (of_property_read_u32(node, "dual_emac_res_vlan",
1377 &prop)) {
1378 pr_err("Missing dual_emac_res_vlan in DT.\n");
1379 slave_data->dual_emac_res_vlan = i+1;
1380 pr_err("Using %d as Reserved VLAN for %d slave\n",
1381 slave_data->dual_emac_res_vlan, i);
1382 } else {
1383 slave_data->dual_emac_res_vlan = prop;
1384 }
1385 }
1386
Richard Cochran549985e2012-11-14 09:07:56 +00001387 i++;
1388 }
1389
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001390 return 0;
1391
1392error_ret:
1393 kfree(data->slave_data);
1394 return ret;
1395}
1396
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001397static int cpsw_probe_dual_emac(struct platform_device *pdev,
1398 struct cpsw_priv *priv)
1399{
1400 struct cpsw_platform_data *data = &priv->data;
1401 struct net_device *ndev;
1402 struct cpsw_priv *priv_sl2;
1403 int ret = 0, i;
1404
1405 ndev = alloc_etherdev(sizeof(struct cpsw_priv));
1406 if (!ndev) {
1407 pr_err("cpsw: error allocating net_device\n");
1408 return -ENOMEM;
1409 }
1410
1411 priv_sl2 = netdev_priv(ndev);
1412 spin_lock_init(&priv_sl2->lock);
1413 priv_sl2->data = *data;
1414 priv_sl2->pdev = pdev;
1415 priv_sl2->ndev = ndev;
1416 priv_sl2->dev = &ndev->dev;
1417 priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
1418 priv_sl2->rx_packet_max = max(rx_packet_max, 128);
1419
1420 if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
1421 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
1422 ETH_ALEN);
1423 pr_info("cpsw: Detected MACID = %pM\n", priv_sl2->mac_addr);
1424 } else {
1425 random_ether_addr(priv_sl2->mac_addr);
1426 pr_info("cpsw: Random MACID = %pM\n", priv_sl2->mac_addr);
1427 }
1428 memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
1429
1430 priv_sl2->slaves = priv->slaves;
1431 priv_sl2->clk = priv->clk;
1432
1433 priv_sl2->cpsw_res = priv->cpsw_res;
1434 priv_sl2->regs = priv->regs;
1435 priv_sl2->host_port = priv->host_port;
1436 priv_sl2->host_port_regs = priv->host_port_regs;
1437 priv_sl2->wr_regs = priv->wr_regs;
1438 priv_sl2->dma = priv->dma;
1439 priv_sl2->txch = priv->txch;
1440 priv_sl2->rxch = priv->rxch;
1441 priv_sl2->ale = priv->ale;
1442 priv_sl2->emac_port = 1;
1443 priv->slaves[1].ndev = ndev;
1444 priv_sl2->cpts = priv->cpts;
1445 priv_sl2->version = priv->version;
1446
1447 for (i = 0; i < priv->num_irqs; i++) {
1448 priv_sl2->irqs_table[i] = priv->irqs_table[i];
1449 priv_sl2->num_irqs = priv->num_irqs;
1450 }
1451
1452 ndev->features |= NETIF_F_HW_VLAN_FILTER;
1453
1454 ndev->netdev_ops = &cpsw_netdev_ops;
1455 SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
1456 netif_napi_add(ndev, &priv_sl2->napi, cpsw_poll, CPSW_POLL_WEIGHT);
1457
1458 /* register the network device */
1459 SET_NETDEV_DEV(ndev, &pdev->dev);
1460 ret = register_netdev(ndev);
1461 if (ret) {
1462 pr_err("cpsw: error registering net device\n");
1463 free_netdev(ndev);
1464 ret = -ENODEV;
1465 }
1466
1467 return ret;
1468}
1469
Bill Pemberton663e12e2012-12-03 09:23:45 -05001470static int cpsw_probe(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001471{
1472 struct cpsw_platform_data *data = pdev->dev.platform_data;
1473 struct net_device *ndev;
1474 struct cpsw_priv *priv;
1475 struct cpdma_params dma_params;
1476 struct cpsw_ale_params ale_params;
Richard Cochran549985e2012-11-14 09:07:56 +00001477 void __iomem *ss_regs, *wr_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001478 struct resource *res;
Richard Cochran549985e2012-11-14 09:07:56 +00001479 u32 slave_offset, sliver_offset, slave_size;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001480 int ret = 0, i, k = 0;
1481
Mugunthan V Ndf828592012-03-18 20:17:54 +00001482 ndev = alloc_etherdev(sizeof(struct cpsw_priv));
1483 if (!ndev) {
1484 pr_err("error allocating net_device\n");
1485 return -ENOMEM;
1486 }
1487
1488 platform_set_drvdata(pdev, ndev);
1489 priv = netdev_priv(ndev);
1490 spin_lock_init(&priv->lock);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001491 priv->pdev = pdev;
1492 priv->ndev = ndev;
1493 priv->dev = &ndev->dev;
1494 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
1495 priv->rx_packet_max = max(rx_packet_max, 128);
Mugunthan V N9232b162013-02-11 09:52:19 +00001496 priv->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL);
1497 if (!ndev) {
1498 pr_err("error allocating cpts\n");
1499 goto clean_ndev_ret;
1500 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001501
Vaibhav Hiremath1fb19aa2012-11-14 09:07:55 +00001502 /*
1503 * This may be required here for child devices.
1504 */
1505 pm_runtime_enable(&pdev->dev);
1506
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001507 if (cpsw_probe_dt(&priv->data, pdev)) {
1508 pr_err("cpsw: platform data missing\n");
1509 ret = -ENODEV;
1510 goto clean_ndev_ret;
1511 }
1512 data = &priv->data;
1513
Mugunthan V Ndf828592012-03-18 20:17:54 +00001514 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
1515 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
1516 pr_info("Detected MACID = %pM", priv->mac_addr);
1517 } else {
Joe Perches7efd26d2012-07-12 19:33:06 +00001518 eth_random_addr(priv->mac_addr);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001519 pr_info("Random MACID = %pM", priv->mac_addr);
1520 }
1521
1522 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
1523
1524 priv->slaves = kzalloc(sizeof(struct cpsw_slave) * data->slaves,
1525 GFP_KERNEL);
1526 if (!priv->slaves) {
1527 ret = -EBUSY;
1528 goto clean_ndev_ret;
1529 }
1530 for (i = 0; i < data->slaves; i++)
1531 priv->slaves[i].slave_num = i;
1532
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001533 priv->slaves[0].ndev = ndev;
1534 priv->emac_port = 0;
1535
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001536 priv->clk = clk_get(&pdev->dev, "fck");
Mugunthan V Ndf828592012-03-18 20:17:54 +00001537 if (IS_ERR(priv->clk)) {
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001538 dev_err(&pdev->dev, "fck is not found\n");
1539 ret = -ENODEV;
1540 goto clean_slave_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001541 }
1542
1543 priv->cpsw_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1544 if (!priv->cpsw_res) {
1545 dev_err(priv->dev, "error getting i/o resource\n");
1546 ret = -ENOENT;
1547 goto clean_clk_ret;
1548 }
Mugunthan V Ndf828592012-03-18 20:17:54 +00001549 if (!request_mem_region(priv->cpsw_res->start,
1550 resource_size(priv->cpsw_res), ndev->name)) {
1551 dev_err(priv->dev, "failed request i/o region\n");
1552 ret = -ENXIO;
1553 goto clean_clk_ret;
1554 }
Richard Cochran549985e2012-11-14 09:07:56 +00001555 ss_regs = ioremap(priv->cpsw_res->start, resource_size(priv->cpsw_res));
1556 if (!ss_regs) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00001557 dev_err(priv->dev, "unable to map i/o region\n");
1558 goto clean_cpsw_iores_ret;
1559 }
Richard Cochran549985e2012-11-14 09:07:56 +00001560 priv->regs = ss_regs;
1561 priv->version = __raw_readl(&priv->regs->id_ver);
1562 priv->host_port = HOST_PORT_NUM;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001563
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001564 priv->cpsw_wr_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1565 if (!priv->cpsw_wr_res) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00001566 dev_err(priv->dev, "error getting i/o resource\n");
1567 ret = -ENOENT;
Richard Cochran5250c962012-11-02 22:25:30 +00001568 goto clean_iomap_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001569 }
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001570 if (!request_mem_region(priv->cpsw_wr_res->start,
1571 resource_size(priv->cpsw_wr_res), ndev->name)) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00001572 dev_err(priv->dev, "failed request i/o region\n");
1573 ret = -ENXIO;
Richard Cochran5250c962012-11-02 22:25:30 +00001574 goto clean_iomap_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001575 }
Richard Cochran549985e2012-11-14 09:07:56 +00001576 wr_regs = ioremap(priv->cpsw_wr_res->start,
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001577 resource_size(priv->cpsw_wr_res));
Richard Cochran549985e2012-11-14 09:07:56 +00001578 if (!wr_regs) {
Mugunthan V Ndf828592012-03-18 20:17:54 +00001579 dev_err(priv->dev, "unable to map i/o region\n");
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001580 goto clean_cpsw_wr_iores_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001581 }
Richard Cochran549985e2012-11-14 09:07:56 +00001582 priv->wr_regs = wr_regs;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001583
1584 memset(&dma_params, 0, sizeof(dma_params));
Richard Cochran549985e2012-11-14 09:07:56 +00001585 memset(&ale_params, 0, sizeof(ale_params));
1586
1587 switch (priv->version) {
1588 case CPSW_VERSION_1:
1589 priv->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
Mugunthan V N9232b162013-02-11 09:52:19 +00001590 priv->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET;
Richard Cochran549985e2012-11-14 09:07:56 +00001591 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
1592 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
1593 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
1594 slave_offset = CPSW1_SLAVE_OFFSET;
1595 slave_size = CPSW1_SLAVE_SIZE;
1596 sliver_offset = CPSW1_SLIVER_OFFSET;
1597 dma_params.desc_mem_phys = 0;
1598 break;
1599 case CPSW_VERSION_2:
1600 priv->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
Mugunthan V N9232b162013-02-11 09:52:19 +00001601 priv->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET;
Richard Cochran549985e2012-11-14 09:07:56 +00001602 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
1603 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
1604 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
1605 slave_offset = CPSW2_SLAVE_OFFSET;
1606 slave_size = CPSW2_SLAVE_SIZE;
1607 sliver_offset = CPSW2_SLIVER_OFFSET;
1608 dma_params.desc_mem_phys =
1609 (u32 __force) priv->cpsw_res->start + CPSW2_BD_OFFSET;
1610 break;
1611 default:
1612 dev_err(priv->dev, "unknown version 0x%08x\n", priv->version);
1613 ret = -ENODEV;
1614 goto clean_cpsw_wr_iores_ret;
1615 }
1616 for (i = 0; i < priv->data.slaves; i++) {
1617 struct cpsw_slave *slave = &priv->slaves[i];
1618 cpsw_slave_init(slave, priv, slave_offset, sliver_offset);
1619 slave_offset += slave_size;
1620 sliver_offset += SLIVER_SIZE;
1621 }
1622
Mugunthan V Ndf828592012-03-18 20:17:54 +00001623 dma_params.dev = &pdev->dev;
Richard Cochran549985e2012-11-14 09:07:56 +00001624 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
1625 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
1626 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
1627 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
1628 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001629
1630 dma_params.num_chan = data->channels;
1631 dma_params.has_soft_reset = true;
1632 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
1633 dma_params.desc_mem_size = data->bd_ram_size;
1634 dma_params.desc_align = 16;
1635 dma_params.has_ext_regs = true;
Richard Cochran549985e2012-11-14 09:07:56 +00001636 dma_params.desc_hw_addr = dma_params.desc_mem_phys;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001637
1638 priv->dma = cpdma_ctlr_create(&dma_params);
1639 if (!priv->dma) {
1640 dev_err(priv->dev, "error initializing dma\n");
1641 ret = -ENOMEM;
Richard Cochran5250c962012-11-02 22:25:30 +00001642 goto clean_wr_iomap_ret;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001643 }
1644
1645 priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
1646 cpsw_tx_handler);
1647 priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
1648 cpsw_rx_handler);
1649
1650 if (WARN_ON(!priv->txch || !priv->rxch)) {
1651 dev_err(priv->dev, "error initializing dma channels\n");
1652 ret = -ENOMEM;
1653 goto clean_dma_ret;
1654 }
1655
Mugunthan V Ndf828592012-03-18 20:17:54 +00001656 ale_params.dev = &ndev->dev;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001657 ale_params.ale_ageout = ale_ageout;
1658 ale_params.ale_entries = data->ale_entries;
1659 ale_params.ale_ports = data->slaves;
1660
1661 priv->ale = cpsw_ale_create(&ale_params);
1662 if (!priv->ale) {
1663 dev_err(priv->dev, "error initializing ale engine\n");
1664 ret = -ENODEV;
1665 goto clean_dma_ret;
1666 }
1667
1668 ndev->irq = platform_get_irq(pdev, 0);
1669 if (ndev->irq < 0) {
1670 dev_err(priv->dev, "error getting irq resource\n");
1671 ret = -ENOENT;
1672 goto clean_ale_ret;
1673 }
1674
1675 while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
1676 for (i = res->start; i <= res->end; i++) {
1677 if (request_irq(i, cpsw_interrupt, IRQF_DISABLED,
1678 dev_name(&pdev->dev), priv)) {
1679 dev_err(priv->dev, "error attaching irq\n");
1680 goto clean_ale_ret;
1681 }
1682 priv->irqs_table[k] = i;
1683 priv->num_irqs = k;
1684 }
1685 k++;
1686 }
1687
Mugunthan V N3b72c2f2013-02-05 08:26:48 +00001688 ndev->features |= NETIF_F_HW_VLAN_FILTER;
Mugunthan V Ndf828592012-03-18 20:17:54 +00001689
1690 ndev->netdev_ops = &cpsw_netdev_ops;
1691 SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
1692 netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT);
1693
1694 /* register the network device */
1695 SET_NETDEV_DEV(ndev, &pdev->dev);
1696 ret = register_netdev(ndev);
1697 if (ret) {
1698 dev_err(priv->dev, "error registering net device\n");
1699 ret = -ENODEV;
1700 goto clean_irq_ret;
1701 }
1702
Mugunthan V N9232b162013-02-11 09:52:19 +00001703 if (cpts_register(&pdev->dev, priv->cpts,
Richard Cochran2e5b38a2012-10-29 08:45:20 +00001704 data->cpts_clock_mult, data->cpts_clock_shift))
1705 dev_err(priv->dev, "error registering cpts device\n");
1706
Mugunthan V Ndf828592012-03-18 20:17:54 +00001707 cpsw_notice(priv, probe, "initialized device (regs %x, irq %d)\n",
1708 priv->cpsw_res->start, ndev->irq);
1709
Mugunthan V Nd9ba8f92013-02-11 09:52:20 +00001710 if (priv->data.dual_emac) {
1711 ret = cpsw_probe_dual_emac(pdev, priv);
1712 if (ret) {
1713 cpsw_err(priv, probe, "error probe slave 2 emac interface\n");
1714 goto clean_irq_ret;
1715 }
1716 }
1717
Mugunthan V Ndf828592012-03-18 20:17:54 +00001718 return 0;
1719
1720clean_irq_ret:
1721 free_irq(ndev->irq, priv);
1722clean_ale_ret:
1723 cpsw_ale_destroy(priv->ale);
1724clean_dma_ret:
1725 cpdma_chan_destroy(priv->txch);
1726 cpdma_chan_destroy(priv->rxch);
1727 cpdma_ctlr_destroy(priv->dma);
Richard Cochran5250c962012-11-02 22:25:30 +00001728clean_wr_iomap_ret:
1729 iounmap(priv->wr_regs);
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001730clean_cpsw_wr_iores_ret:
1731 release_mem_region(priv->cpsw_wr_res->start,
1732 resource_size(priv->cpsw_wr_res));
Richard Cochran5250c962012-11-02 22:25:30 +00001733clean_iomap_ret:
1734 iounmap(priv->regs);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001735clean_cpsw_iores_ret:
1736 release_mem_region(priv->cpsw_res->start,
1737 resource_size(priv->cpsw_res));
1738clean_clk_ret:
1739 clk_put(priv->clk);
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001740clean_slave_ret:
1741 pm_runtime_disable(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001742 kfree(priv->slaves);
1743clean_ndev_ret:
1744 free_netdev(ndev);
1745 return ret;
1746}
1747
Bill Pemberton663e12e2012-12-03 09:23:45 -05001748static int cpsw_remove(struct platform_device *pdev)
Mugunthan V Ndf828592012-03-18 20:17:54 +00001749{
1750 struct net_device *ndev = platform_get_drvdata(pdev);
1751 struct cpsw_priv *priv = netdev_priv(ndev);
1752
1753 pr_info("removing device");
1754 platform_set_drvdata(pdev, NULL);
1755
Mugunthan V N9232b162013-02-11 09:52:19 +00001756 cpts_unregister(priv->cpts);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001757 free_irq(ndev->irq, priv);
1758 cpsw_ale_destroy(priv->ale);
1759 cpdma_chan_destroy(priv->txch);
1760 cpdma_chan_destroy(priv->rxch);
1761 cpdma_ctlr_destroy(priv->dma);
1762 iounmap(priv->regs);
1763 release_mem_region(priv->cpsw_res->start,
1764 resource_size(priv->cpsw_res));
Richard Cochran5250c962012-11-02 22:25:30 +00001765 iounmap(priv->wr_regs);
Richard Cochrana65dd5b2012-11-02 22:25:29 +00001766 release_mem_region(priv->cpsw_wr_res->start,
1767 resource_size(priv->cpsw_wr_res));
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001768 pm_runtime_disable(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001769 clk_put(priv->clk);
1770 kfree(priv->slaves);
1771 free_netdev(ndev);
1772
1773 return 0;
1774}
1775
1776static int cpsw_suspend(struct device *dev)
1777{
1778 struct platform_device *pdev = to_platform_device(dev);
1779 struct net_device *ndev = platform_get_drvdata(pdev);
1780
1781 if (netif_running(ndev))
1782 cpsw_ndo_stop(ndev);
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001783 pm_runtime_put_sync(&pdev->dev);
1784
Mugunthan V Ndf828592012-03-18 20:17:54 +00001785 return 0;
1786}
1787
1788static int cpsw_resume(struct device *dev)
1789{
1790 struct platform_device *pdev = to_platform_device(dev);
1791 struct net_device *ndev = platform_get_drvdata(pdev);
1792
Mugunthan V Nf150bd72012-07-17 08:09:50 +00001793 pm_runtime_get_sync(&pdev->dev);
Mugunthan V Ndf828592012-03-18 20:17:54 +00001794 if (netif_running(ndev))
1795 cpsw_ndo_open(ndev);
1796 return 0;
1797}
1798
1799static const struct dev_pm_ops cpsw_pm_ops = {
1800 .suspend = cpsw_suspend,
1801 .resume = cpsw_resume,
1802};
1803
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001804static const struct of_device_id cpsw_of_mtable[] = {
1805 { .compatible = "ti,cpsw", },
1806 { /* sentinel */ },
1807};
1808
Mugunthan V Ndf828592012-03-18 20:17:54 +00001809static struct platform_driver cpsw_driver = {
1810 .driver = {
1811 .name = "cpsw",
1812 .owner = THIS_MODULE,
1813 .pm = &cpsw_pm_ops,
Mugunthan V N2eb32b02012-07-30 10:17:14 +00001814 .of_match_table = of_match_ptr(cpsw_of_mtable),
Mugunthan V Ndf828592012-03-18 20:17:54 +00001815 },
1816 .probe = cpsw_probe,
Bill Pemberton663e12e2012-12-03 09:23:45 -05001817 .remove = cpsw_remove,
Mugunthan V Ndf828592012-03-18 20:17:54 +00001818};
1819
1820static int __init cpsw_init(void)
1821{
1822 return platform_driver_register(&cpsw_driver);
1823}
1824late_initcall(cpsw_init);
1825
1826static void __exit cpsw_exit(void)
1827{
1828 platform_driver_unregister(&cpsw_driver);
1829}
1830module_exit(cpsw_exit);
1831
1832MODULE_LICENSE("GPL");
1833MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
1834MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
1835MODULE_DESCRIPTION("TI CPSW Ethernet driver");