arm:omap:cpsw: Enable/Disable gig support for different evm rev
[sitara-epos/sitara-epos-kernel.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>
30 #include <linux/cpsw.h>
32 #include "cpsw_ale.h"
33 #include "davinci_cpdma.h"
36 #define CPSW_DEBUG      (NETIF_MSG_HW           | NETIF_MSG_WOL         | \
37                          NETIF_MSG_DRV          | NETIF_MSG_LINK        | \
38                          NETIF_MSG_IFUP         | NETIF_MSG_INTR        | \
39                          NETIF_MSG_PROBE        | NETIF_MSG_TIMER       | \
40                          NETIF_MSG_IFDOWN       | NETIF_MSG_RX_ERR      | \
41                          NETIF_MSG_TX_ERR       | NETIF_MSG_TX_DONE     | \
42                          NETIF_MSG_PKTDATA      | NETIF_MSG_TX_QUEUED   | \
43                          NETIF_MSG_RX_STATUS)
45 #define msg(level, type, format, ...)                           \
46 do {                                                            \
47         if (netif_msg_##type(priv) && net_ratelimit())          \
48                 dev_##level(priv->dev, format, ## __VA_ARGS__); \
49 } while (0)
51 #define CPDMA_RXTHRESH          0x0c0
52 #define CPDMA_RXFREE            0x0e0
53 #define CPDMA_TXHDP_VER1        0x100
54 #define CPDMA_TXHDP_VER2        0x200
55 #define CPDMA_RXHDP_VER1        0x120
56 #define CPDMA_RXHDP_VER2        0x220
57 #define CPDMA_TXCP_VER1         0x140
58 #define CPDMA_TXCP_VER2         0x240
59 #define CPDMA_RXCP_VER1         0x160
60 #define CPDMA_RXCP_VER2         0x260
62 #define CPSW_POLL_WEIGHT        64
63 #define CPSW_MIN_PACKET_SIZE    60
64 #define CPSW_MAX_PACKET_SIZE    (1500 + 14 + 4 + 4)
66 #define CPSW_PHY_SPEED          1000
68 #define CPSW_IRQ_QUIRK
69 #ifdef CPSW_IRQ_QUIRK
70 #define cpsw_enable_irq(priv)   \
71         do {                    \
72                 u32 i;          \
73                 for (i = 0; i < priv->num_irqs; i++) \
74                         enable_irq(priv->irqs_table[i]); \
75         } while (0);
76 #define cpsw_disable_irq(priv)  \
77         do {                    \
78                 u32 i;          \
79                 for (i = 0; i < priv->num_irqs; i++) \
80                         disable_irq_nosync(priv->irqs_table[i]); \
81         } while (0);
82 #else
83 #define cpsw_enable_irq(priv) do { } while (0);
84 #define cpsw_disable_irq(priv) do { } while (0);
85 #endif
87 static int debug_level;
88 module_param(debug_level, int, 0);
89 MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
91 static int ale_ageout = 10;
92 module_param(ale_ageout, int, 0);
93 MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
95 static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
96 module_param(rx_packet_max, int, 0);
97 MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
99 struct cpsw_ss_regs {
100         u32     id_ver;
101         u32     soft_reset;
102         u32     control;
103         u32     int_control;
104         u32     rx_thresh_en;
105         u32     rx_en;
106         u32     tx_en;
107         u32     misc_en;
108 };
110 struct cpsw_regs {
111         u32     id_ver;
112         u32     control;
113         u32     soft_reset;
114         u32     stat_port_en;
115         u32     ptype;
116 };
118 struct cpsw_slave_regs {
119         u32     max_blks;
120         u32     blk_cnt;
121         u32     flow_thresh;
122         u32     port_vlan;
123         u32     tx_pri_map;
124         u32     ts_seq_mtype;
125 #ifdef CONFIG_ARCH_TI814X
126         u32     ts_ctl;
127         u32     ts_seq_ltype;
128         u32     ts_vlan;
129 #endif
130         u32     sa_lo;
131         u32     sa_hi;
132 };
134 struct cpsw_host_regs {
135         u32     max_blks;
136         u32     blk_cnt;
137         u32     flow_thresh;
138         u32     port_vlan;
139         u32     tx_pri_map;
140         u32     cpdma_tx_pri_map;
141         u32     cpdma_rx_chan_map;
142 };
144 struct cpsw_sliver_regs {
145         u32     id_ver;
146         u32     mac_control;
147         u32     mac_status;
148         u32     soft_reset;
149         u32     rx_maxlen;
150         u32     __reserved_0;
151         u32     rx_pause;
152         u32     tx_pause;
153         u32     __reserved_1;
154         u32     rx_pri_map;
155 };
157 struct cpsw_hw_stats {
158         u32     rxgoodframes;
159         u32     rxbroadcastframes;
160         u32     rxmulticastframes;
161         u32     rxpauseframes;
162         u32     rxcrcerrors;
163         u32     rxaligncodeerrors;
164         u32     rxoversizedframes;
165         u32     rxjabberframes;
166         u32     rxundersizedframes;
167         u32     rxfragments;
168         u32     __pad_0[2];
169         u32     rxoctets;
170         u32     txgoodframes;
171         u32     txbroadcastframes;
172         u32     txmulticastframes;
173         u32     txpauseframes;
174         u32     txdeferredframes;
175         u32     txcollisionframes;
176         u32     txsinglecollframes;
177         u32     txmultcollframes;
178         u32     txexcessivecollisions;
179         u32     txlatecollisions;
180         u32     txunderrun;
181         u32     txcarriersenseerrors;
182         u32     txoctets;
183         u32     octetframes64;
184         u32     octetframes65t127;
185         u32     octetframes128t255;
186         u32     octetframes256t511;
187         u32     octetframes512t1023;
188         u32     octetframes1024tup;
189         u32     netoctets;
190         u32     rxsofoverruns;
191         u32     rxmofoverruns;
192         u32     rxdmaoverruns;
193 };
195 struct cpsw_slave {
196         struct cpsw_slave_regs __iomem  *regs;
197         struct cpsw_sliver_regs __iomem *sliver;
198         int                             slave_num;
199         u32                             mac_control;
200         struct cpsw_slave_data          *data;
201         struct phy_device               *phy;
202 };
204 struct cpsw_priv {
205         spinlock_t                      lock;
206         struct platform_device          *pdev;
207         struct net_device               *ndev;
208         struct resource                 *cpsw_res;
209         struct resource                 *cpsw_ss_res;
210         struct napi_struct              napi;
211 #define napi_to_priv(napi)      container_of(napi, struct cpsw_priv, napi)
212         struct device                   *dev;
213         struct cpsw_platform_data       data;
214         struct cpsw_regs __iomem        *regs;
215         struct cpsw_ss_regs __iomem     *ss_regs;
216         struct cpsw_hw_stats __iomem    *hw_stats;
217         struct cpsw_host_regs __iomem   *host_port_regs;
218         u32                             msg_enable;
219         struct net_device_stats         stats;
220         int                             rx_packet_max;
221         int                             host_port;
222         struct clk                      *clk;
223         u8                              mac_addr[ETH_ALEN];
224         struct cpsw_slave               *slaves;
225 #define for_each_slave(priv, func, arg...)                      \
226         do {                                                    \
227                 int idx;                                        \
228                 for (idx = 0; idx < (priv)->data.slaves; idx++) \
229                         (func)((priv)->slaves + idx, ##arg);    \
230         } while (0)
232         struct cpdma_ctlr               *dma;
233         struct cpdma_chan               *txch, *rxch;
234         struct cpsw_ale                 *ale;
236 #ifdef CPSW_IRQ_QUIRK
237         /* snapshot of IRQ numbers */
238         u32 irqs_table[4];
239         u32 num_irqs;
240 #endif
242 };
244 static void cpsw_intr_enable(struct cpsw_priv *priv)
246         __raw_writel(0xFF, &priv->ss_regs->tx_en);
247         __raw_writel(0xFF, &priv->ss_regs->rx_en);
249         cpdma_ctlr_int_ctrl(priv->dma, true);
250         return;
253 static void cpsw_intr_disable(struct cpsw_priv *priv)
255         __raw_writel(0, &priv->ss_regs->tx_en);
256         __raw_writel(0, &priv->ss_regs->rx_en);
258         cpdma_ctlr_int_ctrl(priv->dma, false);
259         return;
262 void cpsw_tx_handler(void *token, int len, int status)
264         struct sk_buff          *skb = token;
265         struct net_device       *ndev = skb->dev;
266         struct cpsw_priv        *priv = netdev_priv(ndev);
268         if (unlikely(netif_queue_stopped(ndev)))
269                 netif_start_queue(ndev);
270         priv->stats.tx_packets++;
271         priv->stats.tx_bytes += len;
272         dev_kfree_skb_any(skb);
275 void cpsw_rx_handler(void *token, int len, int status)
277         struct sk_buff          *skb = token;
278         struct net_device       *ndev = skb->dev;
279         struct cpsw_priv        *priv = netdev_priv(ndev);
280         int                     ret = 0;
282         if (likely(status >= 0)) {
283                 skb_put(skb, len);
284                 skb->protocol = eth_type_trans(skb, ndev);
285                 netif_receive_skb(skb);
286                 priv->stats.rx_bytes += len;
287                 priv->stats.rx_packets++;
288                 skb = NULL;
289         }
292         if (unlikely(!netif_running(ndev))) {
293                 if (skb)
294                         dev_kfree_skb_any(skb);
295                 return;
296         }
298         if (likely(!skb)) {
299                 skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
300                 if (WARN_ON(!skb))
301                         return;
303                 ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
304                                 skb_tailroom(skb), GFP_KERNEL);
305         }
307         WARN_ON(ret < 0);
311 static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
313         struct cpsw_priv *priv = dev_id;
315         if (likely(netif_running(priv->ndev))) {
316                 cpsw_intr_disable(priv);
317                 cpsw_disable_irq(priv);
318                 napi_schedule(&priv->napi);
319         }
322         return IRQ_HANDLED;
325 static int cpsw_poll(struct napi_struct *napi, int budget)
327         struct cpsw_priv        *priv = napi_to_priv(napi);
328         int                     num_tx, num_rx;
331         num_tx = cpdma_chan_process(priv->txch, 128);
332         num_rx = cpdma_chan_process(priv->rxch, budget);
334         if (num_rx || num_tx)
335                 msg(dbg, intr, "poll %d rx, %d tx pkts\n", num_rx, num_tx);
338         if (num_rx < budget) {
339                 napi_complete(napi);
340                 cpdma_ctlr_eoi(priv->dma);
341                 cpsw_intr_enable(priv);
342                 cpsw_enable_irq(priv);
343         }
345         return num_rx;
348 static inline void soft_reset(const char *module, void __iomem *reg)
350         unsigned long timeout = jiffies + HZ;
352         __raw_writel(1, reg);
353         do {
354                 cpu_relax();
355         } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
357         WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
360 #define mac_hi(mac)     (((mac)[0] << 0) | ((mac)[1] << 8) |    \
361                          ((mac)[2] << 16) | ((mac)[3] << 24))
362 #define mac_lo(mac)     (((mac)[4] << 0) | ((mac)[5] << 8))
364 static void cpsw_set_slave_mac(struct cpsw_slave *slave,
365                                struct cpsw_priv *priv)
367         __raw_writel(mac_hi(priv->mac_addr), &slave->regs->sa_hi);
368         __raw_writel(mac_lo(priv->mac_addr), &slave->regs->sa_lo);
371 static inline u32 cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
373         if (priv->host_port == 0)
374                 return slave_num + 1;
375         else
376                 return slave_num;
379 static void _cpsw_adjust_link(struct cpsw_slave *slave,
380                               struct cpsw_priv *priv, bool *link)
382         struct phy_device       *phy = slave->phy;
383         u32                     mac_control = 0;
384         u32                     slave_port;
386         if (!phy)
387                 return;
389         slave_port = cpsw_get_slave_port(priv, slave->slave_num);
391         if (phy->link) {
392                 /* enable forwarding */
393                 cpsw_ale_control_set(priv->ale, slave_port,
394                         ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
396                 mac_control = priv->data.mac_control;
397                 if (phy->speed == 10)
398                         mac_control |= BIT(18); /* In Band mode */
399                 if (phy->speed == 1000) {
400                         mac_control |= BIT(7);  /* Enable gigabit mode */
401                 }
402                 if (phy->speed == 100)
403                         mac_control |= BIT(15);
404                 if (phy->duplex)
405                         mac_control |= BIT(0);  /* FULLDUPLEXEN */
406                 if (phy->interface == PHY_INTERFACE_MODE_RGMII) /* RGMII */
407                         mac_control |= (BIT(15)|BIT(16));
408                 *link = true;
409         } else {
410                 cpsw_ale_control_set(priv->ale, slave_port,
411                              ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
412                 mac_control = 0;
413         }
415         if (mac_control != slave->mac_control) {
416                 phy_print_status(phy);
417                 __raw_writel(mac_control, &slave->sliver->mac_control);
418         }
420         slave->mac_control = mac_control;
423 static void cpsw_adjust_link(struct net_device *ndev)
425         struct cpsw_priv        *priv = netdev_priv(ndev);
426         bool                    link = false;
428         for_each_slave(priv, _cpsw_adjust_link, priv, &link);
430         if (link) {
431                 netif_carrier_on(ndev);
432                 if (netif_running(ndev))
433                         netif_wake_queue(ndev);
434         } else {
435                 netif_carrier_off(ndev);
436                 netif_stop_queue(ndev);
437         }
440 static inline int __show_stat(char *buf, int maxlen, const char* name, u32 val)
442         static char *leader = "........................................";
444         if (!val)
445                 return 0;
446         else
447                 return snprintf(buf, maxlen, "%s %s %10d\n", name,
448                                 leader + strlen(name), val);
451 static ssize_t cpsw_hw_stats_show(struct device *dev,
452                                      struct device_attribute *attr,
453                                      char *buf)
455         struct net_device       *ndev = to_net_dev(dev);
456         struct cpsw_priv        *priv = netdev_priv(ndev);
457         int                     len = 0;
458         struct cpdma_chan_stats dma_stats;
460 #define show_stat(x) do {                                               \
461         len += __show_stat(buf + len, SZ_4K - len, #x,                  \
462                            __raw_readl(&priv->hw_stats->x));            \
463 } while (0)
465 #define show_dma_stat(x) do {                                           \
466         len += __show_stat(buf + len, SZ_4K - len, #x, dma_stats.x);    \
467 } while (0)
469         len += snprintf(buf + len, SZ_4K - len, "CPSW Statistics:\n");
470         show_stat(rxgoodframes);        show_stat(rxbroadcastframes);
471         show_stat(rxmulticastframes);   show_stat(rxpauseframes);
472         show_stat(rxcrcerrors);         show_stat(rxaligncodeerrors);
473         show_stat(rxoversizedframes);   show_stat(rxjabberframes);
474         show_stat(rxundersizedframes);  show_stat(rxfragments);
475         show_stat(rxoctets);            show_stat(txgoodframes);
476         show_stat(txbroadcastframes);   show_stat(txmulticastframes);
477         show_stat(txpauseframes);       show_stat(txdeferredframes);
478         show_stat(txcollisionframes);   show_stat(txsinglecollframes);
479         show_stat(txmultcollframes);    show_stat(txexcessivecollisions);
480         show_stat(txlatecollisions);    show_stat(txunderrun);
481         show_stat(txcarriersenseerrors); show_stat(txoctets);
482         show_stat(octetframes64);       show_stat(octetframes65t127);
483         show_stat(octetframes128t255);  show_stat(octetframes256t511);
484         show_stat(octetframes512t1023); show_stat(octetframes1024tup);
485         show_stat(netoctets);           show_stat(rxsofoverruns);
486         show_stat(rxmofoverruns);       show_stat(rxdmaoverruns);
488         cpdma_chan_get_stats(priv->rxch, &dma_stats);
489         len += snprintf(buf + len, SZ_4K - len, "\nRX DMA Statistics:\n");
490         show_dma_stat(head_enqueue);    show_dma_stat(tail_enqueue);
491         show_dma_stat(pad_enqueue);     show_dma_stat(misqueued);
492         show_dma_stat(desc_alloc_fail); show_dma_stat(pad_alloc_fail);
493         show_dma_stat(runt_receive_buff); show_dma_stat(runt_transmit_buff);
494         show_dma_stat(empty_dequeue);   show_dma_stat(busy_dequeue);
495         show_dma_stat(good_dequeue);    show_dma_stat(teardown_dequeue);
497         cpdma_chan_get_stats(priv->txch, &dma_stats);
498         len += snprintf(buf + len, SZ_4K - len, "\nTX DMA Statistics:\n");
499         show_dma_stat(head_enqueue);    show_dma_stat(tail_enqueue);
500         show_dma_stat(pad_enqueue);     show_dma_stat(misqueued);
501         show_dma_stat(desc_alloc_fail); show_dma_stat(pad_alloc_fail);
502         show_dma_stat(runt_receive_buff); show_dma_stat(runt_transmit_buff);
503         show_dma_stat(empty_dequeue);   show_dma_stat(busy_dequeue);
504         show_dma_stat(good_dequeue);    show_dma_stat(teardown_dequeue);
506         return len;
509 DEVICE_ATTR(hw_stats, S_IRUGO, cpsw_hw_stats_show, NULL);
511 #define PHY_CONFIG_REG  22
512 static void cpsw_set_phy_config(struct cpsw_priv *priv, struct phy_device *phy)
514         struct cpsw_platform_data *pdata = priv->pdev->dev.platform_data;
515         struct mii_bus *miibus;
516         int phy_addr = 0;
517         u16 val = 0;
518         u16 tmp = 0;
520         if (!phy)
521                 return;
523         miibus = phy->bus;
525         if (!miibus)
526                 return;
528         phy_addr = phy->addr;
530         /* Disable 1 Gig mode support if it is not supported */
531         if (!pdata->gigabit_en)
532                 phy->supported &= ~(SUPPORTED_1000baseT_Half |
533                                         SUPPORTED_1000baseT_Full);
535         /* Following lines enable gigbit advertisement capability even in case
536          * the advertisement is not enabled by default
537          */
538         val = miibus->read(miibus, phy_addr, MII_BMCR);
539         val |= (BMCR_SPEED100 | BMCR_ANENABLE | BMCR_FULLDPLX);
540         miibus->write(miibus, phy_addr, MII_BMCR, val);
541         tmp = miibus->read(miibus, phy_addr, MII_BMCR);
543         /* Enable gigabit support only if the speed is 1000Mbps */
544         if (phy->speed == CPSW_PHY_SPEED) {
545                 tmp = miibus->read(miibus, phy_addr, MII_BMSR);
546                 if (tmp & 0x1) {
547                         val = miibus->read(miibus, phy_addr, MII_CTRL1000);
548                         val |= BIT(9);
549                         miibus->write(miibus, phy_addr, MII_CTRL1000, val);
550                         tmp = miibus->read(miibus, phy_addr, MII_CTRL1000);
551                 }
552         }
554         val = miibus->read(miibus, phy_addr, MII_ADVERTISE);
555         val |= (ADVERTISE_10HALF | ADVERTISE_10FULL | \
556                 ADVERTISE_100HALF | ADVERTISE_100FULL);
557         miibus->write(miibus, phy_addr, MII_ADVERTISE, val);
558         tmp = miibus->read(miibus, phy_addr, MII_ADVERTISE);
560         /* TODO : This check is required. This should be
561          * moved to a board init section as its specific
562          * to a phy.*/
563         if (phy->phy_id == 0x0282F014) {
564                 /* This enables TX_CLK-ing in case of 10/100MBps operation */
565                 val = miibus->read(miibus, phy_addr, PHY_CONFIG_REG);
566                 val |= BIT(5);
567                 miibus->write(miibus, phy_addr, PHY_CONFIG_REG, val);
568                 tmp = miibus->read(miibus, phy_addr, PHY_CONFIG_REG);
569         }
571         return;
574 static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
576         char name[32];
577         u32 slave_port;
579         sprintf(name, "slave-%d", slave->slave_num);
581         soft_reset(name, &slave->sliver->soft_reset);
583         /* setup priority mapping */
584         __raw_writel(0x76543210, &slave->sliver->rx_pri_map);
585         __raw_writel(0x33221100, &slave->regs->tx_pri_map);
587         /* setup max packet size, and mac address */
588         __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
589         cpsw_set_slave_mac(slave, priv);
591         slave->mac_control = 0; /* no link yet */
593         slave_port = cpsw_get_slave_port(priv, slave->slave_num);
594         cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
595                            1 << slave_port);
597         slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
598                                  &cpsw_adjust_link, 0, slave->data->phy_if);
599         if (IS_ERR(slave->phy)) {
600                 msg(err, ifup, "phy %s not found on slave %d\n",
601                     slave->data->phy_id, slave->slave_num);
602                 slave->phy = NULL;
603         } else {
604                 printk(KERN_ERR"\nCPSW phy found : id is : 0x%x\n",
605                         slave->phy->phy_id);
606                 cpsw_set_phy_config(priv, slave->phy);
607                 phy_start(slave->phy);
608         }
611 static void cpsw_init_host_port(struct cpsw_priv *priv)
613         /* soft reset the controller and initialize ale */
614         soft_reset("cpsw", &priv->regs->soft_reset);
615         cpsw_ale_start(priv->ale);
617         /* switch to vlan unaware mode */
618         cpsw_ale_control_set(priv->ale, 0, ALE_VLAN_AWARE, 0);
620         /* setup host port priority mapping */
621         __raw_writel(0x76543210, &priv->host_port_regs->cpdma_tx_pri_map);
622         __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
624         cpsw_ale_control_set(priv->ale, priv->host_port,
625                              ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
627         cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port,
628                           0);
629                            /* ALE_SECURE); */
630         cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
631                            1 << priv->host_port);
634 static int cpsw_ndo_open(struct net_device *ndev)
636         struct cpsw_priv *priv = netdev_priv(ndev);
637         int i, ret;
638         u32 reg;
640         cpsw_intr_disable(priv);
641         netif_carrier_off(ndev);
643         ret = clk_enable(priv->clk);
644         if (ret < 0) {
645                 dev_err(priv->dev, "unable to turn on device clock\n");
646                 return ret;
647         }
649         ret = device_create_file(&ndev->dev, &dev_attr_hw_stats);
650         if (ret < 0) {
651                 dev_err(priv->dev, "unable to add device attr\n");
652                 return ret;
653         }
655         if (priv->data.phy_control)
656                 (*priv->data.phy_control)(true);
658         reg = __raw_readl(&priv->regs->id_ver);
660         msg(info, ifup, "initializing cpsw version %d.%d (%d)\n",
661             (reg >> 8 & 0x7), reg & 0xff, (reg >> 11) & 0x1f);
663         /* initialize host and slave ports */
664         cpsw_init_host_port(priv);
665         for_each_slave(priv, cpsw_slave_open, priv);
667         /* setup tx dma to fixed prio and zero offset */
668         cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
669         cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
671         /* disable priority elevation and enable statistics on all ports */
672         __raw_writel(0, &priv->regs->ptype);
674         /* enable statistics collection only on the host port */
675         /* __raw_writel(BIT(priv->host_port), &priv->regs->stat_port_en); */
676         __raw_writel(0x7, &priv->regs->stat_port_en);
678         if (WARN_ON(!priv->data.rx_descs))
679                 priv->data.rx_descs = 128;
681         for (i = 0; i < priv->data.rx_descs; i++) {
682                 struct sk_buff *skb;
684                 ret = -ENOMEM;
685                 skb = netdev_alloc_skb_ip_align(priv->ndev,
686                                                 priv->rx_packet_max);
687                 if (!skb)
688                         break;
689                 ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
690                                         skb_tailroom(skb), GFP_KERNEL);
691                 if (WARN_ON(ret < 0))
692                         break;
693         }
694         /* continue even if we didn't manage to submit all receive descs */
695         msg(info, ifup, "submitted %d rx descriptors\n", i);
697         cpdma_ctlr_start(priv->dma);
698         cpsw_intr_enable(priv);
699         napi_enable(&priv->napi);
700         cpdma_ctlr_eoi(priv->dma);
702         return 0;
705 static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
707         if (!slave->phy)
708                 return;
709         phy_stop(slave->phy);
710         phy_disconnect(slave->phy);
711         slave->phy = NULL;
714 static int cpsw_ndo_stop(struct net_device *ndev)
716         struct cpsw_priv *priv = netdev_priv(ndev);
718         msg(info, ifdown, "shutting down cpsw device\n");
719         cpsw_intr_disable(priv);
720         cpdma_ctlr_int_ctrl(priv->dma, false);
721         cpdma_ctlr_stop(priv->dma);
722         netif_stop_queue(priv->ndev);
723         napi_disable(&priv->napi);
724         netif_carrier_off(priv->ndev);
725         cpsw_ale_stop(priv->ale);
726         device_remove_file(&ndev->dev, &dev_attr_hw_stats);
727         for_each_slave(priv, cpsw_slave_stop, priv);
728         if (priv->data.phy_control)
729                 (*priv->data.phy_control)(false);
730         clk_disable(priv->clk);
731         return 0;
734 static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
735                                        struct net_device *ndev)
737         struct cpsw_priv *priv = netdev_priv(ndev);
738         int ret;
740         ndev->trans_start = jiffies;
742         ret = skb_padto(skb, CPSW_MIN_PACKET_SIZE);
743         if (unlikely(ret < 0)) {
744                 msg(err, tx_err, "packet pad failed");
745                 goto fail;
746         }
748         ret = cpdma_chan_submit(priv->txch, skb, skb->data,
749                                 skb->len, GFP_KERNEL);
750         if (unlikely(ret != 0)) {
751                 msg(err, tx_err, "desc submit failed");
752                 goto fail;
753         }
755         return NETDEV_TX_OK;
756 fail:
757         priv->stats.tx_dropped++;
758         netif_stop_queue(ndev);
759         return NETDEV_TX_BUSY;
762 static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags)
764         /*
765          * The switch cannot operate in promiscuous mode without substantial
766          * headache.  For promiscuous mode to work, we would need to put the
767          * ALE in bypass mode and route all traffic to the host port.
768          * Subsequently, the host will need to operate as a "bridge", learn,
769          * and flood as needed.  For now, we simply complain here and
770          * do nothing about it :-)
771          */
772         if ((flags & IFF_PROMISC) && (ndev->flags & IFF_PROMISC))
773                 dev_err(&ndev->dev, "promiscuity ignored!\n");
775         /*
776          * The switch cannot filter multicast traffic unless it is configured
777          * in "VLAN Aware" mode.  Unfortunately, VLAN awareness requires a
778          * whole bunch of additional logic that this driver does not implement
779          * at present.
780          */
781         if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI))
782                 dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n");
785 static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *addr)
787         struct cpsw_priv *priv = netdev_priv(ndev);
789         cpsw_ale_del_ucast(priv->ale, priv->mac_addr, priv->host_port);
790         memcpy(priv->mac_addr, ndev->dev_addr, ETH_ALEN);
791         cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port,
792                            0);
793                            /* ALE_SECURE); */
794         for_each_slave(priv, cpsw_set_slave_mac, priv);
795         return 0;
798 static void cpsw_ndo_tx_timeout(struct net_device *ndev)
800         struct cpsw_priv *priv = netdev_priv(ndev);
802         msg(err, tx_err, "transmit timeout, restarting dma");
803         priv->stats.tx_errors++;
804         cpsw_intr_disable(priv);
805         cpdma_ctlr_int_ctrl(priv->dma, false);
806         cpdma_chan_stop(priv->txch);
807         cpdma_chan_start(priv->txch);
808         cpdma_ctlr_int_ctrl(priv->dma, true);
809         cpsw_intr_enable(priv);
810         cpdma_ctlr_eoi(priv->dma);
813 static struct net_device_stats *cpsw_ndo_get_stats(struct net_device *ndev)
815         struct cpsw_priv *priv = netdev_priv(ndev);
816         return &priv->stats;
819 #ifdef CONFIG_NET_POLL_CONTROLLER
820 static void cpsw_ndo_poll_controller(struct net_device *ndev)
822         struct cpsw_priv *priv = netdev_priv(ndev);
824         cpsw_intr_disable(priv);
825         cpdma_ctlr_int_ctrl(priv->dma, false);
826         cpsw_interrupt(ndev->irq, priv);
827         cpdma_ctlr_int_ctrl(priv->dma, true);
828         cpsw_intr_enable(priv);
829         cpdma_ctlr_eoi(priv->dma);
831 #endif
833 static const struct net_device_ops cpsw_netdev_ops = {
834         .ndo_open               = cpsw_ndo_open,
835         .ndo_stop               = cpsw_ndo_stop,
836         .ndo_start_xmit         = cpsw_ndo_start_xmit,
837         .ndo_change_rx_flags    = cpsw_ndo_change_rx_flags,
838         .ndo_set_mac_address    = cpsw_ndo_set_mac_address,
839         .ndo_validate_addr      = eth_validate_addr,
840         .ndo_tx_timeout         = cpsw_ndo_tx_timeout,
841         .ndo_get_stats          = cpsw_ndo_get_stats,
842 #ifdef CONFIG_NET_POLL_CONTROLLER
843         .ndo_poll_controller    = cpsw_ndo_poll_controller,
844 #endif
845 };
847 static void cpsw_get_drvinfo(struct net_device *ndev,
848                              struct ethtool_drvinfo *info)
850         struct cpsw_priv *priv = netdev_priv(ndev);
851         strcpy(info->driver, "TI CPSW Driver v1.0");
852         strcpy(info->version, "1.0");
853         strcpy(info->bus_info, priv->pdev->name);
856 static u32 cpsw_get_msglevel(struct net_device *ndev)
858         struct cpsw_priv *priv = netdev_priv(ndev);
859         return priv->msg_enable;
862 static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
864         struct cpsw_priv *priv = netdev_priv(ndev);
865         priv->msg_enable = value;
868 static const struct ethtool_ops cpsw_ethtool_ops = {
869         .get_drvinfo    = cpsw_get_drvinfo,
870         .get_msglevel   = cpsw_get_msglevel,
871         .set_msglevel   = cpsw_set_msglevel,
872         .get_link       = ethtool_op_get_link,
873 };
875 static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv)
877         void __iomem            *regs = priv->regs;
878         int                     slave_num = slave->slave_num;
879         struct cpsw_slave_data  *data = priv->data.slave_data + slave_num;
881         slave->data     = data;
882         slave->regs     = regs + data->slave_reg_ofs;
883         slave->sliver   = regs + data->sliver_reg_ofs;
886 static int __devinit cpsw_probe(struct platform_device *pdev)
888         struct cpsw_platform_data       *data = pdev->dev.platform_data;
889         struct net_device               *ndev;
890         struct cpsw_priv                *priv;
891         struct cpdma_params             dma_params;
892         struct cpsw_ale_params          ale_params;
893         void __iomem                    *regs;
894         struct resource                 *res;
895         int ret = 0, i, k = 0;
897         if (!data) {
898                 pr_err("cpsw: platform data missing\n");
899                 return -ENODEV;
900         }
902         ndev = alloc_etherdev(sizeof(struct cpsw_priv));
903         if (!ndev) {
904                 pr_err("cpsw: error allocating net_device\n");
905                 return -ENOMEM;
906         }
908         platform_set_drvdata(pdev, ndev);
909         priv = netdev_priv(ndev);
910         spin_lock_init(&priv->lock);
911         priv->data = *data;
912         priv->pdev = pdev;
913         priv->ndev = ndev;
914         priv->dev  = &ndev->dev;
915         priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
916         priv->rx_packet_max = max(rx_packet_max, 128);
918         if (is_valid_ether_addr(data->mac_addr)) {
919                 memcpy(priv->mac_addr, data->mac_addr, ETH_ALEN);
920                 printk(KERN_INFO"Detected MACID=%x:%x:%x:%x:%x:%x\n",
921                         priv->mac_addr[0], priv->mac_addr[1],
922                         priv->mac_addr[2], priv->mac_addr[3],
923                         priv->mac_addr[4], priv->mac_addr[5]);
924         } else {
925                 random_ether_addr(priv->mac_addr);
926                 printk(KERN_INFO"Random MACID=%x:%x:%x:%x:%x:%x\n",
927                         priv->mac_addr[0], priv->mac_addr[1],
928                         priv->mac_addr[2], priv->mac_addr[3],
929                         priv->mac_addr[4], priv->mac_addr[5]);
930         }
932         memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
934         priv->slaves = kzalloc(sizeof(struct cpsw_slave) * data->slaves,
935                                GFP_KERNEL);
936         if (!priv->slaves) {
937                 dev_err(priv->dev, "failed to allocate slave ports\n");
938                 ret = -EBUSY;
939                 goto clean_ndev_ret;
940         }
941         for (i = 0; i < data->slaves; i++)
942                 priv->slaves[i].slave_num = i;
944         priv->clk = clk_get(&pdev->dev, NULL);
945         if (IS_ERR(priv->clk))
946                 dev_err(priv->dev, "failed to get device clock\n");
947         priv->cpsw_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
948         if (!priv->cpsw_res) {
949                 dev_err(priv->dev, "error getting i/o resource\n");
950                 ret = -ENOENT;
951                 goto clean_clk_ret;
952         }
954         if (!request_mem_region(priv->cpsw_res->start,
955                 resource_size(priv->cpsw_res), ndev->name)) {
956                 dev_err(priv->dev, "failed request i/o region\n");
957                 ret = -ENXIO;
958                 goto clean_clk_ret;
959         }
961         regs = ioremap(priv->cpsw_res->start, resource_size(priv->cpsw_res));
962         if (!regs) {
963                 dev_err(priv->dev, "unable to map i/o region\n");
964                 goto clean_cpsw_iores_ret;
965         }
966         priv->regs = regs;
967         priv->host_port = data->host_port_num;
968         priv->host_port_regs = regs + data->host_port_reg_ofs;
969         priv->hw_stats = regs + data->hw_stats_reg_ofs;
971         priv->cpsw_ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
972         if (!priv->cpsw_ss_res) {
973                 dev_err(priv->dev, "error getting i/o resource\n");
974                 ret = -ENOENT;
975                 goto clean_clk_ret;
976         }
978         if (!request_mem_region(priv->cpsw_ss_res->start,
979                 resource_size(priv->cpsw_ss_res), ndev->name)) {
980                 dev_err(priv->dev, "failed request i/o region\n");
981                 ret = -ENXIO;
982                 goto clean_clk_ret;
983         }
985         regs = ioremap(priv->cpsw_ss_res->start,
986                         resource_size(priv->cpsw_ss_res));
987         if (!regs) {
988                 dev_err(priv->dev, "unable to map i/o region\n");
989                 goto clean_cpsw_ss_iores_ret;
990         }
991         priv->ss_regs = regs;
994         for_each_slave(priv, cpsw_slave_init, priv);
996         memset(&dma_params, 0, sizeof(dma_params));
997         dma_params.dev                  = &pdev->dev;
998         dma_params.dmaregs              = (void __iomem *)(((u32)priv->regs) +
999                                                 data->cpdma_reg_ofs);
1000         dma_params.rxthresh             = (void __iomem *)(((u32)priv->regs) +
1001                                         data->cpdma_reg_ofs + CPDMA_RXTHRESH);
1002         dma_params.rxfree               = (void __iomem *)(((u32)priv->regs) +
1003                                         data->cpdma_reg_ofs + CPDMA_RXFREE);
1005         if (data->version == CPSW_VERSION_2) {
1006                 dma_params.txhdp        = (void __iomem *)(((u32)priv->regs) +
1007                                         data->cpdma_reg_ofs + CPDMA_TXHDP_VER2);
1008                 dma_params.rxhdp        = (void __iomem *)(((u32)priv->regs) +
1009                                         data->cpdma_reg_ofs + CPDMA_RXHDP_VER2);
1010                 dma_params.txcp         = (void __iomem *)(((u32)priv->regs) +
1011                                         data->cpdma_reg_ofs + CPDMA_TXCP_VER2);
1012                 dma_params.rxcp         = (void __iomem *)(((u32)priv->regs) +
1013                                         data->cpdma_reg_ofs + CPDMA_RXCP_VER2);
1014         } else {
1015                 dma_params.txhdp        = (void __iomem *)(((u32)priv->regs) +
1016                                         data->cpdma_reg_ofs + CPDMA_TXHDP_VER1);
1017                 dma_params.rxhdp        = (void __iomem *)(((u32)priv->regs) +
1018                                         data->cpdma_reg_ofs + CPDMA_RXHDP_VER1);
1019                 dma_params.txcp         = (void __iomem *)(((u32)priv->regs) +
1020                                         data->cpdma_reg_ofs + CPDMA_TXCP_VER1);
1021                 dma_params.rxcp         = (void __iomem *)(((u32)priv->regs) +
1022                                         data->cpdma_reg_ofs + CPDMA_RXCP_VER1);
1023         }
1025         dma_params.num_chan             = data->channels;
1026         dma_params.has_soft_reset       = true;
1027         dma_params.min_packet_size      = CPSW_MIN_PACKET_SIZE;
1028         dma_params.desc_mem_size        = data->bd_ram_size;
1029         dma_params.desc_align           = 16;
1030         dma_params.has_ext_regs         = true;
1031         dma_params.desc_mem_phys        = data->no_bd_ram ? 0 :
1032                         (u32 __force)priv->cpsw_res->start + data->bd_ram_ofs;
1033         dma_params.desc_hw_addr         = data->hw_ram_addr ?
1034                                 data->hw_ram_addr : dma_params.desc_mem_phys ;
1036         priv->dma = cpdma_ctlr_create(&dma_params);
1037         if (!priv->dma) {
1038                 dev_err(priv->dev, "error initializing dma\n");
1039                 ret = -ENOMEM;
1040                 goto clean_iomap_ret;
1041         }
1043         priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
1044                                        cpsw_tx_handler);
1045         priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
1046                                        cpsw_rx_handler);
1048         if (WARN_ON(!priv->txch || !priv->rxch)) {
1049                 dev_err(priv->dev, "error initializing dma channels\n");
1050                 ret = -ENOMEM;
1051                 goto clean_dma_ret;
1052         }
1054         memset(&ale_params, 0, sizeof(ale_params));
1055         ale_params.dev          = &ndev->dev;
1056         ale_params.ale_regs     = (void *)((u32)priv->regs) +
1057                                         ((u32)data->ale_reg_ofs);
1058         ale_params.ale_ageout   = ale_ageout;
1059         ale_params.ale_entries  = data->ale_entries;
1060         ale_params.ale_ports    = data->slaves;
1062         priv->ale = cpsw_ale_create(&ale_params);
1063         if (!priv->ale) {
1064                 dev_err(priv->dev, "error initializing ale engine\n");
1065                 ret = -ENODEV;
1066                 goto clean_dma_ret;
1067         }
1069         ndev->irq = platform_get_irq(pdev, 0);
1070         if (ndev->irq < 0) {
1071                 dev_err(priv->dev, "error getting irq resource\n");
1072                 ret = -ENOENT;
1073                 goto clean_ale_ret;
1074         }
1076         while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
1077                 for (i = res->start; i <= res->end; i++) {
1078                         if (request_irq(i, cpsw_interrupt, IRQF_DISABLED,
1079                                         dev_name(&pdev->dev), priv)) {
1080                                 dev_err(priv->dev, "error attaching irq\n");
1081                                 goto clean_ale_ret;
1082                         }
1083                         #ifdef CPSW_IRQ_QUIRK
1084                         priv->irqs_table[k] = i;
1085                         priv->num_irqs = k;
1086                         #endif
1087                 }
1088                 k++;
1089         }
1091         ndev->flags |= IFF_ALLMULTI;    /* see cpsw_ndo_change_rx_flags() */
1093         ndev->netdev_ops = &cpsw_netdev_ops;
1094         SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
1095         netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT);
1097         /* register the network device */
1098         SET_NETDEV_DEV(ndev, &pdev->dev);
1099         ret = register_netdev(ndev);
1100         if (ret) {
1101                 dev_err(priv->dev, "error registering net device\n");
1102                 ret = -ENODEV;
1103                 goto clean_irq_ret;
1104         }
1106         msg(notice, probe, "initialized device (regs %x, irq %d)\n",
1107             priv->cpsw_res->start, ndev->irq);
1109         return 0;
1111 clean_irq_ret:
1112         free_irq(ndev->irq, priv);
1113 clean_ale_ret:
1114         cpsw_ale_destroy(priv->ale);
1115 clean_dma_ret:
1116         cpdma_chan_destroy(priv->txch);
1117         cpdma_chan_destroy(priv->rxch);
1118         cpdma_ctlr_destroy(priv->dma);
1119 clean_iomap_ret:
1120         iounmap(priv->regs);
1121 clean_cpsw_ss_iores_ret:
1122         release_mem_region(priv->cpsw_ss_res->start,
1123                                 resource_size(priv->cpsw_ss_res));
1124 clean_cpsw_iores_ret:
1125         release_mem_region(priv->cpsw_res->start,
1126                                 resource_size(priv->cpsw_res));
1127 clean_clk_ret:
1128         clk_put(priv->clk);
1129         kfree(priv->slaves);
1130 clean_ndev_ret:
1131         free_netdev(ndev);
1132         return ret;
1135 static int __devexit cpsw_remove(struct platform_device *pdev)
1137         struct net_device *ndev = platform_get_drvdata(pdev);
1138         struct cpsw_priv *priv = netdev_priv(ndev);
1140         msg(notice, probe, "removing device\n");
1141         platform_set_drvdata(pdev, NULL);
1143         free_irq(ndev->irq, priv);
1144         cpsw_ale_destroy(priv->ale);
1145         cpdma_chan_destroy(priv->txch);
1146         cpdma_chan_destroy(priv->rxch);
1147         cpdma_ctlr_destroy(priv->dma);
1148         iounmap(priv->regs);
1149         release_mem_region(priv->cpsw_res->start,
1150                                 resource_size(priv->cpsw_res));
1151         release_mem_region(priv->cpsw_ss_res->start,
1152                                 resource_size(priv->cpsw_ss_res));
1153         clk_put(priv->clk);
1154         kfree(priv->slaves);
1155         free_netdev(ndev);
1157         return 0;
1160 static int cpsw_suspend(struct device *dev)
1162         struct platform_device  *pdev = to_platform_device(dev);
1163         struct net_device       *ndev = platform_get_drvdata(pdev);
1165         if (netif_running(ndev))
1166                 cpsw_ndo_stop(ndev);
1167         return 0;
1170 static int cpsw_resume(struct device *dev)
1172         struct platform_device  *pdev = to_platform_device(dev);
1173         struct net_device       *ndev = platform_get_drvdata(pdev);
1175         if (netif_running(ndev))
1176                 cpsw_ndo_open(ndev);
1177         return 0;
1180 static const struct dev_pm_ops cpsw_pm_ops = {
1181         .suspend        = cpsw_suspend,
1182         .resume         = cpsw_resume,
1183 };
1185 static struct platform_driver cpsw_driver = {
1186         .driver = {
1187                 .name    = "cpsw",
1188                 .owner   = THIS_MODULE,
1189                 .pm      = &cpsw_pm_ops,
1190         },
1191         .probe = cpsw_probe,
1192         .remove = __devexit_p(cpsw_remove),
1193 };
1195 static int __init cpsw_init(void)
1197         return platform_driver_register(&cpsw_driver);
1199 late_initcall(cpsw_init);
1201 static void __exit cpsw_exit(void)
1203         platform_driver_unregister(&cpsw_driver);
1205 module_exit(cpsw_exit);
1207 MODULE_LICENSE("GPL");
1208 MODULE_DESCRIPTION("TI CPSW Ethernet driver");