2 * Texas Instruments Ethernet Switch Driver
4 * Copyright (C) 2012 Texas Instruments
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.
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.
16 #include <linux/kernel.h>
18 #include <linux/clk.h>
19 #include <linux/timer.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/irqreturn.h>
23 #include <linux/interrupt.h>
24 #include <linux/if_ether.h>
25 #include <linux/etherdevice.h>
26 #include <linux/netdevice.h>
27 #include <linux/net_tstamp.h>
28 #include <linux/phy.h>
29 #include <linux/workqueue.h>
30 #include <linux/delay.h>
31 #include <linux/pm_runtime.h>
33 #include <linux/of_net.h>
34 #include <linux/of_device.h>
36 #include <linux/platform_data/cpsw.h>
40 #include "davinci_cpdma.h"
42 #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
43 NETIF_MSG_DRV | NETIF_MSG_LINK | \
44 NETIF_MSG_IFUP | NETIF_MSG_INTR | \
45 NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
46 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
47 NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
48 NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
51 #define cpsw_info(priv, type, format, ...) \
53 if (netif_msg_##type(priv) && net_ratelimit()) \
54 dev_info(priv->dev, format, ## __VA_ARGS__); \
57 #define cpsw_err(priv, type, format, ...) \
59 if (netif_msg_##type(priv) && net_ratelimit()) \
60 dev_err(priv->dev, format, ## __VA_ARGS__); \
63 #define cpsw_dbg(priv, type, format, ...) \
65 if (netif_msg_##type(priv) && net_ratelimit()) \
66 dev_dbg(priv->dev, format, ## __VA_ARGS__); \
69 #define cpsw_notice(priv, type, format, ...) \
71 if (netif_msg_##type(priv) && net_ratelimit()) \
72 dev_notice(priv->dev, format, ## __VA_ARGS__); \
75 #define ALE_ALL_PORTS 0x7
77 #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
78 #define CPSW_MINOR_VERSION(reg) (reg & 0xff)
79 #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
81 #define CPSW_VERSION_1 0x19010a
82 #define CPSW_VERSION_2 0x19010c
84 #define HOST_PORT_NUM 0
85 #define SLIVER_SIZE 0x40
87 #define CPSW1_HOST_PORT_OFFSET 0x028
88 #define CPSW1_SLAVE_OFFSET 0x050
89 #define CPSW1_SLAVE_SIZE 0x040
90 #define CPSW1_CPDMA_OFFSET 0x100
91 #define CPSW1_STATERAM_OFFSET 0x200
92 #define CPSW1_CPTS_OFFSET 0x500
93 #define CPSW1_ALE_OFFSET 0x600
94 #define CPSW1_SLIVER_OFFSET 0x700
96 #define CPSW2_HOST_PORT_OFFSET 0x108
97 #define CPSW2_SLAVE_OFFSET 0x200
98 #define CPSW2_SLAVE_SIZE 0x100
99 #define CPSW2_CPDMA_OFFSET 0x800
100 #define CPSW2_STATERAM_OFFSET 0xa00
101 #define CPSW2_CPTS_OFFSET 0xc00
102 #define CPSW2_ALE_OFFSET 0xd00
103 #define CPSW2_SLIVER_OFFSET 0xd80
104 #define CPSW2_BD_OFFSET 0x2000
106 #define CPDMA_RXTHRESH 0x0c0
107 #define CPDMA_RXFREE 0x0e0
108 #define CPDMA_TXHDP 0x00
109 #define CPDMA_RXHDP 0x20
110 #define CPDMA_TXCP 0x40
111 #define CPDMA_RXCP 0x60
113 #define CPSW_POLL_WEIGHT 64
114 #define CPSW_MIN_PACKET_SIZE 60
115 #define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
117 #define RX_PRIORITY_MAPPING 0x76543210
118 #define TX_PRIORITY_MAPPING 0x33221100
119 #define CPDMA_TX_PRIORITY_MAP 0x76543210
121 #define cpsw_enable_irq(priv) \
124 for (i = 0; i < priv->num_irqs; i++) \
125 enable_irq(priv->irqs_table[i]); \
127 #define cpsw_disable_irq(priv) \
130 for (i = 0; i < priv->num_irqs; i++) \
131 disable_irq_nosync(priv->irqs_table[i]); \
134 static int debug_level;
135 module_param(debug_level, int, 0);
136 MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
138 static int ale_ageout = 10;
139 module_param(ale_ageout, int, 0);
140 MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
142 static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
143 module_param(rx_packet_max, int, 0);
144 MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
146 struct cpsw_wr_regs {
157 struct cpsw_ss_regs {
174 #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
175 #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
176 #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
177 #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
178 #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
179 #define CPSW1_TS_CTL 0x14 /* Time Sync Control */
180 #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
181 #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
184 #define CPSW2_CONTROL 0x00 /* Control Register */
185 #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
186 #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
187 #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
188 #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
189 #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
190 #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
192 /* CPSW_PORT_V1 and V2 */
193 #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
194 #define SA_HI 0x24 /* CPGMAC_SL Source Address High */
195 #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
197 /* CPSW_PORT_V2 only */
198 #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
199 #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
200 #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
201 #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
202 #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
203 #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
204 #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
205 #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
207 /* Bit definitions for the CPSW2_CONTROL register */
208 #define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
209 #define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
210 #define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
211 #define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
212 #define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
213 #define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
214 #define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
215 #define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
216 #define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
217 #define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
218 #define TS_BIT8 (1<<8) /* ts_ttl_nonzero? */
219 #define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
220 #define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
221 #define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
222 #define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
223 #define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
225 #define CTRL_TS_BITS \
226 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 | TS_BIT8 | \
227 TS_ANNEX_D_EN | TS_LTYPE1_EN)
229 #define CTRL_ALL_TS_MASK (CTRL_TS_BITS | TS_TX_EN | TS_RX_EN)
230 #define CTRL_TX_TS_BITS (CTRL_TS_BITS | TS_TX_EN)
231 #define CTRL_RX_TS_BITS (CTRL_TS_BITS | TS_RX_EN)
233 /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
234 #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
235 #define TS_SEQ_ID_OFFSET_MASK (0x3f)
236 #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
237 #define TS_MSG_TYPE_EN_MASK (0xffff)
239 /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
240 #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
242 /* Bit definitions for the CPSW1_TS_CTL register */
243 #define CPSW_V1_TS_RX_EN BIT(0)
244 #define CPSW_V1_TS_TX_EN BIT(4)
245 #define CPSW_V1_MSG_TYPE_OFS 16
247 /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
248 #define CPSW_V1_SEQ_ID_OFS_SHIFT 16
250 struct cpsw_host_regs {
256 u32 cpdma_tx_pri_map;
257 u32 cpdma_rx_chan_map;
260 struct cpsw_sliver_regs {
275 struct cpsw_sliver_regs __iomem *sliver;
278 struct cpsw_slave_data *data;
279 struct phy_device *phy;
282 static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
284 return __raw_readl(slave->regs + offset);
287 static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
289 __raw_writel(val, slave->regs + offset);
294 struct platform_device *pdev;
295 struct net_device *ndev;
296 struct resource *cpsw_res;
297 struct resource *cpsw_wr_res;
298 struct napi_struct napi;
300 struct cpsw_platform_data data;
301 struct cpsw_ss_regs __iomem *regs;
302 struct cpsw_wr_regs __iomem *wr_regs;
303 struct cpsw_host_regs __iomem *host_port_regs;
306 struct net_device_stats stats;
310 u8 mac_addr[ETH_ALEN];
311 struct cpsw_slave *slaves;
312 struct cpdma_ctlr *dma;
313 struct cpdma_chan *txch, *rxch;
314 struct cpsw_ale *ale;
315 /* snapshot of IRQ numbers */
321 #define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi)
322 #define for_each_slave(priv, func, arg...) \
325 for (idx = 0; idx < (priv)->data.slaves; idx++) \
326 (func)((priv)->slaves + idx, ##arg); \
329 static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
331 struct cpsw_priv *priv = netdev_priv(ndev);
333 if (ndev->flags & IFF_PROMISC) {
334 /* Enable promiscuous mode */
335 dev_err(priv->dev, "Ignoring Promiscuous mode\n");
339 /* Clear all mcast from ALE */
340 cpsw_ale_flush_multicast(priv->ale, ALE_ALL_PORTS << priv->host_port);
342 if (!netdev_mc_empty(ndev)) {
343 struct netdev_hw_addr *ha;
345 /* program multicast address list into ALE register */
346 netdev_for_each_mc_addr(ha, ndev) {
347 cpsw_ale_add_mcast(priv->ale, (u8 *)ha->addr,
348 ALE_ALL_PORTS << priv->host_port, 0, 0);
353 static void cpsw_intr_enable(struct cpsw_priv *priv)
355 __raw_writel(0xFF, &priv->wr_regs->tx_en);
356 __raw_writel(0xFF, &priv->wr_regs->rx_en);
358 cpdma_ctlr_int_ctrl(priv->dma, true);
362 static void cpsw_intr_disable(struct cpsw_priv *priv)
364 __raw_writel(0, &priv->wr_regs->tx_en);
365 __raw_writel(0, &priv->wr_regs->rx_en);
367 cpdma_ctlr_int_ctrl(priv->dma, false);
371 void cpsw_tx_handler(void *token, int len, int status)
373 struct sk_buff *skb = token;
374 struct net_device *ndev = skb->dev;
375 struct cpsw_priv *priv = netdev_priv(ndev);
377 if (unlikely(netif_queue_stopped(ndev)))
378 netif_start_queue(ndev);
379 cpts_tx_timestamp(&priv->cpts, skb);
380 priv->stats.tx_packets++;
381 priv->stats.tx_bytes += len;
382 dev_kfree_skb_any(skb);
385 void cpsw_rx_handler(void *token, int len, int status)
387 struct sk_buff *skb = token;
388 struct net_device *ndev = skb->dev;
389 struct cpsw_priv *priv = netdev_priv(ndev);
392 /* free and bail if we are shutting down */
393 if (unlikely(!netif_running(ndev)) ||
394 unlikely(!netif_carrier_ok(ndev))) {
395 dev_kfree_skb_any(skb);
398 if (likely(status >= 0)) {
400 cpts_rx_timestamp(&priv->cpts, skb);
401 skb->protocol = eth_type_trans(skb, ndev);
402 netif_receive_skb(skb);
403 priv->stats.rx_bytes += len;
404 priv->stats.rx_packets++;
408 if (unlikely(!netif_running(ndev))) {
410 dev_kfree_skb_any(skb);
415 skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
419 ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
420 skb_tailroom(skb), GFP_KERNEL);
425 static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
427 struct cpsw_priv *priv = dev_id;
429 if (likely(netif_running(priv->ndev))) {
430 cpsw_intr_disable(priv);
431 cpsw_disable_irq(priv);
432 napi_schedule(&priv->napi);
437 static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
439 if (priv->host_port == 0)
440 return slave_num + 1;
445 static int cpsw_poll(struct napi_struct *napi, int budget)
447 struct cpsw_priv *priv = napi_to_priv(napi);
450 num_tx = cpdma_chan_process(priv->txch, 128);
451 num_rx = cpdma_chan_process(priv->rxch, budget);
453 if (num_rx || num_tx)
454 cpsw_dbg(priv, intr, "poll %d rx, %d tx pkts\n",
457 if (num_rx < budget) {
459 cpsw_intr_enable(priv);
460 cpdma_ctlr_eoi(priv->dma);
461 cpsw_enable_irq(priv);
467 static inline void soft_reset(const char *module, void __iomem *reg)
469 unsigned long timeout = jiffies + HZ;
471 __raw_writel(1, reg);
474 } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
476 WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
479 #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
480 ((mac)[2] << 16) | ((mac)[3] << 24))
481 #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
483 static void cpsw_set_slave_mac(struct cpsw_slave *slave,
484 struct cpsw_priv *priv)
486 slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
487 slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
490 static void _cpsw_adjust_link(struct cpsw_slave *slave,
491 struct cpsw_priv *priv, bool *link)
493 struct phy_device *phy = slave->phy;
500 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
503 mac_control = priv->data.mac_control;
505 /* enable forwarding */
506 cpsw_ale_control_set(priv->ale, slave_port,
507 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
509 if (phy->speed == 1000)
510 mac_control |= BIT(7); /* GIGABITEN */
512 mac_control |= BIT(0); /* FULLDUPLEXEN */
514 /* set speed_in input in case RMII mode is used in 100Mbps */
515 if (phy->speed == 100)
516 mac_control |= BIT(15);
521 /* disable forwarding */
522 cpsw_ale_control_set(priv->ale, slave_port,
523 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
526 if (mac_control != slave->mac_control) {
527 phy_print_status(phy);
528 __raw_writel(mac_control, &slave->sliver->mac_control);
531 slave->mac_control = mac_control;
534 static void cpsw_adjust_link(struct net_device *ndev)
536 struct cpsw_priv *priv = netdev_priv(ndev);
539 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
542 netif_carrier_on(ndev);
543 if (netif_running(ndev))
544 netif_wake_queue(ndev);
546 netif_carrier_off(ndev);
547 netif_stop_queue(ndev);
551 static inline int __show_stat(char *buf, int maxlen, const char *name, u32 val)
553 static char *leader = "........................................";
558 return snprintf(buf, maxlen, "%s %s %10d\n", name,
559 leader + strlen(name), val);
562 static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
567 sprintf(name, "slave-%d", slave->slave_num);
569 soft_reset(name, &slave->sliver->soft_reset);
571 /* setup priority mapping */
572 __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
574 switch (priv->version) {
576 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
579 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
583 /* setup max packet size, and mac address */
584 __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
585 cpsw_set_slave_mac(slave, priv);
587 slave->mac_control = 0; /* no link yet */
589 slave_port = cpsw_get_slave_port(priv, slave->slave_num);
591 cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
592 1 << slave_port, 0, ALE_MCAST_FWD_2);
594 slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
595 &cpsw_adjust_link, slave->data->phy_if);
596 if (IS_ERR(slave->phy)) {
597 dev_err(priv->dev, "phy %s not found on slave %d\n",
598 slave->data->phy_id, slave->slave_num);
601 dev_info(priv->dev, "phy found : id is : 0x%x\n",
603 phy_start(slave->phy);
607 static void cpsw_init_host_port(struct cpsw_priv *priv)
609 /* soft reset the controller and initialize ale */
610 soft_reset("cpsw", &priv->regs->soft_reset);
611 cpsw_ale_start(priv->ale);
613 /* switch to vlan unaware mode */
614 cpsw_ale_control_set(priv->ale, 0, ALE_VLAN_AWARE, 0);
616 /* setup host port priority mapping */
617 __raw_writel(CPDMA_TX_PRIORITY_MAP,
618 &priv->host_port_regs->cpdma_tx_pri_map);
619 __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
621 cpsw_ale_control_set(priv->ale, priv->host_port,
622 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
624 cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port, 0);
625 cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
626 1 << priv->host_port, 0, ALE_MCAST_FWD_2);
629 static int cpsw_ndo_open(struct net_device *ndev)
631 struct cpsw_priv *priv = netdev_priv(ndev);
635 cpsw_intr_disable(priv);
636 netif_carrier_off(ndev);
638 pm_runtime_get_sync(&priv->pdev->dev);
642 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
643 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
644 CPSW_RTL_VERSION(reg));
646 /* initialize host and slave ports */
647 cpsw_init_host_port(priv);
648 for_each_slave(priv, cpsw_slave_open, priv);
650 /* setup tx dma to fixed prio and zero offset */
651 cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
652 cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
654 /* disable priority elevation and enable statistics on all ports */
655 __raw_writel(0, &priv->regs->ptype);
657 /* enable statistics collection only on the host port */
658 __raw_writel(0x7, &priv->regs->stat_port_en);
660 if (WARN_ON(!priv->data.rx_descs))
661 priv->data.rx_descs = 128;
663 for (i = 0; i < priv->data.rx_descs; i++) {
667 skb = netdev_alloc_skb_ip_align(priv->ndev,
668 priv->rx_packet_max);
671 ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
672 skb_tailroom(skb), GFP_KERNEL);
673 if (WARN_ON(ret < 0))
676 /* continue even if we didn't manage to submit all receive descs */
677 cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
679 cpdma_ctlr_start(priv->dma);
680 cpsw_intr_enable(priv);
681 napi_enable(&priv->napi);
682 cpdma_ctlr_eoi(priv->dma);
687 static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
691 phy_stop(slave->phy);
692 phy_disconnect(slave->phy);
696 static int cpsw_ndo_stop(struct net_device *ndev)
698 struct cpsw_priv *priv = netdev_priv(ndev);
700 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
701 netif_stop_queue(priv->ndev);
702 napi_disable(&priv->napi);
703 netif_carrier_off(priv->ndev);
704 cpsw_intr_disable(priv);
705 cpdma_ctlr_int_ctrl(priv->dma, false);
706 cpdma_ctlr_stop(priv->dma);
707 cpsw_ale_stop(priv->ale);
708 for_each_slave(priv, cpsw_slave_stop, priv);
709 pm_runtime_put_sync(&priv->pdev->dev);
713 static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
714 struct net_device *ndev)
716 struct cpsw_priv *priv = netdev_priv(ndev);
719 ndev->trans_start = jiffies;
721 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
722 cpsw_err(priv, tx_err, "packet pad failed\n");
723 priv->stats.tx_dropped++;
727 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && priv->cpts.tx_enable)
728 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
730 skb_tx_timestamp(skb);
732 ret = cpdma_chan_submit(priv->txch, skb, skb->data,
733 skb->len, GFP_KERNEL);
734 if (unlikely(ret != 0)) {
735 cpsw_err(priv, tx_err, "desc submit failed\n");
741 priv->stats.tx_dropped++;
742 netif_stop_queue(ndev);
743 return NETDEV_TX_BUSY;
746 static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags)
749 * The switch cannot operate in promiscuous mode without substantial
750 * headache. For promiscuous mode to work, we would need to put the
751 * ALE in bypass mode and route all traffic to the host port.
752 * Subsequently, the host will need to operate as a "bridge", learn,
753 * and flood as needed. For now, we simply complain here and
754 * do nothing about it :-)
756 if ((flags & IFF_PROMISC) && (ndev->flags & IFF_PROMISC))
757 dev_err(&ndev->dev, "promiscuity ignored!\n");
760 * The switch cannot filter multicast traffic unless it is configured
761 * in "VLAN Aware" mode. Unfortunately, VLAN awareness requires a
762 * whole bunch of additional logic that this driver does not implement
765 if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI))
766 dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n");
769 #ifdef CONFIG_TI_CPTS
771 static void cpsw_hwtstamp_v1(struct cpsw_priv *priv)
773 struct cpsw_slave *slave = &priv->slaves[priv->data.cpts_active_slave];
776 if (!priv->cpts.tx_enable && !priv->cpts.rx_enable) {
777 slave_write(slave, 0, CPSW1_TS_CTL);
781 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
782 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
784 if (priv->cpts.tx_enable)
785 ts_en |= CPSW_V1_TS_TX_EN;
787 if (priv->cpts.rx_enable)
788 ts_en |= CPSW_V1_TS_RX_EN;
790 slave_write(slave, ts_en, CPSW1_TS_CTL);
791 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
794 static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
796 struct cpsw_slave *slave = &priv->slaves[priv->data.cpts_active_slave];
799 ctrl = slave_read(slave, CPSW2_CONTROL);
800 ctrl &= ~CTRL_ALL_TS_MASK;
802 if (priv->cpts.tx_enable)
803 ctrl |= CTRL_TX_TS_BITS;
805 if (priv->cpts.rx_enable)
806 ctrl |= CTRL_RX_TS_BITS;
808 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
810 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
811 slave_write(slave, ctrl, CPSW2_CONTROL);
812 __raw_writel(ETH_P_1588, &priv->regs->ts_ltype);
815 static int cpsw_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
817 struct cpsw_priv *priv = netdev_priv(dev);
818 struct cpts *cpts = &priv->cpts;
819 struct hwtstamp_config cfg;
821 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
824 /* reserved for future extensions */
828 switch (cfg.tx_type) {
829 case HWTSTAMP_TX_OFF:
839 switch (cfg.rx_filter) {
840 case HWTSTAMP_FILTER_NONE:
843 case HWTSTAMP_FILTER_ALL:
844 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
845 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
846 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
848 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
849 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
850 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
851 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
852 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
853 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
854 case HWTSTAMP_FILTER_PTP_V2_EVENT:
855 case HWTSTAMP_FILTER_PTP_V2_SYNC:
856 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
858 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
864 switch (priv->version) {
866 cpsw_hwtstamp_v1(priv);
869 cpsw_hwtstamp_v2(priv);
875 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
878 #endif /*CONFIG_TI_CPTS*/
880 static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
882 if (!netif_running(dev))
885 #ifdef CONFIG_TI_CPTS
886 if (cmd == SIOCSHWTSTAMP)
887 return cpsw_hwtstamp_ioctl(dev, req);
892 static void cpsw_ndo_tx_timeout(struct net_device *ndev)
894 struct cpsw_priv *priv = netdev_priv(ndev);
896 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
897 priv->stats.tx_errors++;
898 cpsw_intr_disable(priv);
899 cpdma_ctlr_int_ctrl(priv->dma, false);
900 cpdma_chan_stop(priv->txch);
901 cpdma_chan_start(priv->txch);
902 cpdma_ctlr_int_ctrl(priv->dma, true);
903 cpsw_intr_enable(priv);
904 cpdma_ctlr_eoi(priv->dma);
907 static struct net_device_stats *cpsw_ndo_get_stats(struct net_device *ndev)
909 struct cpsw_priv *priv = netdev_priv(ndev);
913 #ifdef CONFIG_NET_POLL_CONTROLLER
914 static void cpsw_ndo_poll_controller(struct net_device *ndev)
916 struct cpsw_priv *priv = netdev_priv(ndev);
918 cpsw_intr_disable(priv);
919 cpdma_ctlr_int_ctrl(priv->dma, false);
920 cpsw_interrupt(ndev->irq, priv);
921 cpdma_ctlr_int_ctrl(priv->dma, true);
922 cpsw_intr_enable(priv);
923 cpdma_ctlr_eoi(priv->dma);
927 static const struct net_device_ops cpsw_netdev_ops = {
928 .ndo_open = cpsw_ndo_open,
929 .ndo_stop = cpsw_ndo_stop,
930 .ndo_start_xmit = cpsw_ndo_start_xmit,
931 .ndo_change_rx_flags = cpsw_ndo_change_rx_flags,
932 .ndo_do_ioctl = cpsw_ndo_ioctl,
933 .ndo_validate_addr = eth_validate_addr,
934 .ndo_change_mtu = eth_change_mtu,
935 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
936 .ndo_get_stats = cpsw_ndo_get_stats,
937 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
938 #ifdef CONFIG_NET_POLL_CONTROLLER
939 .ndo_poll_controller = cpsw_ndo_poll_controller,
943 static void cpsw_get_drvinfo(struct net_device *ndev,
944 struct ethtool_drvinfo *info)
946 struct cpsw_priv *priv = netdev_priv(ndev);
948 strlcpy(info->driver, "TI CPSW Driver v1.0", sizeof(info->driver));
949 strlcpy(info->version, "1.0", sizeof(info->version));
950 strlcpy(info->bus_info, priv->pdev->name, sizeof(info->bus_info));
953 static u32 cpsw_get_msglevel(struct net_device *ndev)
955 struct cpsw_priv *priv = netdev_priv(ndev);
956 return priv->msg_enable;
959 static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
961 struct cpsw_priv *priv = netdev_priv(ndev);
962 priv->msg_enable = value;
965 static int cpsw_get_ts_info(struct net_device *ndev,
966 struct ethtool_ts_info *info)
968 #ifdef CONFIG_TI_CPTS
969 struct cpsw_priv *priv = netdev_priv(ndev);
971 info->so_timestamping =
972 SOF_TIMESTAMPING_TX_HARDWARE |
973 SOF_TIMESTAMPING_TX_SOFTWARE |
974 SOF_TIMESTAMPING_RX_HARDWARE |
975 SOF_TIMESTAMPING_RX_SOFTWARE |
976 SOF_TIMESTAMPING_SOFTWARE |
977 SOF_TIMESTAMPING_RAW_HARDWARE;
978 info->phc_index = priv->cpts.phc_index;
980 (1 << HWTSTAMP_TX_OFF) |
981 (1 << HWTSTAMP_TX_ON);
983 (1 << HWTSTAMP_FILTER_NONE) |
984 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
986 info->so_timestamping =
987 SOF_TIMESTAMPING_TX_SOFTWARE |
988 SOF_TIMESTAMPING_RX_SOFTWARE |
989 SOF_TIMESTAMPING_SOFTWARE;
990 info->phc_index = -1;
992 info->rx_filters = 0;
997 static const struct ethtool_ops cpsw_ethtool_ops = {
998 .get_drvinfo = cpsw_get_drvinfo,
999 .get_msglevel = cpsw_get_msglevel,
1000 .set_msglevel = cpsw_set_msglevel,
1001 .get_link = ethtool_op_get_link,
1002 .get_ts_info = cpsw_get_ts_info,
1005 static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv,
1006 u32 slave_reg_ofs, u32 sliver_reg_ofs)
1008 void __iomem *regs = priv->regs;
1009 int slave_num = slave->slave_num;
1010 struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
1013 slave->regs = regs + slave_reg_ofs;
1014 slave->sliver = regs + sliver_reg_ofs;
1017 static int cpsw_probe_dt(struct cpsw_platform_data *data,
1018 struct platform_device *pdev)
1020 struct device_node *node = pdev->dev.of_node;
1021 struct device_node *slave_node;
1028 if (of_property_read_u32(node, "slaves", &prop)) {
1029 pr_err("Missing slaves property in the DT.\n");
1032 data->slaves = prop;
1034 if (of_property_read_u32(node, "cpts_active_slave", &prop)) {
1035 pr_err("Missing cpts_active_slave property in the DT.\n");
1039 data->cpts_active_slave = prop;
1041 if (of_property_read_u32(node, "cpts_clock_mult", &prop)) {
1042 pr_err("Missing cpts_clock_mult property in the DT.\n");
1046 data->cpts_clock_mult = prop;
1048 if (of_property_read_u32(node, "cpts_clock_shift", &prop)) {
1049 pr_err("Missing cpts_clock_shift property in the DT.\n");
1053 data->cpts_clock_shift = prop;
1055 data->slave_data = kzalloc(sizeof(struct cpsw_slave_data) *
1056 data->slaves, GFP_KERNEL);
1057 if (!data->slave_data) {
1058 pr_err("Could not allocate slave memory.\n");
1062 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
1063 pr_err("Missing cpdma_channels property in the DT.\n");
1067 data->channels = prop;
1069 if (of_property_read_u32(node, "ale_entries", &prop)) {
1070 pr_err("Missing ale_entries property in the DT.\n");
1074 data->ale_entries = prop;
1076 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
1077 pr_err("Missing bd_ram_size property in the DT.\n");
1081 data->bd_ram_size = prop;
1083 if (of_property_read_u32(node, "rx_descs", &prop)) {
1084 pr_err("Missing rx_descs property in the DT.\n");
1088 data->rx_descs = prop;
1090 if (of_property_read_u32(node, "mac_control", &prop)) {
1091 pr_err("Missing mac_control property in the DT.\n");
1095 data->mac_control = prop;
1098 * Populate all the child nodes here...
1100 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
1101 /* We do not want to force this, as in some cases may not have child */
1103 pr_warn("Doesn't have any child node\n");
1105 for_each_node_by_name(slave_node, "slave") {
1106 struct cpsw_slave_data *slave_data = data->slave_data + i;
1107 const void *mac_addr = NULL;
1111 struct device_node *mdio_node;
1112 struct platform_device *mdio;
1114 parp = of_get_property(slave_node, "phy_id", &lenp);
1115 if ((parp == NULL) && (lenp != (sizeof(void *) * 2))) {
1116 pr_err("Missing slave[%d] phy_id property\n", i);
1120 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
1121 phyid = be32_to_cpup(parp+1);
1122 mdio = of_find_device_by_node(mdio_node);
1123 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
1124 PHY_ID_FMT, mdio->name, phyid);
1126 mac_addr = of_get_mac_address(slave_node);
1128 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
1136 kfree(data->slave_data);
1140 static int cpsw_probe(struct platform_device *pdev)
1142 struct cpsw_platform_data *data = pdev->dev.platform_data;
1143 struct net_device *ndev;
1144 struct cpsw_priv *priv;
1145 struct cpdma_params dma_params;
1146 struct cpsw_ale_params ale_params;
1147 void __iomem *ss_regs, *wr_regs;
1148 struct resource *res;
1149 u32 slave_offset, sliver_offset, slave_size;
1150 int ret = 0, i, k = 0;
1152 ndev = alloc_etherdev(sizeof(struct cpsw_priv));
1154 pr_err("error allocating net_device\n");
1158 platform_set_drvdata(pdev, ndev);
1159 priv = netdev_priv(ndev);
1160 spin_lock_init(&priv->lock);
1163 priv->dev = &ndev->dev;
1164 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
1165 priv->rx_packet_max = max(rx_packet_max, 128);
1168 * This may be required here for child devices.
1170 pm_runtime_enable(&pdev->dev);
1172 if (cpsw_probe_dt(&priv->data, pdev)) {
1173 pr_err("cpsw: platform data missing\n");
1175 goto clean_ndev_ret;
1179 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
1180 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
1181 pr_info("Detected MACID = %pM", priv->mac_addr);
1183 eth_random_addr(priv->mac_addr);
1184 pr_info("Random MACID = %pM", priv->mac_addr);
1187 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
1189 priv->slaves = kzalloc(sizeof(struct cpsw_slave) * data->slaves,
1191 if (!priv->slaves) {
1193 goto clean_ndev_ret;
1195 for (i = 0; i < data->slaves; i++)
1196 priv->slaves[i].slave_num = i;
1198 priv->clk = clk_get(&pdev->dev, "fck");
1199 if (IS_ERR(priv->clk)) {
1200 dev_err(&pdev->dev, "fck is not found\n");
1202 goto clean_slave_ret;
1205 priv->cpsw_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1206 if (!priv->cpsw_res) {
1207 dev_err(priv->dev, "error getting i/o resource\n");
1211 if (!request_mem_region(priv->cpsw_res->start,
1212 resource_size(priv->cpsw_res), ndev->name)) {
1213 dev_err(priv->dev, "failed request i/o region\n");
1217 ss_regs = ioremap(priv->cpsw_res->start, resource_size(priv->cpsw_res));
1219 dev_err(priv->dev, "unable to map i/o region\n");
1220 goto clean_cpsw_iores_ret;
1222 priv->regs = ss_regs;
1223 priv->version = __raw_readl(&priv->regs->id_ver);
1224 priv->host_port = HOST_PORT_NUM;
1226 priv->cpsw_wr_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1227 if (!priv->cpsw_wr_res) {
1228 dev_err(priv->dev, "error getting i/o resource\n");
1230 goto clean_iomap_ret;
1232 if (!request_mem_region(priv->cpsw_wr_res->start,
1233 resource_size(priv->cpsw_wr_res), ndev->name)) {
1234 dev_err(priv->dev, "failed request i/o region\n");
1236 goto clean_iomap_ret;
1238 wr_regs = ioremap(priv->cpsw_wr_res->start,
1239 resource_size(priv->cpsw_wr_res));
1241 dev_err(priv->dev, "unable to map i/o region\n");
1242 goto clean_cpsw_wr_iores_ret;
1244 priv->wr_regs = wr_regs;
1246 memset(&dma_params, 0, sizeof(dma_params));
1247 memset(&ale_params, 0, sizeof(ale_params));
1249 switch (priv->version) {
1250 case CPSW_VERSION_1:
1251 priv->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
1252 priv->cpts.reg = ss_regs + CPSW1_CPTS_OFFSET;
1253 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET;
1254 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET;
1255 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET;
1256 slave_offset = CPSW1_SLAVE_OFFSET;
1257 slave_size = CPSW1_SLAVE_SIZE;
1258 sliver_offset = CPSW1_SLIVER_OFFSET;
1259 dma_params.desc_mem_phys = 0;
1261 case CPSW_VERSION_2:
1262 priv->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
1263 priv->cpts.reg = ss_regs + CPSW2_CPTS_OFFSET;
1264 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET;
1265 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET;
1266 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET;
1267 slave_offset = CPSW2_SLAVE_OFFSET;
1268 slave_size = CPSW2_SLAVE_SIZE;
1269 sliver_offset = CPSW2_SLIVER_OFFSET;
1270 dma_params.desc_mem_phys =
1271 (u32 __force) priv->cpsw_res->start + CPSW2_BD_OFFSET;
1274 dev_err(priv->dev, "unknown version 0x%08x\n", priv->version);
1276 goto clean_cpsw_wr_iores_ret;
1278 for (i = 0; i < priv->data.slaves; i++) {
1279 struct cpsw_slave *slave = &priv->slaves[i];
1280 cpsw_slave_init(slave, priv, slave_offset, sliver_offset);
1281 slave_offset += slave_size;
1282 sliver_offset += SLIVER_SIZE;
1285 dma_params.dev = &pdev->dev;
1286 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH;
1287 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE;
1288 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP;
1289 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP;
1290 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP;
1292 dma_params.num_chan = data->channels;
1293 dma_params.has_soft_reset = true;
1294 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
1295 dma_params.desc_mem_size = data->bd_ram_size;
1296 dma_params.desc_align = 16;
1297 dma_params.has_ext_regs = true;
1298 dma_params.desc_hw_addr = dma_params.desc_mem_phys;
1300 priv->dma = cpdma_ctlr_create(&dma_params);
1302 dev_err(priv->dev, "error initializing dma\n");
1304 goto clean_wr_iomap_ret;
1307 priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
1309 priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
1312 if (WARN_ON(!priv->txch || !priv->rxch)) {
1313 dev_err(priv->dev, "error initializing dma channels\n");
1318 ale_params.dev = &ndev->dev;
1319 ale_params.ale_ageout = ale_ageout;
1320 ale_params.ale_entries = data->ale_entries;
1321 ale_params.ale_ports = data->slaves;
1323 priv->ale = cpsw_ale_create(&ale_params);
1325 dev_err(priv->dev, "error initializing ale engine\n");
1330 ndev->irq = platform_get_irq(pdev, 0);
1331 if (ndev->irq < 0) {
1332 dev_err(priv->dev, "error getting irq resource\n");
1337 while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
1338 for (i = res->start; i <= res->end; i++) {
1339 if (request_irq(i, cpsw_interrupt, IRQF_DISABLED,
1340 dev_name(&pdev->dev), priv)) {
1341 dev_err(priv->dev, "error attaching irq\n");
1344 priv->irqs_table[k] = i;
1350 ndev->flags |= IFF_ALLMULTI; /* see cpsw_ndo_change_rx_flags() */
1352 ndev->netdev_ops = &cpsw_netdev_ops;
1353 SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
1354 netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT);
1356 /* register the network device */
1357 SET_NETDEV_DEV(ndev, &pdev->dev);
1358 ret = register_netdev(ndev);
1360 dev_err(priv->dev, "error registering net device\n");
1365 if (cpts_register(&pdev->dev, &priv->cpts,
1366 data->cpts_clock_mult, data->cpts_clock_shift))
1367 dev_err(priv->dev, "error registering cpts device\n");
1369 cpsw_notice(priv, probe, "initialized device (regs %x, irq %d)\n",
1370 priv->cpsw_res->start, ndev->irq);
1375 free_irq(ndev->irq, priv);
1377 cpsw_ale_destroy(priv->ale);
1379 cpdma_chan_destroy(priv->txch);
1380 cpdma_chan_destroy(priv->rxch);
1381 cpdma_ctlr_destroy(priv->dma);
1383 iounmap(priv->wr_regs);
1384 clean_cpsw_wr_iores_ret:
1385 release_mem_region(priv->cpsw_wr_res->start,
1386 resource_size(priv->cpsw_wr_res));
1388 iounmap(priv->regs);
1389 clean_cpsw_iores_ret:
1390 release_mem_region(priv->cpsw_res->start,
1391 resource_size(priv->cpsw_res));
1395 pm_runtime_disable(&pdev->dev);
1396 kfree(priv->slaves);
1402 static int cpsw_remove(struct platform_device *pdev)
1404 struct net_device *ndev = platform_get_drvdata(pdev);
1405 struct cpsw_priv *priv = netdev_priv(ndev);
1407 pr_info("removing device");
1408 platform_set_drvdata(pdev, NULL);
1410 cpts_unregister(&priv->cpts);
1411 free_irq(ndev->irq, priv);
1412 cpsw_ale_destroy(priv->ale);
1413 cpdma_chan_destroy(priv->txch);
1414 cpdma_chan_destroy(priv->rxch);
1415 cpdma_ctlr_destroy(priv->dma);
1416 iounmap(priv->regs);
1417 release_mem_region(priv->cpsw_res->start,
1418 resource_size(priv->cpsw_res));
1419 iounmap(priv->wr_regs);
1420 release_mem_region(priv->cpsw_wr_res->start,
1421 resource_size(priv->cpsw_wr_res));
1422 pm_runtime_disable(&pdev->dev);
1424 kfree(priv->slaves);
1430 static int cpsw_suspend(struct device *dev)
1432 struct platform_device *pdev = to_platform_device(dev);
1433 struct net_device *ndev = platform_get_drvdata(pdev);
1435 if (netif_running(ndev))
1436 cpsw_ndo_stop(ndev);
1437 pm_runtime_put_sync(&pdev->dev);
1442 static int cpsw_resume(struct device *dev)
1444 struct platform_device *pdev = to_platform_device(dev);
1445 struct net_device *ndev = platform_get_drvdata(pdev);
1447 pm_runtime_get_sync(&pdev->dev);
1448 if (netif_running(ndev))
1449 cpsw_ndo_open(ndev);
1453 static const struct dev_pm_ops cpsw_pm_ops = {
1454 .suspend = cpsw_suspend,
1455 .resume = cpsw_resume,
1458 static const struct of_device_id cpsw_of_mtable[] = {
1459 { .compatible = "ti,cpsw", },
1463 static struct platform_driver cpsw_driver = {
1466 .owner = THIS_MODULE,
1468 .of_match_table = of_match_ptr(cpsw_of_mtable),
1470 .probe = cpsw_probe,
1471 .remove = cpsw_remove,
1474 static int __init cpsw_init(void)
1476 return platform_driver_register(&cpsw_driver);
1478 late_initcall(cpsw_init);
1480 static void __exit cpsw_exit(void)
1482 platform_driver_unregister(&cpsw_driver);
1484 module_exit(cpsw_exit);
1486 MODULE_LICENSE("GPL");
1487 MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
1488 MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
1489 MODULE_DESCRIPTION("TI CPSW Ethernet driver");