cpsw: add missing fields to the CPSW_SS register bank.
[am335x:linux-am33x.git] / drivers / net / ethernet / ti / cpsw.c
1 /*
2  * Texas Instruments Ethernet Switch Driver
3  *
4  * Copyright (C) 2010 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 #include <linux/kernel.h>
16 #include <linux/io.h>
17 #include <linux/clk.h>
18 #include <linux/timer.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/if_ether.h>
22 #include <linux/etherdevice.h>
23 #include <linux/ethtool.h>
24 #include <linux/netdevice.h>
25 #include <linux/phy.h>
26 #include <linux/workqueue.h>
27 #include <linux/delay.h>
28 #include <linux/interrupt.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/if_vlan.h>
31 #include <linux/net_switch_config.h>
32
33 #include <linux/cpsw.h>
34 #include <plat/dmtimer.h>
35 #include "cpsw_ale.h"
36 #include "davinci_cpdma.h"
37
38
39 #define CPSW_DEBUG      (NETIF_MSG_HW           | NETIF_MSG_WOL         | \
40                          NETIF_MSG_DRV          | NETIF_MSG_LINK        | \
41                          NETIF_MSG_IFUP         | NETIF_MSG_INTR        | \
42                          NETIF_MSG_PROBE        | NETIF_MSG_TIMER       | \
43                          NETIF_MSG_IFDOWN       | NETIF_MSG_RX_ERR      | \
44                          NETIF_MSG_TX_ERR       | NETIF_MSG_TX_DONE     | \
45                          NETIF_MSG_PKTDATA      | NETIF_MSG_TX_QUEUED   | \
46                          NETIF_MSG_RX_STATUS)
47
48 #define msg(level, type, format, ...)                           \
49 do {                                                            \
50         if (netif_msg_##type(priv) && net_ratelimit())          \
51                 dev_##level(priv->dev, format, ## __VA_ARGS__); \
52 } while (0)
53
54 #define CPDMA_RXTHRESH          0x0c0
55 #define CPDMA_RXFREE            0x0e0
56 #define CPDMA_TXHDP_VER1        0x100
57 #define CPDMA_TXHDP_VER2        0x200
58 #define CPDMA_RXHDP_VER1        0x120
59 #define CPDMA_RXHDP_VER2        0x220
60 #define CPDMA_TXCP_VER1         0x140
61 #define CPDMA_TXCP_VER2         0x240
62 #define CPDMA_RXCP_VER1         0x160
63 #define CPDMA_RXCP_VER2         0x260
64
65 #define CPSW_POLL_WEIGHT        64
66 #define CPSW_MIN_PACKET_SIZE    60
67 #define CPSW_MAX_PACKET_SIZE    (1500 + 14 + 4 + 4)
68 #define CPSW_PHY_SPEED          1000
69
70 #define CPSW_PRIMAP(shift, priority)    (priority << (shift * 4))
71
72 #define slave(priv, idx)        ((priv)->slaves + idx)
73 #define switchcmd(__cmd__)      ((__cmd__)->cmd_data.switchcmd)
74 #define portcmd(__cmd__)        ((__cmd__)->cmd_data.portcmd)
75 #define priocmd(__cmd__)        ((__cmd__)->cmd_data.priocmd)
76
77 /* CPSW control module masks */
78 #define CPSW_INTPACEEN          (0x3 << 16)
79 #define CPSW_INTPRESCALE_MASK   (0x7FF << 0)
80 #define CPSW_CMINTMAX_CNT       63
81 #define CPSW_CMINTMIN_CNT       2
82 #define CPSW_CMINTMAX_INTVL     (1000 / CPSW_CMINTMIN_CNT)
83 #define CPSW_CMINTMIN_INTVL     ((1000 / CPSW_CMINTMAX_CNT) + 1)
84
85 #define cpsw_enable_irq(priv)   \
86         do {                    \
87                 u32 i;          \
88                 for (i = 0; i < priv->num_irqs; i++) \
89                         enable_irq(priv->irqs_table[i]); \
90         } while (0);
91 #define cpsw_disable_irq(priv)  \
92         do {                    \
93                 u32 i;          \
94                 for (i = 0; i < priv->num_irqs; i++) \
95                         disable_irq_nosync(priv->irqs_table[i]); \
96         } while (0);
97
98 #define ALE_ALL_PORTS           0x7
99
100 #define CPSW_CPDMA_EOI_REG      0x894
101 #define CPSW_TIMER_MASK         0xA0908
102 #define CPSW_TIMER_CAP_REG      0xFD0
103 #define CPSW_RX_TIMER_REQ       5
104 #define CPSW_TX_TIMER_REQ       6
105
106 #ifdef CONFIG_TI_CPSW_DUAL_EMAC
107
108 /* Enable VLAN aware mode to add VLAN for induvudual interface */
109 #define CPSW_VLAN_AWARE_MODE
110
111 #define cpsw_get_slave_ndev(priv, __slave_no__)         \
112         (priv->slaves[__slave_no__].ndev)
113 #define cpsw_get_slave_priv(priv, __slave_no__)         \
114         netdev_priv(priv->slaves[__slave_no__].ndev)
115 #define for_each_slave(priv, func, arg...)                      \
116         do {                                                    \
117                 (func)((priv)->slaves + priv->emac_port, ##arg);\
118         } while (0)
119 #define cpsw_dual_emac_source_port_detect(status, priv, ndev, skb)      \
120         do {                                                            \
121                 if (CPDMA_RX_SOURCE_PORT(status) == 1) {                \
122                         ndev = cpsw_get_slave_ndev(priv, 0);            \
123                         priv = netdev_priv(ndev);                       \
124                         skb->dev = ndev;                                \
125                 } else if (CPDMA_RX_SOURCE_PORT(status) == 2) {         \
126                         ndev = cpsw_get_slave_ndev(priv, 1);            \
127                         priv = netdev_priv(ndev);                       \
128                         skb->dev = ndev;                                \
129                 }                                                       \
130         } while (0)
131 #define cpsw_add_switch_mode_bcast_ale_entries(priv, slave_port)
132 #define cpsw_update_slave_open_state(priv, state)               \
133         priv->slaves[priv->emac_port].open_stat = state;
134 #define cpsw_slave_phy_index(priv)      ((priv)->emac_port)
135
136 #else   /* CONFIG_TI_CPSW_DUAL_EMAC */
137
138 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
139 #define VLAN_SUPPORT
140 #define CPSW_VLAN_AWARE_MODE
141 #endif
142
143 #define cpsw_get_slave_ndev(priv, __slave_no__)         NULL
144 #define cpsw_get_slave_priv(priv, __slave_no__)         NULL
145 #define for_each_slave(priv, func, arg...)                      \
146         do {                                                    \
147                 int idx;                                        \
148                 for (idx = 0; idx < (priv)->data.slaves; idx++) \
149                         (func)((priv)->slaves + idx, ##arg);    \
150         } while (0)
151 #define cpsw_dual_emac_source_port_detect(status, priv, ndev, skb)
152 #define cpsw_add_switch_mode_bcast_ale_entries(priv, slave_port)        \
153         cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,            \
154                            1 << slave_port, 0, 0)
155 #define cpsw_update_slave_open_state(priv, state)
156 #define cpsw_slave_phy_index(priv)      ((priv)->data.ethtool_slave)
157
158 #endif  /* CONFIG_TI_CPSW_DUAL_EMAC */
159
160 #ifdef CPSW_VLAN_AWARE_MODE
161 #define CPSW_VLAN_AWARE         BIT(1)
162 #define CPSW_ALE_VLAN_AWARE     1
163 #else
164 #define CPSW_VLAN_AWARE         0x0
165 #define CPSW_ALE_VLAN_AWARE     0
166 #endif
167
168 struct omap_dm_timer *dmtimer_rx;
169 struct omap_dm_timer *dmtimer_tx;
170
171 extern u32 omap_ctrl_readl(u16 offset);
172 extern void omap_ctrl_writel(u32 val, u16 offset);
173
174 static int debug_level;
175 module_param(debug_level, int, 0);
176 MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
177
178 static int ale_ageout = 10;
179 module_param(ale_ageout, int, 0);
180 MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
181
182 static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
183 module_param(rx_packet_max, int, 0);
184 MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
185
186 struct cpsw_wr_regs {
187         u32     id_ver;
188         u32     soft_reset;
189         u32     control;
190         u32     int_control;
191         u32     rx_thresh_en;
192         u32     rx_en;
193         u32     tx_en;
194         u32     misc_en;
195         u32     mem_allign1[8];
196         u32     rx_thresh_stat;
197         u32     rx_stat;
198         u32     tx_stat;
199         u32     misc_stat;
200         u32     mem_allign2[8];
201         u32     rx_imax;
202         u32     tx_imax;
203 };
204
205 struct cpsw_ss_regs {
206         u32     id_ver;
207         u32     control;
208         u32     soft_reset;
209         u32     stat_port_en;
210         u32     ptype;
211         u32     soft_idle;
212         u32     thru_rate;
213         u32     gap_thresh;
214         u32     tx_start_wds;
215         u32     flow_control;
216         u32     vlan_ltype;
217         u32     ts_ltype;
218         u32     dlr_ltype;
219 };
220
221 struct cpsw_slave_regs {
222         u32     max_blks;
223         u32     blk_cnt;
224         u32     flow_thresh;
225         u32     port_vlan;
226         u32     tx_pri_map;
227         u32     ts_seq_mtype;
228 #ifdef CONFIG_ARCH_TI814X
229         u32     ts_ctl;
230         u32     ts_seq_ltype;
231         u32     ts_vlan;
232 #endif
233         u32     sa_lo;
234         u32     sa_hi;
235 };
236
237 struct cpsw_host_regs {
238         u32     max_blks;
239         u32     blk_cnt;
240         u32     flow_thresh;
241         u32     port_vlan;
242         u32     tx_pri_map;
243         u32     cpdma_tx_pri_map;
244         u32     cpdma_rx_chan_map;
245 };
246
247 struct cpsw_sliver_regs {
248         u32     id_ver;
249         u32     mac_control;
250         u32     mac_status;
251         u32     soft_reset;
252         u32     rx_maxlen;
253         u32     __reserved_0;
254         u32     rx_pause;
255         u32     tx_pause;
256         u32     __reserved_1;
257         u32     rx_pri_map;
258 };
259
260 struct cpsw_hw_stats {
261         u32     rxgoodframes;
262         u32     rxbroadcastframes;
263         u32     rxmulticastframes;
264         u32     rxpauseframes;
265         u32     rxcrcerrors;
266         u32     rxaligncodeerrors;
267         u32     rxoversizedframes;
268         u32     rxjabberframes;
269         u32     rxundersizedframes;
270         u32     rxfragments;
271         u32     __pad_0[2];
272         u32     rxoctets;
273         u32     txgoodframes;
274         u32     txbroadcastframes;
275         u32     txmulticastframes;
276         u32     txpauseframes;
277         u32     txdeferredframes;
278         u32     txcollisionframes;
279         u32     txsinglecollframes;
280         u32     txmultcollframes;
281         u32     txexcessivecollisions;
282         u32     txlatecollisions;
283         u32     txunderrun;
284         u32     txcarriersenseerrors;
285         u32     txoctets;
286         u32     octetframes64;
287         u32     octetframes65t127;
288         u32     octetframes128t255;
289         u32     octetframes256t511;
290         u32     octetframes512t1023;
291         u32     octetframes1024tup;
292         u32     netoctets;
293         u32     rxsofoverruns;
294         u32     rxmofoverruns;
295         u32     rxdmaoverruns;
296 };
297
298 struct cpsw_slave {
299         struct cpsw_slave_regs __iomem  *regs;
300         struct cpsw_sliver_regs __iomem *sliver;
301         int                             slave_num;
302         u32                             mac_control;
303         struct cpsw_slave_data          *data;
304         struct phy_device               *phy;
305         u32                             port_vlan;
306         struct net_device               *ndev;
307         u32                             open_stat;
308 };
309
310 struct cpsw_priv {
311         spinlock_t                      lock;
312         struct platform_device          *pdev;
313         struct net_device               *ndev;
314         struct resource                 *cpsw_res;
315         struct resource                 *cpsw_ss_res;
316         struct napi_struct              napi;
317 #define napi_to_priv(napi)      container_of(napi, struct cpsw_priv, napi)
318         struct device                   *dev;
319         struct cpsw_platform_data       data;
320         struct cpsw_ss_regs __iomem     *regs;
321         struct cpsw_wr_regs __iomem     *wr_regs;
322         struct cpsw_hw_stats __iomem    *hw_stats;
323         struct cpsw_host_regs __iomem   *host_port_regs;
324         u32                             msg_enable;
325         u32                             coal_intvl;
326         u32                             bus_freq_mhz;
327         struct net_device_stats         stats;
328         int                             rx_packet_max;
329         int                             host_port;
330         struct clk                      *clk;
331         u8                              mac_addr[ETH_ALEN];
332         struct cpsw_slave               *slaves;
333         struct cpdma_ctlr               *dma;
334         struct cpdma_chan               *txch, *rxch;
335         struct cpsw_ale                 *ale;
336         u32                             emac_port;
337         u8                              port_state[3];
338         /* snapshot of IRQ numbers */
339         u32 irqs_table[4];
340         u32 num_irqs;
341 };
342
343 #ifdef CONFIG_TI_CPSW_DUAL_EMAC
344
345 static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
346 {
347         struct cpsw_priv *priv = netdev_priv(ndev);
348         struct cpsw_slave *slave = priv->slaves + priv->emac_port;
349
350         /* Clear all mcast from ALE */
351         cpsw_ale_flush_vlan_multicast(priv->ale, slave->port_vlan,
352                         1 << (priv->emac_port + 1) | 1 << priv->host_port);
353
354         if (!netdev_mc_empty(ndev)) {
355                 struct netdev_hw_addr *ha;
356
357                 /* program multicast address list into ALE register */
358                 netdev_for_each_mc_addr(ha, ndev) {
359                         cpsw_ale_vlan_add_mcast(priv->ale, (u8 *)ha->addr,
360                                         1 << (priv->emac_port + 1) |
361                                         1 << priv->host_port,
362                                         slave->port_vlan, 0, 0);
363                 }
364         }
365 }
366
367 static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags)
368 {
369         struct cpsw_priv *priv = netdev_priv(ndev);
370         struct cpsw_ale *ale = priv->ale;
371
372         if (flags & IFF_PROMISC) {
373                 if ((priv->slaves[0].ndev->flags & IFF_PROMISC) ||
374                                 (priv->slaves[1].ndev->flags & IFF_PROMISC)) {
375                         /*
376                          * Enabling promiscuous mode for one interface will be
377                          * common for both the interface as the interface
378                          * shares the same hardware resource.
379                          */
380
381                         /* Enable Bypass */
382                         cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
383
384                         dev_err(&ndev->dev, "promiscuity enabled\n");
385                 } else {
386                         /* Disable Bypass */
387                         cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
388                         dev_err(&ndev->dev, "promiscuity disabled\n");
389                 }
390         }
391
392         /*
393          * The switch cannot filter multicast traffic unless it is configured
394          * in "VLAN Aware" mode.  Unfortunately, VLAN awareness requires a
395          * whole bunch of additional logic that this driver does not implement
396          * at present.
397          */
398         if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI))
399                 dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n");
400 }
401
402 static inline void cpsw_p0_fifo_type_select(struct cpsw_priv *priv)
403 {
404         u32 reg;
405         reg = readl(&priv->host_port_regs->flow_thresh);
406         reg &= ~(0x3 << 16);
407         reg |= 0x1 << 16;
408         writel(reg, &priv->host_port_regs->flow_thresh);
409 }
410
411 static inline void cpsw_add_dual_emac_mode_default_ale_entries(
412                 struct cpsw_priv *priv, struct cpsw_slave *slave,
413                 u32 slave_port)
414 {
415         writel(slave->port_vlan, &slave->regs->port_vlan);
416         cpsw_ale_add_vlan(priv->ale, slave->port_vlan,
417                 1 << slave_port | 1 << priv->host_port, 0,
418                 1 << slave_port | 1 << priv->host_port,
419                 1 << slave_port | 1 << priv->host_port);
420         cpsw_ale_vlan_add_mcast(priv->ale,
421                 priv->ndev->broadcast,
422                 1 << slave_port | 1 << priv->host_port,
423                 slave->port_vlan, 0, 0);
424         cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
425                 priv->host_port, 0);
426         cpsw_ale_add_vlan(priv->ale, 0,
427                 ALE_ALL_PORTS << priv->host_port,
428                 ALE_ALL_PORTS << priv->host_port, priv->host_port, 0);
429 }
430
431 static int cpsw_common_res_usage_state(struct cpsw_priv *priv)
432 {
433         u32 i;
434         u32 usage_count = 0;
435
436         for (i = 0; i < priv->data.slaves; i++)
437                 if (priv->slaves[i].open_stat)
438                         usage_count++;
439
440         return usage_count;
441 }
442
443 static inline int cpsw_tx_packet_submit(struct net_device *ndev,
444                         struct cpsw_priv *priv, struct sk_buff *skb)
445 {
446         if (ndev == cpsw_get_slave_ndev(priv, 0))
447                 return cpdma_chan_submit(priv->txch, skb, skb->data,
448                                   skb->len, 1, GFP_KERNEL);
449         else
450                 return cpdma_chan_submit(priv->txch, skb, skb->data,
451                                   skb->len, 2, GFP_KERNEL);
452 }
453
454 #define cpsw_add_switch_mode_default_ale_entries(priv)
455
456 #else   /* CONFIG_TI_CPSW_DUAL_EMAC */
457
458 static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
459 {
460         struct cpsw_priv *priv = netdev_priv(ndev);
461
462         /* Clear all mcast from ALE */
463         cpsw_ale_flush_multicast(priv->ale, ALE_ALL_PORTS << priv->host_port);
464
465         if (!netdev_mc_empty(ndev)) {
466                 struct netdev_hw_addr *ha;
467
468                 /* program multicast address list into ALE register */
469                 netdev_for_each_mc_addr(ha, ndev) {
470                         cpsw_ale_add_mcast(priv->ale, (u8 *)ha->addr,
471                                 ALE_ALL_PORTS << priv->host_port, 0, 0);
472                 }
473         }
474 }
475
476 static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags)
477 {
478         struct cpsw_priv *priv = netdev_priv(ndev);
479         struct cpsw_ale *ale = priv->ale;
480         int i;
481
482         if (flags & IFF_PROMISC) {
483                 if (ndev->flags & IFF_PROMISC) {
484                         unsigned long timeout = jiffies + HZ;
485
486                         /* Disable Learn for all ports */
487                         for (i = 0; i <= priv->data.slaves; i++) {
488                                 cpsw_ale_control_set(ale, i,
489                                                      ALE_PORT_NOLEARN, 1);
490                                 cpsw_ale_control_set(ale, i,
491                                                      ALE_PORT_NO_SA_UPDATE, 1);
492                         }
493
494                         /* Clear All Untouched entries */
495                         cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
496                         do {
497                                 cpu_relax();
498                                 if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
499                                         break;
500                         } while (time_after(timeout, jiffies));
501                         cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
502
503                         /* Clear all mcast from ALE */
504                         cpsw_ale_flush_multicast(ale,
505                                         ALE_ALL_PORTS << priv->host_port);
506
507                         /* Flood All Unicast Packets to Host port */
508                         cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
509                         dev_err(&ndev->dev, "promiscuity enabled\n");
510                 } else {
511                         /* Flood All Unicast Packets to Host port */
512                         cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
513
514                         /* Enable Learn for all ports */
515                         for (i = 0; i <= priv->data.slaves; i++) {
516                                 cpsw_ale_control_set(ale, i,
517                                                      ALE_PORT_NOLEARN, 0);
518                                 cpsw_ale_control_set(ale, i,
519                                                      ALE_PORT_NO_SA_UPDATE, 0);
520                         }
521
522                         cpsw_ndo_set_rx_mode(ndev);
523                         dev_err(&ndev->dev, "promiscuity disabled\n");
524                 }
525         }
526
527         /*
528          * The switch cannot filter multicast traffic unless it is configured
529          * in "VLAN Aware" mode.  Unfortunately, VLAN awareness requires a
530          * whole bunch of additional logic that this driver does not implement
531          * at present.
532          */
533         if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI))
534                 dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n");
535 }
536
537 #define cpsw_p0_fifo_type_select(priv)
538 #define cpsw_add_dual_emac_mode_default_ale_entries(priv, slave, slave_port)
539 #define cpsw_update_slave_open_state(priv, state)
540 #define cpsw_common_res_usage_state(priv)       0
541 #define cpsw_tx_packet_submit(ndev, priv, skb)          \
542         cpdma_chan_submit(priv->txch, skb, skb->data,   \
543                           skb->len, 0, GFP_KERNEL)
544
545 static inline void cpsw_add_switch_mode_default_ale_entries(
546                         struct cpsw_priv *priv)
547 {
548         cpsw_ale_add_ucast(priv->ale, priv->mac_addr,
549                            priv->host_port, 0);
550         cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
551                            1 << priv->host_port, 0, 0);
552 }
553
554 #endif  /* CONFIG_TI_CPSW_DUAL_EMAC */
555
556 static int cpsw_set_coalesce(struct net_device *ndev,
557                         struct ethtool_coalesce *coal);
558
559 static void cpsw_intr_enable(struct cpsw_priv *priv)
560 {
561         __raw_writel(0xFF, &priv->wr_regs->tx_en);
562         __raw_writel(0xFF, &priv->wr_regs->rx_en);
563
564         cpdma_ctlr_int_ctrl(priv->dma, true);
565         return;
566 }
567
568 static void cpsw_intr_disable(struct cpsw_priv *priv)
569 {
570         __raw_writel(0, &priv->wr_regs->tx_en);
571         __raw_writel(0, &priv->wr_regs->rx_en);
572
573         cpdma_ctlr_int_ctrl(priv->dma, false);
574         return;
575 }
576
577 void cpsw_tx_handler(void *token, int len, int status)
578 {
579         struct sk_buff          *skb = token;
580         struct net_device       *ndev = skb->dev;
581         struct cpsw_priv        *priv = netdev_priv(ndev);
582
583         if (unlikely(netif_queue_stopped(ndev)))
584                 netif_start_queue(ndev);
585         priv->stats.tx_packets++;
586         priv->stats.tx_bytes += len;
587         dev_kfree_skb_any(skb);
588 }
589
590 void cpsw_rx_handler(void *token, int len, int status)
591 {
592         struct sk_buff          *skb = token;
593         struct net_device       *ndev = skb->dev;
594         struct cpsw_priv        *priv = netdev_priv(ndev);
595         int                     ret = 0;
596
597         cpsw_dual_emac_source_port_detect(status, priv, ndev, skb);
598
599         if (unlikely(!netif_running(ndev)) ||
600                         unlikely(!netif_carrier_ok(ndev))) {
601                 dev_kfree_skb_any(skb);
602                 return;
603         }
604
605         if (likely(status >= 0)) {
606                 skb_put(skb, len);
607                 skb->protocol = eth_type_trans(skb, ndev);
608                 netif_receive_skb(skb);
609                 priv->stats.rx_bytes += len;
610                 priv->stats.rx_packets++;
611                 skb = NULL;
612         }
613
614
615         if (unlikely(!netif_running(ndev))) {
616                 if (skb)
617                         dev_kfree_skb_any(skb);
618                 return;
619         }
620
621         if (likely(!skb)) {
622                 skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
623                 if (WARN_ON(!skb))
624                         return;
625
626                 ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
627                                 skb_tailroom(skb), 0, GFP_KERNEL);
628         }
629
630         WARN_ON(ret < 0);
631
632 }
633
634 static void set_cpsw_dmtimer_clear(void)
635 {
636         omap_dm_timer_write_status(dmtimer_rx, OMAP_TIMER_INT_CAPTURE);
637         omap_dm_timer_write_status(dmtimer_tx, OMAP_TIMER_INT_CAPTURE);
638
639         return;
640 }
641
642 static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
643 {
644         struct cpsw_priv *priv = dev_id;
645
646         if (likely(netif_running(priv->ndev))) {
647                 cpsw_intr_disable(priv);
648                 cpsw_disable_irq(priv);
649                 napi_schedule(&priv->napi);
650         } else {
651                 priv = cpsw_get_slave_priv(priv, 1);
652                 if (likely(priv) && likely(netif_running(priv->ndev))) {
653                         cpsw_intr_disable(priv);
654                         cpsw_disable_irq(priv);
655                         napi_schedule(&priv->napi);
656                 }
657         }
658
659         return IRQ_HANDLED;
660 }
661
662 static int cpsw_poll(struct napi_struct *napi, int budget)
663 {
664         struct cpsw_priv        *priv = napi_to_priv(napi);
665         int                     num_tx, num_rx;
666
667         num_tx = cpdma_chan_process(priv->txch, 128);
668         num_rx = cpdma_chan_process(priv->rxch, budget);
669
670         if (num_rx || num_tx)
671                 msg(dbg, intr, "poll %d rx, %d tx pkts\n", num_rx, num_tx);
672
673         if (num_rx < budget) {
674                 napi_complete(napi);
675                 cpdma_ctlr_eoi(priv->dma);
676                 set_cpsw_dmtimer_clear();
677                 cpsw_intr_enable(priv);
678                 cpsw_enable_irq(priv);
679         }
680
681         return num_rx;
682 }
683
684 static inline void soft_reset(const char *module, void __iomem *reg)
685 {
686         unsigned long timeout = jiffies + HZ;
687
688         __raw_writel(1, reg);
689         do {
690                 cpu_relax();
691         } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
692
693         WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
694 }
695
696 #define mac_hi(mac)     (((mac)[0] << 0) | ((mac)[1] << 8) |    \
697                          ((mac)[2] << 16) | ((mac)[3] << 24))
698 #define mac_lo(mac)     (((mac)[4] << 0) | ((mac)[5] << 8))
699
700 static void cpsw_set_slave_mac(struct cpsw_slave *slave,
701                                struct cpsw_priv *priv)
702 {
703         __raw_writel(mac_hi(priv->mac_addr), &slave->regs->sa_hi);
704         __raw_writel(mac_lo(priv->mac_addr), &slave->regs->sa_lo);
705 }
706
707 static inline u32 cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
708 {
709         if (priv->host_port == 0)
710                 return slave_num + 1;
711         else
712                 return slave_num;
713 }
714
715 static void _cpsw_adjust_link(struct cpsw_slave *slave,
716                               struct cpsw_priv *priv, bool *link)
717 {
718         struct phy_device       *phy = slave->phy;
719         u32                     mac_control = 0;
720         u32                     slave_port;
721
722         if (!phy)
723                 return;
724
725         slave_port = cpsw_get_slave_port(priv, slave->slave_num);
726
727         if (phy->link) {
728                 /* enable forwarding */
729                 cpsw_ale_control_set(priv->ale, slave_port,
730                         ALE_PORT_STATE, priv->port_state[slave_port]);
731
732                 mac_control = priv->data.mac_control;
733                 if (phy->speed == 10)
734                         mac_control |= BIT(18); /* In Band mode */
735                 if (phy->speed == 1000) {
736                         mac_control |= BIT(7);  /* Enable gigabit mode */
737                 }
738                 if (phy->speed == 100)
739                         mac_control |= BIT(15);
740                 if (phy->duplex)
741                         mac_control |= BIT(0);  /* FULLDUPLEXEN */
742                 if (phy->interface == PHY_INTERFACE_MODE_RGMII) /* RGMII */
743                         mac_control |= (BIT(15)|BIT(16));
744                 *link = true;
745         } else {
746                 cpsw_ale_control_set(priv->ale, slave_port,
747                              ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
748                 mac_control = 0;
749         }
750
751         if (mac_control != slave->mac_control) {
752                 phy_print_status(phy);
753                 __raw_writel(mac_control, &slave->sliver->mac_control);
754         }
755
756         slave->mac_control = mac_control;
757 }
758
759 static void cpsw_adjust_link(struct net_device *ndev)
760 {
761         struct cpsw_priv        *priv = netdev_priv(ndev);
762         bool                    link = false;
763
764         for_each_slave(priv, _cpsw_adjust_link, priv, &link);
765
766         if (link) {
767                 netif_carrier_on(ndev);
768                 if (netif_running(ndev))
769                         netif_wake_queue(ndev);
770         } else {
771                 netif_carrier_off(ndev);
772                 netif_stop_queue(ndev);
773         }
774 }
775
776 static inline int __show_stat(char *buf, int maxlen, const char* name, u32 val)
777 {
778         static char *leader = "........................................";
779
780         if (!val)
781                 return 0;
782         else
783                 return snprintf(buf, maxlen, "%s %s %10d\n", name,
784                                 leader + strlen(name), val);
785 }
786
787 static ssize_t cpsw_hw_stats_show(struct device *dev,
788                                      struct device_attribute *attr,
789                                      char *buf)
790 {
791         struct net_device       *ndev = to_net_dev(dev);
792         struct cpsw_priv        *priv = netdev_priv(ndev);
793         int                     len = 0;
794         struct cpdma_chan_stats dma_stats;
795
796 #define show_stat(x) do {                                               \
797         len += __show_stat(buf + len, SZ_4K - len, #x,                  \
798                            __raw_readl(&priv->hw_stats->x));            \
799 } while (0)
800
801 #define show_dma_stat(x) do {                                           \
802         len += __show_stat(buf + len, SZ_4K - len, #x, dma_stats.x);    \
803 } while (0)
804
805         len += snprintf(buf + len, SZ_4K - len, "CPSW Statistics:\n");
806         show_stat(rxgoodframes);        show_stat(rxbroadcastframes);
807         show_stat(rxmulticastframes);   show_stat(rxpauseframes);
808         show_stat(rxcrcerrors);         show_stat(rxaligncodeerrors);
809         show_stat(rxoversizedframes);   show_stat(rxjabberframes);
810         show_stat(rxundersizedframes);  show_stat(rxfragments);
811         show_stat(rxoctets);            show_stat(txgoodframes);
812         show_stat(txbroadcastframes);   show_stat(txmulticastframes);
813         show_stat(txpauseframes);       show_stat(txdeferredframes);
814         show_stat(txcollisionframes);   show_stat(txsinglecollframes);
815         show_stat(txmultcollframes);    show_stat(txexcessivecollisions);
816         show_stat(txlatecollisions);    show_stat(txunderrun);
817         show_stat(txcarriersenseerrors); show_stat(txoctets);
818         show_stat(octetframes64);       show_stat(octetframes65t127);
819         show_stat(octetframes128t255);  show_stat(octetframes256t511);
820         show_stat(octetframes512t1023); show_stat(octetframes1024tup);
821         show_stat(netoctets);           show_stat(rxsofoverruns);
822         show_stat(rxmofoverruns);       show_stat(rxdmaoverruns);
823
824         cpdma_chan_get_stats(priv->rxch, &dma_stats);
825         len += snprintf(buf + len, SZ_4K - len, "\nRX DMA Statistics:\n");
826         show_dma_stat(head_enqueue);    show_dma_stat(tail_enqueue);
827         show_dma_stat(pad_enqueue);     show_dma_stat(misqueued);
828         show_dma_stat(desc_alloc_fail); show_dma_stat(pad_alloc_fail);
829         show_dma_stat(runt_receive_buff); show_dma_stat(runt_transmit_buff);
830         show_dma_stat(empty_dequeue);   show_dma_stat(busy_dequeue);
831         show_dma_stat(good_dequeue);    show_dma_stat(teardown_dequeue);
832
833         cpdma_chan_get_stats(priv->txch, &dma_stats);
834         len += snprintf(buf + len, SZ_4K - len, "\nTX DMA Statistics:\n");
835         show_dma_stat(head_enqueue);    show_dma_stat(tail_enqueue);
836         show_dma_stat(pad_enqueue);     show_dma_stat(misqueued);
837         show_dma_stat(desc_alloc_fail); show_dma_stat(pad_alloc_fail);
838         show_dma_stat(runt_receive_buff); show_dma_stat(runt_transmit_buff);
839         show_dma_stat(empty_dequeue);   show_dma_stat(busy_dequeue);
840         show_dma_stat(good_dequeue);    show_dma_stat(teardown_dequeue);
841
842         return len;
843 }
844
845 DEVICE_ATTR(hw_stats, S_IRUGO, cpsw_hw_stats_show, NULL);
846
847 #define PHY_CONFIG_REG  22
848 static void cpsw_set_phy_config(struct cpsw_priv *priv, struct phy_device *phy)
849 {
850         struct cpsw_platform_data *pdata = priv->pdev->dev.platform_data;
851         struct mii_bus *miibus;
852         int phy_addr = 0;
853         u16 val = 0;
854         u16 tmp = 0;
855
856         if (!phy)
857                 return;
858
859         miibus = phy->bus;
860
861         if (!miibus)
862                 return;
863
864         phy_addr = phy->addr;
865
866         /* Disable 1 Gig mode support if it is not supported */
867         if (!pdata->gigabit_en)
868                 phy->supported &= ~(SUPPORTED_1000baseT_Half |
869                                         SUPPORTED_1000baseT_Full);
870
871         /* Following lines enable gigbit advertisement capability even in case
872          * the advertisement is not enabled by default
873          */
874         val = miibus->read(miibus, phy_addr, MII_BMCR);
875         val |= (BMCR_SPEED100 | BMCR_ANENABLE | BMCR_FULLDPLX);
876         miibus->write(miibus, phy_addr, MII_BMCR, val);
877         tmp = miibus->read(miibus, phy_addr, MII_BMCR);
878
879         /* Enable gigabit support only if the speed is 1000Mbps */
880         if (phy->speed == CPSW_PHY_SPEED) {
881                 tmp = miibus->read(miibus, phy_addr, MII_BMSR);
882                 if (tmp & 0x1) {
883                         val = miibus->read(miibus, phy_addr, MII_CTRL1000);
884                         val |= BIT(9);
885                         miibus->write(miibus, phy_addr, MII_CTRL1000, val);
886                         tmp = miibus->read(miibus, phy_addr, MII_CTRL1000);
887                 }
888         }
889
890         val = miibus->read(miibus, phy_addr, MII_ADVERTISE);
891         val |= (ADVERTISE_10HALF | ADVERTISE_10FULL | \
892                 ADVERTISE_100HALF | ADVERTISE_100FULL);
893         miibus->write(miibus, phy_addr, MII_ADVERTISE, val);
894         tmp = miibus->read(miibus, phy_addr, MII_ADVERTISE);
895
896         /* TODO : This check is required. This should be
897          * moved to a board init section as its specific
898          * to a phy.*/
899         if (phy->phy_id == 0x0282F014) {
900                 /* This enables TX_CLK-ing in case of 10/100MBps operation */
901                 val = miibus->read(miibus, phy_addr, PHY_CONFIG_REG);
902                 val |= BIT(5);
903                 miibus->write(miibus, phy_addr, PHY_CONFIG_REG, val);
904                 tmp = miibus->read(miibus, phy_addr, PHY_CONFIG_REG);
905         }
906
907         return;
908 }
909
910 static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
911 {
912         char name[32];
913         u32 slave_port;
914
915         sprintf(name, "slave-%d", slave->slave_num);
916
917         soft_reset(name, &slave->sliver->soft_reset);
918
919         /* setup priority mapping */
920         __raw_writel(0x76543210, &slave->sliver->rx_pri_map);
921         __raw_writel(0x33221100, &slave->regs->tx_pri_map);
922
923         /* setup max packet size, and mac address */
924         __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
925         cpsw_set_slave_mac(slave, priv);
926
927         slave->mac_control = 0; /* no link yet */
928
929         slave_port = cpsw_get_slave_port(priv, slave->slave_num);
930
931         cpsw_add_dual_emac_mode_default_ale_entries(priv, slave, slave_port);
932         cpsw_add_switch_mode_bcast_ale_entries(priv, slave_port);
933         priv->port_state[slave_port] = ALE_PORT_STATE_FORWARD;
934
935         slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
936                                  &cpsw_adjust_link, 0, slave->data->phy_if);
937         if (IS_ERR(slave->phy)) {
938                 msg(err, ifup, "phy %s not found on slave %d\n",
939                     slave->data->phy_id, slave->slave_num);
940                 slave->phy = NULL;
941         } else {
942                 dev_info(priv->dev, "CPSW phy found : id is : 0x%x\n",
943                         slave->phy->phy_id);
944                 cpsw_set_phy_config(priv, slave->phy);
945                 phy_start(slave->phy);
946         }
947 }
948
949 #if defined(VLAN_SUPPORT) && !defined(CONFIG_TI_CPSW_DUAL_EMAC)
950 static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
951 {
952         writel(priv->data.default_vlan, &priv->host_port_regs->port_vlan);
953         writel(priv->data.default_vlan, &priv->slaves[0].regs->port_vlan);
954         writel(priv->data.default_vlan, &priv->slaves[1].regs->port_vlan);
955         cpsw_ale_add_vlan(priv->ale, priv->data.default_vlan,
956                         ALE_ALL_PORTS << priv->host_port,
957                         ALE_ALL_PORTS << priv->host_port,
958                         ALE_ALL_PORTS << priv->host_port, 0);
959 }
960 #else
961 #define cpsw_add_default_vlan(priv)
962 #endif
963
964 static void cpsw_init_host_port(struct cpsw_priv *priv)
965 {
966         u32 control_reg;
967
968         /* soft reset the controller and initialize ale */
969         soft_reset("cpsw", &priv->regs->soft_reset);
970         cpsw_ale_start(priv->ale);
971
972         /* switch to vlan aware/unaware mode */
973         cpsw_ale_control_set(priv->ale, priv->host_port, ALE_VLAN_AWARE,
974                              CPSW_ALE_VLAN_AWARE);
975         control_reg = readl(&priv->regs->control);
976         control_reg |= CPSW_VLAN_AWARE;
977         writel(control_reg, &priv->regs->control);
978         cpsw_p0_fifo_type_select(priv);
979
980         /* setup host port priority mapping */
981         __raw_writel(0x76543210, &priv->host_port_regs->cpdma_tx_pri_map);
982         __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
983
984         cpsw_ale_control_set(priv->ale, priv->host_port,
985                              ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
986
987         cpsw_add_switch_mode_default_ale_entries(priv);
988 }
989
990 static int cpsw_ndo_open(struct net_device *ndev)
991 {
992         struct cpsw_priv *priv = netdev_priv(ndev);
993         int i, ret;
994         u32 reg;
995
996         if (!cpsw_common_res_usage_state(priv))
997                 cpsw_intr_disable(priv);
998         netif_carrier_off(ndev);
999
1000         if (priv->data.phy_control)
1001                 (*priv->data.phy_control)(true);
1002
1003         reg = __raw_readl(&priv->regs->id_ver);
1004
1005         msg(info, ifup, "initializing cpsw version %d.%d (%d)\n",
1006             (reg >> 8 & 0x7), reg & 0xff, (reg >> 11) & 0x1f);
1007
1008         /* initialize host and slave ports */
1009         if (!cpsw_common_res_usage_state(priv))
1010                 cpsw_init_host_port(priv);
1011         for_each_slave(priv, cpsw_slave_open, priv);
1012
1013         /* Add default VLAN */
1014         cpsw_add_default_vlan(priv);
1015
1016         if (!cpsw_common_res_usage_state(priv)) {
1017                 ret = device_create_file(&ndev->dev, &dev_attr_hw_stats);
1018                 if (ret < 0) {
1019                         dev_err(priv->dev, "unable to add device attr\n");
1020                         return ret;
1021                 }
1022
1023                 /* setup tx dma to fixed prio and zero offset */
1024                 cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
1025                 cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
1026
1027                 /* disable priority elevation and enable statistics */
1028                 /* on all ports */
1029                 writel(0, &priv->regs->ptype);
1030                 writel(0x7, &priv->regs->stat_port_en);
1031
1032                 if (WARN_ON(!priv->data.rx_descs))
1033                         priv->data.rx_descs = 128;
1034
1035                 for (i = 0; i < priv->data.rx_descs; i++) {
1036                         struct sk_buff *skb;
1037
1038                         ret = -ENOMEM;
1039                         skb = netdev_alloc_skb_ip_align(priv->ndev,
1040                                                         priv->rx_packet_max);
1041                         if (!skb)
1042                                 break;
1043                         ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
1044                                         skb_tailroom(skb), 0, GFP_KERNEL);
1045                         if (WARN_ON(ret < 0)) {
1046                                 dev_kfree_skb_any(skb);
1047                                 break;
1048                         }
1049                 }
1050                 /*
1051                  * continue even if we didn't manage to submit all
1052                  * receive descs
1053                  */
1054                 msg(info, ifup, "submitted %d rx descriptors\n", i);
1055         }
1056
1057         /* Enable Interrupt pacing if configured */
1058         if (priv->coal_intvl != 0) {
1059                 struct ethtool_coalesce coal;
1060
1061                 coal.rx_coalesce_usecs = (priv->coal_intvl << 4);
1062                 cpsw_set_coalesce(ndev, &coal);
1063         }
1064
1065         /* Enable Timer for capturing cpsw rx interrupts */
1066         omap_dm_timer_set_int_enable(dmtimer_rx, OMAP_TIMER_INT_CAPTURE);
1067         omap_dm_timer_set_capture(dmtimer_rx, 1, 0, 0);
1068         omap_dm_timer_enable(dmtimer_rx);
1069
1070         /* Enable Timer for capturing cpsw tx interrupts */
1071         omap_dm_timer_set_int_enable(dmtimer_tx, OMAP_TIMER_INT_CAPTURE);
1072         omap_dm_timer_set_capture(dmtimer_tx, 1, 0, 0);
1073         omap_dm_timer_enable(dmtimer_tx);
1074
1075         cpdma_ctlr_start(priv->dma);
1076         cpsw_intr_enable(priv);
1077         napi_enable(&priv->napi);
1078         cpdma_ctlr_eoi(priv->dma);
1079
1080         cpsw_update_slave_open_state(priv, true)
1081
1082         return 0;
1083 }
1084
1085 static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
1086 {
1087         if (!slave->phy)
1088                 return;
1089         phy_stop(slave->phy);
1090         phy_disconnect(slave->phy);
1091         slave->phy = NULL;
1092 }
1093
1094 static int cpsw_ndo_stop(struct net_device *ndev)
1095 {
1096         struct cpsw_priv *priv = netdev_priv(ndev);
1097
1098         msg(info, ifdown, "shutting down cpsw device\n");
1099
1100         netif_stop_queue(priv->ndev);
1101         napi_disable(&priv->napi);
1102         netif_carrier_off(priv->ndev);
1103
1104         if (cpsw_common_res_usage_state(priv) <= 1) {
1105                 cpsw_intr_disable(priv);
1106                 cpdma_ctlr_int_ctrl(priv->dma, false);
1107
1108                 omap_dm_timer_set_int_enable(dmtimer_rx, 0);
1109                 omap_dm_timer_set_int_enable(dmtimer_tx, 0);
1110
1111                 cpdma_ctlr_stop(priv->dma);
1112                 cpsw_ale_stop(priv->ale);
1113         }
1114
1115         device_remove_file(&ndev->dev, &dev_attr_hw_stats);
1116         for_each_slave(priv, cpsw_slave_stop, priv);
1117         if (priv->data.phy_control)
1118                 (*priv->data.phy_control)(false);
1119
1120         cpsw_update_slave_open_state(priv, false)
1121
1122         return 0;
1123 }
1124
1125 static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1126                                        struct net_device *ndev)
1127 {
1128         struct cpsw_priv *priv = netdev_priv(ndev);
1129         int ret;
1130
1131         ndev->trans_start = jiffies;
1132
1133         ret = skb_padto(skb, CPSW_MIN_PACKET_SIZE);
1134         if (unlikely(ret < 0)) {
1135                 msg(err, tx_err, "packet pad failed");
1136                 goto fail;
1137         }
1138
1139         ret = cpsw_tx_packet_submit(ndev, priv, skb);
1140         if (unlikely(ret != 0)) {
1141                 msg(err, tx_err, "desc submit failed");
1142                 goto fail;
1143         }
1144
1145         return NETDEV_TX_OK;
1146 fail:
1147         priv->stats.tx_dropped++;
1148         netif_stop_queue(ndev);
1149         return NETDEV_TX_BUSY;
1150 }
1151
1152 #ifndef CONFIG_TI_CPSW_DUAL_EMAC
1153 /*
1154  *  cpsw_set_priority_mapping
1155  *    This function configures the packet priority handling on each port.
1156  */
1157 static int cpsw_set_priority_mapping(struct cpsw_priv *priv, int port,
1158         int rxpriority, int txpriority, int switchpriority)
1159 {
1160         if (port == 0) {
1161                 writel(CPSW_PRIMAP(rxpriority, txpriority),
1162                         &priv->host_port_regs->cpdma_tx_pri_map);
1163                 writel(CPSW_PRIMAP(txpriority, switchpriority),
1164                         &priv->host_port_regs->cpdma_tx_pri_map);
1165         } else {
1166                 /* Configure sliver priority mapping registers */
1167                 writel(CPSW_PRIMAP(rxpriority, txpriority),
1168                         &slave(priv, port-1)->sliver->rx_pri_map);
1169                 writel(CPSW_PRIMAP(txpriority, switchpriority),
1170                         &slave(priv, port-1)->sliver->rx_pri_map);
1171         }
1172         return 0;
1173 }
1174
1175 /*
1176  * cpsw_rate_limit_tx
1177  * Limits the number of Tx packets on a port for a 100ms time period.
1178  * Rate limit can be done either on Tx or Rx for all the ports due to
1179  * hardware limitation
1180  */
1181 static int cpsw_rate_limit_tx(struct cpsw_ale *ale, u32 enable, u32 port,
1182                                 u32 addr_type, u32 limit)
1183 {
1184         u32 type;
1185
1186         if (!enable) {
1187                 cpsw_ale_control_set(ale, port, ALE_RATE_LIMIT, 0);
1188                 return 0;
1189         }
1190
1191         /* ALE prescale is set for 100 ms time */
1192         if (addr_type == ADDR_TYPE_BROADCAST)
1193                 type = ALE_PORT_BCAST_LIMIT;
1194         else if (addr_type == ADDR_TYPE_MULTICAST)
1195                 type = ALE_PORT_MCAST_LIMIT;
1196         else
1197                 return -EFAULT;
1198
1199         cpsw_ale_control_set(ale, port, type, limit);
1200         /* Enable the bit for rate limiting transmission */
1201         cpsw_ale_control_set(ale, port, ALE_RATE_LIMIT_TX, 1);
1202         cpsw_ale_control_set(ale, port, ALE_RATE_LIMIT, 1);
1203
1204         return 0;
1205 }
1206
1207 /*
1208  * cpsw_rate_limit_rx
1209  * Limits the number of Rx packets on a port for a 100ms time period.
1210  * Rate limit can be done either on Tx or Rx for all the ports due to
1211  * hardware limitation
1212  */
1213 static int cpsw_rate_limit_rx(struct cpsw_ale *ale, u32 enable, u32 port,
1214                                 u32 addr_type, u32 limit)
1215 {
1216         u32 type;
1217
1218         if (!enable) {
1219                 cpsw_ale_control_set(ale, port, ALE_RATE_LIMIT, 0);
1220                 return 0;
1221         }
1222
1223         /* ALE prescale is set for 100 ms time */
1224         if (addr_type == ADDR_TYPE_BROADCAST)
1225                 type = ALE_PORT_BCAST_LIMIT;
1226         else if (addr_type == ADDR_TYPE_MULTICAST)
1227                 type = ALE_PORT_MCAST_LIMIT;
1228         else
1229                 return -EFAULT;
1230
1231         cpsw_ale_control_set(ale, port, type, limit);
1232         /* Disable the bit for rate limiting reception */
1233         cpsw_ale_control_set(ale, port, ALE_RATE_LIMIT_TX, 0);
1234         cpsw_ale_control_set(ale, port, ALE_RATE_LIMIT, 1);
1235
1236         return 0;
1237 }
1238
1239 int cpsw_rate_limit(struct cpsw_ale *ale, u32 enable, u32 direction, u32 port,
1240                         u32 packet_type, u32 limit)
1241 {
1242
1243         if (port == 0) {
1244                 /*
1245                  * For host port transmit/receive terminlogy is inverse
1246                  * of cpgmac port
1247                  */
1248                 if (direction)
1249                         cpsw_rate_limit_rx(ale, enable, port,
1250                                         packet_type, limit);
1251                 else
1252                         cpsw_rate_limit_tx(ale, enable, port,
1253                                         packet_type, limit);
1254         } else {
1255                 if (direction)
1256                         cpsw_rate_limit_tx(ale, enable, port,
1257                                         packet_type, limit);
1258                 else
1259                         cpsw_rate_limit_rx(ale, enable, port,
1260                                         packet_type, limit);
1261         }
1262         return 0;
1263 }
1264
1265 static int cpsw_config_dump(struct cpsw_priv *priv, u8 *buf, u32 size)
1266 {
1267         int vlan_aware = 0;
1268         int out_len = 0;
1269         u32 port_state;
1270         char *port_state_str[] = {"disabled", "blocked", "learn", "forward"};
1271
1272         out_len += snprintf(buf, size, "Switch Configuarion\n");
1273
1274         vlan_aware = readl(&priv->regs->control) & CPSW_VLAN_AWARE;
1275
1276         out_len += snprintf(buf + out_len, size - out_len, "VLAN Aware Mode"
1277                         "                              :  %s\n",
1278                         (vlan_aware ? "Yes" : "No"));
1279         out_len += snprintf(buf + out_len, size - out_len, "ALE VLAN Aware "
1280                         "Mode                          :  %s\n",
1281                         (cpsw_ale_control_get(priv->ale, priv->host_port,
1282                                 ALE_VLAN_AWARE) ? "Yes" : "No"));
1283         out_len += snprintf(buf + out_len, size - out_len, "Unknown VLAN "
1284                         "Members                         :  %u\n",
1285                         cpsw_ale_control_get(priv->ale, 0,
1286                                 ALE_PORT_UNKNOWN_VLAN_MEMBER));
1287         out_len += snprintf(buf + out_len, size - out_len, "Unknown Multicast"
1288                         " Flood Mask                 :  %u\n",
1289                         cpsw_ale_control_get(priv->ale, 0,
1290                                 ALE_PORT_UNKNOWN_MCAST_FLOOD));
1291         out_len += snprintf(buf + out_len, size - out_len, "Unknown Registered"
1292                         " Multicast Flood Mask      :  %u\n",
1293                         cpsw_ale_control_get(priv->ale, 0,
1294                                 ALE_PORT_UNKNOWN_REG_MCAST_FLOOD));
1295         out_len += snprintf(buf + out_len, size - out_len, "Unknown VLAN Force"
1296                         " Untagged Egress           :  %u\n",
1297                         cpsw_ale_control_get(priv->ale, 0,
1298                                 ALE_PORT_UNTAGGED_EGRESS));
1299
1300         out_len += snprintf(buf + out_len, size - out_len,
1301                         "\nPort Configuration\n");
1302         out_len += snprintf(buf + out_len, size - out_len,
1303                         "\t%-8s %-8s %-8s %s\n", "PORT", "PVID", "PRIORITY",
1304                         "STATE");
1305         out_len += snprintf(buf + out_len, size - out_len,
1306                         "\t---------------------------------\n");
1307
1308         if (vlan_aware) {
1309                 int port_vlan;
1310
1311                 port_state = cpsw_ale_control_get(priv->ale, 0, ALE_PORT_STATE);
1312                 port_vlan = readl(&priv->host_port_regs->port_vlan);
1313                 out_len += snprintf(buf + out_len, size - out_len,
1314                         "\t%-8u %-8u %-8u %s\n", 0,
1315                         port_vlan & 0xfff, (port_vlan > 13) & 0x7,
1316                         port_state_str[port_state]);
1317
1318                 port_state = cpsw_ale_control_get(priv->ale, 1, ALE_PORT_STATE);
1319                 port_vlan = readl(&priv->slaves[0].regs->port_vlan);
1320                 out_len += snprintf(buf + out_len, size - out_len,
1321                         "\t%-8u %-8u %-8u %s\n", 1,
1322                         port_vlan & 0xfff, (port_vlan > 13) & 0x7,
1323                         port_state_str[port_state]);
1324
1325                 port_state = cpsw_ale_control_get(priv->ale, 2, ALE_PORT_STATE);
1326                 port_vlan = readl(&priv->slaves[1].regs->port_vlan);
1327                 out_len += snprintf(buf + out_len, size - out_len,
1328                         "\t%-8u %-8u %-8u %s\n", 2,
1329                         port_vlan & 0xfff, (port_vlan > 13) & 0x7,
1330                         port_state_str[port_state]);
1331         } else {
1332                 port_state = cpsw_ale_control_get(priv->ale, 0, ALE_PORT_STATE);
1333                 out_len += snprintf(buf + out_len, size - out_len,
1334                         "\t%-8u %-8s %-8s %s\n", 0,
1335                         "-", "-", port_state_str[port_state]);
1336
1337                 port_state = cpsw_ale_control_get(priv->ale, 1, ALE_PORT_STATE);
1338                 out_len += snprintf(buf + out_len, size - out_len,
1339                         "\t%-8u %-8s %-8s %s\n", 1,
1340                         "-", "-", port_state_str[port_state]);
1341
1342                 port_state = cpsw_ale_control_get(priv->ale, 2, ALE_PORT_STATE);
1343                 out_len += snprintf(buf + out_len, size - out_len,
1344                         "\t%-8u %-8s %-8s %s\n", 2,
1345                         "-", "-", port_state_str[port_state]);
1346         }
1347
1348         return out_len;
1349 }
1350
1351 static int cpsw_set_port_state(struct cpsw_priv *priv, int port,
1352                         int port_state)
1353 {
1354         int ret = -EFAULT;
1355         switch (port_state) {
1356         case PORT_STATE_DISABLED:
1357                 priv->port_state[port] = ALE_PORT_STATE_DISABLE;
1358                 break;
1359         case PORT_STATE_BLOCKED:
1360                 priv->port_state[port] = ALE_PORT_STATE_BLOCK;
1361                 break;
1362         case PORT_STATE_LEARN:
1363                 priv->port_state[port] = ALE_PORT_STATE_LEARN;
1364                 break;
1365         case PORT_STATE_FORWARD:
1366                 priv->port_state[port] = ALE_PORT_STATE_FORWARD;
1367                 break;
1368         }
1369         ret = cpsw_ale_control_set(priv->ale, port, ALE_PORT_STATE,
1370                         priv->port_state[port]);
1371
1372         return ret;
1373 }
1374
1375 static int cpsw_switch_config_ioctl(struct net_device *ndev,
1376                 struct ifreq *ifrq, int cmd)
1377 {
1378         struct cpsw_priv *priv = netdev_priv(ndev);
1379         struct net_switch_config *switch_config;
1380         int ret = -EFAULT;
1381
1382         /*
1383         * Only SIOCSWITCHCONFIG is used as cmd argument and hence, there is no
1384         * switch statement required.
1385         * Function calls are based on switch_config.cmd
1386         */
1387         if (cmd != SIOCSWITCHCONFIG)
1388                 return ret;
1389
1390         switch_config = kzalloc(sizeof(struct net_switch_config), GFP_KERNEL);
1391         if (copy_from_user(switch_config, (ifrq->ifr_data),
1392                         sizeof(struct net_switch_config)))
1393                 return ret;
1394
1395         switch (switch_config->cmd) {
1396         case CONFIG_SWITCH_ADD_MULTICAST:
1397                 if ((switchcmd(switch_config).untag_port <= 3) &&
1398                                 (switchcmd(switch_config).vid <= 4095) &&
1399                                 (switchcmd(switch_config).mem_port > 0) &&
1400                                 (switchcmd(switch_config).mem_port <= 7) &&
1401                                 is_multicast_ether_addr(
1402                                 switchcmd(switch_config).addr)) {
1403                         if (switchcmd(switch_config).vid == 0)
1404                                 ret = cpsw_ale_add_mcast(priv->ale,
1405                                         switchcmd(switch_config).addr,
1406                                         switchcmd(switch_config).mem_port
1407                                                 << priv->host_port,
1408                                         switchcmd(switch_config).flag,
1409                                         switchcmd(switch_config).untag_port);
1410                         else
1411                                 ret = cpsw_ale_vlan_add_mcast(priv->ale,
1412                                         switchcmd(switch_config).addr,
1413                                         switchcmd(switch_config).mem_port
1414                                                 << priv->host_port,
1415                                         switchcmd(switch_config).vid,
1416                                         switchcmd(switch_config).flag,
1417                                         switchcmd(switch_config).untag_port);
1418                 } else {
1419                         dev_err(priv->dev, "Invalid Switch config arguments\n");
1420                         ret = -EFAULT;
1421                 }
1422                 break;
1423
1424         case CONFIG_SWITCH_ADD_UNICAST:
1425                 if ((switchcmd(switch_config).vid <= 4095) &&
1426                                 (switchcmd(switch_config).mem_port <= 2) &&
1427                                 is_valid_ether_addr(
1428                                         switchcmd(switch_config).addr)) {
1429                         if (switchcmd(switch_config).vid == 0)
1430                                 ret = cpsw_ale_add_ucast(priv->ale,
1431                                         switchcmd(switch_config).addr,
1432                                         priv->host_port, 0);
1433                         else
1434                                 ret = cpsw_ale_vlan_add_ucast(priv->ale,
1435                                         switchcmd(switch_config).addr,
1436                                         priv->host_port, 0,
1437                                         switchcmd(switch_config).vid);
1438                 } else {
1439                         dev_err(priv->dev, "Invalid Switch config arguments\n");
1440                         ret = -EFAULT;
1441                 }
1442                 break;
1443
1444         case CONFIG_SWITCH_ADD_OUI:
1445                 if (!is_zero_ether_addr(switchcmd(switch_config).addr)) {
1446                         ret = cpsw_ale_add_oui(priv->ale,
1447                                 switchcmd(switch_config).addr);
1448                 } else {
1449                         dev_err(priv->dev, "Invalid Switch config arguments\n");
1450                         ret = -EFAULT;
1451                 }
1452                 break;
1453
1454         case CONFIG_SWITCH_FIND_ADDR:
1455                 if ((switchcmd(switch_config).vid <= 4095) &&
1456                         !is_zero_ether_addr(switchcmd(switch_config).addr)) {
1457                         ret = cpsw_ale_match_addr(priv->ale,
1458                                 switchcmd(switch_config).addr,
1459                                 switchcmd(switch_config).vid);
1460                         if (ret >= 0) {
1461                                 switch_config->ret_type = ret;
1462                                 ret = copy_to_user(ifrq->ifr_data,
1463                                         switch_config,
1464                                         sizeof(struct net_switch_config)) ?
1465                                         -EFAULT : 0;
1466                         }
1467                 } else {
1468                         dev_err(priv->dev, "Invalid Arguments\n");
1469                         ret = -EFAULT;
1470                 }
1471                 break;
1472
1473         case CONFIG_SWITCH_DEL_MULTICAST:
1474                 if ((switchcmd(switch_config).vid <= 4095) &&
1475                                 is_multicast_ether_addr(
1476                                 switchcmd(switch_config).addr)) {
1477                         if (switchcmd(switch_config).vid == 0)
1478                                 ret = cpsw_ale_del_mcast(priv->ale,
1479                                         switchcmd(switch_config).addr,
1480                                         switchcmd(switch_config).mem_port
1481                                                 << priv->host_port);
1482                         else
1483                                 ret = cpsw_ale_vlan_del_mcast(priv->ale,
1484                                         switchcmd(switch_config).addr,
1485                                         switchcmd(switch_config).mem_port
1486                                                 << priv->host_port,
1487                                         switchcmd(switch_config).vid);
1488                 } else {
1489                         dev_err(priv->dev, "Invalid Arguments\n");
1490                         ret = -EFAULT;
1491                 }
1492                 break;
1493
1494         case CONFIG_SWITCH_DEL_UNICAST:
1495                 if ((switchcmd(switch_config).vid <= 4095) &&
1496                         is_valid_ether_addr(switchcmd(switch_config).addr)) {
1497                         if (switchcmd(switch_config).vid == 0) {
1498                                 if (!memcmp(switchcmd(switch_config).addr,
1499                                                 priv->mac_addr, 6)) {
1500                                         ret = -EPERM;
1501                                         break;
1502                                 }
1503                                 ret = cpsw_ale_del_ucast(priv->ale,
1504                                         switchcmd(switch_config).addr, 0);
1505                         } else
1506                                 ret = cpsw_ale_del_ucast(priv->ale,
1507                                         switchcmd(switch_config).addr,
1508                                         switchcmd(switch_config).vid);
1509                 } else {
1510                         dev_err(priv->dev, "Invalid Arguments\n");
1511                         ret = -EFAULT;
1512                 }
1513                 break;
1514
1515         case CONFIG_SWITCH_ADD_VLAN:
1516                 if ((switchcmd(switch_config).vid <= 4095) &&
1517                                 (switchcmd(switch_config).mem_port > 0) &&
1518                                 (switchcmd(switch_config).mem_port <= 7)) {
1519                         ret = cpsw_ale_add_vlan(priv->ale,
1520                                 switchcmd(switch_config).vid,
1521                                 switchcmd(switch_config).mem_port
1522                                         << priv->host_port,
1523                                 switchcmd(switch_config).untag_port,
1524                                 switchcmd(switch_config).reg_multi,
1525                                 switchcmd(switch_config).unreg_multi);
1526                 } else {
1527                         dev_err(priv->dev, "Invalid Arguments\n");
1528                         ret = -EFAULT;
1529                 }
1530                 break;
1531
1532         case CONFIG_SWITCH_FIND_VLAN:
1533                 if (switchcmd(switch_config).vid <= 4095) {
1534                         switch_config->ret_type = cpsw_ale_match_vlan(priv->ale,
1535                                 switchcmd(switch_config).vid);
1536                         ret = copy_to_user(ifrq->ifr_data, switch_config,
1537                                 sizeof(struct net_switch_config)) ?
1538                                 -EFAULT : 0;
1539                 } else {
1540                         dev_err(priv->dev, "Invalid Arguments\n");
1541                         ret = -EFAULT;
1542                 }
1543                 break;
1544
1545         case CONFIG_SWITCH_DEL_VLAN:
1546                 if ((switchcmd(switch_config).vid <= 4095) &&
1547                                 (switchcmd(switch_config).mem_port <= 7)) {
1548                         ret = cpsw_ale_del_vlan(priv->ale,
1549                                 switchcmd(switch_config).vid,
1550                                 switchcmd(switch_config).mem_port
1551                                 << priv->host_port);
1552                 } else {
1553                         dev_err(priv->dev, "Invalid Arguments\n");
1554                         ret = -EFAULT;
1555                 }
1556                 break;
1557
1558         case CONFIG_SWITCH_SET_PORT_VLAN_CONFIG:
1559         {
1560                 int port_vlan = switchcmd(switch_config).vid |
1561                                 switchcmd(switch_config).prio_port << 13;
1562
1563                 if ((switchcmd(switch_config).vid <= 4095) &&
1564                                 (switchcmd(switch_config).port <= 2) &&
1565                                 (switchcmd(switch_config).prio_port <= 7)) {
1566                         if (switchcmd(switch_config).CFI_port)
1567                                 port_vlan |= (1 << 12);
1568
1569                         if (switchcmd(switch_config).port == 0)
1570                                 writel(port_vlan,
1571                                         &priv->host_port_regs->port_vlan);
1572                         else if (switchcmd(switch_config).port == 1)
1573                                 writel(port_vlan,
1574                                         &(priv->slaves[0].regs->port_vlan));
1575                         else
1576                                 writel(port_vlan,
1577                                         &(priv->slaves[1].regs->port_vlan));
1578                         ret = 0;
1579                 } else {
1580                         dev_err(priv->dev, "Invalid Arguments\n");
1581                         ret = -EFAULT;
1582                 }
1583                 break;
1584         }
1585
1586         case CONFIG_SWITCH_TIMEOUT:
1587                 ret = cpsw_ale_set_ageout(priv->ale,
1588                         switchcmd(switch_config).ale_timeout);
1589                 break;
1590
1591         case CONFIG_SWITCH_DUMP:
1592                 ret = cpsw_ale_dump(priv->ale,
1593                         switchcmd(switch_config).aledump,
1594                         switch_config->cmd_data.buf, 4095);
1595                 if (ret)
1596                         ret = copy_to_user(ifrq->ifr_data, switch_config,
1597                                 sizeof(struct net_switch_config)) ?
1598                                 -EFAULT : 0;
1599                 break;
1600
1601         case CONFIG_SWITCH_SET_FLOW_CONTROL:
1602                 if (portcmd(switch_config).port <= 7) {
1603                         writel(portcmd(switch_config).port,
1604                                 &priv->regs->flow_control);
1605                 } else {
1606                         dev_err(priv->dev, "Invalid Arguments\n");
1607                         ret = -EFAULT;
1608                 }
1609                 break;
1610
1611         case CONFIG_SWITCH_SET_PRIORITY_MAPPING:
1612                 if ((priocmd(switch_config).port <= 2) &&
1613                         (priocmd(switch_config).prio_rx <= 7) &&
1614                         (priocmd(switch_config).prio_tx <= 7) &&
1615                         (priocmd(switch_config).prio_switch <= 3)) {
1616                         ret = cpsw_set_priority_mapping(priv,
1617                                 priocmd(switch_config).port,
1618                                 priocmd(switch_config).prio_rx,
1619                                 priocmd(switch_config).prio_tx,
1620                                 priocmd(switch_config).prio_switch);
1621                 } else {
1622                         dev_err(priv->dev, "Invalid Arguments\n");
1623                         ret = -EFAULT;
1624                 }
1625                 break;
1626
1627         case CONFIG_SWITCH_PORT_STATISTICS_ENABLE:
1628                 if (portcmd(switch_config).port <= 7) {
1629                         writel(portcmd(switch_config).port,
1630                                 &priv->regs->stat_port_en);
1631                         ret = 0;
1632                 } else {
1633                         dev_err(priv->dev, "Invalid Arguments\n");
1634                         ret = -EFAULT;
1635                 }
1636                 break;
1637
1638         case CONFIG_SWITCH_CONFIG_DUMP:
1639                 cpsw_config_dump(priv, switch_config->cmd_data.buf, 4096);
1640                 ret = copy_to_user(ifrq->ifr_data, switch_config,
1641                         sizeof(struct net_switch_config)) ?
1642                         -EFAULT : 0;
1643                 break;
1644
1645         case CONFIG_SWITCH_RATELIMIT:
1646                 if ((portcmd(switch_config).port <= 2) &&
1647                                 (portcmd(switch_config).enable == 0))
1648                         ret = cpsw_ale_control_set(priv->ale,
1649                                 portcmd(switch_config).port,
1650                                 ALE_RATE_LIMIT, 0);
1651                 else if ((portcmd(switch_config).port <= 2) &&
1652                                 ((portcmd(switch_config).addr_type
1653                                         == ADDR_TYPE_BROADCAST) ||
1654                                 (portcmd(switch_config).addr_type
1655                                         == ADDR_TYPE_MULTICAST)) &&
1656                                 (portcmd(switch_config).limit <= 255)) {
1657                         ret = cpsw_rate_limit(priv->ale,
1658                                 portcmd(switch_config).enable,
1659                                 portcmd(switch_config).direction,
1660                                 portcmd(switch_config).port,
1661                                 portcmd(switch_config).addr_type,
1662                                 portcmd(switch_config).limit);
1663                 } else {
1664                         dev_err(priv->dev, "Invalid Arguments\n");
1665                         ret = -EFAULT;
1666                 }
1667                 break;
1668
1669         case CONFIG_SWITCH_VID_INGRESS_CHECK:
1670                 if (portcmd(switch_config).port <= 2) {
1671                         cpsw_ale_control_set(priv->ale,
1672                                 portcmd(switch_config).port,
1673                                 ALE_PORT_DROP_UNTAGGED, 1);
1674                         cpsw_ale_control_set(priv->ale,
1675                                 portcmd(switch_config).port,
1676                                 ALE_PORT_DROP_UNKNOWN_VLAN, 1);
1677                         ret = 0;
1678                 } else {
1679                         dev_err(priv->dev, "Invalid Arguments\n");
1680                         ret = -EFAULT;
1681                 }
1682                 break;
1683
1684         case CONFIG_SWITCH_ADD_UNKNOWN_VLAN_INFO:
1685                 if ((portcmd(switch_config).port <= 7) &&
1686                                 (portcmd(switch_config).
1687                                         reg_multi_port_mask <= 7) &&
1688                                 (portcmd(switch_config).
1689                                         unknown_reg_multi_port_mask <= 7) &&
1690                                 (portcmd(switch_config).
1691                                         unknown_vlan_member <= 7)) {
1692                         cpsw_ale_control_set(priv->ale, 0,
1693                                 ALE_PORT_UNTAGGED_EGRESS,
1694                                 portcmd(switch_config).port);
1695                         cpsw_ale_control_set(priv->ale, 0,
1696                                 ALE_PORT_UNKNOWN_REG_MCAST_FLOOD,
1697                                 portcmd(switch_config).reg_multi_port_mask);
1698                         cpsw_ale_control_set(priv->ale, 0,
1699                                 ALE_PORT_UNKNOWN_MCAST_FLOOD,
1700                                 portcmd(switch_config).
1701                                         unknown_reg_multi_port_mask);
1702                         cpsw_ale_control_set(priv->ale, 0,
1703                                 ALE_PORT_UNKNOWN_VLAN_MEMBER,
1704                                 portcmd(switch_config).unknown_vlan_member);
1705                         ret = 0;
1706                 } else {
1707                         dev_err(priv->dev, "Invalid Arguments\n");
1708                         ret = -EFAULT;
1709                 }
1710                 break;
1711
1712         case CONFIG_SWITCH_802_1:
1713         {
1714                 char addr_802_1[6] = {0x01, 0x80, 0xc2, 0x0, 0x0, 0x03};
1715                 if (portcmd(switch_config).enable)
1716                         ret = cpsw_ale_add_mcast(priv->ale,
1717                                         addr_802_1, 0x7, 0, 0);
1718                 else
1719                         ret = cpsw_ale_add_mcast(priv->ale,
1720                                         addr_802_1, 0x3, 0, 0);
1721                 break;
1722         }
1723
1724         case CONFIG_SWITCH_MACAUTH:
1725                 ret = cpsw_ale_control_set(priv->ale, priv->host_port,
1726                         ALE_AUTH_ENABLE, 1);
1727                 break;
1728
1729         case CONFIG_SWITCH_SET_PORT_CONFIG:
1730         {
1731                 struct ethtool_cmd ecmd;
1732
1733                 if (portcmd(switch_config).port == 1)
1734                         if (priv->slaves[0].phy) {
1735                                 ecmd.phy_address = priv->slaves[0].phy->addr;
1736                                 ret = phy_ethtool_gset(priv->slaves[0].phy,
1737                                                 &ecmd);
1738                         } else {
1739                                 dev_err(priv->dev, "Phy not Found\n");
1740                                 ret = -EFAULT;
1741                                 break;
1742                         }
1743                 else if (portcmd(switch_config).port == 2)
1744                         if (priv->slaves[1].phy) {
1745                                 ecmd.phy_address = priv->slaves[1].phy->addr;
1746                                 ret = phy_ethtool_gset(priv->slaves[1].phy,
1747                                                 &ecmd);
1748                         } else {
1749                                 dev_err(priv->dev, "Phy not Found\n");
1750                                 ret = -EFAULT;
1751                                 break;
1752                         }
1753                 else {
1754                         dev_err(priv->dev, "Invalid Arguments\n");
1755                         ret = -EFAULT;
1756                         break;
1757                 }
1758
1759                 if (((portcmd(switch_config).limit == 0) ||
1760                         (portcmd(switch_config).limit == 10) ||
1761                         (portcmd(switch_config).limit == 100) ||
1762                         (portcmd(switch_config).limit == 1000))) {
1763
1764                         if (portcmd(switch_config).limit == 0)
1765                                 ecmd.autoneg = AUTONEG_ENABLE;
1766                         else {
1767                                 ecmd.autoneg = AUTONEG_DISABLE;
1768                                 ecmd.speed =
1769                                         portcmd(switch_config).limit;
1770                                 if (portcmd(switch_config).direction
1771                                                 == 1)
1772                                         ecmd.duplex = DUPLEX_FULL;
1773                                 else
1774                                         ecmd.duplex = DUPLEX_HALF;
1775                         }
1776
1777                         if (portcmd(switch_config).port == 1)
1778                                 ret = phy_ethtool_sset(priv->slaves[0].phy,
1779                                         &ecmd);
1780                         else
1781                                 ret = phy_ethtool_sset(priv->slaves[1].phy,
1782                                         &ecmd);
1783                 } else {
1784                         dev_err(priv->dev, "Invalid Arguments\n");
1785                         ret = -EFAULT;
1786                 }
1787                 break;
1788         }
1789
1790         case CONFIG_SWITCH_GET_PORT_CONFIG:
1791         {
1792                 struct ethtool_cmd ecmd;
1793
1794                 if (portcmd(switch_config).port == 1)
1795                         if (priv->slaves[0].phy) {
1796                                 ecmd.phy_address = priv->slaves[0].phy->addr;
1797                                 ret = phy_ethtool_gset(priv->slaves[0].phy,
1798                                                 &ecmd);
1799                         } else {
1800                                 dev_err(priv->dev, "Phy not Found\n");
1801                                 ret = -EFAULT;
1802                                 break;
1803                         }
1804                 else if (portcmd(switch_config).port == 2)
1805                         if (priv->slaves[1].phy) {
1806                                 ecmd.phy_address = priv->slaves[1].phy->addr;
1807                                 ret = phy_ethtool_gset(priv->slaves[1].phy,
1808                                                 &ecmd);
1809                         } else {
1810                                 dev_err(priv->dev, "Phy not Found\n");
1811                                 ret = -EFAULT;
1812                                 break;
1813                         }
1814                 else {
1815                         dev_err(priv->dev, "Invalid Arguments\n");
1816                         ret = -EFAULT;
1817                         break;
1818                 }
1819
1820                 if (!ret) {
1821                         portcmd(switch_config).limit = ecmd.speed;
1822                         if (ecmd.duplex == DUPLEX_FULL)
1823                                 portcmd(switch_config).direction = 1;
1824                         else
1825                                 portcmd(switch_config).direction = 0;
1826                         ret = copy_to_user(ifrq->ifr_data, switch_config,
1827                                 sizeof(struct net_switch_config)) ?
1828                                 -EFAULT : 0;
1829                 }
1830
1831                 break;
1832         }
1833
1834         case CONFIG_SWITCH_PORT_STATE:
1835                 if (portcmd(switch_config).port <= 2) {
1836                         ret = cpsw_set_port_state(priv,
1837                                 portcmd(switch_config).port,
1838                                 portcmd(switch_config).port_state);
1839                 } else {
1840                         dev_err(priv->dev, "Invalid Arguments\n");
1841                         ret = -EFAULT;
1842                 }
1843                 break;
1844
1845         case CONFIG_SWITCH_RESET:
1846                 ret = cpsw_ndo_stop(ndev);
1847                 if (!ret)
1848                         ret = cpsw_ndo_open(ndev);
1849                 break;
1850
1851         default:
1852                 ret = -EOPNOTSUPP;
1853         }
1854
1855         kfree(switch_config);
1856         return ret;
1857 }
1858
1859 #else
1860 #define cpsw_switch_config_ioctl(ndev, ifrq, cmd) (-EOPNOTSUPP)
1861 #endif /* CONFIG_TI_CPSW_DUAL_EMAC */
1862
1863 static int cpsw_ndo_do_ioctl(struct net_device *ndev, struct ifreq *ifrq,
1864                 int cmd)
1865 {
1866         if (!(netif_running(ndev)))
1867                 return -EINVAL;
1868
1869         switch (cmd) {
1870
1871         case SIOCSWITCHCONFIG:
1872                 return cpsw_switch_config_ioctl(ndev, ifrq, cmd);
1873
1874         default:
1875                 return -EOPNOTSUPP;
1876         }
1877         return 0;
1878 }
1879
1880 static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
1881 {
1882         struct cpsw_priv *priv = netdev_priv(ndev);
1883         struct sockaddr *addr = (struct sockaddr *)p;
1884
1885         if (!is_valid_ether_addr(addr->sa_data))
1886                 return -EADDRNOTAVAIL;
1887
1888         cpsw_ale_del_ucast(priv->ale, priv->mac_addr, priv->host_port);
1889
1890         memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
1891         memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
1892
1893         cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port,
1894                            0);
1895         for_each_slave(priv, cpsw_set_slave_mac, priv);
1896         return 0;
1897 }
1898
1899 static void cpsw_ndo_tx_timeout(struct net_device *ndev)
1900 {
1901         struct cpsw_priv *priv = netdev_priv(ndev);
1902
1903         msg(err, tx_err, "transmit timeout, restarting dma");
1904         priv->stats.tx_errors++;
1905         cpsw_intr_disable(priv);
1906         cpdma_ctlr_int_ctrl(priv->dma, false);
1907         cpdma_chan_stop(priv->txch);
1908         cpdma_chan_start(priv->txch);
1909         cpdma_ctlr_int_ctrl(priv->dma, true);
1910         cpsw_intr_enable(priv);
1911         cpdma_ctlr_eoi(priv->dma);
1912 }
1913
1914 static struct net_device_stats *cpsw_ndo_get_stats(struct net_device *ndev)
1915 {
1916         struct cpsw_priv *priv = netdev_priv(ndev);
1917         return &priv->stats;
1918 }
1919
1920 #ifdef CONFIG_NET_POLL_CONTROLLER
1921 static void cpsw_ndo_poll_controller(struct net_device *ndev)
1922 {
1923         struct cpsw_priv *priv = netdev_priv(ndev);
1924
1925         cpsw_intr_disable(priv);
1926         cpdma_ctlr_int_ctrl(priv->dma, false);
1927         cpsw_interrupt(ndev->irq, priv);
1928         cpdma_ctlr_int_ctrl(priv->dma, true);
1929         cpsw_intr_enable(priv);
1930         cpdma_ctlr_eoi(priv->dma);
1931 }
1932 #endif
1933
1934 /**
1935  * cpsw_get_coalesce : Get interrupt coalesce settings for this device
1936  * @ndev : CPSW network adapter
1937  * @coal : ethtool coalesce settings structure
1938  *
1939  * Fetch the current interrupt coalesce settings
1940  *
1941  */
1942 static int cpsw_get_coalesce(struct net_device *ndev,
1943                                 struct ethtool_coalesce *coal)
1944 {
1945         struct cpsw_priv *priv = netdev_priv(ndev);
1946
1947         coal->rx_coalesce_usecs = priv->coal_intvl;
1948         return 0;
1949 }
1950
1951 /**
1952  * cpsw_set_coalesce : Set interrupt coalesce settings for this device
1953  * @ndev : CPSW network adapter
1954  * @coal : ethtool coalesce settings structure
1955  *
1956  * Set interrupt coalesce parameters
1957  *
1958  */
1959 static int cpsw_set_coalesce(struct net_device *ndev,
1960                                 struct ethtool_coalesce *coal)
1961 {
1962         struct cpsw_priv *priv = netdev_priv(ndev);
1963         u32 int_ctrl;
1964         u32 num_interrupts = 0;
1965         u32 prescale = 0;
1966         u32 addnl_dvdr = 1;
1967         u32 coal_intvl = 0;
1968
1969         if (!coal->rx_coalesce_usecs)
1970                 return -EINVAL;
1971
1972         coal_intvl = coal->rx_coalesce_usecs;
1973
1974         int_ctrl =  __raw_readl(&priv->wr_regs->int_control);
1975         prescale = priv->bus_freq_mhz * 4;
1976
1977         if (coal_intvl < CPSW_CMINTMIN_INTVL)
1978                 coal_intvl = CPSW_CMINTMIN_INTVL;
1979
1980         if (coal_intvl > CPSW_CMINTMAX_INTVL) {
1981                 /*
1982                  * Interrupt pacer works with 4us Pulse, we can
1983                  * throttle further by dilating the 4us pulse.
1984                  */
1985                 addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
1986
1987                 if (addnl_dvdr > 1) {
1988                         prescale *= addnl_dvdr;
1989                         if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
1990                                 coal_intvl = (CPSW_CMINTMAX_INTVL
1991                                                 * addnl_dvdr);
1992                 } else {
1993                         addnl_dvdr = 1;
1994                         coal_intvl = CPSW_CMINTMAX_INTVL;
1995                 }
1996         }
1997
1998         num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
1999
2000         int_ctrl |= CPSW_INTPACEEN;
2001         int_ctrl &= (~CPSW_INTPRESCALE_MASK);
2002         int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
2003         __raw_writel(int_ctrl, &priv->wr_regs->int_control);
2004
2005         __raw_writel(num_interrupts, &priv->wr_regs->rx_imax);
2006         __raw_writel(num_interrupts, &priv->wr_regs->tx_imax);
2007
2008         printk(KERN_INFO"Set coalesce to %d usecs.\n", coal_intvl);
2009         priv->coal_intvl = coal_intvl;
2010
2011         return 0;
2012 }
2013
2014 #ifdef VLAN_SUPPORT
2015
2016 #ifdef CONFIG_TI_CPSW_DUAL_EMAC
2017
2018 static inline void cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
2019                                            unsigned short vid)
2020 {
2021         cpsw_ale_add_vlan(priv->ale, vid, 1 << priv->host_port |
2022                         1 << (priv->emac_port + 1), 0,
2023                         1 << priv->host_port | 1 << (priv->emac_port + 1), 0);
2024         cpsw_ale_vlan_add_ucast(priv->ale, priv->mac_addr, priv->host_port,
2025                         0, vid);
2026         cpsw_ale_vlan_add_mcast(priv->ale, priv->ndev->broadcast,
2027                         1 << priv->host_port | 1 << (priv->emac_port + 1),
2028                         vid, 0, 0);
2029 }
2030
2031 #else
2032
2033 static inline void cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
2034                                 unsigned short vid)
2035 {
2036         cpsw_ale_add_vlan(priv->ale, vid, ALE_ALL_PORTS << priv->host_port,
2037                         0, ALE_ALL_PORTS << priv->host_port,
2038                         (BIT(1) | BIT(2)) << priv->host_port);
2039         cpsw_ale_vlan_add_ucast(priv->ale, priv->mac_addr,
2040                         priv->host_port, 0, vid);
2041         cpsw_ale_vlan_add_mcast(priv->ale, priv->ndev->broadcast,
2042                         ALE_ALL_PORTS << priv->host_port, vid, 0, 0);
2043 }
2044
2045 #endif
2046
2047 static void cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
2048                 unsigned short vid)
2049 {
2050         struct cpsw_priv *priv = netdev_priv(ndev);
2051
2052         spin_lock(&priv->lock);
2053
2054         dev_dbg(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
2055         if (vid) {
2056                 cpsw_add_vlan_ale_entry(priv, vid);
2057         } else {
2058                 cpsw_ale_add_vlan(priv->ale, vid,
2059                                 ALE_ALL_PORTS << priv->host_port,
2060                                 ALE_ALL_PORTS << priv->host_port,
2061                                 ALE_ALL_PORTS << priv->host_port,
2062                                 (BIT(1) | BIT(2)) << priv->host_port);
2063         }
2064
2065         spin_unlock(&priv->lock);
2066 }
2067
2068 static void cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
2069                 unsigned short vid)
2070 {
2071         struct cpsw_priv *priv = netdev_priv(ndev);
2072
2073         spin_lock(&priv->lock);
2074
2075         dev_dbg(priv->dev, "removing vlanid %d from vlan filter\n", vid);
2076         cpsw_ale_del_vlan(priv->ale, vid, ALE_ALL_PORTS << priv->host_port);
2077         cpsw_ale_vlan_del_ucast(priv->ale, priv->mac_addr,
2078                                 priv->host_port, vid);
2079         cpsw_ale_vlan_del_mcast(priv->ale, priv->ndev->broadcast, 0, vid);
2080
2081         spin_unlock(&priv->lock);
2082 }
2083
2084 #endif /* VLAN_SUPPORT */
2085
2086 static const struct net_device_ops cpsw_netdev_ops = {
2087         .ndo_open               = cpsw_ndo_open,
2088         .ndo_stop               = cpsw_ndo_stop,
2089         .ndo_start_xmit         = cpsw_ndo_start_xmit,
2090         .ndo_change_rx_flags    = cpsw_ndo_change_rx_flags,
2091         .ndo_set_mac_address    = cpsw_ndo_set_mac_address,
2092         .ndo_do_ioctl           = cpsw_ndo_do_ioctl,
2093         .ndo_validate_addr      = eth_validate_addr,
2094         .ndo_tx_timeout         = cpsw_ndo_tx_timeout,
2095         .ndo_get_stats          = cpsw_ndo_get_stats,
2096         .ndo_set_rx_mode        = cpsw_ndo_set_rx_mode,
2097 #ifdef CONFIG_NET_POLL_CONTROLLER
2098         .ndo_poll_controller    = cpsw_ndo_poll_controller,
2099 #endif
2100 #ifdef VLAN_SUPPORT
2101         .ndo_vlan_rx_add_vid    = cpsw_ndo_vlan_rx_add_vid,
2102         .ndo_vlan_rx_kill_vid   = cpsw_ndo_vlan_rx_kill_vid,
2103 #endif
2104 };
2105
2106 static void cpsw_get_drvinfo(struct net_device *ndev,
2107                              struct ethtool_drvinfo *info)
2108 {
2109         struct cpsw_priv *priv = netdev_priv(ndev);
2110         strcpy(info->driver, "TI CPSW Driver v1.0");
2111         strcpy(info->version, "1.0");
2112         strcpy(info->bus_info, priv->pdev->name);
2113 }
2114
2115 static u32 cpsw_get_msglevel(struct net_device *ndev)
2116 {
2117         struct cpsw_priv *priv = netdev_priv(ndev);
2118         return priv->msg_enable;
2119 }
2120
2121 static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
2122 {
2123         struct cpsw_priv *priv = netdev_priv(ndev);
2124         priv->msg_enable = value;
2125 }
2126
2127 /**
2128  * cpsw_get_settings: Get EMAC settings
2129  * @ndev: CPSW network adapter
2130  * @ecmd: ethtool command
2131  *
2132  * Executes ethool get command
2133  *
2134  */
2135 static int cpsw_get_settings(struct net_device *ndev,
2136                              struct ethtool_cmd *ecmd)
2137 {
2138         struct cpsw_priv *priv = netdev_priv(ndev);
2139         int slave_no = cpsw_slave_phy_index(priv);
2140
2141         if (priv->slaves[slave_no].phy)
2142                 return phy_ethtool_gset(priv->slaves[slave_no].phy, ecmd);
2143         else
2144                 return -EOPNOTSUPP;
2145 }
2146
2147 /**
2148  * cpsw_set_settings: Set EMAC settings
2149  * @ndev: CPSW network adapter
2150  * @ecmd: ethtool command
2151  *
2152  * Executes ethool set command
2153  *
2154  */
2155 static int cpsw_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd)
2156 {
2157         struct cpsw_priv *priv = netdev_priv(ndev);
2158         int slave_no = cpsw_slave_phy_index(priv);
2159
2160         if (priv->slaves[slave_no].phy)
2161                 return phy_ethtool_sset(priv->slaves[slave_no].phy, ecmd);
2162         else
2163                 return -EOPNOTSUPP;
2164 }
2165
2166 static const struct ethtool_ops cpsw_ethtool_ops = {
2167         .get_drvinfo    = cpsw_get_drvinfo,
2168         .get_msglevel   = cpsw_get_msglevel,
2169         .set_msglevel   = cpsw_set_msglevel,
2170         .get_link       = ethtool_op_get_link,
2171         .get_settings   = cpsw_get_settings,
2172         .set_settings   = cpsw_set_settings,
2173         .get_coalesce   = cpsw_get_coalesce,
2174         .set_coalesce   = cpsw_set_coalesce,
2175 };
2176
2177 static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv)
2178 {
2179         void __iomem            *regs = priv->regs;
2180         int                     slave_num = slave->slave_num;
2181         struct cpsw_slave_data  *data = priv->data.slave_data + slave_num;
2182
2183         slave->data     = data;
2184         slave->regs     = regs + data->slave_reg_ofs;
2185         slave->sliver   = regs + data->sliver_reg_ofs;
2186         slave->port_vlan = data->dual_emac_reserved_vlan;
2187 }
2188
2189 #ifdef CONFIG_TI_CPSW_DUAL_EMAC
2190
2191 static int cpsw_init_slave_emac(struct platform_device *pdev,
2192                                 struct cpsw_priv *priv)
2193 {
2194         struct cpsw_platform_data       *data = pdev->dev.platform_data;
2195         struct net_device               *ndev;
2196         struct cpsw_priv                *priv_sl2;
2197         int ret = 0, i;
2198
2199         ndev = alloc_etherdev(sizeof(struct cpsw_priv));
2200         if (!ndev) {
2201                 pr_err("cpsw: error allocating net_device\n");
2202                 return -ENOMEM;
2203         }
2204
2205         priv_sl2 = netdev_priv(ndev);
2206         spin_lock_init(&priv_sl2->lock);
2207         priv_sl2->data = *data;
2208         priv_sl2->pdev = pdev;
2209         priv_sl2->ndev = ndev;
2210         priv_sl2->dev  = &ndev->dev;
2211         priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
2212         priv_sl2->rx_packet_max = max(rx_packet_max, 128);
2213
2214         if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
2215                 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
2216                         ETH_ALEN);
2217                 pr_info("cpsw: Detected MACID = %pM\n", priv_sl2->mac_addr);
2218         } else {
2219                 random_ether_addr(priv_sl2->mac_addr);
2220                 pr_info("cpsw: Random MACID = %pM\n", priv_sl2->mac_addr);
2221         }
2222         memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
2223
2224         priv_sl2->slaves = priv->slaves;
2225         priv_sl2->clk = priv->clk;
2226
2227         priv_sl2->coal_intvl = 0;
2228         priv_sl2->bus_freq_mhz = clk_get_rate(priv_sl2->clk) / 1000000;
2229
2230         priv_sl2->cpsw_res = priv->cpsw_res;
2231         priv_sl2->regs = priv->regs;
2232         priv_sl2->host_port = priv->host_port;
2233         priv_sl2->host_port_regs = priv->host_port_regs;
2234         priv_sl2->hw_stats = priv->hw_stats;
2235         priv_sl2->cpsw_ss_res = priv->cpsw_ss_res;
2236         priv_sl2->wr_regs = priv->wr_regs;
2237         priv_sl2->dma = priv->dma;
2238         priv_sl2->txch = priv->txch;
2239         priv_sl2->rxch = priv->rxch;
2240         priv_sl2->ale = priv->ale;
2241         priv_sl2->emac_port = 1;
2242         priv->slaves[1].ndev = ndev;
2243         for_each_slave(priv_sl2, cpsw_slave_init, priv_sl2);
2244
2245         for (i = 0; i < priv->num_irqs; i++) {
2246                 priv_sl2->irqs_table[i] = priv->irqs_table[i];
2247                 priv_sl2->num_irqs = priv->num_irqs;
2248         }
2249
2250 #ifdef VLAN_SUPPORT
2251         ndev->features |= NETIF_F_HW_VLAN_FILTER;
2252 #endif
2253
2254         ndev->flags |= IFF_ALLMULTI;    /* see cpsw_ndo_change_rx_flags() */
2255
2256         ndev->netdev_ops = &cpsw_netdev_ops;
2257         SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
2258         netif_napi_add(ndev, &priv_sl2->napi, cpsw_poll, CPSW_POLL_WEIGHT);
2259
2260         /* register the network device */
2261         SET_NETDEV_DEV(ndev, &pdev->dev);
2262         ret = register_netdev(ndev);
2263         if (ret) {
2264                 pr_err("cpsw: error registering net device\n");
2265                 ret = -ENODEV;
2266         }
2267
2268         return ret;
2269 }
2270
2271 static inline void cpsw_deinit_slave_emac(struct cpsw_priv *priv)
2272 {
2273         struct net_device *ndev = priv->slaves[1].ndev;
2274
2275         unregister_netdev(ndev);
2276         free_netdev(ndev);
2277 }
2278
2279 #else
2280 #define cpsw_init_slave_emac(pdev, priv)        0
2281 #define cpsw_deinit_slave_emac(priv)
2282 #endif
2283
2284 static int __devinit cpsw_probe(struct platform_device *pdev)
2285 {
2286         struct cpsw_platform_data       *data = pdev->dev.platform_data;
2287         struct net_device               *ndev;
2288         struct cpsw_priv                *priv;
2289         struct cpdma_params             dma_params;
2290         struct cpsw_ale_params          ale_params;
2291         void __iomem                    *regs;
2292         int ret = 0, i, k = 0;
2293
2294         if (!data) {
2295                 pr_err("cpsw: platform data missing\n");
2296                 return -ENODEV;
2297         }
2298
2299         ndev = alloc_etherdev(sizeof(struct cpsw_priv));
2300         if (!ndev) {
2301                 pr_err("cpsw: error allocating net_device\n");
2302                 return -ENOMEM;
2303         }
2304
2305         platform_set_drvdata(pdev, ndev);
2306         priv = netdev_priv(ndev);
2307         spin_lock_init(&priv->lock);
2308         priv->data = *data;
2309         priv->pdev = pdev;
2310         priv->ndev = ndev;
2311         priv->dev  = &ndev->dev;
2312         priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
2313         priv->rx_packet_max = max(rx_packet_max, 128);
2314
2315         if (is_valid_ether_addr(data->mac_addr)) {
2316                 memcpy(priv->mac_addr, data->mac_addr, ETH_ALEN);
2317                 printk(KERN_INFO"Detected MACID=%x:%x:%x:%x:%x:%x\n",
2318                         priv->mac_addr[0], priv->mac_addr[1],
2319                         priv->mac_addr[2], priv->mac_addr[3],
2320                         priv->mac_addr[4], priv->mac_addr[5]);
2321         } else {
2322                 random_ether_addr(priv->mac_addr);
2323                 printk(KERN_INFO"Random MACID=%x:%x:%x:%x:%x:%x\n",
2324                         priv->mac_addr[0], priv->mac_addr[1],
2325                         priv->mac_addr[2], priv->mac_addr[3],
2326                         priv->mac_addr[4], priv->mac_addr[5]);
2327         }
2328
2329         memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2330
2331         priv->slaves = kzalloc(sizeof(struct cpsw_slave) * data->slaves,
2332                                GFP_KERNEL);
2333         if (!priv->slaves) {
2334                 dev_err(priv->dev, "failed to allocate slave ports\n");
2335                 ret = -EBUSY;
2336                 goto clean_ndev_ret;
2337         }
2338         for (i = 0; i < data->slaves; i++)
2339                 priv->slaves[i].slave_num = i;
2340
2341         priv->slaves[0].ndev = ndev;
2342         priv->emac_port = 0;
2343
2344         pm_runtime_enable(&pdev->dev);
2345         pm_runtime_get_sync(&pdev->dev);
2346         priv->clk = clk_get(&pdev->dev, "fck");
2347         if (IS_ERR(priv->clk)) {
2348                 dev_err(&pdev->dev, "fck is not found\n");
2349                 ret = -ENODEV;
2350                 goto clean_slave_ret;
2351         }
2352
2353         priv->coal_intvl = 0;
2354         priv->bus_freq_mhz = clk_get_rate(priv->clk) / 1000000;
2355
2356         priv->cpsw_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2357         if (!priv->cpsw_res) {
2358                 dev_err(priv->dev, "error getting i/o resource\n");
2359                 ret = -ENOENT;
2360                 goto clean_clk_ret;
2361         }
2362
2363         if (!request_mem_region(priv->cpsw_res->start,
2364                 resource_size(priv->cpsw_res), ndev->name)) {
2365                 dev_err(priv->dev, "failed request i/o region\n");
2366                 ret = -ENXIO;
2367                 goto clean_clk_ret;
2368         }
2369
2370         regs = ioremap(priv->cpsw_res->start, resource_size(priv->cpsw_res));
2371         if (!regs) {
2372                 dev_err(priv->dev, "unable to map i/o region\n");
2373                 goto clean_cpsw_iores_ret;
2374         }
2375         priv->regs = regs;
2376         priv->host_port = data->host_port_num;
2377         priv->host_port_regs = regs + data->host_port_reg_ofs;
2378         priv->hw_stats = regs + data->hw_stats_reg_ofs;
2379
2380         priv->cpsw_ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
2381         if (!priv->cpsw_ss_res) {
2382                 dev_err(priv->dev, "error getting i/o resource\n");
2383                 ret = -ENOENT;
2384                 goto clean_clk_ret;
2385         }
2386
2387         if (!request_mem_region(priv->cpsw_ss_res->start,
2388                 resource_size(priv->cpsw_ss_res), ndev->name)) {
2389                 dev_err(priv->dev, "failed request i/o region\n");
2390                 ret = -ENXIO;
2391                 goto clean_clk_ret;
2392         }
2393
2394         regs = ioremap(priv->cpsw_ss_res->start,
2395                         resource_size(priv->cpsw_ss_res));
2396         if (!regs) {
2397                 dev_err(priv->dev, "unable to map i/o region\n");
2398                 goto clean_cpsw_ss_iores_ret;
2399         }
2400         priv->wr_regs = regs;
2401
2402         for_each_slave(priv, cpsw_slave_init, priv);
2403
2404         omap_ctrl_writel(CPSW_TIMER_MASK, CPSW_TIMER_CAP_REG);
2405
2406         dmtimer_rx = omap_dm_timer_request_specific(CPSW_RX_TIMER_REQ);
2407         if (!dmtimer_rx) {
2408                 dev_err(priv->dev, "Error getting Rx Timer resource\n");
2409                 ret = -ENODEV;
2410                 goto clean_iomap_ret;
2411         }
2412         dmtimer_tx = omap_dm_timer_request_specific(CPSW_TX_TIMER_REQ);
2413         if (!dmtimer_tx) {
2414                 dev_err(priv->dev, "Error getting Tx Timer resource\n");
2415                 ret = -ENODEV;
2416                 goto clean_timer_rx_ret;
2417         }
2418
2419         memset(&dma_params, 0, sizeof(dma_params));
2420         dma_params.dev                  = &pdev->dev;
2421         dma_params.dmaregs              = (void __iomem *)(((u32)priv->regs) +
2422                                                 data->cpdma_reg_ofs);
2423         dma_params.rxthresh             = (void __iomem *)(((u32)priv->regs) +
2424                                         data->cpdma_reg_ofs + CPDMA_RXTHRESH);
2425         dma_params.rxfree               = (void __iomem *)(((u32)priv->regs) +
2426                                         data->cpdma_reg_ofs + CPDMA_RXFREE);
2427
2428         if (data->version == CPSW_VERSION_2) {
2429                 dma_params.txhdp        = (void __iomem *)(((u32)priv->regs) +
2430                                         data->cpdma_reg_ofs + CPDMA_TXHDP_VER2);
2431                 dma_params.rxhdp        = (void __iomem *)(((u32)priv->regs) +
2432                                         data->cpdma_reg_ofs + CPDMA_RXHDP_VER2);
2433                 dma_params.txcp         = (void __iomem *)(((u32)priv->regs) +
2434                                         data->cpdma_reg_ofs + CPDMA_TXCP_VER2);
2435                 dma_params.rxcp         = (void __iomem *)(((u32)priv->regs) +
2436                                         data->cpdma_reg_ofs + CPDMA_RXCP_VER2);
2437         } else {
2438                 dma_params.txhdp        = (void __iomem *)(((u32)priv->regs) +
2439                                         data->cpdma_reg_ofs + CPDMA_TXHDP_VER1);
2440                 dma_params.rxhdp        = (void __iomem *)(((u32)priv->regs) +
2441                                         data->cpdma_reg_ofs + CPDMA_RXHDP_VER1);
2442                 dma_params.txcp         = (void __iomem *)(((u32)priv->regs) +
2443                                         data->cpdma_reg_ofs + CPDMA_TXCP_VER1);
2444                 dma_params.rxcp         = (void __iomem *)(((u32)priv->regs) +
2445                                         data->cpdma_reg_ofs + CPDMA_RXCP_VER1);
2446         }
2447
2448         dma_params.num_chan             = data->channels;
2449         dma_params.has_soft_reset       = true;
2450         dma_params.min_packet_size      = CPSW_MIN_PACKET_SIZE;
2451         dma_params.desc_mem_size        = data->bd_ram_size;
2452         dma_params.desc_align           = 16;
2453         dma_params.has_ext_regs         = true;
2454         dma_params.desc_mem_phys        = data->no_bd_ram ? 0 :
2455                         (u32 __force)priv->cpsw_res->start + data->bd_ram_ofs;
2456         dma_params.desc_hw_addr         = data->hw_ram_addr ?
2457                                 data->hw_ram_addr : dma_params.desc_mem_phys ;
2458
2459         priv->dma = cpdma_ctlr_create(&dma_params);
2460         if (!priv->dma) {
2461                 dev_err(priv->dev, "error initializing dma\n");
2462                 ret = -ENOMEM;
2463                 goto clean_timer_ret;
2464         }
2465
2466         priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
2467                                        cpsw_tx_handler);
2468         priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
2469                                        cpsw_rx_handler);
2470
2471         if (WARN_ON(!priv->txch || !priv->rxch)) {
2472                 dev_err(priv->dev, "error initializing dma channels\n");
2473                 ret = -ENOMEM;
2474                 goto clean_dma_ret;
2475         }
2476
2477         memset(&ale_params, 0, sizeof(ale_params));
2478         ale_params.dev          = &ndev->dev;
2479         ale_params.ale_regs     = (void *)((u32)priv->regs) +
2480                                         ((u32)data->ale_reg_ofs);
2481         ale_params.ale_ageout   = ale_ageout;
2482         ale_params.ale_entries  = data->ale_entries;
2483         ale_params.ale_ports    = data->slaves;
2484
2485         priv->ale = cpsw_ale_create(&ale_params);
2486         if (!priv->ale) {
2487                 dev_err(priv->dev, "error initializing ale engine\n");
2488                 ret = -ENODEV;
2489                 goto clean_dma_ret;
2490         }
2491
2492         while ((i = platform_get_irq(pdev, k)) >= 0) {
2493                 if (request_irq(i, cpsw_interrupt, IRQF_DISABLED,
2494                                 dev_name(&pdev->dev), priv)) {
2495                         dev_err(priv->dev, "error attaching irq\n");
2496                         goto clean_ale_ret;
2497                 }
2498                 priv->irqs_table[k] = i;
2499                 priv->num_irqs = ++k;
2500         }
2501
2502 #ifdef VLAN_SUPPORT
2503         ndev->features |= NETIF_F_HW_VLAN_FILTER;
2504 #endif
2505
2506         ndev->flags |= IFF_ALLMULTI;    /* see cpsw_ndo_change_rx_flags() */
2507
2508         ndev->netdev_ops = &cpsw_netdev_ops;
2509         SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
2510         netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT);
2511
2512         /* register the network device */
2513         SET_NETDEV_DEV(ndev, &pdev->dev);
2514         ret = register_netdev(ndev);
2515         if (ret) {
2516                 dev_err(priv->dev, "error registering net device\n");
2517                 ret = -ENODEV;
2518                 goto clean_irq_ret;
2519         }
2520
2521         msg(notice, probe, "initialized device (regs %x, irq %d)\n",
2522             priv->cpsw_res->start, ndev->irq);
2523
2524         ret = cpsw_init_slave_emac(pdev, priv);
2525         if (ret) {
2526                 dev_err(priv->dev, "error probe slave 2 emac interface\n");
2527                 goto clean_irq_ret;
2528         }
2529
2530         return 0;
2531
2532 clean_irq_ret:
2533         free_irq(ndev->irq, priv);
2534 clean_ale_ret:
2535         cpsw_ale_destroy(priv->ale);
2536 clean_dma_ret:
2537         cpdma_chan_destroy(priv->txch);
2538         cpdma_chan_destroy(priv->rxch);
2539         cpdma_ctlr_destroy(priv->dma);
2540 clean_timer_ret:
2541         omap_dm_timer_free(dmtimer_tx);
2542 clean_timer_rx_ret:
2543         omap_dm_timer_free(dmtimer_rx);
2544 clean_iomap_ret:
2545         iounmap(priv->regs);
2546 clean_cpsw_ss_iores_ret:
2547         release_mem_region(priv->cpsw_ss_res->start,
2548                                 resource_size(priv->cpsw_ss_res));
2549 clean_cpsw_iores_ret:
2550         release_mem_region(priv->cpsw_res->start,
2551                                 resource_size(priv->cpsw_res));
2552 clean_clk_ret:
2553         clk_put(priv->clk);
2554 clean_slave_ret:
2555         pm_runtime_get_sync(&pdev->dev);
2556         pm_runtime_disable(&pdev->dev);
2557         kfree(priv->slaves);
2558 clean_ndev_ret:
2559         free_netdev(ndev);
2560         return ret;
2561 }
2562
2563 static int __devexit cpsw_remove(struct platform_device *pdev)
2564 {
2565         struct net_device *ndev = platform_get_drvdata(pdev);
2566         struct cpsw_priv *priv = netdev_priv(ndev);
2567         u32 i;
2568
2569         msg(notice, probe, "removing device\n");
2570         platform_set_drvdata(pdev, NULL);
2571
2572         cpsw_deinit_slave_emac(priv);
2573         omap_dm_timer_free(dmtimer_rx);
2574         omap_dm_timer_free(dmtimer_tx);
2575         for (i = 0; i < priv->num_irqs; i++)
2576                 free_irq(priv->irqs_table[i], priv);
2577         cpsw_ale_destroy(priv->ale);