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>
35 #include <linux/if_vlan.h>
37 #include <linux/platform_data/cpsw.h>
41 #include "davinci_cpdma.h"
43 #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
44 NETIF_MSG_DRV | NETIF_MSG_LINK | \
45 NETIF_MSG_IFUP | NETIF_MSG_INTR | \
46 NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
47 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
48 NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
49 NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
52 #define cpsw_info(priv, type, format, ...) \
54 if (netif_msg_##type(priv) && net_ratelimit()) \
55 dev_info(priv->dev, format, ## __VA_ARGS__); \
58 #define cpsw_err(priv, type, format, ...) \
60 if (netif_msg_##type(priv) && net_ratelimit()) \
61 dev_err(priv->dev, format, ## __VA_ARGS__); \
64 #define cpsw_dbg(priv, type, format, ...) \
66 if (netif_msg_##type(priv) && net_ratelimit()) \
67 dev_dbg(priv->dev, format, ## __VA_ARGS__); \
70 #define cpsw_notice(priv, type, format, ...) \
72 if (netif_msg_##type(priv) && net_ratelimit()) \
73 dev_notice(priv->dev, format, ## __VA_ARGS__); \
76 #define ALE_ALL_PORTS 0x7
78 #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
79 #define CPSW_MINOR_VERSION(reg) (reg & 0xff)
80 #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
82 #define CPSW_VERSION_1 0x19010a
83 #define CPSW_VERSION_2 0x19010c
85 #define HOST_PORT_NUM 0
86 #define SLIVER_SIZE 0x40
88 #define CPSW1_HOST_PORT_OFFSET 0x028
89 #define CPSW1_SLAVE_OFFSET 0x050
90 #define CPSW1_SLAVE_SIZE 0x040
91 #define CPSW1_CPDMA_OFFSET 0x100
92 #define CPSW1_STATERAM_OFFSET 0x200
93 #define CPSW1_CPTS_OFFSET 0x500
94 #define CPSW1_ALE_OFFSET 0x600
95 #define CPSW1_SLIVER_OFFSET 0x700
97 #define CPSW2_HOST_PORT_OFFSET 0x108
98 #define CPSW2_SLAVE_OFFSET 0x200
99 #define CPSW2_SLAVE_SIZE 0x100
100 #define CPSW2_CPDMA_OFFSET 0x800
101 #define CPSW2_STATERAM_OFFSET 0xa00
102 #define CPSW2_CPTS_OFFSET 0xc00
103 #define CPSW2_ALE_OFFSET 0xd00
104 #define CPSW2_SLIVER_OFFSET 0xd80
105 #define CPSW2_BD_OFFSET 0x2000
107 #define CPDMA_RXTHRESH 0x0c0
108 #define CPDMA_RXFREE 0x0e0
109 #define CPDMA_TXHDP 0x00
110 #define CPDMA_RXHDP 0x20
111 #define CPDMA_TXCP 0x40
112 #define CPDMA_RXCP 0x60
114 #define CPSW_POLL_WEIGHT 64
115 #define CPSW_MIN_PACKET_SIZE 60
116 #define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
118 #define RX_PRIORITY_MAPPING 0x76543210
119 #define TX_PRIORITY_MAPPING 0x33221100
120 #define CPDMA_TX_PRIORITY_MAP 0x76543210
122 #define CPSW_VLAN_AWARE BIT(1)
123 #define CPSW_ALE_VLAN_AWARE 1
125 #define CPSW_FIFO_NORMAL_MODE (0 << 15)
126 #define CPSW_FIFO_DUAL_MAC_MODE (1 << 15)
127 #define CPSW_FIFO_RATE_LIMIT_MODE (2 << 15)
129 #define cpsw_enable_irq(priv) \
132 for (i = 0; i < priv->num_irqs; i++) \
133 enable_irq(priv->irqs_table[i]); \
135 #define cpsw_disable_irq(priv) \
138 for (i = 0; i < priv->num_irqs; i++) \
139 disable_irq_nosync(priv->irqs_table[i]); \
142 static int debug_level
;
143 module_param(debug_level
, int, 0);
144 MODULE_PARM_DESC(debug_level
, "cpsw debug level (NETIF_MSG bits)");
146 static int ale_ageout
= 10;
147 module_param(ale_ageout
, int, 0);
148 MODULE_PARM_DESC(ale_ageout
, "cpsw ale ageout interval (seconds)");
150 static int rx_packet_max
= CPSW_MAX_PACKET_SIZE
;
151 module_param(rx_packet_max
, int, 0);
152 MODULE_PARM_DESC(rx_packet_max
, "maximum receive packet size (bytes)");
154 struct cpsw_wr_regs
{
165 struct cpsw_ss_regs
{
182 #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
183 #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
184 #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
185 #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
186 #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
187 #define CPSW1_TS_CTL 0x14 /* Time Sync Control */
188 #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
189 #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
192 #define CPSW2_CONTROL 0x00 /* Control Register */
193 #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
194 #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
195 #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
196 #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
197 #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
198 #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
200 /* CPSW_PORT_V1 and V2 */
201 #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
202 #define SA_HI 0x24 /* CPGMAC_SL Source Address High */
203 #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
205 /* CPSW_PORT_V2 only */
206 #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
207 #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
208 #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
209 #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
210 #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
211 #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
212 #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
213 #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
215 /* Bit definitions for the CPSW2_CONTROL register */
216 #define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */
217 #define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */
218 #define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */
219 #define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */
220 #define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */
221 #define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */
222 #define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */
223 #define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */
224 #define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */
225 #define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */
226 #define TS_BIT8 (1<<8) /* ts_ttl_nonzero? */
227 #define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */
228 #define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */
229 #define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */
230 #define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */
231 #define TS_RX_EN (1<<0) /* Time Sync Receive Enable */
233 #define CTRL_TS_BITS \
234 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 | TS_BIT8 | \
235 TS_ANNEX_D_EN | TS_LTYPE1_EN)
237 #define CTRL_ALL_TS_MASK (CTRL_TS_BITS | TS_TX_EN | TS_RX_EN)
238 #define CTRL_TX_TS_BITS (CTRL_TS_BITS | TS_TX_EN)
239 #define CTRL_RX_TS_BITS (CTRL_TS_BITS | TS_RX_EN)
241 /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
242 #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
243 #define TS_SEQ_ID_OFFSET_MASK (0x3f)
244 #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
245 #define TS_MSG_TYPE_EN_MASK (0xffff)
247 /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
248 #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
250 /* Bit definitions for the CPSW1_TS_CTL register */
251 #define CPSW_V1_TS_RX_EN BIT(0)
252 #define CPSW_V1_TS_TX_EN BIT(4)
253 #define CPSW_V1_MSG_TYPE_OFS 16
255 /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
256 #define CPSW_V1_SEQ_ID_OFS_SHIFT 16
258 struct cpsw_host_regs
{
264 u32 cpdma_tx_pri_map
;
265 u32 cpdma_rx_chan_map
;
268 struct cpsw_sliver_regs
{
283 struct cpsw_sliver_regs __iomem
*sliver
;
286 struct cpsw_slave_data
*data
;
287 struct phy_device
*phy
;
288 struct net_device
*ndev
;
293 static inline u32
slave_read(struct cpsw_slave
*slave
, u32 offset
)
295 return __raw_readl(slave
->regs
+ offset
);
298 static inline void slave_write(struct cpsw_slave
*slave
, u32 val
, u32 offset
)
300 __raw_writel(val
, slave
->regs
+ offset
);
305 struct platform_device
*pdev
;
306 struct net_device
*ndev
;
307 struct resource
*cpsw_res
;
308 struct resource
*cpsw_wr_res
;
309 struct napi_struct napi
;
311 struct cpsw_platform_data data
;
312 struct cpsw_ss_regs __iomem
*regs
;
313 struct cpsw_wr_regs __iomem
*wr_regs
;
314 struct cpsw_host_regs __iomem
*host_port_regs
;
317 struct net_device_stats stats
;
321 u8 mac_addr
[ETH_ALEN
];
322 struct cpsw_slave
*slaves
;
323 struct cpdma_ctlr
*dma
;
324 struct cpdma_chan
*txch
, *rxch
;
325 struct cpsw_ale
*ale
;
326 /* snapshot of IRQ numbers */
333 #define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi)
334 #define for_each_slave(priv, func, arg...) \
337 if (priv->data.dual_emac) \
338 (func)((priv)->slaves + priv->emac_port, ##arg);\
340 for (idx = 0; idx < (priv)->data.slaves; idx++) \
341 (func)((priv)->slaves + idx, ##arg); \
343 #define cpsw_get_slave_ndev(priv, __slave_no__) \
344 (priv->slaves[__slave_no__].ndev)
345 #define cpsw_get_slave_priv(priv, __slave_no__) \
346 ((priv->slaves[__slave_no__].ndev) ? \
347 netdev_priv(priv->slaves[__slave_no__].ndev) : NULL) \
349 #define cpsw_dual_emac_src_port_detect(status, priv, ndev, skb) \
351 if (!priv->data.dual_emac) \
353 if (CPDMA_RX_SOURCE_PORT(status) == 1) { \
354 ndev = cpsw_get_slave_ndev(priv, 0); \
355 priv = netdev_priv(ndev); \
357 } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \
358 ndev = cpsw_get_slave_ndev(priv, 1); \
359 priv = netdev_priv(ndev); \
363 #define cpsw_add_mcast(priv, addr) \
365 if (priv->data.dual_emac) { \
366 struct cpsw_slave *slave = priv->slaves + \
368 int slave_port = cpsw_get_slave_port(priv, \
370 cpsw_ale_add_mcast(priv->ale, addr, \
371 1 << slave_port | 1 << priv->host_port, \
372 ALE_VLAN, slave->port_vlan, 0); \
374 cpsw_ale_add_mcast(priv->ale, addr, \
375 ALE_ALL_PORTS << priv->host_port, \
380 static inline int cpsw_get_slave_port(struct cpsw_priv
*priv
, u32 slave_num
)
382 if (priv
->host_port
== 0)
383 return slave_num
+ 1;
388 static void cpsw_ndo_set_rx_mode(struct net_device
*ndev
)
390 struct cpsw_priv
*priv
= netdev_priv(ndev
);
392 if (ndev
->flags
& IFF_PROMISC
) {
393 /* Enable promiscuous mode */
394 dev_err(priv
->dev
, "Ignoring Promiscuous mode\n");
398 /* Clear all mcast from ALE */
399 cpsw_ale_flush_multicast(priv
->ale
, ALE_ALL_PORTS
<< priv
->host_port
);
401 if (!netdev_mc_empty(ndev
)) {
402 struct netdev_hw_addr
*ha
;
404 /* program multicast address list into ALE register */
405 netdev_for_each_mc_addr(ha
, ndev
) {
406 cpsw_add_mcast(priv
, (u8
*)ha
->addr
);
411 static void cpsw_intr_enable(struct cpsw_priv
*priv
)
413 __raw_writel(0xFF, &priv
->wr_regs
->tx_en
);
414 __raw_writel(0xFF, &priv
->wr_regs
->rx_en
);
416 cpdma_ctlr_int_ctrl(priv
->dma
, true);
420 static void cpsw_intr_disable(struct cpsw_priv
*priv
)
422 __raw_writel(0, &priv
->wr_regs
->tx_en
);
423 __raw_writel(0, &priv
->wr_regs
->rx_en
);
425 cpdma_ctlr_int_ctrl(priv
->dma
, false);
429 void cpsw_tx_handler(void *token
, int len
, int status
)
431 struct sk_buff
*skb
= token
;
432 struct net_device
*ndev
= skb
->dev
;
433 struct cpsw_priv
*priv
= netdev_priv(ndev
);
435 /* Check whether the queue is stopped due to stalled tx dma, if the
436 * queue is stopped then start the queue as we have free desc for tx
438 if (unlikely(netif_queue_stopped(ndev
)))
439 netif_start_queue(ndev
);
440 cpts_tx_timestamp(priv
->cpts
, skb
);
441 priv
->stats
.tx_packets
++;
442 priv
->stats
.tx_bytes
+= len
;
443 dev_kfree_skb_any(skb
);
446 void cpsw_rx_handler(void *token
, int len
, int status
)
448 struct sk_buff
*skb
= token
;
449 struct net_device
*ndev
= skb
->dev
;
450 struct cpsw_priv
*priv
= netdev_priv(ndev
);
453 cpsw_dual_emac_src_port_detect(status
, priv
, ndev
, skb
);
455 /* free and bail if we are shutting down */
456 if (unlikely(!netif_running(ndev
)) ||
457 unlikely(!netif_carrier_ok(ndev
))) {
458 dev_kfree_skb_any(skb
);
461 if (likely(status
>= 0)) {
463 cpts_rx_timestamp(priv
->cpts
, skb
);
464 skb
->protocol
= eth_type_trans(skb
, ndev
);
465 netif_receive_skb(skb
);
466 priv
->stats
.rx_bytes
+= len
;
467 priv
->stats
.rx_packets
++;
471 if (unlikely(!netif_running(ndev
))) {
473 dev_kfree_skb_any(skb
);
478 skb
= netdev_alloc_skb_ip_align(ndev
, priv
->rx_packet_max
);
482 ret
= cpdma_chan_submit(priv
->rxch
, skb
, skb
->data
,
483 skb_tailroom(skb
), 0, GFP_KERNEL
);
488 static irqreturn_t
cpsw_interrupt(int irq
, void *dev_id
)
490 struct cpsw_priv
*priv
= dev_id
;
492 if (likely(netif_running(priv
->ndev
))) {
493 cpsw_intr_disable(priv
);
494 cpsw_disable_irq(priv
);
495 napi_schedule(&priv
->napi
);
497 priv
= cpsw_get_slave_priv(priv
, 1);
498 if (likely(priv
) && likely(netif_running(priv
->ndev
))) {
499 cpsw_intr_disable(priv
);
500 cpsw_disable_irq(priv
);
501 napi_schedule(&priv
->napi
);
507 static int cpsw_poll(struct napi_struct
*napi
, int budget
)
509 struct cpsw_priv
*priv
= napi_to_priv(napi
);
512 num_tx
= cpdma_chan_process(priv
->txch
, 128);
514 cpdma_ctlr_eoi(priv
->dma
, CPDMA_EOI_TX
);
516 num_rx
= cpdma_chan_process(priv
->rxch
, budget
);
517 if (num_rx
< budget
) {
519 cpsw_intr_enable(priv
);
520 cpdma_ctlr_eoi(priv
->dma
, CPDMA_EOI_RX
);
521 cpsw_enable_irq(priv
);
524 if (num_rx
|| num_tx
)
525 cpsw_dbg(priv
, intr
, "poll %d rx, %d tx pkts\n",
531 static inline void soft_reset(const char *module
, void __iomem
*reg
)
533 unsigned long timeout
= jiffies
+ HZ
;
535 __raw_writel(1, reg
);
538 } while ((__raw_readl(reg
) & 1) && time_after(timeout
, jiffies
));
540 WARN(__raw_readl(reg
) & 1, "failed to soft-reset %s\n", module
);
543 #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
544 ((mac)[2] << 16) | ((mac)[3] << 24))
545 #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
547 static void cpsw_set_slave_mac(struct cpsw_slave
*slave
,
548 struct cpsw_priv
*priv
)
550 slave_write(slave
, mac_hi(priv
->mac_addr
), SA_HI
);
551 slave_write(slave
, mac_lo(priv
->mac_addr
), SA_LO
);
554 static void _cpsw_adjust_link(struct cpsw_slave
*slave
,
555 struct cpsw_priv
*priv
, bool *link
)
557 struct phy_device
*phy
= slave
->phy
;
564 slave_port
= cpsw_get_slave_port(priv
, slave
->slave_num
);
567 mac_control
= priv
->data
.mac_control
;
569 /* enable forwarding */
570 cpsw_ale_control_set(priv
->ale
, slave_port
,
571 ALE_PORT_STATE
, ALE_PORT_STATE_FORWARD
);
573 if (phy
->speed
== 1000)
574 mac_control
|= BIT(7); /* GIGABITEN */
576 mac_control
|= BIT(0); /* FULLDUPLEXEN */
578 /* set speed_in input in case RMII mode is used in 100Mbps */
579 if (phy
->speed
== 100)
580 mac_control
|= BIT(15);
585 /* disable forwarding */
586 cpsw_ale_control_set(priv
->ale
, slave_port
,
587 ALE_PORT_STATE
, ALE_PORT_STATE_DISABLE
);
590 if (mac_control
!= slave
->mac_control
) {
591 phy_print_status(phy
);
592 __raw_writel(mac_control
, &slave
->sliver
->mac_control
);
595 slave
->mac_control
= mac_control
;
598 static void cpsw_adjust_link(struct net_device
*ndev
)
600 struct cpsw_priv
*priv
= netdev_priv(ndev
);
603 for_each_slave(priv
, _cpsw_adjust_link
, priv
, &link
);
606 netif_carrier_on(ndev
);
607 if (netif_running(ndev
))
608 netif_wake_queue(ndev
);
610 netif_carrier_off(ndev
);
611 netif_stop_queue(ndev
);
615 static inline int __show_stat(char *buf
, int maxlen
, const char *name
, u32 val
)
617 static char *leader
= "........................................";
622 return snprintf(buf
, maxlen
, "%s %s %10d\n", name
,
623 leader
+ strlen(name
), val
);
626 static int cpsw_common_res_usage_state(struct cpsw_priv
*priv
)
631 if (!priv
->data
.dual_emac
)
634 for (i
= 0; i
< priv
->data
.slaves
; i
++)
635 if (priv
->slaves
[i
].open_stat
)
641 static inline int cpsw_tx_packet_submit(struct net_device
*ndev
,
642 struct cpsw_priv
*priv
, struct sk_buff
*skb
)
644 if (!priv
->data
.dual_emac
)
645 return cpdma_chan_submit(priv
->txch
, skb
, skb
->data
,
646 skb
->len
, 0, GFP_KERNEL
);
648 if (ndev
== cpsw_get_slave_ndev(priv
, 0))
649 return cpdma_chan_submit(priv
->txch
, skb
, skb
->data
,
650 skb
->len
, 1, GFP_KERNEL
);
652 return cpdma_chan_submit(priv
->txch
, skb
, skb
->data
,
653 skb
->len
, 2, GFP_KERNEL
);
656 static inline void cpsw_add_dual_emac_def_ale_entries(
657 struct cpsw_priv
*priv
, struct cpsw_slave
*slave
,
660 u32 port_mask
= 1 << slave_port
| 1 << priv
->host_port
;
662 if (priv
->version
== CPSW_VERSION_1
)
663 slave_write(slave
, slave
->port_vlan
, CPSW1_PORT_VLAN
);
665 slave_write(slave
, slave
->port_vlan
, CPSW2_PORT_VLAN
);
666 cpsw_ale_add_vlan(priv
->ale
, slave
->port_vlan
, port_mask
,
667 port_mask
, port_mask
, 0);
668 cpsw_ale_add_mcast(priv
->ale
, priv
->ndev
->broadcast
,
669 port_mask
, ALE_VLAN
, slave
->port_vlan
, 0);
670 cpsw_ale_add_ucast(priv
->ale
, priv
->mac_addr
,
671 priv
->host_port
, ALE_VLAN
, slave
->port_vlan
);
674 static void cpsw_slave_open(struct cpsw_slave
*slave
, struct cpsw_priv
*priv
)
679 sprintf(name
, "slave-%d", slave
->slave_num
);
681 soft_reset(name
, &slave
->sliver
->soft_reset
);
683 /* setup priority mapping */
684 __raw_writel(RX_PRIORITY_MAPPING
, &slave
->sliver
->rx_pri_map
);
686 switch (priv
->version
) {
688 slave_write(slave
, TX_PRIORITY_MAPPING
, CPSW1_TX_PRI_MAP
);
691 slave_write(slave
, TX_PRIORITY_MAPPING
, CPSW2_TX_PRI_MAP
);
695 /* setup max packet size, and mac address */
696 __raw_writel(priv
->rx_packet_max
, &slave
->sliver
->rx_maxlen
);
697 cpsw_set_slave_mac(slave
, priv
);
699 slave
->mac_control
= 0; /* no link yet */
701 slave_port
= cpsw_get_slave_port(priv
, slave
->slave_num
);
703 if (priv
->data
.dual_emac
)
704 cpsw_add_dual_emac_def_ale_entries(priv
, slave
, slave_port
);
706 cpsw_ale_add_mcast(priv
->ale
, priv
->ndev
->broadcast
,
707 1 << slave_port
, 0, 0, ALE_MCAST_FWD_2
);
709 slave
->phy
= phy_connect(priv
->ndev
, slave
->data
->phy_id
,
710 &cpsw_adjust_link
, slave
->data
->phy_if
);
711 if (IS_ERR(slave
->phy
)) {
712 dev_err(priv
->dev
, "phy %s not found on slave %d\n",
713 slave
->data
->phy_id
, slave
->slave_num
);
716 dev_info(priv
->dev
, "phy found : id is : 0x%x\n",
718 phy_start(slave
->phy
);
722 static inline void cpsw_add_default_vlan(struct cpsw_priv
*priv
)
724 const int vlan
= priv
->data
.default_vlan
;
725 const int port
= priv
->host_port
;
729 reg
= (priv
->version
== CPSW_VERSION_1
) ? CPSW1_PORT_VLAN
:
732 writel(vlan
, &priv
->host_port_regs
->port_vlan
);
734 for (i
= 0; i
< priv
->data
.slaves
; i
++)
735 slave_write(priv
->slaves
+ i
, vlan
, reg
);
737 cpsw_ale_add_vlan(priv
->ale
, vlan
, ALE_ALL_PORTS
<< port
,
738 ALE_ALL_PORTS
<< port
, ALE_ALL_PORTS
<< port
,
739 (ALE_PORT_1
| ALE_PORT_2
) << port
);
742 static void cpsw_init_host_port(struct cpsw_priv
*priv
)
747 /* soft reset the controller and initialize ale */
748 soft_reset("cpsw", &priv
->regs
->soft_reset
);
749 cpsw_ale_start(priv
->ale
);
751 /* switch to vlan unaware mode */
752 cpsw_ale_control_set(priv
->ale
, priv
->host_port
, ALE_VLAN_AWARE
,
753 CPSW_ALE_VLAN_AWARE
);
754 control_reg
= readl(&priv
->regs
->control
);
755 control_reg
|= CPSW_VLAN_AWARE
;
756 writel(control_reg
, &priv
->regs
->control
);
757 fifo_mode
= (priv
->data
.dual_emac
) ? CPSW_FIFO_DUAL_MAC_MODE
:
758 CPSW_FIFO_NORMAL_MODE
;
759 writel(fifo_mode
, &priv
->host_port_regs
->tx_in_ctl
);
761 /* setup host port priority mapping */
762 __raw_writel(CPDMA_TX_PRIORITY_MAP
,
763 &priv
->host_port_regs
->cpdma_tx_pri_map
);
764 __raw_writel(0, &priv
->host_port_regs
->cpdma_rx_chan_map
);
766 cpsw_ale_control_set(priv
->ale
, priv
->host_port
,
767 ALE_PORT_STATE
, ALE_PORT_STATE_FORWARD
);
769 if (!priv
->data
.dual_emac
) {
770 cpsw_ale_add_ucast(priv
->ale
, priv
->mac_addr
, priv
->host_port
,
772 cpsw_ale_add_mcast(priv
->ale
, priv
->ndev
->broadcast
,
773 1 << priv
->host_port
, 0, 0, ALE_MCAST_FWD_2
);
777 static int cpsw_ndo_open(struct net_device
*ndev
)
779 struct cpsw_priv
*priv
= netdev_priv(ndev
);
783 if (!cpsw_common_res_usage_state(priv
))
784 cpsw_intr_disable(priv
);
785 netif_carrier_off(ndev
);
787 pm_runtime_get_sync(&priv
->pdev
->dev
);
791 dev_info(priv
->dev
, "initializing cpsw version %d.%d (%d)\n",
792 CPSW_MAJOR_VERSION(reg
), CPSW_MINOR_VERSION(reg
),
793 CPSW_RTL_VERSION(reg
));
795 /* initialize host and slave ports */
796 if (!cpsw_common_res_usage_state(priv
))
797 cpsw_init_host_port(priv
);
798 for_each_slave(priv
, cpsw_slave_open
, priv
);
800 /* Add default VLAN */
801 if (!priv
->data
.dual_emac
)
802 cpsw_add_default_vlan(priv
);
804 if (!cpsw_common_res_usage_state(priv
)) {
805 /* setup tx dma to fixed prio and zero offset */
806 cpdma_control_set(priv
->dma
, CPDMA_TX_PRIO_FIXED
, 1);
807 cpdma_control_set(priv
->dma
, CPDMA_RX_BUFFER_OFFSET
, 0);
809 /* disable priority elevation */
810 __raw_writel(0, &priv
->regs
->ptype
);
812 /* enable statistics collection only on all ports */
813 __raw_writel(0x7, &priv
->regs
->stat_port_en
);
815 if (WARN_ON(!priv
->data
.rx_descs
))
816 priv
->data
.rx_descs
= 128;
818 for (i
= 0; i
< priv
->data
.rx_descs
; i
++) {
822 skb
= netdev_alloc_skb_ip_align(priv
->ndev
,
823 priv
->rx_packet_max
);
826 ret
= cpdma_chan_submit(priv
->rxch
, skb
, skb
->data
,
827 skb_tailroom(skb
), 0, GFP_KERNEL
);
828 if (WARN_ON(ret
< 0))
831 /* continue even if we didn't manage to submit all
834 cpsw_info(priv
, ifup
, "submitted %d rx descriptors\n", i
);
837 cpdma_ctlr_start(priv
->dma
);
838 cpsw_intr_enable(priv
);
839 napi_enable(&priv
->napi
);
840 cpdma_ctlr_eoi(priv
->dma
, CPDMA_EOI_RX
);
841 cpdma_ctlr_eoi(priv
->dma
, CPDMA_EOI_TX
);
843 if (priv
->data
.dual_emac
)
844 priv
->slaves
[priv
->emac_port
].open_stat
= true;
848 static void cpsw_slave_stop(struct cpsw_slave
*slave
, struct cpsw_priv
*priv
)
852 phy_stop(slave
->phy
);
853 phy_disconnect(slave
->phy
);
857 static int cpsw_ndo_stop(struct net_device
*ndev
)
859 struct cpsw_priv
*priv
= netdev_priv(ndev
);
861 cpsw_info(priv
, ifdown
, "shutting down cpsw device\n");
862 netif_stop_queue(priv
->ndev
);
863 napi_disable(&priv
->napi
);
864 netif_carrier_off(priv
->ndev
);
866 if (cpsw_common_res_usage_state(priv
) <= 1) {
867 cpsw_intr_disable(priv
);
868 cpdma_ctlr_int_ctrl(priv
->dma
, false);
869 cpdma_ctlr_stop(priv
->dma
);
870 cpsw_ale_stop(priv
->ale
);
872 for_each_slave(priv
, cpsw_slave_stop
, priv
);
873 pm_runtime_put_sync(&priv
->pdev
->dev
);
874 if (priv
->data
.dual_emac
)
875 priv
->slaves
[priv
->emac_port
].open_stat
= false;
879 static netdev_tx_t
cpsw_ndo_start_xmit(struct sk_buff
*skb
,
880 struct net_device
*ndev
)
882 struct cpsw_priv
*priv
= netdev_priv(ndev
);
885 ndev
->trans_start
= jiffies
;
887 if (skb_padto(skb
, CPSW_MIN_PACKET_SIZE
)) {
888 cpsw_err(priv
, tx_err
, "packet pad failed\n");
889 priv
->stats
.tx_dropped
++;
893 if (skb_shinfo(skb
)->tx_flags
& SKBTX_HW_TSTAMP
&&
894 priv
->cpts
->tx_enable
)
895 skb_shinfo(skb
)->tx_flags
|= SKBTX_IN_PROGRESS
;
897 skb_tx_timestamp(skb
);
899 ret
= cpsw_tx_packet_submit(ndev
, priv
, skb
);
900 if (unlikely(ret
!= 0)) {
901 cpsw_err(priv
, tx_err
, "desc submit failed\n");
905 /* If there is no more tx desc left free then we need to
906 * tell the kernel to stop sending us tx frames.
908 if (unlikely(!cpdma_check_free_tx_desc(priv
->txch
)))
909 netif_stop_queue(ndev
);
913 priv
->stats
.tx_dropped
++;
914 netif_stop_queue(ndev
);
915 return NETDEV_TX_BUSY
;
918 static void cpsw_ndo_change_rx_flags(struct net_device
*ndev
, int flags
)
921 * The switch cannot operate in promiscuous mode without substantial
922 * headache. For promiscuous mode to work, we would need to put the
923 * ALE in bypass mode and route all traffic to the host port.
924 * Subsequently, the host will need to operate as a "bridge", learn,
925 * and flood as needed. For now, we simply complain here and
926 * do nothing about it :-)
928 if ((flags
& IFF_PROMISC
) && (ndev
->flags
& IFF_PROMISC
))
929 dev_err(&ndev
->dev
, "promiscuity ignored!\n");
932 * The switch cannot filter multicast traffic unless it is configured
933 * in "VLAN Aware" mode. Unfortunately, VLAN awareness requires a
934 * whole bunch of additional logic that this driver does not implement
937 if ((flags
& IFF_ALLMULTI
) && !(ndev
->flags
& IFF_ALLMULTI
))
938 dev_err(&ndev
->dev
, "multicast traffic cannot be filtered!\n");
941 #ifdef CONFIG_TI_CPTS
943 static void cpsw_hwtstamp_v1(struct cpsw_priv
*priv
)
945 struct cpsw_slave
*slave
= &priv
->slaves
[priv
->data
.cpts_active_slave
];
948 if (!priv
->cpts
->tx_enable
&& !priv
->cpts
->rx_enable
) {
949 slave_write(slave
, 0, CPSW1_TS_CTL
);
953 seq_id
= (30 << CPSW_V1_SEQ_ID_OFS_SHIFT
) | ETH_P_1588
;
954 ts_en
= EVENT_MSG_BITS
<< CPSW_V1_MSG_TYPE_OFS
;
956 if (priv
->cpts
->tx_enable
)
957 ts_en
|= CPSW_V1_TS_TX_EN
;
959 if (priv
->cpts
->rx_enable
)
960 ts_en
|= CPSW_V1_TS_RX_EN
;
962 slave_write(slave
, ts_en
, CPSW1_TS_CTL
);
963 slave_write(slave
, seq_id
, CPSW1_TS_SEQ_LTYPE
);
966 static void cpsw_hwtstamp_v2(struct cpsw_priv
*priv
)
968 struct cpsw_slave
*slave
;
971 if (priv
->data
.dual_emac
)
972 slave
= &priv
->slaves
[priv
->emac_port
];
974 slave
= &priv
->slaves
[priv
->data
.cpts_active_slave
];
976 ctrl
= slave_read(slave
, CPSW2_CONTROL
);
977 ctrl
&= ~CTRL_ALL_TS_MASK
;
979 if (priv
->cpts
->tx_enable
)
980 ctrl
|= CTRL_TX_TS_BITS
;
982 if (priv
->cpts
->rx_enable
)
983 ctrl
|= CTRL_RX_TS_BITS
;
985 mtype
= (30 << TS_SEQ_ID_OFFSET_SHIFT
) | EVENT_MSG_BITS
;
987 slave_write(slave
, mtype
, CPSW2_TS_SEQ_MTYPE
);
988 slave_write(slave
, ctrl
, CPSW2_CONTROL
);
989 __raw_writel(ETH_P_1588
, &priv
->regs
->ts_ltype
);
992 static int cpsw_hwtstamp_ioctl(struct net_device
*dev
, struct ifreq
*ifr
)
994 struct cpsw_priv
*priv
= netdev_priv(dev
);
995 struct cpts
*cpts
= priv
->cpts
;
996 struct hwtstamp_config cfg
;
998 if (copy_from_user(&cfg
, ifr
->ifr_data
, sizeof(cfg
)))
1001 /* reserved for future extensions */
1005 switch (cfg
.tx_type
) {
1006 case HWTSTAMP_TX_OFF
:
1007 cpts
->tx_enable
= 0;
1009 case HWTSTAMP_TX_ON
:
1010 cpts
->tx_enable
= 1;
1016 switch (cfg
.rx_filter
) {
1017 case HWTSTAMP_FILTER_NONE
:
1018 cpts
->rx_enable
= 0;
1020 case HWTSTAMP_FILTER_ALL
:
1021 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT
:
1022 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC
:
1023 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ
:
1025 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
1026 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
1027 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
1028 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
1029 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
1030 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
1031 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
1032 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
1033 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
1034 cpts
->rx_enable
= 1;
1035 cfg
.rx_filter
= HWTSTAMP_FILTER_PTP_V2_EVENT
;
1041 switch (priv
->version
) {
1042 case CPSW_VERSION_1
:
1043 cpsw_hwtstamp_v1(priv
);
1045 case CPSW_VERSION_2
:
1046 cpsw_hwtstamp_v2(priv
);
1052 return copy_to_user(ifr
->ifr_data
, &cfg
, sizeof(cfg
)) ? -EFAULT
: 0;
1055 #endif /*CONFIG_TI_CPTS*/
1057 static int cpsw_ndo_ioctl(struct net_device
*dev
, struct ifreq
*req
, int cmd
)
1059 if (!netif_running(dev
))
1062 #ifdef CONFIG_TI_CPTS
1063 if (cmd
== SIOCSHWTSTAMP
)
1064 return cpsw_hwtstamp_ioctl(dev
, req
);
1069 static void cpsw_ndo_tx_timeout(struct net_device
*ndev
)
1071 struct cpsw_priv
*priv
= netdev_priv(ndev
);
1073 cpsw_err(priv
, tx_err
, "transmit timeout, restarting dma\n");
1074 priv
->stats
.tx_errors
++;
1075 cpsw_intr_disable(priv
);
1076 cpdma_ctlr_int_ctrl(priv
->dma
, false);
1077 cpdma_chan_stop(priv
->txch
);
1078 cpdma_chan_start(priv
->txch
);
1079 cpdma_ctlr_int_ctrl(priv
->dma
, true);
1080 cpsw_intr_enable(priv
);
1081 cpdma_ctlr_eoi(priv
->dma
, CPDMA_EOI_RX
);
1082 cpdma_ctlr_eoi(priv
->dma
, CPDMA_EOI_TX
);
1086 static struct net_device_stats
*cpsw_ndo_get_stats(struct net_device
*ndev
)
1088 struct cpsw_priv
*priv
= netdev_priv(ndev
);
1089 return &priv
->stats
;
1092 #ifdef CONFIG_NET_POLL_CONTROLLER
1093 static void cpsw_ndo_poll_controller(struct net_device
*ndev
)
1095 struct cpsw_priv
*priv
= netdev_priv(ndev
);
1097 cpsw_intr_disable(priv
);
1098 cpdma_ctlr_int_ctrl(priv
->dma
, false);
1099 cpsw_interrupt(ndev
->irq
, priv
);
1100 cpdma_ctlr_int_ctrl(priv
->dma
, true);
1101 cpsw_intr_enable(priv
);
1102 cpdma_ctlr_eoi(priv
->dma
, CPDMA_EOI_RX
);
1103 cpdma_ctlr_eoi(priv
->dma
, CPDMA_EOI_TX
);
1108 static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv
*priv
,
1113 ret
= cpsw_ale_add_vlan(priv
->ale
, vid
,
1114 ALE_ALL_PORTS
<< priv
->host_port
,
1115 0, ALE_ALL_PORTS
<< priv
->host_port
,
1116 (ALE_PORT_1
| ALE_PORT_2
) << priv
->host_port
);
1120 ret
= cpsw_ale_add_ucast(priv
->ale
, priv
->mac_addr
,
1121 priv
->host_port
, ALE_VLAN
, vid
);
1125 ret
= cpsw_ale_add_mcast(priv
->ale
, priv
->ndev
->broadcast
,
1126 ALE_ALL_PORTS
<< priv
->host_port
,
1129 goto clean_vlan_ucast
;
1133 cpsw_ale_del_ucast(priv
->ale
, priv
->mac_addr
,
1134 priv
->host_port
, ALE_VLAN
, vid
);
1136 cpsw_ale_del_vlan(priv
->ale
, vid
, 0);
1140 static int cpsw_ndo_vlan_rx_add_vid(struct net_device
*ndev
,
1143 struct cpsw_priv
*priv
= netdev_priv(ndev
);
1145 if (vid
== priv
->data
.default_vlan
)
1148 dev_info(priv
->dev
, "Adding vlanid %d to vlan filter\n", vid
);
1149 return cpsw_add_vlan_ale_entry(priv
, vid
);
1152 static int cpsw_ndo_vlan_rx_kill_vid(struct net_device
*ndev
,
1155 struct cpsw_priv
*priv
= netdev_priv(ndev
);
1158 if (vid
== priv
->data
.default_vlan
)
1161 dev_info(priv
->dev
, "removing vlanid %d from vlan filter\n", vid
);
1162 ret
= cpsw_ale_del_vlan(priv
->ale
, vid
, 0);
1166 ret
= cpsw_ale_del_ucast(priv
->ale
, priv
->mac_addr
,
1167 priv
->host_port
, ALE_VLAN
, vid
);
1171 return cpsw_ale_del_mcast(priv
->ale
, priv
->ndev
->broadcast
,
1175 static const struct net_device_ops cpsw_netdev_ops
= {
1176 .ndo_open
= cpsw_ndo_open
,
1177 .ndo_stop
= cpsw_ndo_stop
,
1178 .ndo_start_xmit
= cpsw_ndo_start_xmit
,
1179 .ndo_change_rx_flags
= cpsw_ndo_change_rx_flags
,
1180 .ndo_do_ioctl
= cpsw_ndo_ioctl
,
1181 .ndo_validate_addr
= eth_validate_addr
,
1182 .ndo_change_mtu
= eth_change_mtu
,
1183 .ndo_tx_timeout
= cpsw_ndo_tx_timeout
,
1184 .ndo_get_stats
= cpsw_ndo_get_stats
,
1185 .ndo_set_rx_mode
= cpsw_ndo_set_rx_mode
,
1186 #ifdef CONFIG_NET_POLL_CONTROLLER
1187 .ndo_poll_controller
= cpsw_ndo_poll_controller
,
1189 .ndo_vlan_rx_add_vid
= cpsw_ndo_vlan_rx_add_vid
,
1190 .ndo_vlan_rx_kill_vid
= cpsw_ndo_vlan_rx_kill_vid
,
1193 static void cpsw_get_drvinfo(struct net_device
*ndev
,
1194 struct ethtool_drvinfo
*info
)
1196 struct cpsw_priv
*priv
= netdev_priv(ndev
);
1198 strlcpy(info
->driver
, "TI CPSW Driver v1.0", sizeof(info
->driver
));
1199 strlcpy(info
->version
, "1.0", sizeof(info
->version
));
1200 strlcpy(info
->bus_info
, priv
->pdev
->name
, sizeof(info
->bus_info
));
1203 static u32
cpsw_get_msglevel(struct net_device
*ndev
)
1205 struct cpsw_priv
*priv
= netdev_priv(ndev
);
1206 return priv
->msg_enable
;
1209 static void cpsw_set_msglevel(struct net_device
*ndev
, u32 value
)
1211 struct cpsw_priv
*priv
= netdev_priv(ndev
);
1212 priv
->msg_enable
= value
;
1215 static int cpsw_get_ts_info(struct net_device
*ndev
,
1216 struct ethtool_ts_info
*info
)
1218 #ifdef CONFIG_TI_CPTS
1219 struct cpsw_priv
*priv
= netdev_priv(ndev
);
1221 info
->so_timestamping
=
1222 SOF_TIMESTAMPING_TX_HARDWARE
|
1223 SOF_TIMESTAMPING_TX_SOFTWARE
|
1224 SOF_TIMESTAMPING_RX_HARDWARE
|
1225 SOF_TIMESTAMPING_RX_SOFTWARE
|
1226 SOF_TIMESTAMPING_SOFTWARE
|
1227 SOF_TIMESTAMPING_RAW_HARDWARE
;
1228 info
->phc_index
= priv
->cpts
->phc_index
;
1230 (1 << HWTSTAMP_TX_OFF
) |
1231 (1 << HWTSTAMP_TX_ON
);
1233 (1 << HWTSTAMP_FILTER_NONE
) |
1234 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT
);
1236 info
->so_timestamping
=
1237 SOF_TIMESTAMPING_TX_SOFTWARE
|
1238 SOF_TIMESTAMPING_RX_SOFTWARE
|
1239 SOF_TIMESTAMPING_SOFTWARE
;
1240 info
->phc_index
= -1;
1242 info
->rx_filters
= 0;
1247 static const struct ethtool_ops cpsw_ethtool_ops
= {
1248 .get_drvinfo
= cpsw_get_drvinfo
,
1249 .get_msglevel
= cpsw_get_msglevel
,
1250 .set_msglevel
= cpsw_set_msglevel
,
1251 .get_link
= ethtool_op_get_link
,
1252 .get_ts_info
= cpsw_get_ts_info
,
1255 static void cpsw_slave_init(struct cpsw_slave
*slave
, struct cpsw_priv
*priv
,
1256 u32 slave_reg_ofs
, u32 sliver_reg_ofs
)
1258 void __iomem
*regs
= priv
->regs
;
1259 int slave_num
= slave
->slave_num
;
1260 struct cpsw_slave_data
*data
= priv
->data
.slave_data
+ slave_num
;
1263 slave
->regs
= regs
+ slave_reg_ofs
;
1264 slave
->sliver
= regs
+ sliver_reg_ofs
;
1265 slave
->port_vlan
= data
->dual_emac_res_vlan
;
1268 static int cpsw_probe_dt(struct cpsw_platform_data
*data
,
1269 struct platform_device
*pdev
)
1271 struct device_node
*node
= pdev
->dev
.of_node
;
1272 struct device_node
*slave_node
;
1279 if (of_property_read_u32(node
, "slaves", &prop
)) {
1280 pr_err("Missing slaves property in the DT.\n");
1283 data
->slaves
= prop
;
1285 if (of_property_read_u32(node
, "cpts_active_slave", &prop
)) {
1286 pr_err("Missing cpts_active_slave property in the DT.\n");
1290 data
->cpts_active_slave
= prop
;
1292 if (of_property_read_u32(node
, "cpts_clock_mult", &prop
)) {
1293 pr_err("Missing cpts_clock_mult property in the DT.\n");
1297 data
->cpts_clock_mult
= prop
;
1299 if (of_property_read_u32(node
, "cpts_clock_shift", &prop
)) {
1300 pr_err("Missing cpts_clock_shift property in the DT.\n");
1304 data
->cpts_clock_shift
= prop
;
1306 data
->slave_data
= kcalloc(data
->slaves
, sizeof(struct cpsw_slave_data
),
1308 if (!data
->slave_data
)
1311 if (of_property_read_u32(node
, "cpdma_channels", &prop
)) {
1312 pr_err("Missing cpdma_channels property in the DT.\n");
1316 data
->channels
= prop
;
1318 if (of_property_read_u32(node
, "ale_entries", &prop
)) {
1319 pr_err("Missing ale_entries property in the DT.\n");
1323 data
->ale_entries
= prop
;
1325 if (of_property_read_u32(node
, "bd_ram_size", &prop
)) {
1326 pr_err("Missing bd_ram_size property in the DT.\n");
1330 data
->bd_ram_size
= prop
;
1332 if (of_property_read_u32(node
, "rx_descs", &prop
)) {
1333 pr_err("Missing rx_descs property in the DT.\n");
1337 data
->rx_descs
= prop
;
1339 if (of_property_read_u32(node
, "mac_control", &prop
)) {
1340 pr_err("Missing mac_control property in the DT.\n");
1344 data
->mac_control
= prop
;
1346 if (!of_property_read_u32(node
, "dual_emac", &prop
))
1347 data
->dual_emac
= prop
;
1350 * Populate all the child nodes here...
1352 ret
= of_platform_populate(node
, NULL
, NULL
, &pdev
->dev
);
1353 /* We do not want to force this, as in some cases may not have child */
1355 pr_warn("Doesn't have any child node\n");
1357 for_each_node_by_name(slave_node
, "slave") {
1358 struct cpsw_slave_data
*slave_data
= data
->slave_data
+ i
;
1359 const void *mac_addr
= NULL
;
1363 struct device_node
*mdio_node
;
1364 struct platform_device
*mdio
;
1366 parp
= of_get_property(slave_node
, "phy_id", &lenp
);
1367 if ((parp
== NULL
) || (lenp
!= (sizeof(void *) * 2))) {
1368 pr_err("Missing slave[%d] phy_id property\n", i
);
1372 mdio_node
= of_find_node_by_phandle(be32_to_cpup(parp
));
1373 phyid
= be32_to_cpup(parp
+1);
1374 mdio
= of_find_device_by_node(mdio_node
);
1375 snprintf(slave_data
->phy_id
, sizeof(slave_data
->phy_id
),
1376 PHY_ID_FMT
, mdio
->name
, phyid
);
1378 mac_addr
= of_get_mac_address(slave_node
);
1380 memcpy(slave_data
->mac_addr
, mac_addr
, ETH_ALEN
);
1382 if (data
->dual_emac
) {
1383 if (of_property_read_u32(node
, "dual_emac_res_vlan",
1385 pr_err("Missing dual_emac_res_vlan in DT.\n");
1386 slave_data
->dual_emac_res_vlan
= i
+1;
1387 pr_err("Using %d as Reserved VLAN for %d slave\n",
1388 slave_data
->dual_emac_res_vlan
, i
);
1390 slave_data
->dual_emac_res_vlan
= prop
;
1400 kfree(data
->slave_data
);
1404 static int cpsw_probe_dual_emac(struct platform_device
*pdev
,
1405 struct cpsw_priv
*priv
)
1407 struct cpsw_platform_data
*data
= &priv
->data
;
1408 struct net_device
*ndev
;
1409 struct cpsw_priv
*priv_sl2
;
1412 ndev
= alloc_etherdev(sizeof(struct cpsw_priv
));
1414 pr_err("cpsw: error allocating net_device\n");
1418 priv_sl2
= netdev_priv(ndev
);
1419 spin_lock_init(&priv_sl2
->lock
);
1420 priv_sl2
->data
= *data
;
1421 priv_sl2
->pdev
= pdev
;
1422 priv_sl2
->ndev
= ndev
;
1423 priv_sl2
->dev
= &ndev
->dev
;
1424 priv_sl2
->msg_enable
= netif_msg_init(debug_level
, CPSW_DEBUG
);
1425 priv_sl2
->rx_packet_max
= max(rx_packet_max
, 128);
1427 if (is_valid_ether_addr(data
->slave_data
[1].mac_addr
)) {
1428 memcpy(priv_sl2
->mac_addr
, data
->slave_data
[1].mac_addr
,
1430 pr_info("cpsw: Detected MACID = %pM\n", priv_sl2
->mac_addr
);
1432 random_ether_addr(priv_sl2
->mac_addr
);
1433 pr_info("cpsw: Random MACID = %pM\n", priv_sl2
->mac_addr
);
1435 memcpy(ndev
->dev_addr
, priv_sl2
->mac_addr
, ETH_ALEN
);
1437 priv_sl2
->slaves
= priv
->slaves
;
1438 priv_sl2
->clk
= priv
->clk
;
1440 priv_sl2
->cpsw_res
= priv
->cpsw_res
;
1441 priv_sl2
->regs
= priv
->regs
;
1442 priv_sl2
->host_port
= priv
->host_port
;
1443 priv_sl2
->host_port_regs
= priv
->host_port_regs
;
1444 priv_sl2
->wr_regs
= priv
->wr_regs
;
1445 priv_sl2
->dma
= priv
->dma
;
1446 priv_sl2
->txch
= priv
->txch
;
1447 priv_sl2
->rxch
= priv
->rxch
;
1448 priv_sl2
->ale
= priv
->ale
;
1449 priv_sl2
->emac_port
= 1;
1450 priv
->slaves
[1].ndev
= ndev
;
1451 priv_sl2
->cpts
= priv
->cpts
;
1452 priv_sl2
->version
= priv
->version
;
1454 for (i
= 0; i
< priv
->num_irqs
; i
++) {
1455 priv_sl2
->irqs_table
[i
] = priv
->irqs_table
[i
];
1456 priv_sl2
->num_irqs
= priv
->num_irqs
;
1459 ndev
->features
|= NETIF_F_HW_VLAN_FILTER
;
1461 ndev
->netdev_ops
= &cpsw_netdev_ops
;
1462 SET_ETHTOOL_OPS(ndev
, &cpsw_ethtool_ops
);
1463 netif_napi_add(ndev
, &priv_sl2
->napi
, cpsw_poll
, CPSW_POLL_WEIGHT
);
1465 /* register the network device */
1466 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
1467 ret
= register_netdev(ndev
);
1469 pr_err("cpsw: error registering net device\n");
1477 static int cpsw_probe(struct platform_device
*pdev
)
1479 struct cpsw_platform_data
*data
= pdev
->dev
.platform_data
;
1480 struct net_device
*ndev
;
1481 struct cpsw_priv
*priv
;
1482 struct cpdma_params dma_params
;
1483 struct cpsw_ale_params ale_params
;
1484 void __iomem
*ss_regs
, *wr_regs
;
1485 struct resource
*res
;
1486 u32 slave_offset
, sliver_offset
, slave_size
;
1487 int ret
= 0, i
, k
= 0;
1489 ndev
= alloc_etherdev(sizeof(struct cpsw_priv
));
1491 pr_err("error allocating net_device\n");
1495 platform_set_drvdata(pdev
, ndev
);
1496 priv
= netdev_priv(ndev
);
1497 spin_lock_init(&priv
->lock
);
1500 priv
->dev
= &ndev
->dev
;
1501 priv
->msg_enable
= netif_msg_init(debug_level
, CPSW_DEBUG
);
1502 priv
->rx_packet_max
= max(rx_packet_max
, 128);
1503 priv
->cpts
= devm_kzalloc(&pdev
->dev
, sizeof(struct cpts
), GFP_KERNEL
);
1505 pr_err("error allocating cpts\n");
1506 goto clean_ndev_ret
;
1510 * This may be required here for child devices.
1512 pm_runtime_enable(&pdev
->dev
);
1514 if (cpsw_probe_dt(&priv
->data
, pdev
)) {
1515 pr_err("cpsw: platform data missing\n");
1517 goto clean_ndev_ret
;
1521 if (is_valid_ether_addr(data
->slave_data
[0].mac_addr
)) {
1522 memcpy(priv
->mac_addr
, data
->slave_data
[0].mac_addr
, ETH_ALEN
);
1523 pr_info("Detected MACID = %pM", priv
->mac_addr
);
1525 eth_random_addr(priv
->mac_addr
);
1526 pr_info("Random MACID = %pM", priv
->mac_addr
);
1529 memcpy(ndev
->dev_addr
, priv
->mac_addr
, ETH_ALEN
);
1531 priv
->slaves
= kzalloc(sizeof(struct cpsw_slave
) * data
->slaves
,
1533 if (!priv
->slaves
) {
1535 goto clean_ndev_ret
;
1537 for (i
= 0; i
< data
->slaves
; i
++)
1538 priv
->slaves
[i
].slave_num
= i
;
1540 priv
->slaves
[0].ndev
= ndev
;
1541 priv
->emac_port
= 0;
1543 priv
->clk
= clk_get(&pdev
->dev
, "fck");
1544 if (IS_ERR(priv
->clk
)) {
1545 dev_err(&pdev
->dev
, "fck is not found\n");
1547 goto clean_slave_ret
;
1550 priv
->cpsw_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1551 if (!priv
->cpsw_res
) {
1552 dev_err(priv
->dev
, "error getting i/o resource\n");
1556 if (!request_mem_region(priv
->cpsw_res
->start
,
1557 resource_size(priv
->cpsw_res
), ndev
->name
)) {
1558 dev_err(priv
->dev
, "failed request i/o region\n");
1562 ss_regs
= ioremap(priv
->cpsw_res
->start
, resource_size(priv
->cpsw_res
));
1564 dev_err(priv
->dev
, "unable to map i/o region\n");
1565 goto clean_cpsw_iores_ret
;
1567 priv
->regs
= ss_regs
;
1568 priv
->version
= __raw_readl(&priv
->regs
->id_ver
);
1569 priv
->host_port
= HOST_PORT_NUM
;
1571 priv
->cpsw_wr_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1572 if (!priv
->cpsw_wr_res
) {
1573 dev_err(priv
->dev
, "error getting i/o resource\n");
1575 goto clean_iomap_ret
;
1577 if (!request_mem_region(priv
->cpsw_wr_res
->start
,
1578 resource_size(priv
->cpsw_wr_res
), ndev
->name
)) {
1579 dev_err(priv
->dev
, "failed request i/o region\n");
1581 goto clean_iomap_ret
;
1583 wr_regs
= ioremap(priv
->cpsw_wr_res
->start
,
1584 resource_size(priv
->cpsw_wr_res
));
1586 dev_err(priv
->dev
, "unable to map i/o region\n");
1587 goto clean_cpsw_wr_iores_ret
;
1589 priv
->wr_regs
= wr_regs
;
1591 memset(&dma_params
, 0, sizeof(dma_params
));
1592 memset(&ale_params
, 0, sizeof(ale_params
));
1594 switch (priv
->version
) {
1595 case CPSW_VERSION_1
:
1596 priv
->host_port_regs
= ss_regs
+ CPSW1_HOST_PORT_OFFSET
;
1597 priv
->cpts
->reg
= ss_regs
+ CPSW1_CPTS_OFFSET
;
1598 dma_params
.dmaregs
= ss_regs
+ CPSW1_CPDMA_OFFSET
;
1599 dma_params
.txhdp
= ss_regs
+ CPSW1_STATERAM_OFFSET
;
1600 ale_params
.ale_regs
= ss_regs
+ CPSW1_ALE_OFFSET
;
1601 slave_offset
= CPSW1_SLAVE_OFFSET
;
1602 slave_size
= CPSW1_SLAVE_SIZE
;
1603 sliver_offset
= CPSW1_SLIVER_OFFSET
;
1604 dma_params
.desc_mem_phys
= 0;
1606 case CPSW_VERSION_2
:
1607 priv
->host_port_regs
= ss_regs
+ CPSW2_HOST_PORT_OFFSET
;
1608 priv
->cpts
->reg
= ss_regs
+ CPSW2_CPTS_OFFSET
;
1609 dma_params
.dmaregs
= ss_regs
+ CPSW2_CPDMA_OFFSET
;
1610 dma_params
.txhdp
= ss_regs
+ CPSW2_STATERAM_OFFSET
;
1611 ale_params
.ale_regs
= ss_regs
+ CPSW2_ALE_OFFSET
;
1612 slave_offset
= CPSW2_SLAVE_OFFSET
;
1613 slave_size
= CPSW2_SLAVE_SIZE
;
1614 sliver_offset
= CPSW2_SLIVER_OFFSET
;
1615 dma_params
.desc_mem_phys
=
1616 (u32 __force
) priv
->cpsw_res
->start
+ CPSW2_BD_OFFSET
;
1619 dev_err(priv
->dev
, "unknown version 0x%08x\n", priv
->version
);
1621 goto clean_cpsw_wr_iores_ret
;
1623 for (i
= 0; i
< priv
->data
.slaves
; i
++) {
1624 struct cpsw_slave
*slave
= &priv
->slaves
[i
];
1625 cpsw_slave_init(slave
, priv
, slave_offset
, sliver_offset
);
1626 slave_offset
+= slave_size
;
1627 sliver_offset
+= SLIVER_SIZE
;
1630 dma_params
.dev
= &pdev
->dev
;
1631 dma_params
.rxthresh
= dma_params
.dmaregs
+ CPDMA_RXTHRESH
;
1632 dma_params
.rxfree
= dma_params
.dmaregs
+ CPDMA_RXFREE
;
1633 dma_params
.rxhdp
= dma_params
.txhdp
+ CPDMA_RXHDP
;
1634 dma_params
.txcp
= dma_params
.txhdp
+ CPDMA_TXCP
;
1635 dma_params
.rxcp
= dma_params
.txhdp
+ CPDMA_RXCP
;
1637 dma_params
.num_chan
= data
->channels
;
1638 dma_params
.has_soft_reset
= true;
1639 dma_params
.min_packet_size
= CPSW_MIN_PACKET_SIZE
;
1640 dma_params
.desc_mem_size
= data
->bd_ram_size
;
1641 dma_params
.desc_align
= 16;
1642 dma_params
.has_ext_regs
= true;
1643 dma_params
.desc_hw_addr
= dma_params
.desc_mem_phys
;
1645 priv
->dma
= cpdma_ctlr_create(&dma_params
);
1647 dev_err(priv
->dev
, "error initializing dma\n");
1649 goto clean_wr_iomap_ret
;
1652 priv
->txch
= cpdma_chan_create(priv
->dma
, tx_chan_num(0),
1654 priv
->rxch
= cpdma_chan_create(priv
->dma
, rx_chan_num(0),
1657 if (WARN_ON(!priv
->txch
|| !priv
->rxch
)) {
1658 dev_err(priv
->dev
, "error initializing dma channels\n");
1663 ale_params
.dev
= &ndev
->dev
;
1664 ale_params
.ale_ageout
= ale_ageout
;
1665 ale_params
.ale_entries
= data
->ale_entries
;
1666 ale_params
.ale_ports
= data
->slaves
;
1668 priv
->ale
= cpsw_ale_create(&ale_params
);
1670 dev_err(priv
->dev
, "error initializing ale engine\n");
1675 ndev
->irq
= platform_get_irq(pdev
, 0);
1676 if (ndev
->irq
< 0) {
1677 dev_err(priv
->dev
, "error getting irq resource\n");
1682 while ((res
= platform_get_resource(priv
->pdev
, IORESOURCE_IRQ
, k
))) {
1683 for (i
= res
->start
; i
<= res
->end
; i
++) {
1684 if (request_irq(i
, cpsw_interrupt
, IRQF_DISABLED
,
1685 dev_name(&pdev
->dev
), priv
)) {
1686 dev_err(priv
->dev
, "error attaching irq\n");
1689 priv
->irqs_table
[k
] = i
;
1695 ndev
->features
|= NETIF_F_HW_VLAN_FILTER
;
1697 ndev
->netdev_ops
= &cpsw_netdev_ops
;
1698 SET_ETHTOOL_OPS(ndev
, &cpsw_ethtool_ops
);
1699 netif_napi_add(ndev
, &priv
->napi
, cpsw_poll
, CPSW_POLL_WEIGHT
);
1701 /* register the network device */
1702 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
1703 ret
= register_netdev(ndev
);
1705 dev_err(priv
->dev
, "error registering net device\n");
1710 if (cpts_register(&pdev
->dev
, priv
->cpts
,
1711 data
->cpts_clock_mult
, data
->cpts_clock_shift
))
1712 dev_err(priv
->dev
, "error registering cpts device\n");
1714 cpsw_notice(priv
, probe
, "initialized device (regs %x, irq %d)\n",
1715 priv
->cpsw_res
->start
, ndev
->irq
);
1717 if (priv
->data
.dual_emac
) {
1718 ret
= cpsw_probe_dual_emac(pdev
, priv
);
1720 cpsw_err(priv
, probe
, "error probe slave 2 emac interface\n");
1728 free_irq(ndev
->irq
, priv
);
1730 cpsw_ale_destroy(priv
->ale
);
1732 cpdma_chan_destroy(priv
->txch
);
1733 cpdma_chan_destroy(priv
->rxch
);
1734 cpdma_ctlr_destroy(priv
->dma
);
1736 iounmap(priv
->wr_regs
);
1737 clean_cpsw_wr_iores_ret
:
1738 release_mem_region(priv
->cpsw_wr_res
->start
,
1739 resource_size(priv
->cpsw_wr_res
));
1741 iounmap(priv
->regs
);
1742 clean_cpsw_iores_ret
:
1743 release_mem_region(priv
->cpsw_res
->start
,
1744 resource_size(priv
->cpsw_res
));
1748 pm_runtime_disable(&pdev
->dev
);
1749 kfree(priv
->slaves
);
1755 static int cpsw_remove(struct platform_device
*pdev
)
1757 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1758 struct cpsw_priv
*priv
= netdev_priv(ndev
);
1760 pr_info("removing device");
1761 platform_set_drvdata(pdev
, NULL
);
1763 cpts_unregister(priv
->cpts
);
1764 free_irq(ndev
->irq
, priv
);
1765 cpsw_ale_destroy(priv
->ale
);
1766 cpdma_chan_destroy(priv
->txch
);
1767 cpdma_chan_destroy(priv
->rxch
);
1768 cpdma_ctlr_destroy(priv
->dma
);
1769 iounmap(priv
->regs
);
1770 release_mem_region(priv
->cpsw_res
->start
,
1771 resource_size(priv
->cpsw_res
));
1772 iounmap(priv
->wr_regs
);
1773 release_mem_region(priv
->cpsw_wr_res
->start
,
1774 resource_size(priv
->cpsw_wr_res
));
1775 pm_runtime_disable(&pdev
->dev
);
1777 kfree(priv
->slaves
);
1783 static int cpsw_suspend(struct device
*dev
)
1785 struct platform_device
*pdev
= to_platform_device(dev
);
1786 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1788 if (netif_running(ndev
))
1789 cpsw_ndo_stop(ndev
);
1790 pm_runtime_put_sync(&pdev
->dev
);
1795 static int cpsw_resume(struct device
*dev
)
1797 struct platform_device
*pdev
= to_platform_device(dev
);
1798 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1800 pm_runtime_get_sync(&pdev
->dev
);
1801 if (netif_running(ndev
))
1802 cpsw_ndo_open(ndev
);
1806 static const struct dev_pm_ops cpsw_pm_ops
= {
1807 .suspend
= cpsw_suspend
,
1808 .resume
= cpsw_resume
,
1811 static const struct of_device_id cpsw_of_mtable
[] = {
1812 { .compatible
= "ti,cpsw", },
1816 static struct platform_driver cpsw_driver
= {
1819 .owner
= THIS_MODULE
,
1821 .of_match_table
= of_match_ptr(cpsw_of_mtable
),
1823 .probe
= cpsw_probe
,
1824 .remove
= cpsw_remove
,
1827 static int __init
cpsw_init(void)
1829 return platform_driver_register(&cpsw_driver
);
1831 late_initcall(cpsw_init
);
1833 static void __exit
cpsw_exit(void)
1835 platform_driver_unregister(&cpsw_driver
);
1837 module_exit(cpsw_exit
);
1839 MODULE_LICENSE("GPL");
1840 MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
1841 MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
1842 MODULE_DESCRIPTION("TI CPSW Ethernet driver");