1df828598SMugunthan V N /* 2df828598SMugunthan V N * Texas Instruments Ethernet Switch Driver 3df828598SMugunthan V N * 4df828598SMugunthan V N * Copyright (C) 2012 Texas Instruments 5df828598SMugunthan V N * 6df828598SMugunthan V N * This program is free software; you can redistribute it and/or 7df828598SMugunthan V N * modify it under the terms of the GNU General Public License as 8df828598SMugunthan V N * published by the Free Software Foundation version 2. 9df828598SMugunthan V N * 10df828598SMugunthan V N * This program is distributed "as is" WITHOUT ANY WARRANTY of any 11df828598SMugunthan V N * kind, whether express or implied; without even the implied warranty 12df828598SMugunthan V N * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13df828598SMugunthan V N * GNU General Public License for more details. 14df828598SMugunthan V N */ 15df828598SMugunthan V N 16df828598SMugunthan V N #include <linux/kernel.h> 17df828598SMugunthan V N #include <linux/io.h> 18df828598SMugunthan V N #include <linux/clk.h> 19df828598SMugunthan V N #include <linux/timer.h> 20df828598SMugunthan V N #include <linux/module.h> 21df828598SMugunthan V N #include <linux/platform_device.h> 22df828598SMugunthan V N #include <linux/irqreturn.h> 23df828598SMugunthan V N #include <linux/interrupt.h> 24df828598SMugunthan V N #include <linux/if_ether.h> 25df828598SMugunthan V N #include <linux/etherdevice.h> 26df828598SMugunthan V N #include <linux/netdevice.h> 272e5b38abSRichard Cochran #include <linux/net_tstamp.h> 28df828598SMugunthan V N #include <linux/phy.h> 29df828598SMugunthan V N #include <linux/workqueue.h> 30df828598SMugunthan V N #include <linux/delay.h> 31f150bd7fSMugunthan V N #include <linux/pm_runtime.h> 322eb32b0aSMugunthan V N #include <linux/of.h> 332eb32b0aSMugunthan V N #include <linux/of_net.h> 342eb32b0aSMugunthan V N #include <linux/of_device.h> 353b72c2feSMugunthan V N #include <linux/if_vlan.h> 36df828598SMugunthan V N 37df828598SMugunthan V N #include <linux/platform_data/cpsw.h> 38df828598SMugunthan V N 39df828598SMugunthan V N #include "cpsw_ale.h" 402e5b38abSRichard Cochran #include "cpts.h" 41df828598SMugunthan V N #include "davinci_cpdma.h" 42df828598SMugunthan V N 43df828598SMugunthan V N #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \ 44df828598SMugunthan V N NETIF_MSG_DRV | NETIF_MSG_LINK | \ 45df828598SMugunthan V N NETIF_MSG_IFUP | NETIF_MSG_INTR | \ 46df828598SMugunthan V N NETIF_MSG_PROBE | NETIF_MSG_TIMER | \ 47df828598SMugunthan V N NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \ 48df828598SMugunthan V N NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \ 49df828598SMugunthan V N NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \ 50df828598SMugunthan V N NETIF_MSG_RX_STATUS) 51df828598SMugunthan V N 52df828598SMugunthan V N #define cpsw_info(priv, type, format, ...) \ 53df828598SMugunthan V N do { \ 54df828598SMugunthan V N if (netif_msg_##type(priv) && net_ratelimit()) \ 55df828598SMugunthan V N dev_info(priv->dev, format, ## __VA_ARGS__); \ 56df828598SMugunthan V N } while (0) 57df828598SMugunthan V N 58df828598SMugunthan V N #define cpsw_err(priv, type, format, ...) \ 59df828598SMugunthan V N do { \ 60df828598SMugunthan V N if (netif_msg_##type(priv) && net_ratelimit()) \ 61df828598SMugunthan V N dev_err(priv->dev, format, ## __VA_ARGS__); \ 62df828598SMugunthan V N } while (0) 63df828598SMugunthan V N 64df828598SMugunthan V N #define cpsw_dbg(priv, type, format, ...) \ 65df828598SMugunthan V N do { \ 66df828598SMugunthan V N if (netif_msg_##type(priv) && net_ratelimit()) \ 67df828598SMugunthan V N dev_dbg(priv->dev, format, ## __VA_ARGS__); \ 68df828598SMugunthan V N } while (0) 69df828598SMugunthan V N 70df828598SMugunthan V N #define cpsw_notice(priv, type, format, ...) \ 71df828598SMugunthan V N do { \ 72df828598SMugunthan V N if (netif_msg_##type(priv) && net_ratelimit()) \ 73df828598SMugunthan V N dev_notice(priv->dev, format, ## __VA_ARGS__); \ 74df828598SMugunthan V N } while (0) 75df828598SMugunthan V N 765c50a856SMugunthan V N #define ALE_ALL_PORTS 0x7 775c50a856SMugunthan V N 78df828598SMugunthan V N #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7) 79df828598SMugunthan V N #define CPSW_MINOR_VERSION(reg) (reg & 0xff) 80df828598SMugunthan V N #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f) 81df828598SMugunthan V N 82e90cfac6SRichard Cochran #define CPSW_VERSION_1 0x19010a 83e90cfac6SRichard Cochran #define CPSW_VERSION_2 0x19010c 84549985eeSRichard Cochran 85549985eeSRichard Cochran #define HOST_PORT_NUM 0 86549985eeSRichard Cochran #define SLIVER_SIZE 0x40 87549985eeSRichard Cochran 88549985eeSRichard Cochran #define CPSW1_HOST_PORT_OFFSET 0x028 89549985eeSRichard Cochran #define CPSW1_SLAVE_OFFSET 0x050 90549985eeSRichard Cochran #define CPSW1_SLAVE_SIZE 0x040 91549985eeSRichard Cochran #define CPSW1_CPDMA_OFFSET 0x100 92549985eeSRichard Cochran #define CPSW1_STATERAM_OFFSET 0x200 93549985eeSRichard Cochran #define CPSW1_CPTS_OFFSET 0x500 94549985eeSRichard Cochran #define CPSW1_ALE_OFFSET 0x600 95549985eeSRichard Cochran #define CPSW1_SLIVER_OFFSET 0x700 96549985eeSRichard Cochran 97549985eeSRichard Cochran #define CPSW2_HOST_PORT_OFFSET 0x108 98549985eeSRichard Cochran #define CPSW2_SLAVE_OFFSET 0x200 99549985eeSRichard Cochran #define CPSW2_SLAVE_SIZE 0x100 100549985eeSRichard Cochran #define CPSW2_CPDMA_OFFSET 0x800 101549985eeSRichard Cochran #define CPSW2_STATERAM_OFFSET 0xa00 102549985eeSRichard Cochran #define CPSW2_CPTS_OFFSET 0xc00 103549985eeSRichard Cochran #define CPSW2_ALE_OFFSET 0xd00 104549985eeSRichard Cochran #define CPSW2_SLIVER_OFFSET 0xd80 105549985eeSRichard Cochran #define CPSW2_BD_OFFSET 0x2000 106549985eeSRichard Cochran 107df828598SMugunthan V N #define CPDMA_RXTHRESH 0x0c0 108df828598SMugunthan V N #define CPDMA_RXFREE 0x0e0 109df828598SMugunthan V N #define CPDMA_TXHDP 0x00 110df828598SMugunthan V N #define CPDMA_RXHDP 0x20 111df828598SMugunthan V N #define CPDMA_TXCP 0x40 112df828598SMugunthan V N #define CPDMA_RXCP 0x60 113df828598SMugunthan V N 114df828598SMugunthan V N #define CPSW_POLL_WEIGHT 64 115df828598SMugunthan V N #define CPSW_MIN_PACKET_SIZE 60 116df828598SMugunthan V N #define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4) 117df828598SMugunthan V N 118df828598SMugunthan V N #define RX_PRIORITY_MAPPING 0x76543210 119df828598SMugunthan V N #define TX_PRIORITY_MAPPING 0x33221100 120df828598SMugunthan V N #define CPDMA_TX_PRIORITY_MAP 0x76543210 121df828598SMugunthan V N 1223b72c2feSMugunthan V N #define CPSW_VLAN_AWARE BIT(1) 1233b72c2feSMugunthan V N #define CPSW_ALE_VLAN_AWARE 1 1243b72c2feSMugunthan V N 125d9ba8f9eSMugunthan V N #define CPSW_FIFO_NORMAL_MODE (0 << 15) 126d9ba8f9eSMugunthan V N #define CPSW_FIFO_DUAL_MAC_MODE (1 << 15) 127d9ba8f9eSMugunthan V N #define CPSW_FIFO_RATE_LIMIT_MODE (2 << 15) 128d9ba8f9eSMugunthan V N 129ff5b8ef2SMugunthan V N #define CPSW_INTPACEEN (0x3f << 16) 130ff5b8ef2SMugunthan V N #define CPSW_INTPRESCALE_MASK (0x7FF << 0) 131ff5b8ef2SMugunthan V N #define CPSW_CMINTMAX_CNT 63 132ff5b8ef2SMugunthan V N #define CPSW_CMINTMIN_CNT 2 133ff5b8ef2SMugunthan V N #define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT) 134ff5b8ef2SMugunthan V N #define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1) 135ff5b8ef2SMugunthan V N 136df828598SMugunthan V N #define cpsw_enable_irq(priv) \ 137df828598SMugunthan V N do { \ 138df828598SMugunthan V N u32 i; \ 139df828598SMugunthan V N for (i = 0; i < priv->num_irqs; i++) \ 140df828598SMugunthan V N enable_irq(priv->irqs_table[i]); \ 141df828598SMugunthan V N } while (0); 142df828598SMugunthan V N #define cpsw_disable_irq(priv) \ 143df828598SMugunthan V N do { \ 144df828598SMugunthan V N u32 i; \ 145df828598SMugunthan V N for (i = 0; i < priv->num_irqs; i++) \ 146df828598SMugunthan V N disable_irq_nosync(priv->irqs_table[i]); \ 147df828598SMugunthan V N } while (0); 148df828598SMugunthan V N 149d3bb9c58SMugunthan V N #define cpsw_slave_index(priv) \ 150d3bb9c58SMugunthan V N ((priv->data.dual_emac) ? priv->emac_port : \ 151d3bb9c58SMugunthan V N priv->data.active_slave) 152d3bb9c58SMugunthan V N 153df828598SMugunthan V N static int debug_level; 154df828598SMugunthan V N module_param(debug_level, int, 0); 155df828598SMugunthan V N MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)"); 156df828598SMugunthan V N 157df828598SMugunthan V N static int ale_ageout = 10; 158df828598SMugunthan V N module_param(ale_ageout, int, 0); 159df828598SMugunthan V N MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)"); 160df828598SMugunthan V N 161df828598SMugunthan V N static int rx_packet_max = CPSW_MAX_PACKET_SIZE; 162df828598SMugunthan V N module_param(rx_packet_max, int, 0); 163df828598SMugunthan V N MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)"); 164df828598SMugunthan V N 165996a5c27SRichard Cochran struct cpsw_wr_regs { 166df828598SMugunthan V N u32 id_ver; 167df828598SMugunthan V N u32 soft_reset; 168df828598SMugunthan V N u32 control; 169df828598SMugunthan V N u32 int_control; 170df828598SMugunthan V N u32 rx_thresh_en; 171df828598SMugunthan V N u32 rx_en; 172df828598SMugunthan V N u32 tx_en; 173df828598SMugunthan V N u32 misc_en; 174ff5b8ef2SMugunthan V N u32 mem_allign1[8]; 175ff5b8ef2SMugunthan V N u32 rx_thresh_stat; 176ff5b8ef2SMugunthan V N u32 rx_stat; 177ff5b8ef2SMugunthan V N u32 tx_stat; 178ff5b8ef2SMugunthan V N u32 misc_stat; 179ff5b8ef2SMugunthan V N u32 mem_allign2[8]; 180ff5b8ef2SMugunthan V N u32 rx_imax; 181ff5b8ef2SMugunthan V N u32 tx_imax; 182ff5b8ef2SMugunthan V N 183df828598SMugunthan V N }; 184df828598SMugunthan V N 185996a5c27SRichard Cochran struct cpsw_ss_regs { 186df828598SMugunthan V N u32 id_ver; 187df828598SMugunthan V N u32 control; 188df828598SMugunthan V N u32 soft_reset; 189df828598SMugunthan V N u32 stat_port_en; 190df828598SMugunthan V N u32 ptype; 191bd357af2SRichard Cochran u32 soft_idle; 192bd357af2SRichard Cochran u32 thru_rate; 193bd357af2SRichard Cochran u32 gap_thresh; 194bd357af2SRichard Cochran u32 tx_start_wds; 195bd357af2SRichard Cochran u32 flow_control; 196bd357af2SRichard Cochran u32 vlan_ltype; 197bd357af2SRichard Cochran u32 ts_ltype; 198bd357af2SRichard Cochran u32 dlr_ltype; 199df828598SMugunthan V N }; 200df828598SMugunthan V N 2019750a3adSRichard Cochran /* CPSW_PORT_V1 */ 2029750a3adSRichard Cochran #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */ 2039750a3adSRichard Cochran #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */ 2049750a3adSRichard Cochran #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */ 2059750a3adSRichard Cochran #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */ 2069750a3adSRichard Cochran #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */ 2079750a3adSRichard Cochran #define CPSW1_TS_CTL 0x14 /* Time Sync Control */ 2089750a3adSRichard Cochran #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */ 2099750a3adSRichard Cochran #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */ 2109750a3adSRichard Cochran 2119750a3adSRichard Cochran /* CPSW_PORT_V2 */ 2129750a3adSRichard Cochran #define CPSW2_CONTROL 0x00 /* Control Register */ 2139750a3adSRichard Cochran #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */ 2149750a3adSRichard Cochran #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */ 2159750a3adSRichard Cochran #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */ 2169750a3adSRichard Cochran #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */ 2179750a3adSRichard Cochran #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */ 2189750a3adSRichard Cochran #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */ 2199750a3adSRichard Cochran 2209750a3adSRichard Cochran /* CPSW_PORT_V1 and V2 */ 2219750a3adSRichard Cochran #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */ 2229750a3adSRichard Cochran #define SA_HI 0x24 /* CPGMAC_SL Source Address High */ 2239750a3adSRichard Cochran #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */ 2249750a3adSRichard Cochran 2259750a3adSRichard Cochran /* CPSW_PORT_V2 only */ 2269750a3adSRichard Cochran #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */ 2279750a3adSRichard Cochran #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */ 2289750a3adSRichard Cochran #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */ 2299750a3adSRichard Cochran #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */ 2309750a3adSRichard Cochran #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */ 2319750a3adSRichard Cochran #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */ 2329750a3adSRichard Cochran #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */ 2339750a3adSRichard Cochran #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */ 2349750a3adSRichard Cochran 2359750a3adSRichard Cochran /* Bit definitions for the CPSW2_CONTROL register */ 2369750a3adSRichard Cochran #define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */ 2379750a3adSRichard Cochran #define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */ 2389750a3adSRichard Cochran #define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */ 2399750a3adSRichard Cochran #define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */ 2409750a3adSRichard Cochran #define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */ 2419750a3adSRichard Cochran #define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */ 2429750a3adSRichard Cochran #define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */ 2439750a3adSRichard Cochran #define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */ 2449750a3adSRichard Cochran #define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */ 2459750a3adSRichard Cochran #define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */ 2469750a3adSRichard Cochran #define TS_BIT8 (1<<8) /* ts_ttl_nonzero? */ 2479750a3adSRichard Cochran #define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */ 2489750a3adSRichard Cochran #define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */ 2499750a3adSRichard Cochran #define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */ 2509750a3adSRichard Cochran #define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */ 2519750a3adSRichard Cochran #define TS_RX_EN (1<<0) /* Time Sync Receive Enable */ 2529750a3adSRichard Cochran 2539750a3adSRichard Cochran #define CTRL_TS_BITS \ 2549750a3adSRichard Cochran (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 | TS_BIT8 | \ 2559750a3adSRichard Cochran TS_ANNEX_D_EN | TS_LTYPE1_EN) 2569750a3adSRichard Cochran 2579750a3adSRichard Cochran #define CTRL_ALL_TS_MASK (CTRL_TS_BITS | TS_TX_EN | TS_RX_EN) 2589750a3adSRichard Cochran #define CTRL_TX_TS_BITS (CTRL_TS_BITS | TS_TX_EN) 2599750a3adSRichard Cochran #define CTRL_RX_TS_BITS (CTRL_TS_BITS | TS_RX_EN) 2609750a3adSRichard Cochran 2619750a3adSRichard Cochran /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */ 2629750a3adSRichard Cochran #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */ 2639750a3adSRichard Cochran #define TS_SEQ_ID_OFFSET_MASK (0x3f) 2649750a3adSRichard Cochran #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */ 2659750a3adSRichard Cochran #define TS_MSG_TYPE_EN_MASK (0xffff) 2669750a3adSRichard Cochran 2679750a3adSRichard Cochran /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */ 2689750a3adSRichard Cochran #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3)) 269df828598SMugunthan V N 2702e5b38abSRichard Cochran /* Bit definitions for the CPSW1_TS_CTL register */ 2712e5b38abSRichard Cochran #define CPSW_V1_TS_RX_EN BIT(0) 2722e5b38abSRichard Cochran #define CPSW_V1_TS_TX_EN BIT(4) 2732e5b38abSRichard Cochran #define CPSW_V1_MSG_TYPE_OFS 16 2742e5b38abSRichard Cochran 2752e5b38abSRichard Cochran /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */ 2762e5b38abSRichard Cochran #define CPSW_V1_SEQ_ID_OFS_SHIFT 16 2772e5b38abSRichard Cochran 278df828598SMugunthan V N struct cpsw_host_regs { 279df828598SMugunthan V N u32 max_blks; 280df828598SMugunthan V N u32 blk_cnt; 281d9ba8f9eSMugunthan V N u32 tx_in_ctl; 282df828598SMugunthan V N u32 port_vlan; 283df828598SMugunthan V N u32 tx_pri_map; 284df828598SMugunthan V N u32 cpdma_tx_pri_map; 285df828598SMugunthan V N u32 cpdma_rx_chan_map; 286df828598SMugunthan V N }; 287df828598SMugunthan V N 288df828598SMugunthan V N struct cpsw_sliver_regs { 289df828598SMugunthan V N u32 id_ver; 290df828598SMugunthan V N u32 mac_control; 291df828598SMugunthan V N u32 mac_status; 292df828598SMugunthan V N u32 soft_reset; 293df828598SMugunthan V N u32 rx_maxlen; 294df828598SMugunthan V N u32 __reserved_0; 295df828598SMugunthan V N u32 rx_pause; 296df828598SMugunthan V N u32 tx_pause; 297df828598SMugunthan V N u32 __reserved_1; 298df828598SMugunthan V N u32 rx_pri_map; 299df828598SMugunthan V N }; 300df828598SMugunthan V N 301df828598SMugunthan V N struct cpsw_slave { 3029750a3adSRichard Cochran void __iomem *regs; 303df828598SMugunthan V N struct cpsw_sliver_regs __iomem *sliver; 304df828598SMugunthan V N int slave_num; 305df828598SMugunthan V N u32 mac_control; 306df828598SMugunthan V N struct cpsw_slave_data *data; 307df828598SMugunthan V N struct phy_device *phy; 308d9ba8f9eSMugunthan V N struct net_device *ndev; 309d9ba8f9eSMugunthan V N u32 port_vlan; 310d9ba8f9eSMugunthan V N u32 open_stat; 311df828598SMugunthan V N }; 312df828598SMugunthan V N 3139750a3adSRichard Cochran static inline u32 slave_read(struct cpsw_slave *slave, u32 offset) 3149750a3adSRichard Cochran { 3159750a3adSRichard Cochran return __raw_readl(slave->regs + offset); 3169750a3adSRichard Cochran } 3179750a3adSRichard Cochran 3189750a3adSRichard Cochran static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset) 3199750a3adSRichard Cochran { 3209750a3adSRichard Cochran __raw_writel(val, slave->regs + offset); 3219750a3adSRichard Cochran } 3229750a3adSRichard Cochran 323df828598SMugunthan V N struct cpsw_priv { 324df828598SMugunthan V N spinlock_t lock; 325df828598SMugunthan V N struct platform_device *pdev; 326df828598SMugunthan V N struct net_device *ndev; 327df828598SMugunthan V N struct resource *cpsw_res; 328a65dd5b2SRichard Cochran struct resource *cpsw_wr_res; 329df828598SMugunthan V N struct napi_struct napi; 330df828598SMugunthan V N struct device *dev; 331df828598SMugunthan V N struct cpsw_platform_data data; 332996a5c27SRichard Cochran struct cpsw_ss_regs __iomem *regs; 333996a5c27SRichard Cochran struct cpsw_wr_regs __iomem *wr_regs; 334df828598SMugunthan V N struct cpsw_host_regs __iomem *host_port_regs; 335df828598SMugunthan V N u32 msg_enable; 336e90cfac6SRichard Cochran u32 version; 337ff5b8ef2SMugunthan V N u32 coal_intvl; 338ff5b8ef2SMugunthan V N u32 bus_freq_mhz; 339df828598SMugunthan V N struct net_device_stats stats; 340df828598SMugunthan V N int rx_packet_max; 341df828598SMugunthan V N int host_port; 342df828598SMugunthan V N struct clk *clk; 343df828598SMugunthan V N u8 mac_addr[ETH_ALEN]; 344df828598SMugunthan V N struct cpsw_slave *slaves; 345df828598SMugunthan V N struct cpdma_ctlr *dma; 346df828598SMugunthan V N struct cpdma_chan *txch, *rxch; 347df828598SMugunthan V N struct cpsw_ale *ale; 348df828598SMugunthan V N /* snapshot of IRQ numbers */ 349df828598SMugunthan V N u32 irqs_table[4]; 350df828598SMugunthan V N u32 num_irqs; 351a11fbba9SSebastian Siewior bool irq_enabled; 3529232b16dSMugunthan V N struct cpts *cpts; 353d9ba8f9eSMugunthan V N u32 emac_port; 354df828598SMugunthan V N }; 355df828598SMugunthan V N 356df828598SMugunthan V N #define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi) 357df828598SMugunthan V N #define for_each_slave(priv, func, arg...) \ 358df828598SMugunthan V N do { \ 3596e6ceaedSSebastian Siewior struct cpsw_slave *slave; \ 3606e6ceaedSSebastian Siewior int n; \ 361d9ba8f9eSMugunthan V N if (priv->data.dual_emac) \ 362d9ba8f9eSMugunthan V N (func)((priv)->slaves + priv->emac_port, ##arg);\ 363d9ba8f9eSMugunthan V N else \ 3646e6ceaedSSebastian Siewior for (n = (priv)->data.slaves, \ 3656e6ceaedSSebastian Siewior slave = (priv)->slaves; \ 3666e6ceaedSSebastian Siewior n; n--) \ 3676e6ceaedSSebastian Siewior (func)(slave++, ##arg); \ 368df828598SMugunthan V N } while (0) 369d9ba8f9eSMugunthan V N #define cpsw_get_slave_ndev(priv, __slave_no__) \ 370d9ba8f9eSMugunthan V N (priv->slaves[__slave_no__].ndev) 371d9ba8f9eSMugunthan V N #define cpsw_get_slave_priv(priv, __slave_no__) \ 372d9ba8f9eSMugunthan V N ((priv->slaves[__slave_no__].ndev) ? \ 373d9ba8f9eSMugunthan V N netdev_priv(priv->slaves[__slave_no__].ndev) : NULL) \ 374d9ba8f9eSMugunthan V N 375d9ba8f9eSMugunthan V N #define cpsw_dual_emac_src_port_detect(status, priv, ndev, skb) \ 376d9ba8f9eSMugunthan V N do { \ 377d9ba8f9eSMugunthan V N if (!priv->data.dual_emac) \ 378d9ba8f9eSMugunthan V N break; \ 379d9ba8f9eSMugunthan V N if (CPDMA_RX_SOURCE_PORT(status) == 1) { \ 380d9ba8f9eSMugunthan V N ndev = cpsw_get_slave_ndev(priv, 0); \ 381d9ba8f9eSMugunthan V N priv = netdev_priv(ndev); \ 382d9ba8f9eSMugunthan V N skb->dev = ndev; \ 383d9ba8f9eSMugunthan V N } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \ 384d9ba8f9eSMugunthan V N ndev = cpsw_get_slave_ndev(priv, 1); \ 385d9ba8f9eSMugunthan V N priv = netdev_priv(ndev); \ 386d9ba8f9eSMugunthan V N skb->dev = ndev; \ 387d9ba8f9eSMugunthan V N } \ 388d9ba8f9eSMugunthan V N } while (0) 389d9ba8f9eSMugunthan V N #define cpsw_add_mcast(priv, addr) \ 390d9ba8f9eSMugunthan V N do { \ 391d9ba8f9eSMugunthan V N if (priv->data.dual_emac) { \ 392d9ba8f9eSMugunthan V N struct cpsw_slave *slave = priv->slaves + \ 393d9ba8f9eSMugunthan V N priv->emac_port; \ 394d9ba8f9eSMugunthan V N int slave_port = cpsw_get_slave_port(priv, \ 395d9ba8f9eSMugunthan V N slave->slave_num); \ 396d9ba8f9eSMugunthan V N cpsw_ale_add_mcast(priv->ale, addr, \ 397d9ba8f9eSMugunthan V N 1 << slave_port | 1 << priv->host_port, \ 398d9ba8f9eSMugunthan V N ALE_VLAN, slave->port_vlan, 0); \ 399d9ba8f9eSMugunthan V N } else { \ 400d9ba8f9eSMugunthan V N cpsw_ale_add_mcast(priv->ale, addr, \ 401d9ba8f9eSMugunthan V N ALE_ALL_PORTS << priv->host_port, \ 402d9ba8f9eSMugunthan V N 0, 0, 0); \ 403d9ba8f9eSMugunthan V N } \ 404d9ba8f9eSMugunthan V N } while (0) 405d9ba8f9eSMugunthan V N 406d9ba8f9eSMugunthan V N static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num) 407d9ba8f9eSMugunthan V N { 408d9ba8f9eSMugunthan V N if (priv->host_port == 0) 409d9ba8f9eSMugunthan V N return slave_num + 1; 410d9ba8f9eSMugunthan V N else 411d9ba8f9eSMugunthan V N return slave_num; 412d9ba8f9eSMugunthan V N } 413df828598SMugunthan V N 4145c50a856SMugunthan V N static void cpsw_ndo_set_rx_mode(struct net_device *ndev) 4155c50a856SMugunthan V N { 4165c50a856SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 4175c50a856SMugunthan V N 4185c50a856SMugunthan V N if (ndev->flags & IFF_PROMISC) { 4195c50a856SMugunthan V N /* Enable promiscuous mode */ 4205c50a856SMugunthan V N dev_err(priv->dev, "Ignoring Promiscuous mode\n"); 4215c50a856SMugunthan V N return; 4225c50a856SMugunthan V N } 4235c50a856SMugunthan V N 4245c50a856SMugunthan V N /* Clear all mcast from ALE */ 4255c50a856SMugunthan V N cpsw_ale_flush_multicast(priv->ale, ALE_ALL_PORTS << priv->host_port); 4265c50a856SMugunthan V N 4275c50a856SMugunthan V N if (!netdev_mc_empty(ndev)) { 4285c50a856SMugunthan V N struct netdev_hw_addr *ha; 4295c50a856SMugunthan V N 4305c50a856SMugunthan V N /* program multicast address list into ALE register */ 4315c50a856SMugunthan V N netdev_for_each_mc_addr(ha, ndev) { 432d9ba8f9eSMugunthan V N cpsw_add_mcast(priv, (u8 *)ha->addr); 4335c50a856SMugunthan V N } 4345c50a856SMugunthan V N } 4355c50a856SMugunthan V N } 4365c50a856SMugunthan V N 437df828598SMugunthan V N static void cpsw_intr_enable(struct cpsw_priv *priv) 438df828598SMugunthan V N { 439996a5c27SRichard Cochran __raw_writel(0xFF, &priv->wr_regs->tx_en); 440996a5c27SRichard Cochran __raw_writel(0xFF, &priv->wr_regs->rx_en); 441df828598SMugunthan V N 442df828598SMugunthan V N cpdma_ctlr_int_ctrl(priv->dma, true); 443df828598SMugunthan V N return; 444df828598SMugunthan V N } 445df828598SMugunthan V N 446df828598SMugunthan V N static void cpsw_intr_disable(struct cpsw_priv *priv) 447df828598SMugunthan V N { 448996a5c27SRichard Cochran __raw_writel(0, &priv->wr_regs->tx_en); 449996a5c27SRichard Cochran __raw_writel(0, &priv->wr_regs->rx_en); 450df828598SMugunthan V N 451df828598SMugunthan V N cpdma_ctlr_int_ctrl(priv->dma, false); 452df828598SMugunthan V N return; 453df828598SMugunthan V N } 454df828598SMugunthan V N 455df828598SMugunthan V N void cpsw_tx_handler(void *token, int len, int status) 456df828598SMugunthan V N { 457df828598SMugunthan V N struct sk_buff *skb = token; 458df828598SMugunthan V N struct net_device *ndev = skb->dev; 459df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 460df828598SMugunthan V N 461fae50823SMugunthan V N /* Check whether the queue is stopped due to stalled tx dma, if the 462fae50823SMugunthan V N * queue is stopped then start the queue as we have free desc for tx 463fae50823SMugunthan V N */ 464df828598SMugunthan V N if (unlikely(netif_queue_stopped(ndev))) 465b56d6b3fSMugunthan V N netif_wake_queue(ndev); 4669232b16dSMugunthan V N cpts_tx_timestamp(priv->cpts, skb); 467df828598SMugunthan V N priv->stats.tx_packets++; 468df828598SMugunthan V N priv->stats.tx_bytes += len; 469df828598SMugunthan V N dev_kfree_skb_any(skb); 470df828598SMugunthan V N } 471df828598SMugunthan V N 472df828598SMugunthan V N void cpsw_rx_handler(void *token, int len, int status) 473df828598SMugunthan V N { 474df828598SMugunthan V N struct sk_buff *skb = token; 475b4727e69SSebastian Siewior struct sk_buff *new_skb; 476df828598SMugunthan V N struct net_device *ndev = skb->dev; 477df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 478df828598SMugunthan V N int ret = 0; 479df828598SMugunthan V N 480d9ba8f9eSMugunthan V N cpsw_dual_emac_src_port_detect(status, priv, ndev, skb); 481d9ba8f9eSMugunthan V N 482b4727e69SSebastian Siewior if (unlikely(status < 0)) { 483b4727e69SSebastian Siewior /* the interface is going down, skbs are purged */ 484df828598SMugunthan V N dev_kfree_skb_any(skb); 485df828598SMugunthan V N return; 486df828598SMugunthan V N } 487b4727e69SSebastian Siewior 488b4727e69SSebastian Siewior new_skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max); 489b4727e69SSebastian Siewior if (new_skb) { 490df828598SMugunthan V N skb_put(skb, len); 4919232b16dSMugunthan V N cpts_rx_timestamp(priv->cpts, skb); 492df828598SMugunthan V N skb->protocol = eth_type_trans(skb, ndev); 493df828598SMugunthan V N netif_receive_skb(skb); 494df828598SMugunthan V N priv->stats.rx_bytes += len; 495df828598SMugunthan V N priv->stats.rx_packets++; 496b4727e69SSebastian Siewior } else { 497b4727e69SSebastian Siewior priv->stats.rx_dropped++; 498b4727e69SSebastian Siewior new_skb = skb; 499df828598SMugunthan V N } 500df828598SMugunthan V N 501b4727e69SSebastian Siewior ret = cpdma_chan_submit(priv->rxch, new_skb, new_skb->data, 502b4727e69SSebastian Siewior skb_tailroom(new_skb), 0); 503b4727e69SSebastian Siewior if (WARN_ON(ret < 0)) 504b4727e69SSebastian Siewior dev_kfree_skb_any(new_skb); 505df828598SMugunthan V N } 506df828598SMugunthan V N 507df828598SMugunthan V N static irqreturn_t cpsw_interrupt(int irq, void *dev_id) 508df828598SMugunthan V N { 509df828598SMugunthan V N struct cpsw_priv *priv = dev_id; 510fd51cf19SSebastian Siewior u32 rx, tx, rx_thresh; 511df828598SMugunthan V N 512fd51cf19SSebastian Siewior rx_thresh = __raw_readl(&priv->wr_regs->rx_thresh_stat); 513fd51cf19SSebastian Siewior rx = __raw_readl(&priv->wr_regs->rx_stat); 514fd51cf19SSebastian Siewior tx = __raw_readl(&priv->wr_regs->tx_stat); 515fd51cf19SSebastian Siewior if (!rx_thresh && !rx && !tx) 516fd51cf19SSebastian Siewior return IRQ_NONE; 517fd51cf19SSebastian Siewior 518df828598SMugunthan V N cpsw_intr_disable(priv); 519a11fbba9SSebastian Siewior if (priv->irq_enabled == true) { 520df828598SMugunthan V N cpsw_disable_irq(priv); 521a11fbba9SSebastian Siewior priv->irq_enabled = false; 522a11fbba9SSebastian Siewior } 523fd51cf19SSebastian Siewior 524fd51cf19SSebastian Siewior if (netif_running(priv->ndev)) { 525df828598SMugunthan V N napi_schedule(&priv->napi); 526df828598SMugunthan V N return IRQ_HANDLED; 527df828598SMugunthan V N } 528df828598SMugunthan V N 529fd51cf19SSebastian Siewior priv = cpsw_get_slave_priv(priv, 1); 530fd51cf19SSebastian Siewior if (!priv) 531fd51cf19SSebastian Siewior return IRQ_NONE; 532fd51cf19SSebastian Siewior 533fd51cf19SSebastian Siewior if (netif_running(priv->ndev)) { 534fd51cf19SSebastian Siewior napi_schedule(&priv->napi); 535fd51cf19SSebastian Siewior return IRQ_HANDLED; 536fd51cf19SSebastian Siewior } 537fd51cf19SSebastian Siewior return IRQ_NONE; 538fd51cf19SSebastian Siewior } 539fd51cf19SSebastian Siewior 540df828598SMugunthan V N static int cpsw_poll(struct napi_struct *napi, int budget) 541df828598SMugunthan V N { 542df828598SMugunthan V N struct cpsw_priv *priv = napi_to_priv(napi); 543df828598SMugunthan V N int num_tx, num_rx; 544df828598SMugunthan V N 545df828598SMugunthan V N num_tx = cpdma_chan_process(priv->txch, 128); 546510a1e72SMugunthan V N if (num_tx) 547510a1e72SMugunthan V N cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX); 548510a1e72SMugunthan V N 549df828598SMugunthan V N num_rx = cpdma_chan_process(priv->rxch, budget); 550510a1e72SMugunthan V N if (num_rx < budget) { 551a11fbba9SSebastian Siewior struct cpsw_priv *prim_cpsw; 552a11fbba9SSebastian Siewior 553510a1e72SMugunthan V N napi_complete(napi); 554510a1e72SMugunthan V N cpsw_intr_enable(priv); 555510a1e72SMugunthan V N cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX); 556a11fbba9SSebastian Siewior prim_cpsw = cpsw_get_slave_priv(priv, 0); 557a11fbba9SSebastian Siewior if (prim_cpsw->irq_enabled == false) { 558a11fbba9SSebastian Siewior prim_cpsw->irq_enabled = true; 559af5c6df7SMugunthan V N cpsw_enable_irq(priv); 560a11fbba9SSebastian Siewior } 561510a1e72SMugunthan V N } 562df828598SMugunthan V N 563df828598SMugunthan V N if (num_rx || num_tx) 564df828598SMugunthan V N cpsw_dbg(priv, intr, "poll %d rx, %d tx pkts\n", 565df828598SMugunthan V N num_rx, num_tx); 566df828598SMugunthan V N 567df828598SMugunthan V N return num_rx; 568df828598SMugunthan V N } 569df828598SMugunthan V N 570df828598SMugunthan V N static inline void soft_reset(const char *module, void __iomem *reg) 571df828598SMugunthan V N { 572df828598SMugunthan V N unsigned long timeout = jiffies + HZ; 573df828598SMugunthan V N 574df828598SMugunthan V N __raw_writel(1, reg); 575df828598SMugunthan V N do { 576df828598SMugunthan V N cpu_relax(); 577df828598SMugunthan V N } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies)); 578df828598SMugunthan V N 579df828598SMugunthan V N WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module); 580df828598SMugunthan V N } 581df828598SMugunthan V N 582df828598SMugunthan V N #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \ 583df828598SMugunthan V N ((mac)[2] << 16) | ((mac)[3] << 24)) 584df828598SMugunthan V N #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8)) 585df828598SMugunthan V N 586df828598SMugunthan V N static void cpsw_set_slave_mac(struct cpsw_slave *slave, 587df828598SMugunthan V N struct cpsw_priv *priv) 588df828598SMugunthan V N { 5899750a3adSRichard Cochran slave_write(slave, mac_hi(priv->mac_addr), SA_HI); 5909750a3adSRichard Cochran slave_write(slave, mac_lo(priv->mac_addr), SA_LO); 591df828598SMugunthan V N } 592df828598SMugunthan V N 593df828598SMugunthan V N static void _cpsw_adjust_link(struct cpsw_slave *slave, 594df828598SMugunthan V N struct cpsw_priv *priv, bool *link) 595df828598SMugunthan V N { 596df828598SMugunthan V N struct phy_device *phy = slave->phy; 597df828598SMugunthan V N u32 mac_control = 0; 598df828598SMugunthan V N u32 slave_port; 599df828598SMugunthan V N 600df828598SMugunthan V N if (!phy) 601df828598SMugunthan V N return; 602df828598SMugunthan V N 603df828598SMugunthan V N slave_port = cpsw_get_slave_port(priv, slave->slave_num); 604df828598SMugunthan V N 605df828598SMugunthan V N if (phy->link) { 606df828598SMugunthan V N mac_control = priv->data.mac_control; 607df828598SMugunthan V N 608df828598SMugunthan V N /* enable forwarding */ 609df828598SMugunthan V N cpsw_ale_control_set(priv->ale, slave_port, 610df828598SMugunthan V N ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 611df828598SMugunthan V N 612df828598SMugunthan V N if (phy->speed == 1000) 613df828598SMugunthan V N mac_control |= BIT(7); /* GIGABITEN */ 614df828598SMugunthan V N if (phy->duplex) 615df828598SMugunthan V N mac_control |= BIT(0); /* FULLDUPLEXEN */ 616342b7b74SDaniel Mack 617342b7b74SDaniel Mack /* set speed_in input in case RMII mode is used in 100Mbps */ 618342b7b74SDaniel Mack if (phy->speed == 100) 619342b7b74SDaniel Mack mac_control |= BIT(15); 620342b7b74SDaniel Mack 621df828598SMugunthan V N *link = true; 622df828598SMugunthan V N } else { 623df828598SMugunthan V N mac_control = 0; 624df828598SMugunthan V N /* disable forwarding */ 625df828598SMugunthan V N cpsw_ale_control_set(priv->ale, slave_port, 626df828598SMugunthan V N ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 627df828598SMugunthan V N } 628df828598SMugunthan V N 629df828598SMugunthan V N if (mac_control != slave->mac_control) { 630df828598SMugunthan V N phy_print_status(phy); 631df828598SMugunthan V N __raw_writel(mac_control, &slave->sliver->mac_control); 632df828598SMugunthan V N } 633df828598SMugunthan V N 634df828598SMugunthan V N slave->mac_control = mac_control; 635df828598SMugunthan V N } 636df828598SMugunthan V N 637df828598SMugunthan V N static void cpsw_adjust_link(struct net_device *ndev) 638df828598SMugunthan V N { 639df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 640df828598SMugunthan V N bool link = false; 641df828598SMugunthan V N 642df828598SMugunthan V N for_each_slave(priv, _cpsw_adjust_link, priv, &link); 643df828598SMugunthan V N 644df828598SMugunthan V N if (link) { 645df828598SMugunthan V N netif_carrier_on(ndev); 646df828598SMugunthan V N if (netif_running(ndev)) 647df828598SMugunthan V N netif_wake_queue(ndev); 648df828598SMugunthan V N } else { 649df828598SMugunthan V N netif_carrier_off(ndev); 650df828598SMugunthan V N netif_stop_queue(ndev); 651df828598SMugunthan V N } 652df828598SMugunthan V N } 653df828598SMugunthan V N 654ff5b8ef2SMugunthan V N static int cpsw_get_coalesce(struct net_device *ndev, 655ff5b8ef2SMugunthan V N struct ethtool_coalesce *coal) 656ff5b8ef2SMugunthan V N { 657ff5b8ef2SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 658ff5b8ef2SMugunthan V N 659ff5b8ef2SMugunthan V N coal->rx_coalesce_usecs = priv->coal_intvl; 660ff5b8ef2SMugunthan V N return 0; 661ff5b8ef2SMugunthan V N } 662ff5b8ef2SMugunthan V N 663ff5b8ef2SMugunthan V N static int cpsw_set_coalesce(struct net_device *ndev, 664ff5b8ef2SMugunthan V N struct ethtool_coalesce *coal) 665ff5b8ef2SMugunthan V N { 666ff5b8ef2SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 667ff5b8ef2SMugunthan V N u32 int_ctrl; 668ff5b8ef2SMugunthan V N u32 num_interrupts = 0; 669ff5b8ef2SMugunthan V N u32 prescale = 0; 670ff5b8ef2SMugunthan V N u32 addnl_dvdr = 1; 671ff5b8ef2SMugunthan V N u32 coal_intvl = 0; 672ff5b8ef2SMugunthan V N 673ff5b8ef2SMugunthan V N if (!coal->rx_coalesce_usecs) 674ff5b8ef2SMugunthan V N return -EINVAL; 675ff5b8ef2SMugunthan V N 676ff5b8ef2SMugunthan V N coal_intvl = coal->rx_coalesce_usecs; 677ff5b8ef2SMugunthan V N 678ff5b8ef2SMugunthan V N int_ctrl = readl(&priv->wr_regs->int_control); 679ff5b8ef2SMugunthan V N prescale = priv->bus_freq_mhz * 4; 680ff5b8ef2SMugunthan V N 681ff5b8ef2SMugunthan V N if (coal_intvl < CPSW_CMINTMIN_INTVL) 682ff5b8ef2SMugunthan V N coal_intvl = CPSW_CMINTMIN_INTVL; 683ff5b8ef2SMugunthan V N 684ff5b8ef2SMugunthan V N if (coal_intvl > CPSW_CMINTMAX_INTVL) { 685ff5b8ef2SMugunthan V N /* Interrupt pacer works with 4us Pulse, we can 686ff5b8ef2SMugunthan V N * throttle further by dilating the 4us pulse. 687ff5b8ef2SMugunthan V N */ 688ff5b8ef2SMugunthan V N addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale; 689ff5b8ef2SMugunthan V N 690ff5b8ef2SMugunthan V N if (addnl_dvdr > 1) { 691ff5b8ef2SMugunthan V N prescale *= addnl_dvdr; 692ff5b8ef2SMugunthan V N if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr)) 693ff5b8ef2SMugunthan V N coal_intvl = (CPSW_CMINTMAX_INTVL 694ff5b8ef2SMugunthan V N * addnl_dvdr); 695ff5b8ef2SMugunthan V N } else { 696ff5b8ef2SMugunthan V N addnl_dvdr = 1; 697ff5b8ef2SMugunthan V N coal_intvl = CPSW_CMINTMAX_INTVL; 698ff5b8ef2SMugunthan V N } 699ff5b8ef2SMugunthan V N } 700ff5b8ef2SMugunthan V N 701ff5b8ef2SMugunthan V N num_interrupts = (1000 * addnl_dvdr) / coal_intvl; 702ff5b8ef2SMugunthan V N writel(num_interrupts, &priv->wr_regs->rx_imax); 703ff5b8ef2SMugunthan V N writel(num_interrupts, &priv->wr_regs->tx_imax); 704ff5b8ef2SMugunthan V N 705ff5b8ef2SMugunthan V N int_ctrl |= CPSW_INTPACEEN; 706ff5b8ef2SMugunthan V N int_ctrl &= (~CPSW_INTPRESCALE_MASK); 707ff5b8ef2SMugunthan V N int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK); 708ff5b8ef2SMugunthan V N writel(int_ctrl, &priv->wr_regs->int_control); 709ff5b8ef2SMugunthan V N 710ff5b8ef2SMugunthan V N cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl); 711ff5b8ef2SMugunthan V N if (priv->data.dual_emac) { 712ff5b8ef2SMugunthan V N int i; 713ff5b8ef2SMugunthan V N 714ff5b8ef2SMugunthan V N for (i = 0; i < priv->data.slaves; i++) { 715ff5b8ef2SMugunthan V N priv = netdev_priv(priv->slaves[i].ndev); 716ff5b8ef2SMugunthan V N priv->coal_intvl = coal_intvl; 717ff5b8ef2SMugunthan V N } 718ff5b8ef2SMugunthan V N } else { 719ff5b8ef2SMugunthan V N priv->coal_intvl = coal_intvl; 720ff5b8ef2SMugunthan V N } 721ff5b8ef2SMugunthan V N 722ff5b8ef2SMugunthan V N return 0; 723ff5b8ef2SMugunthan V N } 724ff5b8ef2SMugunthan V N 725df828598SMugunthan V N static inline int __show_stat(char *buf, int maxlen, const char *name, u32 val) 726df828598SMugunthan V N { 727df828598SMugunthan V N static char *leader = "........................................"; 728df828598SMugunthan V N 729df828598SMugunthan V N if (!val) 730df828598SMugunthan V N return 0; 731df828598SMugunthan V N else 732df828598SMugunthan V N return snprintf(buf, maxlen, "%s %s %10d\n", name, 733df828598SMugunthan V N leader + strlen(name), val); 734df828598SMugunthan V N } 735df828598SMugunthan V N 736d9ba8f9eSMugunthan V N static int cpsw_common_res_usage_state(struct cpsw_priv *priv) 737d9ba8f9eSMugunthan V N { 738d9ba8f9eSMugunthan V N u32 i; 739d9ba8f9eSMugunthan V N u32 usage_count = 0; 740d9ba8f9eSMugunthan V N 741d9ba8f9eSMugunthan V N if (!priv->data.dual_emac) 742d9ba8f9eSMugunthan V N return 0; 743d9ba8f9eSMugunthan V N 744d9ba8f9eSMugunthan V N for (i = 0; i < priv->data.slaves; i++) 745d9ba8f9eSMugunthan V N if (priv->slaves[i].open_stat) 746d9ba8f9eSMugunthan V N usage_count++; 747d9ba8f9eSMugunthan V N 748d9ba8f9eSMugunthan V N return usage_count; 749d9ba8f9eSMugunthan V N } 750d9ba8f9eSMugunthan V N 751d9ba8f9eSMugunthan V N static inline int cpsw_tx_packet_submit(struct net_device *ndev, 752d9ba8f9eSMugunthan V N struct cpsw_priv *priv, struct sk_buff *skb) 753d9ba8f9eSMugunthan V N { 754d9ba8f9eSMugunthan V N if (!priv->data.dual_emac) 755d9ba8f9eSMugunthan V N return cpdma_chan_submit(priv->txch, skb, skb->data, 756aef614e1SSebastian Siewior skb->len, 0); 757d9ba8f9eSMugunthan V N 758d9ba8f9eSMugunthan V N if (ndev == cpsw_get_slave_ndev(priv, 0)) 759d9ba8f9eSMugunthan V N return cpdma_chan_submit(priv->txch, skb, skb->data, 760aef614e1SSebastian Siewior skb->len, 1); 761d9ba8f9eSMugunthan V N else 762d9ba8f9eSMugunthan V N return cpdma_chan_submit(priv->txch, skb, skb->data, 763aef614e1SSebastian Siewior skb->len, 2); 764d9ba8f9eSMugunthan V N } 765d9ba8f9eSMugunthan V N 766d9ba8f9eSMugunthan V N static inline void cpsw_add_dual_emac_def_ale_entries( 767d9ba8f9eSMugunthan V N struct cpsw_priv *priv, struct cpsw_slave *slave, 768d9ba8f9eSMugunthan V N u32 slave_port) 769d9ba8f9eSMugunthan V N { 770d9ba8f9eSMugunthan V N u32 port_mask = 1 << slave_port | 1 << priv->host_port; 771d9ba8f9eSMugunthan V N 772d9ba8f9eSMugunthan V N if (priv->version == CPSW_VERSION_1) 773d9ba8f9eSMugunthan V N slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN); 774d9ba8f9eSMugunthan V N else 775d9ba8f9eSMugunthan V N slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN); 776d9ba8f9eSMugunthan V N cpsw_ale_add_vlan(priv->ale, slave->port_vlan, port_mask, 777d9ba8f9eSMugunthan V N port_mask, port_mask, 0); 778d9ba8f9eSMugunthan V N cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast, 779d9ba8f9eSMugunthan V N port_mask, ALE_VLAN, slave->port_vlan, 0); 780d9ba8f9eSMugunthan V N cpsw_ale_add_ucast(priv->ale, priv->mac_addr, 781d9ba8f9eSMugunthan V N priv->host_port, ALE_VLAN, slave->port_vlan); 782d9ba8f9eSMugunthan V N } 783d9ba8f9eSMugunthan V N 784df828598SMugunthan V N static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv) 785df828598SMugunthan V N { 786df828598SMugunthan V N char name[32]; 787df828598SMugunthan V N u32 slave_port; 788df828598SMugunthan V N 789df828598SMugunthan V N sprintf(name, "slave-%d", slave->slave_num); 790df828598SMugunthan V N 791df828598SMugunthan V N soft_reset(name, &slave->sliver->soft_reset); 792df828598SMugunthan V N 793df828598SMugunthan V N /* setup priority mapping */ 794df828598SMugunthan V N __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map); 7959750a3adSRichard Cochran 7969750a3adSRichard Cochran switch (priv->version) { 7979750a3adSRichard Cochran case CPSW_VERSION_1: 7989750a3adSRichard Cochran slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP); 7999750a3adSRichard Cochran break; 8009750a3adSRichard Cochran case CPSW_VERSION_2: 8019750a3adSRichard Cochran slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP); 8029750a3adSRichard Cochran break; 8039750a3adSRichard Cochran } 804df828598SMugunthan V N 805df828598SMugunthan V N /* setup max packet size, and mac address */ 806df828598SMugunthan V N __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen); 807df828598SMugunthan V N cpsw_set_slave_mac(slave, priv); 808df828598SMugunthan V N 809df828598SMugunthan V N slave->mac_control = 0; /* no link yet */ 810df828598SMugunthan V N 811df828598SMugunthan V N slave_port = cpsw_get_slave_port(priv, slave->slave_num); 812df828598SMugunthan V N 813d9ba8f9eSMugunthan V N if (priv->data.dual_emac) 814d9ba8f9eSMugunthan V N cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port); 815d9ba8f9eSMugunthan V N else 816df828598SMugunthan V N cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast, 817e11b220fSMugunthan V N 1 << slave_port, 0, 0, ALE_MCAST_FWD_2); 818df828598SMugunthan V N 819df828598SMugunthan V N slave->phy = phy_connect(priv->ndev, slave->data->phy_id, 820f9a8f83bSFlorian Fainelli &cpsw_adjust_link, slave->data->phy_if); 821df828598SMugunthan V N if (IS_ERR(slave->phy)) { 822df828598SMugunthan V N dev_err(priv->dev, "phy %s not found on slave %d\n", 823df828598SMugunthan V N slave->data->phy_id, slave->slave_num); 824df828598SMugunthan V N slave->phy = NULL; 825df828598SMugunthan V N } else { 826df828598SMugunthan V N dev_info(priv->dev, "phy found : id is : 0x%x\n", 827df828598SMugunthan V N slave->phy->phy_id); 828df828598SMugunthan V N phy_start(slave->phy); 829df828598SMugunthan V N } 830df828598SMugunthan V N } 831df828598SMugunthan V N 8323b72c2feSMugunthan V N static inline void cpsw_add_default_vlan(struct cpsw_priv *priv) 8333b72c2feSMugunthan V N { 8343b72c2feSMugunthan V N const int vlan = priv->data.default_vlan; 8353b72c2feSMugunthan V N const int port = priv->host_port; 8363b72c2feSMugunthan V N u32 reg; 8373b72c2feSMugunthan V N int i; 8383b72c2feSMugunthan V N 8393b72c2feSMugunthan V N reg = (priv->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN : 8403b72c2feSMugunthan V N CPSW2_PORT_VLAN; 8413b72c2feSMugunthan V N 8423b72c2feSMugunthan V N writel(vlan, &priv->host_port_regs->port_vlan); 8433b72c2feSMugunthan V N 8440237c110SDaniel Mack for (i = 0; i < priv->data.slaves; i++) 8453b72c2feSMugunthan V N slave_write(priv->slaves + i, vlan, reg); 8463b72c2feSMugunthan V N 8473b72c2feSMugunthan V N cpsw_ale_add_vlan(priv->ale, vlan, ALE_ALL_PORTS << port, 8483b72c2feSMugunthan V N ALE_ALL_PORTS << port, ALE_ALL_PORTS << port, 8493b72c2feSMugunthan V N (ALE_PORT_1 | ALE_PORT_2) << port); 8503b72c2feSMugunthan V N } 8513b72c2feSMugunthan V N 852df828598SMugunthan V N static void cpsw_init_host_port(struct cpsw_priv *priv) 853df828598SMugunthan V N { 8543b72c2feSMugunthan V N u32 control_reg; 855d9ba8f9eSMugunthan V N u32 fifo_mode; 8563b72c2feSMugunthan V N 857df828598SMugunthan V N /* soft reset the controller and initialize ale */ 858df828598SMugunthan V N soft_reset("cpsw", &priv->regs->soft_reset); 859df828598SMugunthan V N cpsw_ale_start(priv->ale); 860df828598SMugunthan V N 861df828598SMugunthan V N /* switch to vlan unaware mode */ 8623b72c2feSMugunthan V N cpsw_ale_control_set(priv->ale, priv->host_port, ALE_VLAN_AWARE, 8633b72c2feSMugunthan V N CPSW_ALE_VLAN_AWARE); 8643b72c2feSMugunthan V N control_reg = readl(&priv->regs->control); 8653b72c2feSMugunthan V N control_reg |= CPSW_VLAN_AWARE; 8663b72c2feSMugunthan V N writel(control_reg, &priv->regs->control); 867d9ba8f9eSMugunthan V N fifo_mode = (priv->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE : 868d9ba8f9eSMugunthan V N CPSW_FIFO_NORMAL_MODE; 869d9ba8f9eSMugunthan V N writel(fifo_mode, &priv->host_port_regs->tx_in_ctl); 870df828598SMugunthan V N 871df828598SMugunthan V N /* setup host port priority mapping */ 872df828598SMugunthan V N __raw_writel(CPDMA_TX_PRIORITY_MAP, 873df828598SMugunthan V N &priv->host_port_regs->cpdma_tx_pri_map); 874df828598SMugunthan V N __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map); 875df828598SMugunthan V N 876df828598SMugunthan V N cpsw_ale_control_set(priv->ale, priv->host_port, 877df828598SMugunthan V N ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 878df828598SMugunthan V N 879d9ba8f9eSMugunthan V N if (!priv->data.dual_emac) { 880d9ba8f9eSMugunthan V N cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port, 881d9ba8f9eSMugunthan V N 0, 0); 882df828598SMugunthan V N cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast, 883e11b220fSMugunthan V N 1 << priv->host_port, 0, 0, ALE_MCAST_FWD_2); 884df828598SMugunthan V N } 885d9ba8f9eSMugunthan V N } 886df828598SMugunthan V N 887aacebbf8SSebastian Siewior static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv) 888aacebbf8SSebastian Siewior { 889aacebbf8SSebastian Siewior if (!slave->phy) 890aacebbf8SSebastian Siewior return; 891aacebbf8SSebastian Siewior phy_stop(slave->phy); 892aacebbf8SSebastian Siewior phy_disconnect(slave->phy); 893aacebbf8SSebastian Siewior slave->phy = NULL; 894aacebbf8SSebastian Siewior } 895aacebbf8SSebastian Siewior 896df828598SMugunthan V N static int cpsw_ndo_open(struct net_device *ndev) 897df828598SMugunthan V N { 898df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 899a11fbba9SSebastian Siewior struct cpsw_priv *prim_cpsw; 900df828598SMugunthan V N int i, ret; 901df828598SMugunthan V N u32 reg; 902df828598SMugunthan V N 903d9ba8f9eSMugunthan V N if (!cpsw_common_res_usage_state(priv)) 904df828598SMugunthan V N cpsw_intr_disable(priv); 905df828598SMugunthan V N netif_carrier_off(ndev); 906df828598SMugunthan V N 907f150bd7fSMugunthan V N pm_runtime_get_sync(&priv->pdev->dev); 908df828598SMugunthan V N 909549985eeSRichard Cochran reg = priv->version; 910df828598SMugunthan V N 911df828598SMugunthan V N dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n", 912df828598SMugunthan V N CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg), 913df828598SMugunthan V N CPSW_RTL_VERSION(reg)); 914df828598SMugunthan V N 915df828598SMugunthan V N /* initialize host and slave ports */ 916d9ba8f9eSMugunthan V N if (!cpsw_common_res_usage_state(priv)) 917df828598SMugunthan V N cpsw_init_host_port(priv); 918df828598SMugunthan V N for_each_slave(priv, cpsw_slave_open, priv); 919df828598SMugunthan V N 9203b72c2feSMugunthan V N /* Add default VLAN */ 921d9ba8f9eSMugunthan V N if (!priv->data.dual_emac) 9223b72c2feSMugunthan V N cpsw_add_default_vlan(priv); 9233b72c2feSMugunthan V N 924d9ba8f9eSMugunthan V N if (!cpsw_common_res_usage_state(priv)) { 925df828598SMugunthan V N /* setup tx dma to fixed prio and zero offset */ 926df828598SMugunthan V N cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1); 927df828598SMugunthan V N cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0); 928df828598SMugunthan V N 929d9ba8f9eSMugunthan V N /* disable priority elevation */ 930df828598SMugunthan V N __raw_writel(0, &priv->regs->ptype); 931df828598SMugunthan V N 932d9ba8f9eSMugunthan V N /* enable statistics collection only on all ports */ 933df828598SMugunthan V N __raw_writel(0x7, &priv->regs->stat_port_en); 934df828598SMugunthan V N 935df828598SMugunthan V N if (WARN_ON(!priv->data.rx_descs)) 936df828598SMugunthan V N priv->data.rx_descs = 128; 937df828598SMugunthan V N 938df828598SMugunthan V N for (i = 0; i < priv->data.rx_descs; i++) { 939df828598SMugunthan V N struct sk_buff *skb; 940df828598SMugunthan V N 941df828598SMugunthan V N ret = -ENOMEM; 942aacebbf8SSebastian Siewior skb = __netdev_alloc_skb_ip_align(priv->ndev, 943aacebbf8SSebastian Siewior priv->rx_packet_max, GFP_KERNEL); 944df828598SMugunthan V N if (!skb) 945aacebbf8SSebastian Siewior goto err_cleanup; 946df828598SMugunthan V N ret = cpdma_chan_submit(priv->rxch, skb, skb->data, 947aef614e1SSebastian Siewior skb_tailroom(skb), 0); 948aacebbf8SSebastian Siewior if (ret < 0) { 949aacebbf8SSebastian Siewior kfree_skb(skb); 950aacebbf8SSebastian Siewior goto err_cleanup; 951aacebbf8SSebastian Siewior } 952df828598SMugunthan V N } 953d9ba8f9eSMugunthan V N /* continue even if we didn't manage to submit all 954d9ba8f9eSMugunthan V N * receive descs 955d9ba8f9eSMugunthan V N */ 956df828598SMugunthan V N cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i); 957d9ba8f9eSMugunthan V N } 958df828598SMugunthan V N 959ff5b8ef2SMugunthan V N /* Enable Interrupt pacing if configured */ 960ff5b8ef2SMugunthan V N if (priv->coal_intvl != 0) { 961ff5b8ef2SMugunthan V N struct ethtool_coalesce coal; 962ff5b8ef2SMugunthan V N 963ff5b8ef2SMugunthan V N coal.rx_coalesce_usecs = (priv->coal_intvl << 4); 964ff5b8ef2SMugunthan V N cpsw_set_coalesce(ndev, &coal); 965ff5b8ef2SMugunthan V N } 966ff5b8ef2SMugunthan V N 967a11fbba9SSebastian Siewior prim_cpsw = cpsw_get_slave_priv(priv, 0); 968a11fbba9SSebastian Siewior if (prim_cpsw->irq_enabled == false) { 969a11fbba9SSebastian Siewior if ((priv == prim_cpsw) || !netif_running(prim_cpsw->ndev)) { 970a11fbba9SSebastian Siewior prim_cpsw->irq_enabled = true; 971a11fbba9SSebastian Siewior cpsw_enable_irq(prim_cpsw); 972a11fbba9SSebastian Siewior } 973a11fbba9SSebastian Siewior } 974a11fbba9SSebastian Siewior 975df828598SMugunthan V N cpdma_ctlr_start(priv->dma); 976df828598SMugunthan V N cpsw_intr_enable(priv); 977df828598SMugunthan V N napi_enable(&priv->napi); 978510a1e72SMugunthan V N cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX); 979510a1e72SMugunthan V N cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX); 980df828598SMugunthan V N 981d9ba8f9eSMugunthan V N if (priv->data.dual_emac) 982d9ba8f9eSMugunthan V N priv->slaves[priv->emac_port].open_stat = true; 983df828598SMugunthan V N return 0; 984df828598SMugunthan V N 985aacebbf8SSebastian Siewior err_cleanup: 986aacebbf8SSebastian Siewior cpdma_ctlr_stop(priv->dma); 987aacebbf8SSebastian Siewior for_each_slave(priv, cpsw_slave_stop, priv); 988aacebbf8SSebastian Siewior pm_runtime_put_sync(&priv->pdev->dev); 989aacebbf8SSebastian Siewior netif_carrier_off(priv->ndev); 990aacebbf8SSebastian Siewior return ret; 991df828598SMugunthan V N } 992df828598SMugunthan V N 993df828598SMugunthan V N static int cpsw_ndo_stop(struct net_device *ndev) 994df828598SMugunthan V N { 995df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 996df828598SMugunthan V N 997df828598SMugunthan V N cpsw_info(priv, ifdown, "shutting down cpsw device\n"); 998df828598SMugunthan V N netif_stop_queue(priv->ndev); 999df828598SMugunthan V N napi_disable(&priv->napi); 1000df828598SMugunthan V N netif_carrier_off(priv->ndev); 1001d9ba8f9eSMugunthan V N 1002d9ba8f9eSMugunthan V N if (cpsw_common_res_usage_state(priv) <= 1) { 100371380f9bSMugunthan V N cpsw_intr_disable(priv); 100471380f9bSMugunthan V N cpdma_ctlr_int_ctrl(priv->dma, false); 100571380f9bSMugunthan V N cpdma_ctlr_stop(priv->dma); 1006df828598SMugunthan V N cpsw_ale_stop(priv->ale); 1007d9ba8f9eSMugunthan V N } 1008df828598SMugunthan V N for_each_slave(priv, cpsw_slave_stop, priv); 1009f150bd7fSMugunthan V N pm_runtime_put_sync(&priv->pdev->dev); 1010d9ba8f9eSMugunthan V N if (priv->data.dual_emac) 1011d9ba8f9eSMugunthan V N priv->slaves[priv->emac_port].open_stat = false; 1012df828598SMugunthan V N return 0; 1013df828598SMugunthan V N } 1014df828598SMugunthan V N 1015df828598SMugunthan V N static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb, 1016df828598SMugunthan V N struct net_device *ndev) 1017df828598SMugunthan V N { 1018df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 1019df828598SMugunthan V N int ret; 1020df828598SMugunthan V N 1021df828598SMugunthan V N ndev->trans_start = jiffies; 1022df828598SMugunthan V N 1023df828598SMugunthan V N if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) { 1024df828598SMugunthan V N cpsw_err(priv, tx_err, "packet pad failed\n"); 1025df828598SMugunthan V N priv->stats.tx_dropped++; 1026df828598SMugunthan V N return NETDEV_TX_OK; 1027df828598SMugunthan V N } 1028df828598SMugunthan V N 10299232b16dSMugunthan V N if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && 10309232b16dSMugunthan V N priv->cpts->tx_enable) 10312e5b38abSRichard Cochran skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 10322e5b38abSRichard Cochran 10332e5b38abSRichard Cochran skb_tx_timestamp(skb); 10342e5b38abSRichard Cochran 1035d9ba8f9eSMugunthan V N ret = cpsw_tx_packet_submit(ndev, priv, skb); 1036df828598SMugunthan V N if (unlikely(ret != 0)) { 1037df828598SMugunthan V N cpsw_err(priv, tx_err, "desc submit failed\n"); 1038df828598SMugunthan V N goto fail; 1039df828598SMugunthan V N } 1040df828598SMugunthan V N 1041fae50823SMugunthan V N /* If there is no more tx desc left free then we need to 1042fae50823SMugunthan V N * tell the kernel to stop sending us tx frames. 1043fae50823SMugunthan V N */ 1044d35162f8SDaniel Mack if (unlikely(!cpdma_check_free_tx_desc(priv->txch))) 1045fae50823SMugunthan V N netif_stop_queue(ndev); 1046fae50823SMugunthan V N 1047df828598SMugunthan V N return NETDEV_TX_OK; 1048df828598SMugunthan V N fail: 1049df828598SMugunthan V N priv->stats.tx_dropped++; 1050df828598SMugunthan V N netif_stop_queue(ndev); 1051df828598SMugunthan V N return NETDEV_TX_BUSY; 1052df828598SMugunthan V N } 1053df828598SMugunthan V N 1054df828598SMugunthan V N static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags) 1055df828598SMugunthan V N { 1056df828598SMugunthan V N /* 1057df828598SMugunthan V N * The switch cannot operate in promiscuous mode without substantial 1058df828598SMugunthan V N * headache. For promiscuous mode to work, we would need to put the 1059df828598SMugunthan V N * ALE in bypass mode and route all traffic to the host port. 1060df828598SMugunthan V N * Subsequently, the host will need to operate as a "bridge", learn, 1061df828598SMugunthan V N * and flood as needed. For now, we simply complain here and 1062df828598SMugunthan V N * do nothing about it :-) 1063df828598SMugunthan V N */ 1064df828598SMugunthan V N if ((flags & IFF_PROMISC) && (ndev->flags & IFF_PROMISC)) 1065df828598SMugunthan V N dev_err(&ndev->dev, "promiscuity ignored!\n"); 1066df828598SMugunthan V N 1067df828598SMugunthan V N /* 1068df828598SMugunthan V N * The switch cannot filter multicast traffic unless it is configured 1069df828598SMugunthan V N * in "VLAN Aware" mode. Unfortunately, VLAN awareness requires a 1070df828598SMugunthan V N * whole bunch of additional logic that this driver does not implement 1071df828598SMugunthan V N * at present. 1072df828598SMugunthan V N */ 1073df828598SMugunthan V N if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI)) 1074df828598SMugunthan V N dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n"); 1075df828598SMugunthan V N } 1076df828598SMugunthan V N 10772e5b38abSRichard Cochran #ifdef CONFIG_TI_CPTS 10782e5b38abSRichard Cochran 10792e5b38abSRichard Cochran static void cpsw_hwtstamp_v1(struct cpsw_priv *priv) 10802e5b38abSRichard Cochran { 1081e86ac13bSMugunthan V N struct cpsw_slave *slave = &priv->slaves[priv->data.active_slave]; 10822e5b38abSRichard Cochran u32 ts_en, seq_id; 10832e5b38abSRichard Cochran 10849232b16dSMugunthan V N if (!priv->cpts->tx_enable && !priv->cpts->rx_enable) { 10852e5b38abSRichard Cochran slave_write(slave, 0, CPSW1_TS_CTL); 10862e5b38abSRichard Cochran return; 10872e5b38abSRichard Cochran } 10882e5b38abSRichard Cochran 10892e5b38abSRichard Cochran seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588; 10902e5b38abSRichard Cochran ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS; 10912e5b38abSRichard Cochran 10929232b16dSMugunthan V N if (priv->cpts->tx_enable) 10932e5b38abSRichard Cochran ts_en |= CPSW_V1_TS_TX_EN; 10942e5b38abSRichard Cochran 10959232b16dSMugunthan V N if (priv->cpts->rx_enable) 10962e5b38abSRichard Cochran ts_en |= CPSW_V1_TS_RX_EN; 10972e5b38abSRichard Cochran 10982e5b38abSRichard Cochran slave_write(slave, ts_en, CPSW1_TS_CTL); 10992e5b38abSRichard Cochran slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE); 11002e5b38abSRichard Cochran } 11012e5b38abSRichard Cochran 11022e5b38abSRichard Cochran static void cpsw_hwtstamp_v2(struct cpsw_priv *priv) 11032e5b38abSRichard Cochran { 1104d9ba8f9eSMugunthan V N struct cpsw_slave *slave; 11052e5b38abSRichard Cochran u32 ctrl, mtype; 11062e5b38abSRichard Cochran 1107d9ba8f9eSMugunthan V N if (priv->data.dual_emac) 1108d9ba8f9eSMugunthan V N slave = &priv->slaves[priv->emac_port]; 1109d9ba8f9eSMugunthan V N else 1110e86ac13bSMugunthan V N slave = &priv->slaves[priv->data.active_slave]; 1111d9ba8f9eSMugunthan V N 11122e5b38abSRichard Cochran ctrl = slave_read(slave, CPSW2_CONTROL); 11132e5b38abSRichard Cochran ctrl &= ~CTRL_ALL_TS_MASK; 11142e5b38abSRichard Cochran 11159232b16dSMugunthan V N if (priv->cpts->tx_enable) 11162e5b38abSRichard Cochran ctrl |= CTRL_TX_TS_BITS; 11172e5b38abSRichard Cochran 11189232b16dSMugunthan V N if (priv->cpts->rx_enable) 11192e5b38abSRichard Cochran ctrl |= CTRL_RX_TS_BITS; 11202e5b38abSRichard Cochran 11212e5b38abSRichard Cochran mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS; 11222e5b38abSRichard Cochran 11232e5b38abSRichard Cochran slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE); 11242e5b38abSRichard Cochran slave_write(slave, ctrl, CPSW2_CONTROL); 11252e5b38abSRichard Cochran __raw_writel(ETH_P_1588, &priv->regs->ts_ltype); 11262e5b38abSRichard Cochran } 11272e5b38abSRichard Cochran 11283177bf6fSMugunthan V N static int cpsw_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr) 11292e5b38abSRichard Cochran { 11303177bf6fSMugunthan V N struct cpsw_priv *priv = netdev_priv(dev); 11319232b16dSMugunthan V N struct cpts *cpts = priv->cpts; 11322e5b38abSRichard Cochran struct hwtstamp_config cfg; 11332e5b38abSRichard Cochran 11342e5b38abSRichard Cochran if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 11352e5b38abSRichard Cochran return -EFAULT; 11362e5b38abSRichard Cochran 11372e5b38abSRichard Cochran /* reserved for future extensions */ 11382e5b38abSRichard Cochran if (cfg.flags) 11392e5b38abSRichard Cochran return -EINVAL; 11402e5b38abSRichard Cochran 11412e5b38abSRichard Cochran switch (cfg.tx_type) { 11422e5b38abSRichard Cochran case HWTSTAMP_TX_OFF: 11432e5b38abSRichard Cochran cpts->tx_enable = 0; 11442e5b38abSRichard Cochran break; 11452e5b38abSRichard Cochran case HWTSTAMP_TX_ON: 11462e5b38abSRichard Cochran cpts->tx_enable = 1; 11472e5b38abSRichard Cochran break; 11482e5b38abSRichard Cochran default: 11492e5b38abSRichard Cochran return -ERANGE; 11502e5b38abSRichard Cochran } 11512e5b38abSRichard Cochran 11522e5b38abSRichard Cochran switch (cfg.rx_filter) { 11532e5b38abSRichard Cochran case HWTSTAMP_FILTER_NONE: 11542e5b38abSRichard Cochran cpts->rx_enable = 0; 11552e5b38abSRichard Cochran break; 11562e5b38abSRichard Cochran case HWTSTAMP_FILTER_ALL: 11572e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 11582e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 11592e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 11602e5b38abSRichard Cochran return -ERANGE; 11612e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 11622e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 11632e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 11642e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 11652e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 11662e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 11672e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_EVENT: 11682e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_SYNC: 11692e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 11702e5b38abSRichard Cochran cpts->rx_enable = 1; 11712e5b38abSRichard Cochran cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 11722e5b38abSRichard Cochran break; 11732e5b38abSRichard Cochran default: 11742e5b38abSRichard Cochran return -ERANGE; 11752e5b38abSRichard Cochran } 11762e5b38abSRichard Cochran 11772e5b38abSRichard Cochran switch (priv->version) { 11782e5b38abSRichard Cochran case CPSW_VERSION_1: 11792e5b38abSRichard Cochran cpsw_hwtstamp_v1(priv); 11802e5b38abSRichard Cochran break; 11812e5b38abSRichard Cochran case CPSW_VERSION_2: 11822e5b38abSRichard Cochran cpsw_hwtstamp_v2(priv); 11832e5b38abSRichard Cochran break; 11842e5b38abSRichard Cochran default: 11852e5b38abSRichard Cochran return -ENOTSUPP; 11862e5b38abSRichard Cochran } 11872e5b38abSRichard Cochran 11882e5b38abSRichard Cochran return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 11892e5b38abSRichard Cochran } 11902e5b38abSRichard Cochran 11912e5b38abSRichard Cochran #endif /*CONFIG_TI_CPTS*/ 11922e5b38abSRichard Cochran 11932e5b38abSRichard Cochran static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd) 11942e5b38abSRichard Cochran { 119511f2c988SMugunthan V N struct cpsw_priv *priv = netdev_priv(dev); 119611f2c988SMugunthan V N struct mii_ioctl_data *data = if_mii(req); 119711f2c988SMugunthan V N int slave_no = cpsw_slave_index(priv); 119811f2c988SMugunthan V N 11992e5b38abSRichard Cochran if (!netif_running(dev)) 12002e5b38abSRichard Cochran return -EINVAL; 12012e5b38abSRichard Cochran 120211f2c988SMugunthan V N switch (cmd) { 12032e5b38abSRichard Cochran #ifdef CONFIG_TI_CPTS 120411f2c988SMugunthan V N case SIOCSHWTSTAMP: 12053177bf6fSMugunthan V N return cpsw_hwtstamp_ioctl(dev, req); 12062e5b38abSRichard Cochran #endif 120711f2c988SMugunthan V N case SIOCGMIIPHY: 120811f2c988SMugunthan V N data->phy_id = priv->slaves[slave_no].phy->addr; 120911f2c988SMugunthan V N break; 121011f2c988SMugunthan V N default: 12112e5b38abSRichard Cochran return -ENOTSUPP; 12122e5b38abSRichard Cochran } 12132e5b38abSRichard Cochran 121411f2c988SMugunthan V N return 0; 121511f2c988SMugunthan V N } 121611f2c988SMugunthan V N 1217df828598SMugunthan V N static void cpsw_ndo_tx_timeout(struct net_device *ndev) 1218df828598SMugunthan V N { 1219df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 1220df828598SMugunthan V N 1221df828598SMugunthan V N cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n"); 1222df828598SMugunthan V N priv->stats.tx_errors++; 1223df828598SMugunthan V N cpsw_intr_disable(priv); 1224df828598SMugunthan V N cpdma_ctlr_int_ctrl(priv->dma, false); 1225df828598SMugunthan V N cpdma_chan_stop(priv->txch); 1226df828598SMugunthan V N cpdma_chan_start(priv->txch); 1227df828598SMugunthan V N cpdma_ctlr_int_ctrl(priv->dma, true); 1228df828598SMugunthan V N cpsw_intr_enable(priv); 1229510a1e72SMugunthan V N cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX); 1230510a1e72SMugunthan V N cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX); 1231510a1e72SMugunthan V N 1232df828598SMugunthan V N } 1233df828598SMugunthan V N 1234df828598SMugunthan V N static struct net_device_stats *cpsw_ndo_get_stats(struct net_device *ndev) 1235df828598SMugunthan V N { 1236df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 1237df828598SMugunthan V N return &priv->stats; 1238df828598SMugunthan V N } 1239df828598SMugunthan V N 1240df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER 1241df828598SMugunthan V N static void cpsw_ndo_poll_controller(struct net_device *ndev) 1242df828598SMugunthan V N { 1243df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 1244df828598SMugunthan V N 1245df828598SMugunthan V N cpsw_intr_disable(priv); 1246df828598SMugunthan V N cpdma_ctlr_int_ctrl(priv->dma, false); 1247df828598SMugunthan V N cpsw_interrupt(ndev->irq, priv); 1248df828598SMugunthan V N cpdma_ctlr_int_ctrl(priv->dma, true); 1249df828598SMugunthan V N cpsw_intr_enable(priv); 1250510a1e72SMugunthan V N cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_RX); 1251510a1e72SMugunthan V N cpdma_ctlr_eoi(priv->dma, CPDMA_EOI_TX); 1252510a1e72SMugunthan V N 1253df828598SMugunthan V N } 1254df828598SMugunthan V N #endif 1255df828598SMugunthan V N 12563b72c2feSMugunthan V N static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv, 12573b72c2feSMugunthan V N unsigned short vid) 12583b72c2feSMugunthan V N { 12593b72c2feSMugunthan V N int ret; 12603b72c2feSMugunthan V N 12613b72c2feSMugunthan V N ret = cpsw_ale_add_vlan(priv->ale, vid, 12623b72c2feSMugunthan V N ALE_ALL_PORTS << priv->host_port, 12633b72c2feSMugunthan V N 0, ALE_ALL_PORTS << priv->host_port, 12643b72c2feSMugunthan V N (ALE_PORT_1 | ALE_PORT_2) << priv->host_port); 12653b72c2feSMugunthan V N if (ret != 0) 12663b72c2feSMugunthan V N return ret; 12673b72c2feSMugunthan V N 12683b72c2feSMugunthan V N ret = cpsw_ale_add_ucast(priv->ale, priv->mac_addr, 12693b72c2feSMugunthan V N priv->host_port, ALE_VLAN, vid); 12703b72c2feSMugunthan V N if (ret != 0) 12713b72c2feSMugunthan V N goto clean_vid; 12723b72c2feSMugunthan V N 12733b72c2feSMugunthan V N ret = cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast, 12743b72c2feSMugunthan V N ALE_ALL_PORTS << priv->host_port, 12753b72c2feSMugunthan V N ALE_VLAN, vid, 0); 12763b72c2feSMugunthan V N if (ret != 0) 12773b72c2feSMugunthan V N goto clean_vlan_ucast; 12783b72c2feSMugunthan V N return 0; 12793b72c2feSMugunthan V N 12803b72c2feSMugunthan V N clean_vlan_ucast: 12813b72c2feSMugunthan V N cpsw_ale_del_ucast(priv->ale, priv->mac_addr, 12823b72c2feSMugunthan V N priv->host_port, ALE_VLAN, vid); 12833b72c2feSMugunthan V N clean_vid: 12843b72c2feSMugunthan V N cpsw_ale_del_vlan(priv->ale, vid, 0); 12853b72c2feSMugunthan V N return ret; 12863b72c2feSMugunthan V N } 12873b72c2feSMugunthan V N 12883b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev, 128980d5c368SPatrick McHardy __be16 proto, u16 vid) 12903b72c2feSMugunthan V N { 12913b72c2feSMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 12923b72c2feSMugunthan V N 12933b72c2feSMugunthan V N if (vid == priv->data.default_vlan) 12943b72c2feSMugunthan V N return 0; 12953b72c2feSMugunthan V N 12963b72c2feSMugunthan V N dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid); 12973b72c2feSMugunthan V N return cpsw_add_vlan_ale_entry(priv, vid); 12983b72c2feSMugunthan V N } 12993b72c2feSMugunthan V N 13003b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev, 130180d5c368SPatrick McHardy __be16 proto, u16 vid) 13023b72c2feSMugunthan V N { 13033b72c2feSMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 13043b72c2feSMugunthan V N int ret; 13053b72c2feSMugunthan V N 13063b72c2feSMugunthan V N if (vid == priv->data.default_vlan) 13073b72c2feSMugunthan V N return 0; 13083b72c2feSMugunthan V N 13093b72c2feSMugunthan V N dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid); 13103b72c2feSMugunthan V N ret = cpsw_ale_del_vlan(priv->ale, vid, 0); 13113b72c2feSMugunthan V N if (ret != 0) 13123b72c2feSMugunthan V N return ret; 13133b72c2feSMugunthan V N 13143b72c2feSMugunthan V N ret = cpsw_ale_del_ucast(priv->ale, priv->mac_addr, 13153b72c2feSMugunthan V N priv->host_port, ALE_VLAN, vid); 13163b72c2feSMugunthan V N if (ret != 0) 13173b72c2feSMugunthan V N return ret; 13183b72c2feSMugunthan V N 13193b72c2feSMugunthan V N return cpsw_ale_del_mcast(priv->ale, priv->ndev->broadcast, 13203b72c2feSMugunthan V N 0, ALE_VLAN, vid); 13213b72c2feSMugunthan V N } 13223b72c2feSMugunthan V N 1323df828598SMugunthan V N static const struct net_device_ops cpsw_netdev_ops = { 1324df828598SMugunthan V N .ndo_open = cpsw_ndo_open, 1325df828598SMugunthan V N .ndo_stop = cpsw_ndo_stop, 1326df828598SMugunthan V N .ndo_start_xmit = cpsw_ndo_start_xmit, 1327df828598SMugunthan V N .ndo_change_rx_flags = cpsw_ndo_change_rx_flags, 13282e5b38abSRichard Cochran .ndo_do_ioctl = cpsw_ndo_ioctl, 1329df828598SMugunthan V N .ndo_validate_addr = eth_validate_addr, 13305c473ed2SDavid S. Miller .ndo_change_mtu = eth_change_mtu, 1331df828598SMugunthan V N .ndo_tx_timeout = cpsw_ndo_tx_timeout, 1332df828598SMugunthan V N .ndo_get_stats = cpsw_ndo_get_stats, 13335c50a856SMugunthan V N .ndo_set_rx_mode = cpsw_ndo_set_rx_mode, 1334df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER 1335df828598SMugunthan V N .ndo_poll_controller = cpsw_ndo_poll_controller, 1336df828598SMugunthan V N #endif 13373b72c2feSMugunthan V N .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid, 13383b72c2feSMugunthan V N .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid, 1339df828598SMugunthan V N }; 1340df828598SMugunthan V N 1341df828598SMugunthan V N static void cpsw_get_drvinfo(struct net_device *ndev, 1342df828598SMugunthan V N struct ethtool_drvinfo *info) 1343df828598SMugunthan V N { 1344df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 13457826d43fSJiri Pirko 13467826d43fSJiri Pirko strlcpy(info->driver, "TI CPSW Driver v1.0", sizeof(info->driver)); 13477826d43fSJiri Pirko strlcpy(info->version, "1.0", sizeof(info->version)); 13487826d43fSJiri Pirko strlcpy(info->bus_info, priv->pdev->name, sizeof(info->bus_info)); 1349df828598SMugunthan V N } 1350df828598SMugunthan V N 1351df828598SMugunthan V N static u32 cpsw_get_msglevel(struct net_device *ndev) 1352df828598SMugunthan V N { 1353df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 1354df828598SMugunthan V N return priv->msg_enable; 1355df828598SMugunthan V N } 1356df828598SMugunthan V N 1357df828598SMugunthan V N static void cpsw_set_msglevel(struct net_device *ndev, u32 value) 1358df828598SMugunthan V N { 1359df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 1360df828598SMugunthan V N priv->msg_enable = value; 1361df828598SMugunthan V N } 1362df828598SMugunthan V N 13632e5b38abSRichard Cochran static int cpsw_get_ts_info(struct net_device *ndev, 13642e5b38abSRichard Cochran struct ethtool_ts_info *info) 13652e5b38abSRichard Cochran { 13662e5b38abSRichard Cochran #ifdef CONFIG_TI_CPTS 13672e5b38abSRichard Cochran struct cpsw_priv *priv = netdev_priv(ndev); 13682e5b38abSRichard Cochran 13692e5b38abSRichard Cochran info->so_timestamping = 13702e5b38abSRichard Cochran SOF_TIMESTAMPING_TX_HARDWARE | 13712e5b38abSRichard Cochran SOF_TIMESTAMPING_TX_SOFTWARE | 13722e5b38abSRichard Cochran SOF_TIMESTAMPING_RX_HARDWARE | 13732e5b38abSRichard Cochran SOF_TIMESTAMPING_RX_SOFTWARE | 13742e5b38abSRichard Cochran SOF_TIMESTAMPING_SOFTWARE | 13752e5b38abSRichard Cochran SOF_TIMESTAMPING_RAW_HARDWARE; 13769232b16dSMugunthan V N info->phc_index = priv->cpts->phc_index; 13772e5b38abSRichard Cochran info->tx_types = 13782e5b38abSRichard Cochran (1 << HWTSTAMP_TX_OFF) | 13792e5b38abSRichard Cochran (1 << HWTSTAMP_TX_ON); 13802e5b38abSRichard Cochran info->rx_filters = 13812e5b38abSRichard Cochran (1 << HWTSTAMP_FILTER_NONE) | 13822e5b38abSRichard Cochran (1 << HWTSTAMP_FILTER_PTP_V2_EVENT); 13832e5b38abSRichard Cochran #else 13842e5b38abSRichard Cochran info->so_timestamping = 13852e5b38abSRichard Cochran SOF_TIMESTAMPING_TX_SOFTWARE | 13862e5b38abSRichard Cochran SOF_TIMESTAMPING_RX_SOFTWARE | 13872e5b38abSRichard Cochran SOF_TIMESTAMPING_SOFTWARE; 13882e5b38abSRichard Cochran info->phc_index = -1; 13892e5b38abSRichard Cochran info->tx_types = 0; 13902e5b38abSRichard Cochran info->rx_filters = 0; 13912e5b38abSRichard Cochran #endif 13922e5b38abSRichard Cochran return 0; 13932e5b38abSRichard Cochran } 13942e5b38abSRichard Cochran 1395d3bb9c58SMugunthan V N static int cpsw_get_settings(struct net_device *ndev, 1396d3bb9c58SMugunthan V N struct ethtool_cmd *ecmd) 1397d3bb9c58SMugunthan V N { 1398d3bb9c58SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 1399d3bb9c58SMugunthan V N int slave_no = cpsw_slave_index(priv); 1400d3bb9c58SMugunthan V N 1401d3bb9c58SMugunthan V N if (priv->slaves[slave_no].phy) 1402d3bb9c58SMugunthan V N return phy_ethtool_gset(priv->slaves[slave_no].phy, ecmd); 1403d3bb9c58SMugunthan V N else 1404d3bb9c58SMugunthan V N return -EOPNOTSUPP; 1405d3bb9c58SMugunthan V N } 1406d3bb9c58SMugunthan V N 1407d3bb9c58SMugunthan V N static int cpsw_set_settings(struct net_device *ndev, struct ethtool_cmd *ecmd) 1408d3bb9c58SMugunthan V N { 1409d3bb9c58SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 1410d3bb9c58SMugunthan V N int slave_no = cpsw_slave_index(priv); 1411d3bb9c58SMugunthan V N 1412d3bb9c58SMugunthan V N if (priv->slaves[slave_no].phy) 1413d3bb9c58SMugunthan V N return phy_ethtool_sset(priv->slaves[slave_no].phy, ecmd); 1414d3bb9c58SMugunthan V N else 1415d3bb9c58SMugunthan V N return -EOPNOTSUPP; 1416d3bb9c58SMugunthan V N } 1417d3bb9c58SMugunthan V N 1418df828598SMugunthan V N static const struct ethtool_ops cpsw_ethtool_ops = { 1419df828598SMugunthan V N .get_drvinfo = cpsw_get_drvinfo, 1420df828598SMugunthan V N .get_msglevel = cpsw_get_msglevel, 1421df828598SMugunthan V N .set_msglevel = cpsw_set_msglevel, 1422df828598SMugunthan V N .get_link = ethtool_op_get_link, 14232e5b38abSRichard Cochran .get_ts_info = cpsw_get_ts_info, 1424d3bb9c58SMugunthan V N .get_settings = cpsw_get_settings, 1425d3bb9c58SMugunthan V N .set_settings = cpsw_set_settings, 1426ff5b8ef2SMugunthan V N .get_coalesce = cpsw_get_coalesce, 1427ff5b8ef2SMugunthan V N .set_coalesce = cpsw_set_coalesce, 1428df828598SMugunthan V N }; 1429df828598SMugunthan V N 1430549985eeSRichard Cochran static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv, 1431549985eeSRichard Cochran u32 slave_reg_ofs, u32 sliver_reg_ofs) 1432df828598SMugunthan V N { 1433df828598SMugunthan V N void __iomem *regs = priv->regs; 1434df828598SMugunthan V N int slave_num = slave->slave_num; 1435df828598SMugunthan V N struct cpsw_slave_data *data = priv->data.slave_data + slave_num; 1436df828598SMugunthan V N 1437df828598SMugunthan V N slave->data = data; 1438549985eeSRichard Cochran slave->regs = regs + slave_reg_ofs; 1439549985eeSRichard Cochran slave->sliver = regs + sliver_reg_ofs; 1440d9ba8f9eSMugunthan V N slave->port_vlan = data->dual_emac_res_vlan; 1441df828598SMugunthan V N } 1442df828598SMugunthan V N 14432eb32b0aSMugunthan V N static int cpsw_probe_dt(struct cpsw_platform_data *data, 14442eb32b0aSMugunthan V N struct platform_device *pdev) 14452eb32b0aSMugunthan V N { 14462eb32b0aSMugunthan V N struct device_node *node = pdev->dev.of_node; 14472eb32b0aSMugunthan V N struct device_node *slave_node; 14482eb32b0aSMugunthan V N int i = 0, ret; 14492eb32b0aSMugunthan V N u32 prop; 14502eb32b0aSMugunthan V N 14512eb32b0aSMugunthan V N if (!node) 14522eb32b0aSMugunthan V N return -EINVAL; 14532eb32b0aSMugunthan V N 14542eb32b0aSMugunthan V N if (of_property_read_u32(node, "slaves", &prop)) { 14552eb32b0aSMugunthan V N pr_err("Missing slaves property in the DT.\n"); 14562eb32b0aSMugunthan V N return -EINVAL; 14572eb32b0aSMugunthan V N } 14582eb32b0aSMugunthan V N data->slaves = prop; 14592eb32b0aSMugunthan V N 1460e86ac13bSMugunthan V N if (of_property_read_u32(node, "active_slave", &prop)) { 1461e86ac13bSMugunthan V N pr_err("Missing active_slave property in the DT.\n"); 146278ca0b28SRichard Cochran ret = -EINVAL; 146378ca0b28SRichard Cochran goto error_ret; 146478ca0b28SRichard Cochran } 1465e86ac13bSMugunthan V N data->active_slave = prop; 146678ca0b28SRichard Cochran 146700ab94eeSRichard Cochran if (of_property_read_u32(node, "cpts_clock_mult", &prop)) { 146800ab94eeSRichard Cochran pr_err("Missing cpts_clock_mult property in the DT.\n"); 146900ab94eeSRichard Cochran ret = -EINVAL; 147000ab94eeSRichard Cochran goto error_ret; 147100ab94eeSRichard Cochran } 147200ab94eeSRichard Cochran data->cpts_clock_mult = prop; 147300ab94eeSRichard Cochran 147400ab94eeSRichard Cochran if (of_property_read_u32(node, "cpts_clock_shift", &prop)) { 147500ab94eeSRichard Cochran pr_err("Missing cpts_clock_shift property in the DT.\n"); 147600ab94eeSRichard Cochran ret = -EINVAL; 147700ab94eeSRichard Cochran goto error_ret; 147800ab94eeSRichard Cochran } 147900ab94eeSRichard Cochran data->cpts_clock_shift = prop; 148000ab94eeSRichard Cochran 1481b2adaca9SJoe Perches data->slave_data = kcalloc(data->slaves, sizeof(struct cpsw_slave_data), 1482b2adaca9SJoe Perches GFP_KERNEL); 1483b2adaca9SJoe Perches if (!data->slave_data) 14842eb32b0aSMugunthan V N return -EINVAL; 14852eb32b0aSMugunthan V N 14862eb32b0aSMugunthan V N if (of_property_read_u32(node, "cpdma_channels", &prop)) { 14872eb32b0aSMugunthan V N pr_err("Missing cpdma_channels property in the DT.\n"); 14882eb32b0aSMugunthan V N ret = -EINVAL; 14892eb32b0aSMugunthan V N goto error_ret; 14902eb32b0aSMugunthan V N } 14912eb32b0aSMugunthan V N data->channels = prop; 14922eb32b0aSMugunthan V N 14932eb32b0aSMugunthan V N if (of_property_read_u32(node, "ale_entries", &prop)) { 14942eb32b0aSMugunthan V N pr_err("Missing ale_entries property in the DT.\n"); 14952eb32b0aSMugunthan V N ret = -EINVAL; 14962eb32b0aSMugunthan V N goto error_ret; 14972eb32b0aSMugunthan V N } 14982eb32b0aSMugunthan V N data->ale_entries = prop; 14992eb32b0aSMugunthan V N 15002eb32b0aSMugunthan V N if (of_property_read_u32(node, "bd_ram_size", &prop)) { 15012eb32b0aSMugunthan V N pr_err("Missing bd_ram_size property in the DT.\n"); 15022eb32b0aSMugunthan V N ret = -EINVAL; 15032eb32b0aSMugunthan V N goto error_ret; 15042eb32b0aSMugunthan V N } 15052eb32b0aSMugunthan V N data->bd_ram_size = prop; 15062eb32b0aSMugunthan V N 15072eb32b0aSMugunthan V N if (of_property_read_u32(node, "rx_descs", &prop)) { 15082eb32b0aSMugunthan V N pr_err("Missing rx_descs property in the DT.\n"); 15092eb32b0aSMugunthan V N ret = -EINVAL; 15102eb32b0aSMugunthan V N goto error_ret; 15112eb32b0aSMugunthan V N } 15122eb32b0aSMugunthan V N data->rx_descs = prop; 15132eb32b0aSMugunthan V N 15142eb32b0aSMugunthan V N if (of_property_read_u32(node, "mac_control", &prop)) { 15152eb32b0aSMugunthan V N pr_err("Missing mac_control property in the DT.\n"); 15162eb32b0aSMugunthan V N ret = -EINVAL; 15172eb32b0aSMugunthan V N goto error_ret; 15182eb32b0aSMugunthan V N } 15192eb32b0aSMugunthan V N data->mac_control = prop; 15202eb32b0aSMugunthan V N 1521d9ba8f9eSMugunthan V N if (!of_property_read_u32(node, "dual_emac", &prop)) 1522d9ba8f9eSMugunthan V N data->dual_emac = prop; 1523d9ba8f9eSMugunthan V N 15241fb19aa7SVaibhav Hiremath /* 15251fb19aa7SVaibhav Hiremath * Populate all the child nodes here... 15261fb19aa7SVaibhav Hiremath */ 15271fb19aa7SVaibhav Hiremath ret = of_platform_populate(node, NULL, NULL, &pdev->dev); 15281fb19aa7SVaibhav Hiremath /* We do not want to force this, as in some cases may not have child */ 15291fb19aa7SVaibhav Hiremath if (ret) 15301fb19aa7SVaibhav Hiremath pr_warn("Doesn't have any child node\n"); 15311fb19aa7SVaibhav Hiremath 1532549985eeSRichard Cochran for_each_node_by_name(slave_node, "slave") { 1533549985eeSRichard Cochran struct cpsw_slave_data *slave_data = data->slave_data + i; 1534549985eeSRichard Cochran const void *mac_addr = NULL; 1535549985eeSRichard Cochran u32 phyid; 1536549985eeSRichard Cochran int lenp; 1537549985eeSRichard Cochran const __be32 *parp; 1538549985eeSRichard Cochran struct device_node *mdio_node; 1539549985eeSRichard Cochran struct platform_device *mdio; 1540549985eeSRichard Cochran 1541549985eeSRichard Cochran parp = of_get_property(slave_node, "phy_id", &lenp); 1542ce16294fSLothar Waßmann if ((parp == NULL) || (lenp != (sizeof(void *) * 2))) { 1543549985eeSRichard Cochran pr_err("Missing slave[%d] phy_id property\n", i); 1544549985eeSRichard Cochran ret = -EINVAL; 1545549985eeSRichard Cochran goto error_ret; 1546549985eeSRichard Cochran } 1547549985eeSRichard Cochran mdio_node = of_find_node_by_phandle(be32_to_cpup(parp)); 1548549985eeSRichard Cochran phyid = be32_to_cpup(parp+1); 1549549985eeSRichard Cochran mdio = of_find_device_by_node(mdio_node); 1550549985eeSRichard Cochran snprintf(slave_data->phy_id, sizeof(slave_data->phy_id), 1551549985eeSRichard Cochran PHY_ID_FMT, mdio->name, phyid); 1552549985eeSRichard Cochran 1553549985eeSRichard Cochran mac_addr = of_get_mac_address(slave_node); 1554549985eeSRichard Cochran if (mac_addr) 1555549985eeSRichard Cochran memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN); 1556549985eeSRichard Cochran 1557c5ceea7aSMugunthan V N slave_data->phy_if = of_get_phy_mode(slave_node); 1558c5ceea7aSMugunthan V N 1559d9ba8f9eSMugunthan V N if (data->dual_emac) { 156091c4166cSMugunthan V N if (of_property_read_u32(slave_node, "dual_emac_res_vlan", 1561d9ba8f9eSMugunthan V N &prop)) { 1562d9ba8f9eSMugunthan V N pr_err("Missing dual_emac_res_vlan in DT.\n"); 1563d9ba8f9eSMugunthan V N slave_data->dual_emac_res_vlan = i+1; 1564d9ba8f9eSMugunthan V N pr_err("Using %d as Reserved VLAN for %d slave\n", 1565d9ba8f9eSMugunthan V N slave_data->dual_emac_res_vlan, i); 1566d9ba8f9eSMugunthan V N } else { 1567d9ba8f9eSMugunthan V N slave_data->dual_emac_res_vlan = prop; 1568d9ba8f9eSMugunthan V N } 1569d9ba8f9eSMugunthan V N } 1570d9ba8f9eSMugunthan V N 1571549985eeSRichard Cochran i++; 1572549985eeSRichard Cochran } 1573549985eeSRichard Cochran 15742eb32b0aSMugunthan V N return 0; 15752eb32b0aSMugunthan V N 15762eb32b0aSMugunthan V N error_ret: 15772eb32b0aSMugunthan V N kfree(data->slave_data); 15782eb32b0aSMugunthan V N return ret; 15792eb32b0aSMugunthan V N } 15802eb32b0aSMugunthan V N 1581d9ba8f9eSMugunthan V N static int cpsw_probe_dual_emac(struct platform_device *pdev, 1582d9ba8f9eSMugunthan V N struct cpsw_priv *priv) 1583d9ba8f9eSMugunthan V N { 1584d9ba8f9eSMugunthan V N struct cpsw_platform_data *data = &priv->data; 1585d9ba8f9eSMugunthan V N struct net_device *ndev; 1586d9ba8f9eSMugunthan V N struct cpsw_priv *priv_sl2; 1587d9ba8f9eSMugunthan V N int ret = 0, i; 1588d9ba8f9eSMugunthan V N 1589d9ba8f9eSMugunthan V N ndev = alloc_etherdev(sizeof(struct cpsw_priv)); 1590d9ba8f9eSMugunthan V N if (!ndev) { 1591d9ba8f9eSMugunthan V N pr_err("cpsw: error allocating net_device\n"); 1592d9ba8f9eSMugunthan V N return -ENOMEM; 1593d9ba8f9eSMugunthan V N } 1594d9ba8f9eSMugunthan V N 1595d9ba8f9eSMugunthan V N priv_sl2 = netdev_priv(ndev); 1596d9ba8f9eSMugunthan V N spin_lock_init(&priv_sl2->lock); 1597d9ba8f9eSMugunthan V N priv_sl2->data = *data; 1598d9ba8f9eSMugunthan V N priv_sl2->pdev = pdev; 1599d9ba8f9eSMugunthan V N priv_sl2->ndev = ndev; 1600d9ba8f9eSMugunthan V N priv_sl2->dev = &ndev->dev; 1601d9ba8f9eSMugunthan V N priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG); 1602d9ba8f9eSMugunthan V N priv_sl2->rx_packet_max = max(rx_packet_max, 128); 1603d9ba8f9eSMugunthan V N 1604d9ba8f9eSMugunthan V N if (is_valid_ether_addr(data->slave_data[1].mac_addr)) { 1605d9ba8f9eSMugunthan V N memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr, 1606d9ba8f9eSMugunthan V N ETH_ALEN); 1607d9ba8f9eSMugunthan V N pr_info("cpsw: Detected MACID = %pM\n", priv_sl2->mac_addr); 1608d9ba8f9eSMugunthan V N } else { 1609d9ba8f9eSMugunthan V N random_ether_addr(priv_sl2->mac_addr); 1610d9ba8f9eSMugunthan V N pr_info("cpsw: Random MACID = %pM\n", priv_sl2->mac_addr); 1611d9ba8f9eSMugunthan V N } 1612d9ba8f9eSMugunthan V N memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN); 1613d9ba8f9eSMugunthan V N 1614d9ba8f9eSMugunthan V N priv_sl2->slaves = priv->slaves; 1615d9ba8f9eSMugunthan V N priv_sl2->clk = priv->clk; 1616d9ba8f9eSMugunthan V N 1617ff5b8ef2SMugunthan V N priv_sl2->coal_intvl = 0; 1618ff5b8ef2SMugunthan V N priv_sl2->bus_freq_mhz = priv->bus_freq_mhz; 1619ff5b8ef2SMugunthan V N 1620d9ba8f9eSMugunthan V N priv_sl2->cpsw_res = priv->cpsw_res; 1621d9ba8f9eSMugunthan V N priv_sl2->regs = priv->regs; 1622d9ba8f9eSMugunthan V N priv_sl2->host_port = priv->host_port; 1623d9ba8f9eSMugunthan V N priv_sl2->host_port_regs = priv->host_port_regs; 1624d9ba8f9eSMugunthan V N priv_sl2->wr_regs = priv->wr_regs; 1625d9ba8f9eSMugunthan V N priv_sl2->dma = priv->dma; 1626d9ba8f9eSMugunthan V N priv_sl2->txch = priv->txch; 1627d9ba8f9eSMugunthan V N priv_sl2->rxch = priv->rxch; 1628d9ba8f9eSMugunthan V N priv_sl2->ale = priv->ale; 1629d9ba8f9eSMugunthan V N priv_sl2->emac_port = 1; 1630d9ba8f9eSMugunthan V N priv->slaves[1].ndev = ndev; 1631d9ba8f9eSMugunthan V N priv_sl2->cpts = priv->cpts; 1632d9ba8f9eSMugunthan V N priv_sl2->version = priv->version; 1633d9ba8f9eSMugunthan V N 1634d9ba8f9eSMugunthan V N for (i = 0; i < priv->num_irqs; i++) { 1635d9ba8f9eSMugunthan V N priv_sl2->irqs_table[i] = priv->irqs_table[i]; 1636d9ba8f9eSMugunthan V N priv_sl2->num_irqs = priv->num_irqs; 1637d9ba8f9eSMugunthan V N } 1638f646968fSPatrick McHardy ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 1639d9ba8f9eSMugunthan V N 1640d9ba8f9eSMugunthan V N ndev->netdev_ops = &cpsw_netdev_ops; 1641d9ba8f9eSMugunthan V N SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops); 1642d9ba8f9eSMugunthan V N netif_napi_add(ndev, &priv_sl2->napi, cpsw_poll, CPSW_POLL_WEIGHT); 1643d9ba8f9eSMugunthan V N 1644d9ba8f9eSMugunthan V N /* register the network device */ 1645d9ba8f9eSMugunthan V N SET_NETDEV_DEV(ndev, &pdev->dev); 1646d9ba8f9eSMugunthan V N ret = register_netdev(ndev); 1647d9ba8f9eSMugunthan V N if (ret) { 1648d9ba8f9eSMugunthan V N pr_err("cpsw: error registering net device\n"); 1649d9ba8f9eSMugunthan V N free_netdev(ndev); 1650d9ba8f9eSMugunthan V N ret = -ENODEV; 1651d9ba8f9eSMugunthan V N } 1652d9ba8f9eSMugunthan V N 1653d9ba8f9eSMugunthan V N return ret; 1654d9ba8f9eSMugunthan V N } 1655d9ba8f9eSMugunthan V N 1656663e12e6SBill Pemberton static int cpsw_probe(struct platform_device *pdev) 1657df828598SMugunthan V N { 1658d1bd9acfSSebastian Siewior struct cpsw_platform_data *data; 1659df828598SMugunthan V N struct net_device *ndev; 1660df828598SMugunthan V N struct cpsw_priv *priv; 1661df828598SMugunthan V N struct cpdma_params dma_params; 1662df828598SMugunthan V N struct cpsw_ale_params ale_params; 1663549985eeSRichard Cochran void __iomem *ss_regs, *wr_regs; 1664df828598SMugunthan V N struct resource *res; 1665549985eeSRichard Cochran u32 slave_offset, sliver_offset, slave_size; 1666df828598SMugunthan V N int ret = 0, i, k = 0; 1667df828598SMugunthan V N 1668df828598SMugunthan V N ndev = alloc_etherdev(sizeof(struct cpsw_priv)); 1669df828598SMugunthan V N if (!ndev) { 1670df828598SMugunthan V N pr_err("error allocating net_device\n"); 1671df828598SMugunthan V N return -ENOMEM; 1672df828598SMugunthan V N } 1673df828598SMugunthan V N 1674df828598SMugunthan V N platform_set_drvdata(pdev, ndev); 1675df828598SMugunthan V N priv = netdev_priv(ndev); 1676df828598SMugunthan V N spin_lock_init(&priv->lock); 1677df828598SMugunthan V N priv->pdev = pdev; 1678df828598SMugunthan V N priv->ndev = ndev; 1679df828598SMugunthan V N priv->dev = &ndev->dev; 1680df828598SMugunthan V N priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG); 1681df828598SMugunthan V N priv->rx_packet_max = max(rx_packet_max, 128); 16829232b16dSMugunthan V N priv->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL); 16837dcf313aSMugunthan V N priv->irq_enabled = true; 16849232b16dSMugunthan V N if (!ndev) { 16859232b16dSMugunthan V N pr_err("error allocating cpts\n"); 16869232b16dSMugunthan V N goto clean_ndev_ret; 16879232b16dSMugunthan V N } 1688df828598SMugunthan V N 16891fb19aa7SVaibhav Hiremath /* 16901fb19aa7SVaibhav Hiremath * This may be required here for child devices. 16911fb19aa7SVaibhav Hiremath */ 16921fb19aa7SVaibhav Hiremath pm_runtime_enable(&pdev->dev); 16931fb19aa7SVaibhav Hiremath 16942eb32b0aSMugunthan V N if (cpsw_probe_dt(&priv->data, pdev)) { 16952eb32b0aSMugunthan V N pr_err("cpsw: platform data missing\n"); 16962eb32b0aSMugunthan V N ret = -ENODEV; 16972eb32b0aSMugunthan V N goto clean_ndev_ret; 16982eb32b0aSMugunthan V N } 16992eb32b0aSMugunthan V N data = &priv->data; 17002eb32b0aSMugunthan V N 1701df828598SMugunthan V N if (is_valid_ether_addr(data->slave_data[0].mac_addr)) { 1702df828598SMugunthan V N memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN); 1703df828598SMugunthan V N pr_info("Detected MACID = %pM", priv->mac_addr); 1704df828598SMugunthan V N } else { 17057efd26d0SJoe Perches eth_random_addr(priv->mac_addr); 1706df828598SMugunthan V N pr_info("Random MACID = %pM", priv->mac_addr); 1707df828598SMugunthan V N } 1708df828598SMugunthan V N 1709df828598SMugunthan V N memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN); 1710df828598SMugunthan V N 1711df828598SMugunthan V N priv->slaves = kzalloc(sizeof(struct cpsw_slave) * data->slaves, 1712df828598SMugunthan V N GFP_KERNEL); 1713df828598SMugunthan V N if (!priv->slaves) { 1714df828598SMugunthan V N ret = -EBUSY; 1715df828598SMugunthan V N goto clean_ndev_ret; 1716df828598SMugunthan V N } 1717df828598SMugunthan V N for (i = 0; i < data->slaves; i++) 1718df828598SMugunthan V N priv->slaves[i].slave_num = i; 1719df828598SMugunthan V N 1720d9ba8f9eSMugunthan V N priv->slaves[0].ndev = ndev; 1721d9ba8f9eSMugunthan V N priv->emac_port = 0; 1722d9ba8f9eSMugunthan V N 1723f150bd7fSMugunthan V N priv->clk = clk_get(&pdev->dev, "fck"); 1724df828598SMugunthan V N if (IS_ERR(priv->clk)) { 1725f150bd7fSMugunthan V N dev_err(&pdev->dev, "fck is not found\n"); 1726f150bd7fSMugunthan V N ret = -ENODEV; 1727f150bd7fSMugunthan V N goto clean_slave_ret; 1728df828598SMugunthan V N } 1729ff5b8ef2SMugunthan V N priv->coal_intvl = 0; 1730ff5b8ef2SMugunthan V N priv->bus_freq_mhz = clk_get_rate(priv->clk) / 1000000; 1731df828598SMugunthan V N 1732df828598SMugunthan V N priv->cpsw_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1733df828598SMugunthan V N if (!priv->cpsw_res) { 1734df828598SMugunthan V N dev_err(priv->dev, "error getting i/o resource\n"); 1735df828598SMugunthan V N ret = -ENOENT; 1736df828598SMugunthan V N goto clean_clk_ret; 1737df828598SMugunthan V N } 1738df828598SMugunthan V N if (!request_mem_region(priv->cpsw_res->start, 1739df828598SMugunthan V N resource_size(priv->cpsw_res), ndev->name)) { 1740df828598SMugunthan V N dev_err(priv->dev, "failed request i/o region\n"); 1741df828598SMugunthan V N ret = -ENXIO; 1742df828598SMugunthan V N goto clean_clk_ret; 1743df828598SMugunthan V N } 1744549985eeSRichard Cochran ss_regs = ioremap(priv->cpsw_res->start, resource_size(priv->cpsw_res)); 1745549985eeSRichard Cochran if (!ss_regs) { 1746df828598SMugunthan V N dev_err(priv->dev, "unable to map i/o region\n"); 1747df828598SMugunthan V N goto clean_cpsw_iores_ret; 1748df828598SMugunthan V N } 1749549985eeSRichard Cochran priv->regs = ss_regs; 1750549985eeSRichard Cochran priv->version = __raw_readl(&priv->regs->id_ver); 1751549985eeSRichard Cochran priv->host_port = HOST_PORT_NUM; 1752df828598SMugunthan V N 1753a65dd5b2SRichard Cochran priv->cpsw_wr_res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 1754a65dd5b2SRichard Cochran if (!priv->cpsw_wr_res) { 1755df828598SMugunthan V N dev_err(priv->dev, "error getting i/o resource\n"); 1756df828598SMugunthan V N ret = -ENOENT; 17575250c969SRichard Cochran goto clean_iomap_ret; 1758df828598SMugunthan V N } 1759a65dd5b2SRichard Cochran if (!request_mem_region(priv->cpsw_wr_res->start, 1760a65dd5b2SRichard Cochran resource_size(priv->cpsw_wr_res), ndev->name)) { 1761df828598SMugunthan V N dev_err(priv->dev, "failed request i/o region\n"); 1762df828598SMugunthan V N ret = -ENXIO; 17635250c969SRichard Cochran goto clean_iomap_ret; 1764df828598SMugunthan V N } 1765549985eeSRichard Cochran wr_regs = ioremap(priv->cpsw_wr_res->start, 1766a65dd5b2SRichard Cochran resource_size(priv->cpsw_wr_res)); 1767549985eeSRichard Cochran if (!wr_regs) { 1768df828598SMugunthan V N dev_err(priv->dev, "unable to map i/o region\n"); 1769a65dd5b2SRichard Cochran goto clean_cpsw_wr_iores_ret; 1770df828598SMugunthan V N } 1771549985eeSRichard Cochran priv->wr_regs = wr_regs; 1772df828598SMugunthan V N 1773df828598SMugunthan V N memset(&dma_params, 0, sizeof(dma_params)); 1774549985eeSRichard Cochran memset(&ale_params, 0, sizeof(ale_params)); 1775549985eeSRichard Cochran 1776549985eeSRichard Cochran switch (priv->version) { 1777549985eeSRichard Cochran case CPSW_VERSION_1: 1778549985eeSRichard Cochran priv->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET; 17799232b16dSMugunthan V N priv->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET; 1780549985eeSRichard Cochran dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET; 1781549985eeSRichard Cochran dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET; 1782549985eeSRichard Cochran ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET; 1783549985eeSRichard Cochran slave_offset = CPSW1_SLAVE_OFFSET; 1784549985eeSRichard Cochran slave_size = CPSW1_SLAVE_SIZE; 1785549985eeSRichard Cochran sliver_offset = CPSW1_SLIVER_OFFSET; 1786549985eeSRichard Cochran dma_params.desc_mem_phys = 0; 1787549985eeSRichard Cochran break; 1788549985eeSRichard Cochran case CPSW_VERSION_2: 1789549985eeSRichard Cochran priv->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET; 17909232b16dSMugunthan V N priv->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET; 1791549985eeSRichard Cochran dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET; 1792549985eeSRichard Cochran dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET; 1793549985eeSRichard Cochran ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET; 1794549985eeSRichard Cochran slave_offset = CPSW2_SLAVE_OFFSET; 1795549985eeSRichard Cochran slave_size = CPSW2_SLAVE_SIZE; 1796549985eeSRichard Cochran sliver_offset = CPSW2_SLIVER_OFFSET; 1797549985eeSRichard Cochran dma_params.desc_mem_phys = 1798549985eeSRichard Cochran (u32 __force) priv->cpsw_res->start + CPSW2_BD_OFFSET; 1799549985eeSRichard Cochran break; 1800549985eeSRichard Cochran default: 1801549985eeSRichard Cochran dev_err(priv->dev, "unknown version 0x%08x\n", priv->version); 1802549985eeSRichard Cochran ret = -ENODEV; 1803549985eeSRichard Cochran goto clean_cpsw_wr_iores_ret; 1804549985eeSRichard Cochran } 1805549985eeSRichard Cochran for (i = 0; i < priv->data.slaves; i++) { 1806549985eeSRichard Cochran struct cpsw_slave *slave = &priv->slaves[i]; 1807549985eeSRichard Cochran cpsw_slave_init(slave, priv, slave_offset, sliver_offset); 1808549985eeSRichard Cochran slave_offset += slave_size; 1809549985eeSRichard Cochran sliver_offset += SLIVER_SIZE; 1810549985eeSRichard Cochran } 1811549985eeSRichard Cochran 1812df828598SMugunthan V N dma_params.dev = &pdev->dev; 1813549985eeSRichard Cochran dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH; 1814549985eeSRichard Cochran dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE; 1815549985eeSRichard Cochran dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP; 1816549985eeSRichard Cochran dma_params.txcp = dma_params.txhdp + CPDMA_TXCP; 1817549985eeSRichard Cochran dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP; 1818df828598SMugunthan V N 1819df828598SMugunthan V N dma_params.num_chan = data->channels; 1820df828598SMugunthan V N dma_params.has_soft_reset = true; 1821df828598SMugunthan V N dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE; 1822df828598SMugunthan V N dma_params.desc_mem_size = data->bd_ram_size; 1823df828598SMugunthan V N dma_params.desc_align = 16; 1824df828598SMugunthan V N dma_params.has_ext_regs = true; 1825549985eeSRichard Cochran dma_params.desc_hw_addr = dma_params.desc_mem_phys; 1826df828598SMugunthan V N 1827df828598SMugunthan V N priv->dma = cpdma_ctlr_create(&dma_params); 1828df828598SMugunthan V N if (!priv->dma) { 1829df828598SMugunthan V N dev_err(priv->dev, "error initializing dma\n"); 1830df828598SMugunthan V N ret = -ENOMEM; 18315250c969SRichard Cochran goto clean_wr_iomap_ret; 1832df828598SMugunthan V N } 1833df828598SMugunthan V N 1834df828598SMugunthan V N priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0), 1835df828598SMugunthan V N cpsw_tx_handler); 1836df828598SMugunthan V N priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0), 1837df828598SMugunthan V N cpsw_rx_handler); 1838df828598SMugunthan V N 1839df828598SMugunthan V N if (WARN_ON(!priv->txch || !priv->rxch)) { 1840df828598SMugunthan V N dev_err(priv->dev, "error initializing dma channels\n"); 1841df828598SMugunthan V N ret = -ENOMEM; 1842df828598SMugunthan V N goto clean_dma_ret; 1843df828598SMugunthan V N } 1844df828598SMugunthan V N 1845df828598SMugunthan V N ale_params.dev = &ndev->dev; 1846df828598SMugunthan V N ale_params.ale_ageout = ale_ageout; 1847df828598SMugunthan V N ale_params.ale_entries = data->ale_entries; 1848df828598SMugunthan V N ale_params.ale_ports = data->slaves; 1849df828598SMugunthan V N 1850df828598SMugunthan V N priv->ale = cpsw_ale_create(&ale_params); 1851df828598SMugunthan V N if (!priv->ale) { 1852df828598SMugunthan V N dev_err(priv->dev, "error initializing ale engine\n"); 1853df828598SMugunthan V N ret = -ENODEV; 1854df828598SMugunthan V N goto clean_dma_ret; 1855df828598SMugunthan V N } 1856df828598SMugunthan V N 1857df828598SMugunthan V N ndev->irq = platform_get_irq(pdev, 0); 1858df828598SMugunthan V N if (ndev->irq < 0) { 1859df828598SMugunthan V N dev_err(priv->dev, "error getting irq resource\n"); 1860df828598SMugunthan V N ret = -ENOENT; 1861df828598SMugunthan V N goto clean_ale_ret; 1862df828598SMugunthan V N } 1863df828598SMugunthan V N 1864df828598SMugunthan V N while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) { 1865df828598SMugunthan V N for (i = res->start; i <= res->end; i++) { 1866df828598SMugunthan V N if (request_irq(i, cpsw_interrupt, IRQF_DISABLED, 1867df828598SMugunthan V N dev_name(&pdev->dev), priv)) { 1868df828598SMugunthan V N dev_err(priv->dev, "error attaching irq\n"); 1869df828598SMugunthan V N goto clean_ale_ret; 1870df828598SMugunthan V N } 1871df828598SMugunthan V N priv->irqs_table[k] = i; 1872d1bd9acfSSebastian Siewior priv->num_irqs = k + 1; 1873df828598SMugunthan V N } 1874df828598SMugunthan V N k++; 1875df828598SMugunthan V N } 1876df828598SMugunthan V N 1877f646968fSPatrick McHardy ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 1878df828598SMugunthan V N 1879df828598SMugunthan V N ndev->netdev_ops = &cpsw_netdev_ops; 1880df828598SMugunthan V N SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops); 1881df828598SMugunthan V N netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT); 1882df828598SMugunthan V N 1883df828598SMugunthan V N /* register the network device */ 1884df828598SMugunthan V N SET_NETDEV_DEV(ndev, &pdev->dev); 1885df828598SMugunthan V N ret = register_netdev(ndev); 1886df828598SMugunthan V N if (ret) { 1887df828598SMugunthan V N dev_err(priv->dev, "error registering net device\n"); 1888df828598SMugunthan V N ret = -ENODEV; 1889df828598SMugunthan V N goto clean_irq_ret; 1890df828598SMugunthan V N } 1891df828598SMugunthan V N 18929232b16dSMugunthan V N if (cpts_register(&pdev->dev, priv->cpts, 18932e5b38abSRichard Cochran data->cpts_clock_mult, data->cpts_clock_shift)) 18942e5b38abSRichard Cochran dev_err(priv->dev, "error registering cpts device\n"); 18952e5b38abSRichard Cochran 1896df828598SMugunthan V N cpsw_notice(priv, probe, "initialized device (regs %x, irq %d)\n", 1897df828598SMugunthan V N priv->cpsw_res->start, ndev->irq); 1898df828598SMugunthan V N 1899d9ba8f9eSMugunthan V N if (priv->data.dual_emac) { 1900d9ba8f9eSMugunthan V N ret = cpsw_probe_dual_emac(pdev, priv); 1901d9ba8f9eSMugunthan V N if (ret) { 1902d9ba8f9eSMugunthan V N cpsw_err(priv, probe, "error probe slave 2 emac interface\n"); 1903d9ba8f9eSMugunthan V N goto clean_irq_ret; 1904d9ba8f9eSMugunthan V N } 1905d9ba8f9eSMugunthan V N } 1906d9ba8f9eSMugunthan V N 1907df828598SMugunthan V N return 0; 1908df828598SMugunthan V N 1909df828598SMugunthan V N clean_irq_ret: 1910d1bd9acfSSebastian Siewior for (i = 0; i < priv->num_irqs; i++) 1911d1bd9acfSSebastian Siewior free_irq(priv->irqs_table[i], priv); 1912df828598SMugunthan V N clean_ale_ret: 1913df828598SMugunthan V N cpsw_ale_destroy(priv->ale); 1914df828598SMugunthan V N clean_dma_ret: 1915df828598SMugunthan V N cpdma_chan_destroy(priv->txch); 1916df828598SMugunthan V N cpdma_chan_destroy(priv->rxch); 1917df828598SMugunthan V N cpdma_ctlr_destroy(priv->dma); 19185250c969SRichard Cochran clean_wr_iomap_ret: 19195250c969SRichard Cochran iounmap(priv->wr_regs); 1920a65dd5b2SRichard Cochran clean_cpsw_wr_iores_ret: 1921a65dd5b2SRichard Cochran release_mem_region(priv->cpsw_wr_res->start, 1922a65dd5b2SRichard Cochran resource_size(priv->cpsw_wr_res)); 19235250c969SRichard Cochran clean_iomap_ret: 19245250c969SRichard Cochran iounmap(priv->regs); 1925df828598SMugunthan V N clean_cpsw_iores_ret: 1926df828598SMugunthan V N release_mem_region(priv->cpsw_res->start, 1927df828598SMugunthan V N resource_size(priv->cpsw_res)); 1928df828598SMugunthan V N clean_clk_ret: 1929df828598SMugunthan V N clk_put(priv->clk); 1930f150bd7fSMugunthan V N clean_slave_ret: 1931f150bd7fSMugunthan V N pm_runtime_disable(&pdev->dev); 1932df828598SMugunthan V N kfree(priv->slaves); 1933df828598SMugunthan V N clean_ndev_ret: 1934d1bd9acfSSebastian Siewior kfree(priv->data.slave_data); 1935d1bd9acfSSebastian Siewior free_netdev(priv->ndev); 1936df828598SMugunthan V N return ret; 1937df828598SMugunthan V N } 1938df828598SMugunthan V N 1939663e12e6SBill Pemberton static int cpsw_remove(struct platform_device *pdev) 1940df828598SMugunthan V N { 1941df828598SMugunthan V N struct net_device *ndev = platform_get_drvdata(pdev); 1942df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 1943d1bd9acfSSebastian Siewior int i; 1944df828598SMugunthan V N 1945d1bd9acfSSebastian Siewior if (priv->data.dual_emac) 1946d1bd9acfSSebastian Siewior unregister_netdev(cpsw_get_slave_ndev(priv, 1)); 1947d1bd9acfSSebastian Siewior unregister_netdev(ndev); 1948df828598SMugunthan V N 19499232b16dSMugunthan V N cpts_unregister(priv->cpts); 1950d1bd9acfSSebastian Siewior for (i = 0; i < priv->num_irqs; i++) 1951d1bd9acfSSebastian Siewior free_irq(priv->irqs_table[i], priv); 1952d1bd9acfSSebastian Siewior 1953df828598SMugunthan V N cpsw_ale_destroy(priv->ale); 1954df828598SMugunthan V N cpdma_chan_destroy(priv->txch); 1955df828598SMugunthan V N cpdma_chan_destroy(priv->rxch); 1956df828598SMugunthan V N cpdma_ctlr_destroy(priv->dma); 1957df828598SMugunthan V N iounmap(priv->regs); 1958df828598SMugunthan V N release_mem_region(priv->cpsw_res->start, 1959df828598SMugunthan V N resource_size(priv->cpsw_res)); 19605250c969SRichard Cochran iounmap(priv->wr_regs); 1961a65dd5b2SRichard Cochran release_mem_region(priv->cpsw_wr_res->start, 1962a65dd5b2SRichard Cochran resource_size(priv->cpsw_wr_res)); 1963f150bd7fSMugunthan V N pm_runtime_disable(&pdev->dev); 1964df828598SMugunthan V N clk_put(priv->clk); 1965df828598SMugunthan V N kfree(priv->slaves); 1966d1bd9acfSSebastian Siewior kfree(priv->data.slave_data); 1967d1bd9acfSSebastian Siewior if (priv->data.dual_emac) 1968d1bd9acfSSebastian Siewior free_netdev(cpsw_get_slave_ndev(priv, 1)); 1969df828598SMugunthan V N free_netdev(ndev); 1970df828598SMugunthan V N return 0; 1971df828598SMugunthan V N } 1972df828598SMugunthan V N 1973df828598SMugunthan V N static int cpsw_suspend(struct device *dev) 1974df828598SMugunthan V N { 1975df828598SMugunthan V N struct platform_device *pdev = to_platform_device(dev); 1976df828598SMugunthan V N struct net_device *ndev = platform_get_drvdata(pdev); 1977df828598SMugunthan V N 1978df828598SMugunthan V N if (netif_running(ndev)) 1979df828598SMugunthan V N cpsw_ndo_stop(ndev); 1980f150bd7fSMugunthan V N pm_runtime_put_sync(&pdev->dev); 1981f150bd7fSMugunthan V N 1982df828598SMugunthan V N return 0; 1983df828598SMugunthan V N } 1984df828598SMugunthan V N 1985df828598SMugunthan V N static int cpsw_resume(struct device *dev) 1986df828598SMugunthan V N { 1987df828598SMugunthan V N struct platform_device *pdev = to_platform_device(dev); 1988df828598SMugunthan V N struct net_device *ndev = platform_get_drvdata(pdev); 1989df828598SMugunthan V N 1990f150bd7fSMugunthan V N pm_runtime_get_sync(&pdev->dev); 1991df828598SMugunthan V N if (netif_running(ndev)) 1992df828598SMugunthan V N cpsw_ndo_open(ndev); 1993df828598SMugunthan V N return 0; 1994df828598SMugunthan V N } 1995df828598SMugunthan V N 1996df828598SMugunthan V N static const struct dev_pm_ops cpsw_pm_ops = { 1997df828598SMugunthan V N .suspend = cpsw_suspend, 1998df828598SMugunthan V N .resume = cpsw_resume, 1999df828598SMugunthan V N }; 2000df828598SMugunthan V N 20012eb32b0aSMugunthan V N static const struct of_device_id cpsw_of_mtable[] = { 20022eb32b0aSMugunthan V N { .compatible = "ti,cpsw", }, 20032eb32b0aSMugunthan V N { /* sentinel */ }, 20042eb32b0aSMugunthan V N }; 20054bc21d41SSebastian Siewior MODULE_DEVICE_TABLE(of, cpsw_of_mtable); 20062eb32b0aSMugunthan V N 2007df828598SMugunthan V N static struct platform_driver cpsw_driver = { 2008df828598SMugunthan V N .driver = { 2009df828598SMugunthan V N .name = "cpsw", 2010df828598SMugunthan V N .owner = THIS_MODULE, 2011df828598SMugunthan V N .pm = &cpsw_pm_ops, 20122eb32b0aSMugunthan V N .of_match_table = of_match_ptr(cpsw_of_mtable), 2013df828598SMugunthan V N }, 2014df828598SMugunthan V N .probe = cpsw_probe, 2015663e12e6SBill Pemberton .remove = cpsw_remove, 2016df828598SMugunthan V N }; 2017df828598SMugunthan V N 2018df828598SMugunthan V N static int __init cpsw_init(void) 2019df828598SMugunthan V N { 2020df828598SMugunthan V N return platform_driver_register(&cpsw_driver); 2021df828598SMugunthan V N } 2022df828598SMugunthan V N late_initcall(cpsw_init); 2023df828598SMugunthan V N 2024df828598SMugunthan V N static void __exit cpsw_exit(void) 2025df828598SMugunthan V N { 2026df828598SMugunthan V N platform_driver_unregister(&cpsw_driver); 2027df828598SMugunthan V N } 2028df828598SMugunthan V N module_exit(cpsw_exit); 2029df828598SMugunthan V N 2030df828598SMugunthan V N MODULE_LICENSE("GPL"); 2031df828598SMugunthan V N MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>"); 2032df828598SMugunthan V N MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>"); 2033df828598SMugunthan V N MODULE_DESCRIPTION("TI CPSW Ethernet driver"); 2034