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> 321d147ccbSMugunthan V N #include <linux/gpio.h> 332eb32b0aSMugunthan V N #include <linux/of.h> 349e42f715SHeiko Schocher #include <linux/of_mdio.h> 352eb32b0aSMugunthan V N #include <linux/of_net.h> 362eb32b0aSMugunthan V N #include <linux/of_device.h> 373b72c2feSMugunthan V N #include <linux/if_vlan.h> 38df828598SMugunthan V N 39739683b4SMugunthan V N #include <linux/pinctrl/consumer.h> 40df828598SMugunthan V N 41dbe34724SMugunthan V N #include "cpsw.h" 42df828598SMugunthan V N #include "cpsw_ale.h" 432e5b38abSRichard Cochran #include "cpts.h" 44df828598SMugunthan V N #include "davinci_cpdma.h" 45df828598SMugunthan V N 46df828598SMugunthan V N #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \ 47df828598SMugunthan V N NETIF_MSG_DRV | NETIF_MSG_LINK | \ 48df828598SMugunthan V N NETIF_MSG_IFUP | NETIF_MSG_INTR | \ 49df828598SMugunthan V N NETIF_MSG_PROBE | NETIF_MSG_TIMER | \ 50df828598SMugunthan V N NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \ 51df828598SMugunthan V N NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \ 52df828598SMugunthan V N NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \ 53df828598SMugunthan V N NETIF_MSG_RX_STATUS) 54df828598SMugunthan V N 55df828598SMugunthan V N #define cpsw_info(priv, type, format, ...) \ 56df828598SMugunthan V N do { \ 57df828598SMugunthan V N if (netif_msg_##type(priv) && net_ratelimit()) \ 58df828598SMugunthan V N dev_info(priv->dev, format, ## __VA_ARGS__); \ 59df828598SMugunthan V N } while (0) 60df828598SMugunthan V N 61df828598SMugunthan V N #define cpsw_err(priv, type, format, ...) \ 62df828598SMugunthan V N do { \ 63df828598SMugunthan V N if (netif_msg_##type(priv) && net_ratelimit()) \ 64df828598SMugunthan V N dev_err(priv->dev, format, ## __VA_ARGS__); \ 65df828598SMugunthan V N } while (0) 66df828598SMugunthan V N 67df828598SMugunthan V N #define cpsw_dbg(priv, type, format, ...) \ 68df828598SMugunthan V N do { \ 69df828598SMugunthan V N if (netif_msg_##type(priv) && net_ratelimit()) \ 70df828598SMugunthan V N dev_dbg(priv->dev, format, ## __VA_ARGS__); \ 71df828598SMugunthan V N } while (0) 72df828598SMugunthan V N 73df828598SMugunthan V N #define cpsw_notice(priv, type, format, ...) \ 74df828598SMugunthan V N do { \ 75df828598SMugunthan V N if (netif_msg_##type(priv) && net_ratelimit()) \ 76df828598SMugunthan V N dev_notice(priv->dev, format, ## __VA_ARGS__); \ 77df828598SMugunthan V N } while (0) 78df828598SMugunthan V N 795c50a856SMugunthan V N #define ALE_ALL_PORTS 0x7 805c50a856SMugunthan V N 81df828598SMugunthan V N #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7) 82df828598SMugunthan V N #define CPSW_MINOR_VERSION(reg) (reg & 0xff) 83df828598SMugunthan V N #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f) 84df828598SMugunthan V N 85e90cfac6SRichard Cochran #define CPSW_VERSION_1 0x19010a 86e90cfac6SRichard Cochran #define CPSW_VERSION_2 0x19010c 87c193f365SMugunthan V N #define CPSW_VERSION_3 0x19010f 88926489beSMugunthan V N #define CPSW_VERSION_4 0x190112 89549985eeSRichard Cochran 90549985eeSRichard Cochran #define HOST_PORT_NUM 0 91549985eeSRichard Cochran #define SLIVER_SIZE 0x40 92549985eeSRichard Cochran 93549985eeSRichard Cochran #define CPSW1_HOST_PORT_OFFSET 0x028 94549985eeSRichard Cochran #define CPSW1_SLAVE_OFFSET 0x050 95549985eeSRichard Cochran #define CPSW1_SLAVE_SIZE 0x040 96549985eeSRichard Cochran #define CPSW1_CPDMA_OFFSET 0x100 97549985eeSRichard Cochran #define CPSW1_STATERAM_OFFSET 0x200 98d9718546SMugunthan V N #define CPSW1_HW_STATS 0x400 99549985eeSRichard Cochran #define CPSW1_CPTS_OFFSET 0x500 100549985eeSRichard Cochran #define CPSW1_ALE_OFFSET 0x600 101549985eeSRichard Cochran #define CPSW1_SLIVER_OFFSET 0x700 102549985eeSRichard Cochran 103549985eeSRichard Cochran #define CPSW2_HOST_PORT_OFFSET 0x108 104549985eeSRichard Cochran #define CPSW2_SLAVE_OFFSET 0x200 105549985eeSRichard Cochran #define CPSW2_SLAVE_SIZE 0x100 106549985eeSRichard Cochran #define CPSW2_CPDMA_OFFSET 0x800 107d9718546SMugunthan V N #define CPSW2_HW_STATS 0x900 108549985eeSRichard Cochran #define CPSW2_STATERAM_OFFSET 0xa00 109549985eeSRichard Cochran #define CPSW2_CPTS_OFFSET 0xc00 110549985eeSRichard Cochran #define CPSW2_ALE_OFFSET 0xd00 111549985eeSRichard Cochran #define CPSW2_SLIVER_OFFSET 0xd80 112549985eeSRichard Cochran #define CPSW2_BD_OFFSET 0x2000 113549985eeSRichard Cochran 114df828598SMugunthan V N #define CPDMA_RXTHRESH 0x0c0 115df828598SMugunthan V N #define CPDMA_RXFREE 0x0e0 116df828598SMugunthan V N #define CPDMA_TXHDP 0x00 117df828598SMugunthan V N #define CPDMA_RXHDP 0x20 118df828598SMugunthan V N #define CPDMA_TXCP 0x40 119df828598SMugunthan V N #define CPDMA_RXCP 0x60 120df828598SMugunthan V N 121df828598SMugunthan V N #define CPSW_POLL_WEIGHT 64 122df828598SMugunthan V N #define CPSW_MIN_PACKET_SIZE 60 123df828598SMugunthan V N #define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4) 124df828598SMugunthan V N 125df828598SMugunthan V N #define RX_PRIORITY_MAPPING 0x76543210 126df828598SMugunthan V N #define TX_PRIORITY_MAPPING 0x33221100 127e05107e6SIvan Khoronzhuk #define CPDMA_TX_PRIORITY_MAP 0x01234567 128df828598SMugunthan V N 1293b72c2feSMugunthan V N #define CPSW_VLAN_AWARE BIT(1) 1303b72c2feSMugunthan V N #define CPSW_ALE_VLAN_AWARE 1 1313b72c2feSMugunthan V N 13235717d8dSJohn Ogness #define CPSW_FIFO_NORMAL_MODE (0 << 16) 13335717d8dSJohn Ogness #define CPSW_FIFO_DUAL_MAC_MODE (1 << 16) 13435717d8dSJohn Ogness #define CPSW_FIFO_RATE_LIMIT_MODE (2 << 16) 135d9ba8f9eSMugunthan V N 136ff5b8ef2SMugunthan V N #define CPSW_INTPACEEN (0x3f << 16) 137ff5b8ef2SMugunthan V N #define CPSW_INTPRESCALE_MASK (0x7FF << 0) 138ff5b8ef2SMugunthan V N #define CPSW_CMINTMAX_CNT 63 139ff5b8ef2SMugunthan V N #define CPSW_CMINTMIN_CNT 2 140ff5b8ef2SMugunthan V N #define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT) 141ff5b8ef2SMugunthan V N #define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1) 142ff5b8ef2SMugunthan V N 143606f3993SIvan Khoronzhuk #define cpsw_slave_index(cpsw, priv) \ 144606f3993SIvan Khoronzhuk ((cpsw->data.dual_emac) ? priv->emac_port : \ 145606f3993SIvan Khoronzhuk cpsw->data.active_slave) 146e38b5a3dSIvan Khoronzhuk #define IRQ_NUM 2 147e05107e6SIvan Khoronzhuk #define CPSW_MAX_QUEUES 8 148d3bb9c58SMugunthan V N 149df828598SMugunthan V N static int debug_level; 150df828598SMugunthan V N module_param(debug_level, int, 0); 151df828598SMugunthan V N MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)"); 152df828598SMugunthan V N 153df828598SMugunthan V N static int ale_ageout = 10; 154df828598SMugunthan V N module_param(ale_ageout, int, 0); 155df828598SMugunthan V N MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)"); 156df828598SMugunthan V N 157df828598SMugunthan V N static int rx_packet_max = CPSW_MAX_PACKET_SIZE; 158df828598SMugunthan V N module_param(rx_packet_max, int, 0); 159df828598SMugunthan V N MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)"); 160df828598SMugunthan V N 161996a5c27SRichard Cochran struct cpsw_wr_regs { 162df828598SMugunthan V N u32 id_ver; 163df828598SMugunthan V N u32 soft_reset; 164df828598SMugunthan V N u32 control; 165df828598SMugunthan V N u32 int_control; 166df828598SMugunthan V N u32 rx_thresh_en; 167df828598SMugunthan V N u32 rx_en; 168df828598SMugunthan V N u32 tx_en; 169df828598SMugunthan V N u32 misc_en; 170ff5b8ef2SMugunthan V N u32 mem_allign1[8]; 171ff5b8ef2SMugunthan V N u32 rx_thresh_stat; 172ff5b8ef2SMugunthan V N u32 rx_stat; 173ff5b8ef2SMugunthan V N u32 tx_stat; 174ff5b8ef2SMugunthan V N u32 misc_stat; 175ff5b8ef2SMugunthan V N u32 mem_allign2[8]; 176ff5b8ef2SMugunthan V N u32 rx_imax; 177ff5b8ef2SMugunthan V N u32 tx_imax; 178ff5b8ef2SMugunthan V N 179df828598SMugunthan V N }; 180df828598SMugunthan V N 181996a5c27SRichard Cochran struct cpsw_ss_regs { 182df828598SMugunthan V N u32 id_ver; 183df828598SMugunthan V N u32 control; 184df828598SMugunthan V N u32 soft_reset; 185df828598SMugunthan V N u32 stat_port_en; 186df828598SMugunthan V N u32 ptype; 187bd357af2SRichard Cochran u32 soft_idle; 188bd357af2SRichard Cochran u32 thru_rate; 189bd357af2SRichard Cochran u32 gap_thresh; 190bd357af2SRichard Cochran u32 tx_start_wds; 191bd357af2SRichard Cochran u32 flow_control; 192bd357af2SRichard Cochran u32 vlan_ltype; 193bd357af2SRichard Cochran u32 ts_ltype; 194bd357af2SRichard Cochran u32 dlr_ltype; 195df828598SMugunthan V N }; 196df828598SMugunthan V N 1979750a3adSRichard Cochran /* CPSW_PORT_V1 */ 1989750a3adSRichard Cochran #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */ 1999750a3adSRichard Cochran #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */ 2009750a3adSRichard Cochran #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */ 2019750a3adSRichard Cochran #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */ 2029750a3adSRichard Cochran #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */ 2039750a3adSRichard Cochran #define CPSW1_TS_CTL 0x14 /* Time Sync Control */ 2049750a3adSRichard Cochran #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */ 2059750a3adSRichard Cochran #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */ 2069750a3adSRichard Cochran 2079750a3adSRichard Cochran /* CPSW_PORT_V2 */ 2089750a3adSRichard Cochran #define CPSW2_CONTROL 0x00 /* Control Register */ 2099750a3adSRichard Cochran #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */ 2109750a3adSRichard Cochran #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */ 2119750a3adSRichard Cochran #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */ 2129750a3adSRichard Cochran #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */ 2139750a3adSRichard Cochran #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */ 2149750a3adSRichard Cochran #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */ 2159750a3adSRichard Cochran 2169750a3adSRichard Cochran /* CPSW_PORT_V1 and V2 */ 2179750a3adSRichard Cochran #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */ 2189750a3adSRichard Cochran #define SA_HI 0x24 /* CPGMAC_SL Source Address High */ 2199750a3adSRichard Cochran #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */ 2209750a3adSRichard Cochran 2219750a3adSRichard Cochran /* CPSW_PORT_V2 only */ 2229750a3adSRichard Cochran #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */ 2239750a3adSRichard Cochran #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */ 2249750a3adSRichard Cochran #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */ 2259750a3adSRichard Cochran #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */ 2269750a3adSRichard Cochran #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */ 2279750a3adSRichard Cochran #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */ 2289750a3adSRichard Cochran #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */ 2299750a3adSRichard Cochran #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */ 2309750a3adSRichard Cochran 2319750a3adSRichard Cochran /* Bit definitions for the CPSW2_CONTROL register */ 2329750a3adSRichard Cochran #define PASS_PRI_TAGGED (1<<24) /* Pass Priority Tagged */ 2339750a3adSRichard Cochran #define VLAN_LTYPE2_EN (1<<21) /* VLAN LTYPE 2 enable */ 2349750a3adSRichard Cochran #define VLAN_LTYPE1_EN (1<<20) /* VLAN LTYPE 1 enable */ 2359750a3adSRichard Cochran #define DSCP_PRI_EN (1<<16) /* DSCP Priority Enable */ 2369750a3adSRichard Cochran #define TS_320 (1<<14) /* Time Sync Dest Port 320 enable */ 2379750a3adSRichard Cochran #define TS_319 (1<<13) /* Time Sync Dest Port 319 enable */ 2389750a3adSRichard Cochran #define TS_132 (1<<12) /* Time Sync Dest IP Addr 132 enable */ 2399750a3adSRichard Cochran #define TS_131 (1<<11) /* Time Sync Dest IP Addr 131 enable */ 2409750a3adSRichard Cochran #define TS_130 (1<<10) /* Time Sync Dest IP Addr 130 enable */ 2419750a3adSRichard Cochran #define TS_129 (1<<9) /* Time Sync Dest IP Addr 129 enable */ 24209c55372SGeorge Cherian #define TS_TTL_NONZERO (1<<8) /* Time Sync Time To Live Non-zero enable */ 24309c55372SGeorge Cherian #define TS_ANNEX_F_EN (1<<6) /* Time Sync Annex F enable */ 2449750a3adSRichard Cochran #define TS_ANNEX_D_EN (1<<4) /* Time Sync Annex D enable */ 2459750a3adSRichard Cochran #define TS_LTYPE2_EN (1<<3) /* Time Sync LTYPE 2 enable */ 2469750a3adSRichard Cochran #define TS_LTYPE1_EN (1<<2) /* Time Sync LTYPE 1 enable */ 2479750a3adSRichard Cochran #define TS_TX_EN (1<<1) /* Time Sync Transmit Enable */ 2489750a3adSRichard Cochran #define TS_RX_EN (1<<0) /* Time Sync Receive Enable */ 2499750a3adSRichard Cochran 25009c55372SGeorge Cherian #define CTRL_V2_TS_BITS \ 25109c55372SGeorge Cherian (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\ 25209c55372SGeorge Cherian TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN) 2539750a3adSRichard Cochran 25409c55372SGeorge Cherian #define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN) 25509c55372SGeorge Cherian #define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN) 25609c55372SGeorge Cherian #define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN) 25709c55372SGeorge Cherian 25809c55372SGeorge Cherian 25909c55372SGeorge Cherian #define CTRL_V3_TS_BITS \ 26009c55372SGeorge Cherian (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\ 26109c55372SGeorge Cherian TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\ 26209c55372SGeorge Cherian TS_LTYPE1_EN) 26309c55372SGeorge Cherian 26409c55372SGeorge Cherian #define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN) 26509c55372SGeorge Cherian #define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN) 26609c55372SGeorge Cherian #define CTRL_V3_RX_TS_BITS (CTRL_V3_TS_BITS | TS_RX_EN) 2679750a3adSRichard Cochran 2689750a3adSRichard Cochran /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */ 2699750a3adSRichard Cochran #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */ 2709750a3adSRichard Cochran #define TS_SEQ_ID_OFFSET_MASK (0x3f) 2719750a3adSRichard Cochran #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */ 2729750a3adSRichard Cochran #define TS_MSG_TYPE_EN_MASK (0xffff) 2739750a3adSRichard Cochran 2749750a3adSRichard Cochran /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */ 2759750a3adSRichard Cochran #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3)) 276df828598SMugunthan V N 2772e5b38abSRichard Cochran /* Bit definitions for the CPSW1_TS_CTL register */ 2782e5b38abSRichard Cochran #define CPSW_V1_TS_RX_EN BIT(0) 2792e5b38abSRichard Cochran #define CPSW_V1_TS_TX_EN BIT(4) 2802e5b38abSRichard Cochran #define CPSW_V1_MSG_TYPE_OFS 16 2812e5b38abSRichard Cochran 2822e5b38abSRichard Cochran /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */ 2832e5b38abSRichard Cochran #define CPSW_V1_SEQ_ID_OFS_SHIFT 16 2842e5b38abSRichard Cochran 285df828598SMugunthan V N struct cpsw_host_regs { 286df828598SMugunthan V N u32 max_blks; 287df828598SMugunthan V N u32 blk_cnt; 288d9ba8f9eSMugunthan V N u32 tx_in_ctl; 289df828598SMugunthan V N u32 port_vlan; 290df828598SMugunthan V N u32 tx_pri_map; 291df828598SMugunthan V N u32 cpdma_tx_pri_map; 292df828598SMugunthan V N u32 cpdma_rx_chan_map; 293df828598SMugunthan V N }; 294df828598SMugunthan V N 295df828598SMugunthan V N struct cpsw_sliver_regs { 296df828598SMugunthan V N u32 id_ver; 297df828598SMugunthan V N u32 mac_control; 298df828598SMugunthan V N u32 mac_status; 299df828598SMugunthan V N u32 soft_reset; 300df828598SMugunthan V N u32 rx_maxlen; 301df828598SMugunthan V N u32 __reserved_0; 302df828598SMugunthan V N u32 rx_pause; 303df828598SMugunthan V N u32 tx_pause; 304df828598SMugunthan V N u32 __reserved_1; 305df828598SMugunthan V N u32 rx_pri_map; 306df828598SMugunthan V N }; 307df828598SMugunthan V N 308d9718546SMugunthan V N struct cpsw_hw_stats { 309d9718546SMugunthan V N u32 rxgoodframes; 310d9718546SMugunthan V N u32 rxbroadcastframes; 311d9718546SMugunthan V N u32 rxmulticastframes; 312d9718546SMugunthan V N u32 rxpauseframes; 313d9718546SMugunthan V N u32 rxcrcerrors; 314d9718546SMugunthan V N u32 rxaligncodeerrors; 315d9718546SMugunthan V N u32 rxoversizedframes; 316d9718546SMugunthan V N u32 rxjabberframes; 317d9718546SMugunthan V N u32 rxundersizedframes; 318d9718546SMugunthan V N u32 rxfragments; 319d9718546SMugunthan V N u32 __pad_0[2]; 320d9718546SMugunthan V N u32 rxoctets; 321d9718546SMugunthan V N u32 txgoodframes; 322d9718546SMugunthan V N u32 txbroadcastframes; 323d9718546SMugunthan V N u32 txmulticastframes; 324d9718546SMugunthan V N u32 txpauseframes; 325d9718546SMugunthan V N u32 txdeferredframes; 326d9718546SMugunthan V N u32 txcollisionframes; 327d9718546SMugunthan V N u32 txsinglecollframes; 328d9718546SMugunthan V N u32 txmultcollframes; 329d9718546SMugunthan V N u32 txexcessivecollisions; 330d9718546SMugunthan V N u32 txlatecollisions; 331d9718546SMugunthan V N u32 txunderrun; 332d9718546SMugunthan V N u32 txcarriersenseerrors; 333d9718546SMugunthan V N u32 txoctets; 334d9718546SMugunthan V N u32 octetframes64; 335d9718546SMugunthan V N u32 octetframes65t127; 336d9718546SMugunthan V N u32 octetframes128t255; 337d9718546SMugunthan V N u32 octetframes256t511; 338d9718546SMugunthan V N u32 octetframes512t1023; 339d9718546SMugunthan V N u32 octetframes1024tup; 340d9718546SMugunthan V N u32 netoctets; 341d9718546SMugunthan V N u32 rxsofoverruns; 342d9718546SMugunthan V N u32 rxmofoverruns; 343d9718546SMugunthan V N u32 rxdmaoverruns; 344d9718546SMugunthan V N }; 345d9718546SMugunthan V N 346df828598SMugunthan V N struct cpsw_slave { 3479750a3adSRichard Cochran void __iomem *regs; 348df828598SMugunthan V N struct cpsw_sliver_regs __iomem *sliver; 349df828598SMugunthan V N int slave_num; 350df828598SMugunthan V N u32 mac_control; 351df828598SMugunthan V N struct cpsw_slave_data *data; 352df828598SMugunthan V N struct phy_device *phy; 353d9ba8f9eSMugunthan V N struct net_device *ndev; 354d9ba8f9eSMugunthan V N u32 port_vlan; 355d9ba8f9eSMugunthan V N u32 open_stat; 356df828598SMugunthan V N }; 357df828598SMugunthan V N 3589750a3adSRichard Cochran static inline u32 slave_read(struct cpsw_slave *slave, u32 offset) 3599750a3adSRichard Cochran { 3609750a3adSRichard Cochran return __raw_readl(slave->regs + offset); 3619750a3adSRichard Cochran } 3629750a3adSRichard Cochran 3639750a3adSRichard Cochran static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset) 3649750a3adSRichard Cochran { 3659750a3adSRichard Cochran __raw_writel(val, slave->regs + offset); 3669750a3adSRichard Cochran } 3679750a3adSRichard Cochran 368*8feb0a19SIvan Khoronzhuk struct cpsw_vector { 369*8feb0a19SIvan Khoronzhuk struct cpdma_chan *ch; 370*8feb0a19SIvan Khoronzhuk int budget; 371*8feb0a19SIvan Khoronzhuk }; 372*8feb0a19SIvan Khoronzhuk 373649a1688SIvan Khoronzhuk struct cpsw_common { 37456e31bd8SIvan Khoronzhuk struct device *dev; 375606f3993SIvan Khoronzhuk struct cpsw_platform_data data; 376dbc4ec52SIvan Khoronzhuk struct napi_struct napi_rx; 377dbc4ec52SIvan Khoronzhuk struct napi_struct napi_tx; 3785d8d0d4dSIvan Khoronzhuk struct cpsw_ss_regs __iomem *regs; 3795d8d0d4dSIvan Khoronzhuk struct cpsw_wr_regs __iomem *wr_regs; 3805d8d0d4dSIvan Khoronzhuk u8 __iomem *hw_stats; 3815d8d0d4dSIvan Khoronzhuk struct cpsw_host_regs __iomem *host_port_regs; 3822a05a622SIvan Khoronzhuk u32 version; 3832a05a622SIvan Khoronzhuk u32 coal_intvl; 3842a05a622SIvan Khoronzhuk u32 bus_freq_mhz; 3852a05a622SIvan Khoronzhuk int rx_packet_max; 386606f3993SIvan Khoronzhuk struct cpsw_slave *slaves; 3872c836bd9SIvan Khoronzhuk struct cpdma_ctlr *dma; 388*8feb0a19SIvan Khoronzhuk struct cpsw_vector txv[CPSW_MAX_QUEUES]; 389*8feb0a19SIvan Khoronzhuk struct cpsw_vector rxv[CPSW_MAX_QUEUES]; 3902a05a622SIvan Khoronzhuk struct cpsw_ale *ale; 391e38b5a3dSIvan Khoronzhuk bool quirk_irq; 392e38b5a3dSIvan Khoronzhuk bool rx_irq_disabled; 393e38b5a3dSIvan Khoronzhuk bool tx_irq_disabled; 394e38b5a3dSIvan Khoronzhuk u32 irqs_table[IRQ_NUM]; 3952a05a622SIvan Khoronzhuk struct cpts *cpts; 396e05107e6SIvan Khoronzhuk int rx_ch_num, tx_ch_num; 397649a1688SIvan Khoronzhuk }; 398649a1688SIvan Khoronzhuk 399649a1688SIvan Khoronzhuk struct cpsw_priv { 400df828598SMugunthan V N struct net_device *ndev; 401df828598SMugunthan V N struct device *dev; 402df828598SMugunthan V N u32 msg_enable; 403df828598SMugunthan V N u8 mac_addr[ETH_ALEN]; 4041923d6e4SMugunthan V N bool rx_pause; 4051923d6e4SMugunthan V N bool tx_pause; 406d9ba8f9eSMugunthan V N u32 emac_port; 407649a1688SIvan Khoronzhuk struct cpsw_common *cpsw; 408df828598SMugunthan V N }; 409df828598SMugunthan V N 410d9718546SMugunthan V N struct cpsw_stats { 411d9718546SMugunthan V N char stat_string[ETH_GSTRING_LEN]; 412d9718546SMugunthan V N int type; 413d9718546SMugunthan V N int sizeof_stat; 414d9718546SMugunthan V N int stat_offset; 415d9718546SMugunthan V N }; 416d9718546SMugunthan V N 417d9718546SMugunthan V N enum { 418d9718546SMugunthan V N CPSW_STATS, 419d9718546SMugunthan V N CPDMA_RX_STATS, 420d9718546SMugunthan V N CPDMA_TX_STATS, 421d9718546SMugunthan V N }; 422d9718546SMugunthan V N 423d9718546SMugunthan V N #define CPSW_STAT(m) CPSW_STATS, \ 424d9718546SMugunthan V N sizeof(((struct cpsw_hw_stats *)0)->m), \ 425d9718546SMugunthan V N offsetof(struct cpsw_hw_stats, m) 426d9718546SMugunthan V N #define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \ 427d9718546SMugunthan V N sizeof(((struct cpdma_chan_stats *)0)->m), \ 428d9718546SMugunthan V N offsetof(struct cpdma_chan_stats, m) 429d9718546SMugunthan V N #define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \ 430d9718546SMugunthan V N sizeof(((struct cpdma_chan_stats *)0)->m), \ 431d9718546SMugunthan V N offsetof(struct cpdma_chan_stats, m) 432d9718546SMugunthan V N 433d9718546SMugunthan V N static const struct cpsw_stats cpsw_gstrings_stats[] = { 434d9718546SMugunthan V N { "Good Rx Frames", CPSW_STAT(rxgoodframes) }, 435d9718546SMugunthan V N { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) }, 436d9718546SMugunthan V N { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) }, 437d9718546SMugunthan V N { "Pause Rx Frames", CPSW_STAT(rxpauseframes) }, 438d9718546SMugunthan V N { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) }, 439d9718546SMugunthan V N { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) }, 440d9718546SMugunthan V N { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) }, 441d9718546SMugunthan V N { "Rx Jabbers", CPSW_STAT(rxjabberframes) }, 442d9718546SMugunthan V N { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) }, 443d9718546SMugunthan V N { "Rx Fragments", CPSW_STAT(rxfragments) }, 444d9718546SMugunthan V N { "Rx Octets", CPSW_STAT(rxoctets) }, 445d9718546SMugunthan V N { "Good Tx Frames", CPSW_STAT(txgoodframes) }, 446d9718546SMugunthan V N { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) }, 447d9718546SMugunthan V N { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) }, 448d9718546SMugunthan V N { "Pause Tx Frames", CPSW_STAT(txpauseframes) }, 449d9718546SMugunthan V N { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) }, 450d9718546SMugunthan V N { "Collisions", CPSW_STAT(txcollisionframes) }, 451d9718546SMugunthan V N { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) }, 452d9718546SMugunthan V N { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) }, 453d9718546SMugunthan V N { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) }, 454d9718546SMugunthan V N { "Late Collisions", CPSW_STAT(txlatecollisions) }, 455d9718546SMugunthan V N { "Tx Underrun", CPSW_STAT(txunderrun) }, 456d9718546SMugunthan V N { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) }, 457d9718546SMugunthan V N { "Tx Octets", CPSW_STAT(txoctets) }, 458d9718546SMugunthan V N { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) }, 459d9718546SMugunthan V N { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) }, 460d9718546SMugunthan V N { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) }, 461d9718546SMugunthan V N { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) }, 462d9718546SMugunthan V N { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) }, 463d9718546SMugunthan V N { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) }, 464d9718546SMugunthan V N { "Net Octets", CPSW_STAT(netoctets) }, 465d9718546SMugunthan V N { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) }, 466d9718546SMugunthan V N { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) }, 467d9718546SMugunthan V N { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) }, 468d9718546SMugunthan V N }; 469d9718546SMugunthan V N 470e05107e6SIvan Khoronzhuk static const struct cpsw_stats cpsw_gstrings_ch_stats[] = { 471e05107e6SIvan Khoronzhuk { "head_enqueue", CPDMA_RX_STAT(head_enqueue) }, 472e05107e6SIvan Khoronzhuk { "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) }, 473e05107e6SIvan Khoronzhuk { "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) }, 474e05107e6SIvan Khoronzhuk { "misqueued", CPDMA_RX_STAT(misqueued) }, 475e05107e6SIvan Khoronzhuk { "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) }, 476e05107e6SIvan Khoronzhuk { "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) }, 477e05107e6SIvan Khoronzhuk { "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) }, 478e05107e6SIvan Khoronzhuk { "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) }, 479e05107e6SIvan Khoronzhuk { "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) }, 480e05107e6SIvan Khoronzhuk { "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) }, 481e05107e6SIvan Khoronzhuk { "good_dequeue", CPDMA_RX_STAT(good_dequeue) }, 482e05107e6SIvan Khoronzhuk { "requeue", CPDMA_RX_STAT(requeue) }, 483e05107e6SIvan Khoronzhuk { "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) }, 484e05107e6SIvan Khoronzhuk }; 485e05107e6SIvan Khoronzhuk 486e05107e6SIvan Khoronzhuk #define CPSW_STATS_COMMON_LEN ARRAY_SIZE(cpsw_gstrings_stats) 487e05107e6SIvan Khoronzhuk #define CPSW_STATS_CH_LEN ARRAY_SIZE(cpsw_gstrings_ch_stats) 488d9718546SMugunthan V N 489649a1688SIvan Khoronzhuk #define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw) 490dbc4ec52SIvan Khoronzhuk #define napi_to_cpsw(napi) container_of(napi, struct cpsw_common, napi) 491df828598SMugunthan V N #define for_each_slave(priv, func, arg...) \ 492df828598SMugunthan V N do { \ 4936e6ceaedSSebastian Siewior struct cpsw_slave *slave; \ 494606f3993SIvan Khoronzhuk struct cpsw_common *cpsw = (priv)->cpsw; \ 4956e6ceaedSSebastian Siewior int n; \ 496606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) \ 497606f3993SIvan Khoronzhuk (func)((cpsw)->slaves + priv->emac_port, ##arg);\ 498d9ba8f9eSMugunthan V N else \ 499606f3993SIvan Khoronzhuk for (n = cpsw->data.slaves, \ 500606f3993SIvan Khoronzhuk slave = cpsw->slaves; \ 5016e6ceaedSSebastian Siewior n; n--) \ 5026e6ceaedSSebastian Siewior (func)(slave++, ##arg); \ 503df828598SMugunthan V N } while (0) 504d9ba8f9eSMugunthan V N 5052a05a622SIvan Khoronzhuk #define cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb) \ 506d9ba8f9eSMugunthan V N do { \ 507606f3993SIvan Khoronzhuk if (!cpsw->data.dual_emac) \ 508d9ba8f9eSMugunthan V N break; \ 509d9ba8f9eSMugunthan V N if (CPDMA_RX_SOURCE_PORT(status) == 1) { \ 510606f3993SIvan Khoronzhuk ndev = cpsw->slaves[0].ndev; \ 511d9ba8f9eSMugunthan V N skb->dev = ndev; \ 512d9ba8f9eSMugunthan V N } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \ 513606f3993SIvan Khoronzhuk ndev = cpsw->slaves[1].ndev; \ 514d9ba8f9eSMugunthan V N skb->dev = ndev; \ 515d9ba8f9eSMugunthan V N } \ 516d9ba8f9eSMugunthan V N } while (0) 517606f3993SIvan Khoronzhuk #define cpsw_add_mcast(cpsw, priv, addr) \ 518d9ba8f9eSMugunthan V N do { \ 519606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) { \ 520606f3993SIvan Khoronzhuk struct cpsw_slave *slave = cpsw->slaves + \ 521d9ba8f9eSMugunthan V N priv->emac_port; \ 5226f1f5836SIvan Khoronzhuk int slave_port = cpsw_get_slave_port( \ 523d9ba8f9eSMugunthan V N slave->slave_num); \ 5242a05a622SIvan Khoronzhuk cpsw_ale_add_mcast(cpsw->ale, addr, \ 52571a2cbb7SGrygorii Strashko 1 << slave_port | ALE_PORT_HOST, \ 526d9ba8f9eSMugunthan V N ALE_VLAN, slave->port_vlan, 0); \ 527d9ba8f9eSMugunthan V N } else { \ 5282a05a622SIvan Khoronzhuk cpsw_ale_add_mcast(cpsw->ale, addr, \ 52961f1cef9SGrygorii Strashko ALE_ALL_PORTS, \ 530d9ba8f9eSMugunthan V N 0, 0, 0); \ 531d9ba8f9eSMugunthan V N } \ 532d9ba8f9eSMugunthan V N } while (0) 533d9ba8f9eSMugunthan V N 5346f1f5836SIvan Khoronzhuk static inline int cpsw_get_slave_port(u32 slave_num) 535d9ba8f9eSMugunthan V N { 536d9ba8f9eSMugunthan V N return slave_num + 1; 537d9ba8f9eSMugunthan V N } 538df828598SMugunthan V N 5390cd8f9ccSMugunthan V N static void cpsw_set_promiscious(struct net_device *ndev, bool enable) 5400cd8f9ccSMugunthan V N { 5412a05a622SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 5422a05a622SIvan Khoronzhuk struct cpsw_ale *ale = cpsw->ale; 5430cd8f9ccSMugunthan V N int i; 5440cd8f9ccSMugunthan V N 545606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) { 5460cd8f9ccSMugunthan V N bool flag = false; 5470cd8f9ccSMugunthan V N 5480cd8f9ccSMugunthan V N /* Enabling promiscuous mode for one interface will be 5490cd8f9ccSMugunthan V N * common for both the interface as the interface shares 5500cd8f9ccSMugunthan V N * the same hardware resource. 5510cd8f9ccSMugunthan V N */ 552606f3993SIvan Khoronzhuk for (i = 0; i < cpsw->data.slaves; i++) 553606f3993SIvan Khoronzhuk if (cpsw->slaves[i].ndev->flags & IFF_PROMISC) 5540cd8f9ccSMugunthan V N flag = true; 5550cd8f9ccSMugunthan V N 5560cd8f9ccSMugunthan V N if (!enable && flag) { 5570cd8f9ccSMugunthan V N enable = true; 5580cd8f9ccSMugunthan V N dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n"); 5590cd8f9ccSMugunthan V N } 5600cd8f9ccSMugunthan V N 5610cd8f9ccSMugunthan V N if (enable) { 5620cd8f9ccSMugunthan V N /* Enable Bypass */ 5630cd8f9ccSMugunthan V N cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1); 5640cd8f9ccSMugunthan V N 5650cd8f9ccSMugunthan V N dev_dbg(&ndev->dev, "promiscuity enabled\n"); 5660cd8f9ccSMugunthan V N } else { 5670cd8f9ccSMugunthan V N /* Disable Bypass */ 5680cd8f9ccSMugunthan V N cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0); 5690cd8f9ccSMugunthan V N dev_dbg(&ndev->dev, "promiscuity disabled\n"); 5700cd8f9ccSMugunthan V N } 5710cd8f9ccSMugunthan V N } else { 5720cd8f9ccSMugunthan V N if (enable) { 5730cd8f9ccSMugunthan V N unsigned long timeout = jiffies + HZ; 5740cd8f9ccSMugunthan V N 5756f979eb3SLennart Sorensen /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */ 576606f3993SIvan Khoronzhuk for (i = 0; i <= cpsw->data.slaves; i++) { 5770cd8f9ccSMugunthan V N cpsw_ale_control_set(ale, i, 5780cd8f9ccSMugunthan V N ALE_PORT_NOLEARN, 1); 5790cd8f9ccSMugunthan V N cpsw_ale_control_set(ale, i, 5800cd8f9ccSMugunthan V N ALE_PORT_NO_SA_UPDATE, 1); 5810cd8f9ccSMugunthan V N } 5820cd8f9ccSMugunthan V N 5830cd8f9ccSMugunthan V N /* Clear All Untouched entries */ 5840cd8f9ccSMugunthan V N cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1); 5850cd8f9ccSMugunthan V N do { 5860cd8f9ccSMugunthan V N cpu_relax(); 5870cd8f9ccSMugunthan V N if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT)) 5880cd8f9ccSMugunthan V N break; 5890cd8f9ccSMugunthan V N } while (time_after(timeout, jiffies)); 5900cd8f9ccSMugunthan V N cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1); 5910cd8f9ccSMugunthan V N 5920cd8f9ccSMugunthan V N /* Clear all mcast from ALE */ 59361f1cef9SGrygorii Strashko cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1); 5940cd8f9ccSMugunthan V N 5950cd8f9ccSMugunthan V N /* Flood All Unicast Packets to Host port */ 5960cd8f9ccSMugunthan V N cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1); 5970cd8f9ccSMugunthan V N dev_dbg(&ndev->dev, "promiscuity enabled\n"); 5980cd8f9ccSMugunthan V N } else { 5996f979eb3SLennart Sorensen /* Don't Flood All Unicast Packets to Host port */ 6000cd8f9ccSMugunthan V N cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0); 6010cd8f9ccSMugunthan V N 6026f979eb3SLennart Sorensen /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */ 603606f3993SIvan Khoronzhuk for (i = 0; i <= cpsw->data.slaves; i++) { 6040cd8f9ccSMugunthan V N cpsw_ale_control_set(ale, i, 6050cd8f9ccSMugunthan V N ALE_PORT_NOLEARN, 0); 6060cd8f9ccSMugunthan V N cpsw_ale_control_set(ale, i, 6070cd8f9ccSMugunthan V N ALE_PORT_NO_SA_UPDATE, 0); 6080cd8f9ccSMugunthan V N } 6090cd8f9ccSMugunthan V N dev_dbg(&ndev->dev, "promiscuity disabled\n"); 6100cd8f9ccSMugunthan V N } 6110cd8f9ccSMugunthan V N } 6120cd8f9ccSMugunthan V N } 6130cd8f9ccSMugunthan V N 6145c50a856SMugunthan V N static void cpsw_ndo_set_rx_mode(struct net_device *ndev) 6155c50a856SMugunthan V N { 6165c50a856SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 617606f3993SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 61825906052SMugunthan V N int vid; 61925906052SMugunthan V N 620606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) 621606f3993SIvan Khoronzhuk vid = cpsw->slaves[priv->emac_port].port_vlan; 62225906052SMugunthan V N else 623606f3993SIvan Khoronzhuk vid = cpsw->data.default_vlan; 6245c50a856SMugunthan V N 6255c50a856SMugunthan V N if (ndev->flags & IFF_PROMISC) { 6265c50a856SMugunthan V N /* Enable promiscuous mode */ 6270cd8f9ccSMugunthan V N cpsw_set_promiscious(ndev, true); 6282a05a622SIvan Khoronzhuk cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI); 6295c50a856SMugunthan V N return; 6300cd8f9ccSMugunthan V N } else { 6310cd8f9ccSMugunthan V N /* Disable promiscuous mode */ 6320cd8f9ccSMugunthan V N cpsw_set_promiscious(ndev, false); 6335c50a856SMugunthan V N } 6345c50a856SMugunthan V N 6351e5c4bc4SLennart Sorensen /* Restore allmulti on vlans if necessary */ 6362a05a622SIvan Khoronzhuk cpsw_ale_set_allmulti(cpsw->ale, priv->ndev->flags & IFF_ALLMULTI); 6371e5c4bc4SLennart Sorensen 6385c50a856SMugunthan V N /* Clear all mcast from ALE */ 6392a05a622SIvan Khoronzhuk cpsw_ale_flush_multicast(cpsw->ale, ALE_ALL_PORTS, vid); 6405c50a856SMugunthan V N 6415c50a856SMugunthan V N if (!netdev_mc_empty(ndev)) { 6425c50a856SMugunthan V N struct netdev_hw_addr *ha; 6435c50a856SMugunthan V N 6445c50a856SMugunthan V N /* program multicast address list into ALE register */ 6455c50a856SMugunthan V N netdev_for_each_mc_addr(ha, ndev) { 646606f3993SIvan Khoronzhuk cpsw_add_mcast(cpsw, priv, (u8 *)ha->addr); 6475c50a856SMugunthan V N } 6485c50a856SMugunthan V N } 6495c50a856SMugunthan V N } 6505c50a856SMugunthan V N 6512c836bd9SIvan Khoronzhuk static void cpsw_intr_enable(struct cpsw_common *cpsw) 652df828598SMugunthan V N { 6535d8d0d4dSIvan Khoronzhuk __raw_writel(0xFF, &cpsw->wr_regs->tx_en); 6545d8d0d4dSIvan Khoronzhuk __raw_writel(0xFF, &cpsw->wr_regs->rx_en); 655df828598SMugunthan V N 6562c836bd9SIvan Khoronzhuk cpdma_ctlr_int_ctrl(cpsw->dma, true); 657df828598SMugunthan V N return; 658df828598SMugunthan V N } 659df828598SMugunthan V N 6602c836bd9SIvan Khoronzhuk static void cpsw_intr_disable(struct cpsw_common *cpsw) 661df828598SMugunthan V N { 6625d8d0d4dSIvan Khoronzhuk __raw_writel(0, &cpsw->wr_regs->tx_en); 6635d8d0d4dSIvan Khoronzhuk __raw_writel(0, &cpsw->wr_regs->rx_en); 664df828598SMugunthan V N 6652c836bd9SIvan Khoronzhuk cpdma_ctlr_int_ctrl(cpsw->dma, false); 666df828598SMugunthan V N return; 667df828598SMugunthan V N } 668df828598SMugunthan V N 6691a3b5056SOlof Johansson static void cpsw_tx_handler(void *token, int len, int status) 670df828598SMugunthan V N { 671e05107e6SIvan Khoronzhuk struct netdev_queue *txq; 672df828598SMugunthan V N struct sk_buff *skb = token; 673df828598SMugunthan V N struct net_device *ndev = skb->dev; 6742a05a622SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 675df828598SMugunthan V N 676fae50823SMugunthan V N /* Check whether the queue is stopped due to stalled tx dma, if the 677fae50823SMugunthan V N * queue is stopped then start the queue as we have free desc for tx 678fae50823SMugunthan V N */ 679e05107e6SIvan Khoronzhuk txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb)); 680e05107e6SIvan Khoronzhuk if (unlikely(netif_tx_queue_stopped(txq))) 681e05107e6SIvan Khoronzhuk netif_tx_wake_queue(txq); 682e05107e6SIvan Khoronzhuk 6832a05a622SIvan Khoronzhuk cpts_tx_timestamp(cpsw->cpts, skb); 6848dc43ddcSTobias Klauser ndev->stats.tx_packets++; 6858dc43ddcSTobias Klauser ndev->stats.tx_bytes += len; 686df828598SMugunthan V N dev_kfree_skb_any(skb); 687df828598SMugunthan V N } 688df828598SMugunthan V N 6891a3b5056SOlof Johansson static void cpsw_rx_handler(void *token, int len, int status) 690df828598SMugunthan V N { 691e05107e6SIvan Khoronzhuk struct cpdma_chan *ch; 692df828598SMugunthan V N struct sk_buff *skb = token; 693b4727e69SSebastian Siewior struct sk_buff *new_skb; 694df828598SMugunthan V N struct net_device *ndev = skb->dev; 695df828598SMugunthan V N int ret = 0; 6962a05a622SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 697df828598SMugunthan V N 6982a05a622SIvan Khoronzhuk cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb); 699d9ba8f9eSMugunthan V N 70016e5c57dSMugunthan V N if (unlikely(status < 0) || unlikely(!netif_running(ndev))) { 701a0e2c822SMugunthan V N bool ndev_status = false; 702606f3993SIvan Khoronzhuk struct cpsw_slave *slave = cpsw->slaves; 703a0e2c822SMugunthan V N int n; 704a0e2c822SMugunthan V N 705606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) { 706a0e2c822SMugunthan V N /* In dual emac mode check for all interfaces */ 707606f3993SIvan Khoronzhuk for (n = cpsw->data.slaves; n; n--, slave++) 708a0e2c822SMugunthan V N if (netif_running(slave->ndev)) 709a0e2c822SMugunthan V N ndev_status = true; 710a0e2c822SMugunthan V N } 711a0e2c822SMugunthan V N 712a0e2c822SMugunthan V N if (ndev_status && (status >= 0)) { 713a0e2c822SMugunthan V N /* The packet received is for the interface which 714a0e2c822SMugunthan V N * is already down and the other interface is up 715dbedd44eSJoe Perches * and running, instead of freeing which results 716a0e2c822SMugunthan V N * in reducing of the number of rx descriptor in 717a0e2c822SMugunthan V N * DMA engine, requeue skb back to cpdma. 718a0e2c822SMugunthan V N */ 719a0e2c822SMugunthan V N new_skb = skb; 720a0e2c822SMugunthan V N goto requeue; 721a0e2c822SMugunthan V N } 722a0e2c822SMugunthan V N 723b4727e69SSebastian Siewior /* the interface is going down, skbs are purged */ 724df828598SMugunthan V N dev_kfree_skb_any(skb); 725df828598SMugunthan V N return; 726df828598SMugunthan V N } 727b4727e69SSebastian Siewior 7282a05a622SIvan Khoronzhuk new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max); 729b4727e69SSebastian Siewior if (new_skb) { 730e05107e6SIvan Khoronzhuk skb_copy_queue_mapping(new_skb, skb); 731df828598SMugunthan V N skb_put(skb, len); 7322a05a622SIvan Khoronzhuk cpts_rx_timestamp(cpsw->cpts, skb); 733df828598SMugunthan V N skb->protocol = eth_type_trans(skb, ndev); 734df828598SMugunthan V N netif_receive_skb(skb); 7358dc43ddcSTobias Klauser ndev->stats.rx_bytes += len; 7368dc43ddcSTobias Klauser ndev->stats.rx_packets++; 737254a49d5SGrygorii Strashko kmemleak_not_leak(new_skb); 738b4727e69SSebastian Siewior } else { 7398dc43ddcSTobias Klauser ndev->stats.rx_dropped++; 740b4727e69SSebastian Siewior new_skb = skb; 741df828598SMugunthan V N } 742df828598SMugunthan V N 743a0e2c822SMugunthan V N requeue: 744ce52c744SIvan Khoronzhuk if (netif_dormant(ndev)) { 745ce52c744SIvan Khoronzhuk dev_kfree_skb_any(new_skb); 746ce52c744SIvan Khoronzhuk return; 747ce52c744SIvan Khoronzhuk } 748ce52c744SIvan Khoronzhuk 749*8feb0a19SIvan Khoronzhuk ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch; 750e05107e6SIvan Khoronzhuk ret = cpdma_chan_submit(ch, new_skb, new_skb->data, 751b4727e69SSebastian Siewior skb_tailroom(new_skb), 0); 752b4727e69SSebastian Siewior if (WARN_ON(ret < 0)) 753b4727e69SSebastian Siewior dev_kfree_skb_any(new_skb); 754df828598SMugunthan V N } 755df828598SMugunthan V N 756c03abd84SFelipe Balbi static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id) 757df828598SMugunthan V N { 758dbc4ec52SIvan Khoronzhuk struct cpsw_common *cpsw = dev_id; 7597ce67a38SFelipe Balbi 7605d8d0d4dSIvan Khoronzhuk writel(0, &cpsw->wr_regs->tx_en); 7612c836bd9SIvan Khoronzhuk cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX); 762c03abd84SFelipe Balbi 763e38b5a3dSIvan Khoronzhuk if (cpsw->quirk_irq) { 764e38b5a3dSIvan Khoronzhuk disable_irq_nosync(cpsw->irqs_table[1]); 765e38b5a3dSIvan Khoronzhuk cpsw->tx_irq_disabled = true; 7667da11600SMugunthan V N } 7677da11600SMugunthan V N 768dbc4ec52SIvan Khoronzhuk napi_schedule(&cpsw->napi_tx); 769c03abd84SFelipe Balbi return IRQ_HANDLED; 770c03abd84SFelipe Balbi } 771c03abd84SFelipe Balbi 772c03abd84SFelipe Balbi static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id) 773c03abd84SFelipe Balbi { 774dbc4ec52SIvan Khoronzhuk struct cpsw_common *cpsw = dev_id; 775c03abd84SFelipe Balbi 7762c836bd9SIvan Khoronzhuk cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX); 7775d8d0d4dSIvan Khoronzhuk writel(0, &cpsw->wr_regs->rx_en); 778fd51cf19SSebastian Siewior 779e38b5a3dSIvan Khoronzhuk if (cpsw->quirk_irq) { 780e38b5a3dSIvan Khoronzhuk disable_irq_nosync(cpsw->irqs_table[0]); 781e38b5a3dSIvan Khoronzhuk cpsw->rx_irq_disabled = true; 7827da11600SMugunthan V N } 7837da11600SMugunthan V N 784dbc4ec52SIvan Khoronzhuk napi_schedule(&cpsw->napi_rx); 785df828598SMugunthan V N return IRQ_HANDLED; 786df828598SMugunthan V N } 787df828598SMugunthan V N 78832a7432cSMugunthan V N static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget) 789df828598SMugunthan V N { 790e05107e6SIvan Khoronzhuk u32 ch_map; 791*8feb0a19SIvan Khoronzhuk int num_tx, cur_budget, ch; 792dbc4ec52SIvan Khoronzhuk struct cpsw_common *cpsw = napi_to_cpsw(napi_tx); 793*8feb0a19SIvan Khoronzhuk struct cpsw_vector *txv; 79432a7432cSMugunthan V N 795e05107e6SIvan Khoronzhuk /* process every unprocessed channel */ 796e05107e6SIvan Khoronzhuk ch_map = cpdma_ctrl_txchs_state(cpsw->dma); 797342934a5SIvan Khoronzhuk for (ch = 0, num_tx = 0; ch_map; ch_map >>= 1, ch++) { 798e05107e6SIvan Khoronzhuk if (!(ch_map & 0x01)) 799e05107e6SIvan Khoronzhuk continue; 800e05107e6SIvan Khoronzhuk 801*8feb0a19SIvan Khoronzhuk txv = &cpsw->txv[ch]; 802*8feb0a19SIvan Khoronzhuk if (unlikely(txv->budget > budget - num_tx)) 803*8feb0a19SIvan Khoronzhuk cur_budget = budget - num_tx; 804*8feb0a19SIvan Khoronzhuk else 805*8feb0a19SIvan Khoronzhuk cur_budget = txv->budget; 806*8feb0a19SIvan Khoronzhuk 807*8feb0a19SIvan Khoronzhuk num_tx += cpdma_chan_process(txv->ch, cur_budget); 808342934a5SIvan Khoronzhuk if (num_tx >= budget) 809342934a5SIvan Khoronzhuk break; 810e05107e6SIvan Khoronzhuk } 811e05107e6SIvan Khoronzhuk 81232a7432cSMugunthan V N if (num_tx < budget) { 81332a7432cSMugunthan V N napi_complete(napi_tx); 8145d8d0d4dSIvan Khoronzhuk writel(0xff, &cpsw->wr_regs->tx_en); 815e38b5a3dSIvan Khoronzhuk if (cpsw->quirk_irq && cpsw->tx_irq_disabled) { 816e38b5a3dSIvan Khoronzhuk cpsw->tx_irq_disabled = false; 817e38b5a3dSIvan Khoronzhuk enable_irq(cpsw->irqs_table[1]); 8187da11600SMugunthan V N } 81932a7432cSMugunthan V N } 82032a7432cSMugunthan V N 82132a7432cSMugunthan V N return num_tx; 82232a7432cSMugunthan V N } 82332a7432cSMugunthan V N 82432a7432cSMugunthan V N static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget) 82532a7432cSMugunthan V N { 826e05107e6SIvan Khoronzhuk u32 ch_map; 827*8feb0a19SIvan Khoronzhuk int num_rx, cur_budget, ch; 828dbc4ec52SIvan Khoronzhuk struct cpsw_common *cpsw = napi_to_cpsw(napi_rx); 829*8feb0a19SIvan Khoronzhuk struct cpsw_vector *rxv; 830510a1e72SMugunthan V N 831e05107e6SIvan Khoronzhuk /* process every unprocessed channel */ 832e05107e6SIvan Khoronzhuk ch_map = cpdma_ctrl_rxchs_state(cpsw->dma); 833342934a5SIvan Khoronzhuk for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) { 834e05107e6SIvan Khoronzhuk if (!(ch_map & 0x01)) 835e05107e6SIvan Khoronzhuk continue; 836e05107e6SIvan Khoronzhuk 837*8feb0a19SIvan Khoronzhuk rxv = &cpsw->rxv[ch]; 838*8feb0a19SIvan Khoronzhuk if (unlikely(rxv->budget > budget - num_rx)) 839*8feb0a19SIvan Khoronzhuk cur_budget = budget - num_rx; 840*8feb0a19SIvan Khoronzhuk else 841*8feb0a19SIvan Khoronzhuk cur_budget = rxv->budget; 842*8feb0a19SIvan Khoronzhuk 843*8feb0a19SIvan Khoronzhuk num_rx += cpdma_chan_process(rxv->ch, cur_budget); 844342934a5SIvan Khoronzhuk if (num_rx >= budget) 845342934a5SIvan Khoronzhuk break; 846e05107e6SIvan Khoronzhuk } 847e05107e6SIvan Khoronzhuk 848510a1e72SMugunthan V N if (num_rx < budget) { 84932a7432cSMugunthan V N napi_complete(napi_rx); 8505d8d0d4dSIvan Khoronzhuk writel(0xff, &cpsw->wr_regs->rx_en); 851e38b5a3dSIvan Khoronzhuk if (cpsw->quirk_irq && cpsw->rx_irq_disabled) { 852e38b5a3dSIvan Khoronzhuk cpsw->rx_irq_disabled = false; 853e38b5a3dSIvan Khoronzhuk enable_irq(cpsw->irqs_table[0]); 8547da11600SMugunthan V N } 855510a1e72SMugunthan V N } 856df828598SMugunthan V N 857df828598SMugunthan V N return num_rx; 858df828598SMugunthan V N } 859df828598SMugunthan V N 860df828598SMugunthan V N static inline void soft_reset(const char *module, void __iomem *reg) 861df828598SMugunthan V N { 862df828598SMugunthan V N unsigned long timeout = jiffies + HZ; 863df828598SMugunthan V N 864df828598SMugunthan V N __raw_writel(1, reg); 865df828598SMugunthan V N do { 866df828598SMugunthan V N cpu_relax(); 867df828598SMugunthan V N } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies)); 868df828598SMugunthan V N 869df828598SMugunthan V N WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module); 870df828598SMugunthan V N } 871df828598SMugunthan V N 872df828598SMugunthan V N #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \ 873df828598SMugunthan V N ((mac)[2] << 16) | ((mac)[3] << 24)) 874df828598SMugunthan V N #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8)) 875df828598SMugunthan V N 876df828598SMugunthan V N static void cpsw_set_slave_mac(struct cpsw_slave *slave, 877df828598SMugunthan V N struct cpsw_priv *priv) 878df828598SMugunthan V N { 8799750a3adSRichard Cochran slave_write(slave, mac_hi(priv->mac_addr), SA_HI); 8809750a3adSRichard Cochran slave_write(slave, mac_lo(priv->mac_addr), SA_LO); 881df828598SMugunthan V N } 882df828598SMugunthan V N 883df828598SMugunthan V N static void _cpsw_adjust_link(struct cpsw_slave *slave, 884df828598SMugunthan V N struct cpsw_priv *priv, bool *link) 885df828598SMugunthan V N { 886df828598SMugunthan V N struct phy_device *phy = slave->phy; 887df828598SMugunthan V N u32 mac_control = 0; 888df828598SMugunthan V N u32 slave_port; 889606f3993SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 890df828598SMugunthan V N 891df828598SMugunthan V N if (!phy) 892df828598SMugunthan V N return; 893df828598SMugunthan V N 8946f1f5836SIvan Khoronzhuk slave_port = cpsw_get_slave_port(slave->slave_num); 895df828598SMugunthan V N 896df828598SMugunthan V N if (phy->link) { 897606f3993SIvan Khoronzhuk mac_control = cpsw->data.mac_control; 898df828598SMugunthan V N 899df828598SMugunthan V N /* enable forwarding */ 9002a05a622SIvan Khoronzhuk cpsw_ale_control_set(cpsw->ale, slave_port, 901df828598SMugunthan V N ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 902df828598SMugunthan V N 903df828598SMugunthan V N if (phy->speed == 1000) 904df828598SMugunthan V N mac_control |= BIT(7); /* GIGABITEN */ 905df828598SMugunthan V N if (phy->duplex) 906df828598SMugunthan V N mac_control |= BIT(0); /* FULLDUPLEXEN */ 907342b7b74SDaniel Mack 908342b7b74SDaniel Mack /* set speed_in input in case RMII mode is used in 100Mbps */ 909342b7b74SDaniel Mack if (phy->speed == 100) 910342b7b74SDaniel Mack mac_control |= BIT(15); 911a81d8762SMugunthan V N else if (phy->speed == 10) 912a81d8762SMugunthan V N mac_control |= BIT(18); /* In Band mode */ 913342b7b74SDaniel Mack 9141923d6e4SMugunthan V N if (priv->rx_pause) 9151923d6e4SMugunthan V N mac_control |= BIT(3); 9161923d6e4SMugunthan V N 9171923d6e4SMugunthan V N if (priv->tx_pause) 9181923d6e4SMugunthan V N mac_control |= BIT(4); 9191923d6e4SMugunthan V N 920df828598SMugunthan V N *link = true; 921df828598SMugunthan V N } else { 922df828598SMugunthan V N mac_control = 0; 923df828598SMugunthan V N /* disable forwarding */ 9242a05a622SIvan Khoronzhuk cpsw_ale_control_set(cpsw->ale, slave_port, 925df828598SMugunthan V N ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 926df828598SMugunthan V N } 927df828598SMugunthan V N 928df828598SMugunthan V N if (mac_control != slave->mac_control) { 929df828598SMugunthan V N phy_print_status(phy); 930df828598SMugunthan V N __raw_writel(mac_control, &slave->sliver->mac_control); 931df828598SMugunthan V N } 932df828598SMugunthan V N 933df828598SMugunthan V N slave->mac_control = mac_control; 934df828598SMugunthan V N } 935df828598SMugunthan V N 936df828598SMugunthan V N static void cpsw_adjust_link(struct net_device *ndev) 937df828598SMugunthan V N { 938df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 939df828598SMugunthan V N bool link = false; 940df828598SMugunthan V N 941df828598SMugunthan V N for_each_slave(priv, _cpsw_adjust_link, priv, &link); 942df828598SMugunthan V N 943df828598SMugunthan V N if (link) { 944df828598SMugunthan V N netif_carrier_on(ndev); 945df828598SMugunthan V N if (netif_running(ndev)) 946e05107e6SIvan Khoronzhuk netif_tx_wake_all_queues(ndev); 947df828598SMugunthan V N } else { 948df828598SMugunthan V N netif_carrier_off(ndev); 949e05107e6SIvan Khoronzhuk netif_tx_stop_all_queues(ndev); 950df828598SMugunthan V N } 951df828598SMugunthan V N } 952df828598SMugunthan V N 953ff5b8ef2SMugunthan V N static int cpsw_get_coalesce(struct net_device *ndev, 954ff5b8ef2SMugunthan V N struct ethtool_coalesce *coal) 955ff5b8ef2SMugunthan V N { 9562a05a622SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 957ff5b8ef2SMugunthan V N 9582a05a622SIvan Khoronzhuk coal->rx_coalesce_usecs = cpsw->coal_intvl; 959ff5b8ef2SMugunthan V N return 0; 960ff5b8ef2SMugunthan V N } 961ff5b8ef2SMugunthan V N 962ff5b8ef2SMugunthan V N static int cpsw_set_coalesce(struct net_device *ndev, 963ff5b8ef2SMugunthan V N struct ethtool_coalesce *coal) 964ff5b8ef2SMugunthan V N { 965ff5b8ef2SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 966ff5b8ef2SMugunthan V N u32 int_ctrl; 967ff5b8ef2SMugunthan V N u32 num_interrupts = 0; 968ff5b8ef2SMugunthan V N u32 prescale = 0; 969ff5b8ef2SMugunthan V N u32 addnl_dvdr = 1; 970ff5b8ef2SMugunthan V N u32 coal_intvl = 0; 9715d8d0d4dSIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 972ff5b8ef2SMugunthan V N 973ff5b8ef2SMugunthan V N coal_intvl = coal->rx_coalesce_usecs; 974ff5b8ef2SMugunthan V N 9755d8d0d4dSIvan Khoronzhuk int_ctrl = readl(&cpsw->wr_regs->int_control); 9762a05a622SIvan Khoronzhuk prescale = cpsw->bus_freq_mhz * 4; 977ff5b8ef2SMugunthan V N 978a84bc2a9SMugunthan V N if (!coal->rx_coalesce_usecs) { 979a84bc2a9SMugunthan V N int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN); 980a84bc2a9SMugunthan V N goto update_return; 981a84bc2a9SMugunthan V N } 982a84bc2a9SMugunthan V N 983ff5b8ef2SMugunthan V N if (coal_intvl < CPSW_CMINTMIN_INTVL) 984ff5b8ef2SMugunthan V N coal_intvl = CPSW_CMINTMIN_INTVL; 985ff5b8ef2SMugunthan V N 986ff5b8ef2SMugunthan V N if (coal_intvl > CPSW_CMINTMAX_INTVL) { 987ff5b8ef2SMugunthan V N /* Interrupt pacer works with 4us Pulse, we can 988ff5b8ef2SMugunthan V N * throttle further by dilating the 4us pulse. 989ff5b8ef2SMugunthan V N */ 990ff5b8ef2SMugunthan V N addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale; 991ff5b8ef2SMugunthan V N 992ff5b8ef2SMugunthan V N if (addnl_dvdr > 1) { 993ff5b8ef2SMugunthan V N prescale *= addnl_dvdr; 994ff5b8ef2SMugunthan V N if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr)) 995ff5b8ef2SMugunthan V N coal_intvl = (CPSW_CMINTMAX_INTVL 996ff5b8ef2SMugunthan V N * addnl_dvdr); 997ff5b8ef2SMugunthan V N } else { 998ff5b8ef2SMugunthan V N addnl_dvdr = 1; 999ff5b8ef2SMugunthan V N coal_intvl = CPSW_CMINTMAX_INTVL; 1000ff5b8ef2SMugunthan V N } 1001ff5b8ef2SMugunthan V N } 1002ff5b8ef2SMugunthan V N 1003ff5b8ef2SMugunthan V N num_interrupts = (1000 * addnl_dvdr) / coal_intvl; 10045d8d0d4dSIvan Khoronzhuk writel(num_interrupts, &cpsw->wr_regs->rx_imax); 10055d8d0d4dSIvan Khoronzhuk writel(num_interrupts, &cpsw->wr_regs->tx_imax); 1006ff5b8ef2SMugunthan V N 1007ff5b8ef2SMugunthan V N int_ctrl |= CPSW_INTPACEEN; 1008ff5b8ef2SMugunthan V N int_ctrl &= (~CPSW_INTPRESCALE_MASK); 1009ff5b8ef2SMugunthan V N int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK); 1010a84bc2a9SMugunthan V N 1011a84bc2a9SMugunthan V N update_return: 10125d8d0d4dSIvan Khoronzhuk writel(int_ctrl, &cpsw->wr_regs->int_control); 1013ff5b8ef2SMugunthan V N 1014ff5b8ef2SMugunthan V N cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl); 10152a05a622SIvan Khoronzhuk cpsw->coal_intvl = coal_intvl; 1016ff5b8ef2SMugunthan V N 1017ff5b8ef2SMugunthan V N return 0; 1018ff5b8ef2SMugunthan V N } 1019ff5b8ef2SMugunthan V N 1020d9718546SMugunthan V N static int cpsw_get_sset_count(struct net_device *ndev, int sset) 1021d9718546SMugunthan V N { 1022e05107e6SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 1023e05107e6SIvan Khoronzhuk 1024d9718546SMugunthan V N switch (sset) { 1025d9718546SMugunthan V N case ETH_SS_STATS: 1026e05107e6SIvan Khoronzhuk return (CPSW_STATS_COMMON_LEN + 1027e05107e6SIvan Khoronzhuk (cpsw->rx_ch_num + cpsw->tx_ch_num) * 1028e05107e6SIvan Khoronzhuk CPSW_STATS_CH_LEN); 1029d9718546SMugunthan V N default: 1030d9718546SMugunthan V N return -EOPNOTSUPP; 1031d9718546SMugunthan V N } 1032d9718546SMugunthan V N } 1033d9718546SMugunthan V N 1034e05107e6SIvan Khoronzhuk static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir) 1035e05107e6SIvan Khoronzhuk { 1036e05107e6SIvan Khoronzhuk int ch_stats_len; 1037e05107e6SIvan Khoronzhuk int line; 1038e05107e6SIvan Khoronzhuk int i; 1039e05107e6SIvan Khoronzhuk 1040e05107e6SIvan Khoronzhuk ch_stats_len = CPSW_STATS_CH_LEN * ch_num; 1041e05107e6SIvan Khoronzhuk for (i = 0; i < ch_stats_len; i++) { 1042e05107e6SIvan Khoronzhuk line = i % CPSW_STATS_CH_LEN; 1043e05107e6SIvan Khoronzhuk snprintf(*p, ETH_GSTRING_LEN, 1044e05107e6SIvan Khoronzhuk "%s DMA chan %d: %s", rx_dir ? "Rx" : "Tx", 1045e05107e6SIvan Khoronzhuk i / CPSW_STATS_CH_LEN, 1046e05107e6SIvan Khoronzhuk cpsw_gstrings_ch_stats[line].stat_string); 1047e05107e6SIvan Khoronzhuk *p += ETH_GSTRING_LEN; 1048e05107e6SIvan Khoronzhuk } 1049e05107e6SIvan Khoronzhuk } 1050e05107e6SIvan Khoronzhuk 1051d9718546SMugunthan V N static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data) 1052d9718546SMugunthan V N { 1053e05107e6SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 1054d9718546SMugunthan V N u8 *p = data; 1055d9718546SMugunthan V N int i; 1056d9718546SMugunthan V N 1057d9718546SMugunthan V N switch (stringset) { 1058d9718546SMugunthan V N case ETH_SS_STATS: 1059e05107e6SIvan Khoronzhuk for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) { 1060d9718546SMugunthan V N memcpy(p, cpsw_gstrings_stats[i].stat_string, 1061d9718546SMugunthan V N ETH_GSTRING_LEN); 1062d9718546SMugunthan V N p += ETH_GSTRING_LEN; 1063d9718546SMugunthan V N } 1064e05107e6SIvan Khoronzhuk 1065e05107e6SIvan Khoronzhuk cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1); 1066e05107e6SIvan Khoronzhuk cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0); 1067d9718546SMugunthan V N break; 1068d9718546SMugunthan V N } 1069d9718546SMugunthan V N } 1070d9718546SMugunthan V N 1071d9718546SMugunthan V N static void cpsw_get_ethtool_stats(struct net_device *ndev, 1072d9718546SMugunthan V N struct ethtool_stats *stats, u64 *data) 1073d9718546SMugunthan V N { 1074d9718546SMugunthan V N u8 *p; 10752c836bd9SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 1076e05107e6SIvan Khoronzhuk struct cpdma_chan_stats ch_stats; 1077e05107e6SIvan Khoronzhuk int i, l, ch; 1078d9718546SMugunthan V N 1079d9718546SMugunthan V N /* Collect Davinci CPDMA stats for Rx and Tx Channel */ 1080e05107e6SIvan Khoronzhuk for (l = 0; l < CPSW_STATS_COMMON_LEN; l++) 1081e05107e6SIvan Khoronzhuk data[l] = readl(cpsw->hw_stats + 1082e05107e6SIvan Khoronzhuk cpsw_gstrings_stats[l].stat_offset); 1083d9718546SMugunthan V N 1084e05107e6SIvan Khoronzhuk for (ch = 0; ch < cpsw->rx_ch_num; ch++) { 1085*8feb0a19SIvan Khoronzhuk cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats); 1086e05107e6SIvan Khoronzhuk for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) { 1087e05107e6SIvan Khoronzhuk p = (u8 *)&ch_stats + 1088e05107e6SIvan Khoronzhuk cpsw_gstrings_ch_stats[i].stat_offset; 1089e05107e6SIvan Khoronzhuk data[l] = *(u32 *)p; 1090e05107e6SIvan Khoronzhuk } 1091e05107e6SIvan Khoronzhuk } 1092d9718546SMugunthan V N 1093e05107e6SIvan Khoronzhuk for (ch = 0; ch < cpsw->tx_ch_num; ch++) { 1094*8feb0a19SIvan Khoronzhuk cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats); 1095e05107e6SIvan Khoronzhuk for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) { 1096e05107e6SIvan Khoronzhuk p = (u8 *)&ch_stats + 1097e05107e6SIvan Khoronzhuk cpsw_gstrings_ch_stats[i].stat_offset; 1098e05107e6SIvan Khoronzhuk data[l] = *(u32 *)p; 1099d9718546SMugunthan V N } 1100d9718546SMugunthan V N } 1101d9718546SMugunthan V N } 1102d9718546SMugunthan V N 1103606f3993SIvan Khoronzhuk static int cpsw_common_res_usage_state(struct cpsw_common *cpsw) 1104d9ba8f9eSMugunthan V N { 1105d9ba8f9eSMugunthan V N u32 i; 1106d9ba8f9eSMugunthan V N u32 usage_count = 0; 1107d9ba8f9eSMugunthan V N 1108606f3993SIvan Khoronzhuk if (!cpsw->data.dual_emac) 1109d9ba8f9eSMugunthan V N return 0; 1110d9ba8f9eSMugunthan V N 1111606f3993SIvan Khoronzhuk for (i = 0; i < cpsw->data.slaves; i++) 1112606f3993SIvan Khoronzhuk if (cpsw->slaves[i].open_stat) 1113d9ba8f9eSMugunthan V N usage_count++; 1114d9ba8f9eSMugunthan V N 1115d9ba8f9eSMugunthan V N return usage_count; 1116d9ba8f9eSMugunthan V N } 1117d9ba8f9eSMugunthan V N 111827e9e103SIvan Khoronzhuk static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv, 1119e05107e6SIvan Khoronzhuk struct sk_buff *skb, 1120e05107e6SIvan Khoronzhuk struct cpdma_chan *txch) 1121d9ba8f9eSMugunthan V N { 11222c836bd9SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 11232c836bd9SIvan Khoronzhuk 1124e05107e6SIvan Khoronzhuk return cpdma_chan_submit(txch, skb, skb->data, skb->len, 1125606f3993SIvan Khoronzhuk priv->emac_port + cpsw->data.dual_emac); 1126d9ba8f9eSMugunthan V N } 1127d9ba8f9eSMugunthan V N 1128d9ba8f9eSMugunthan V N static inline void cpsw_add_dual_emac_def_ale_entries( 1129d9ba8f9eSMugunthan V N struct cpsw_priv *priv, struct cpsw_slave *slave, 1130d9ba8f9eSMugunthan V N u32 slave_port) 1131d9ba8f9eSMugunthan V N { 11322a05a622SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 113371a2cbb7SGrygorii Strashko u32 port_mask = 1 << slave_port | ALE_PORT_HOST; 1134d9ba8f9eSMugunthan V N 11352a05a622SIvan Khoronzhuk if (cpsw->version == CPSW_VERSION_1) 1136d9ba8f9eSMugunthan V N slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN); 1137d9ba8f9eSMugunthan V N else 1138d9ba8f9eSMugunthan V N slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN); 11392a05a622SIvan Khoronzhuk cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask, 1140d9ba8f9eSMugunthan V N port_mask, port_mask, 0); 11412a05a622SIvan Khoronzhuk cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, 1142d9ba8f9eSMugunthan V N port_mask, ALE_VLAN, slave->port_vlan, 0); 11432a05a622SIvan Khoronzhuk cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, 11442a05a622SIvan Khoronzhuk HOST_PORT_NUM, ALE_VLAN | 11452a05a622SIvan Khoronzhuk ALE_SECURE, slave->port_vlan); 1146d9ba8f9eSMugunthan V N } 1147d9ba8f9eSMugunthan V N 11481e7a2e21SDaniel Mack static void soft_reset_slave(struct cpsw_slave *slave) 1149df828598SMugunthan V N { 1150df828598SMugunthan V N char name[32]; 11511e7a2e21SDaniel Mack 11521e7a2e21SDaniel Mack snprintf(name, sizeof(name), "slave-%d", slave->slave_num); 11531e7a2e21SDaniel Mack soft_reset(name, &slave->sliver->soft_reset); 11541e7a2e21SDaniel Mack } 11551e7a2e21SDaniel Mack 11561e7a2e21SDaniel Mack static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv) 11571e7a2e21SDaniel Mack { 1158df828598SMugunthan V N u32 slave_port; 1159649a1688SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 1160df828598SMugunthan V N 11611e7a2e21SDaniel Mack soft_reset_slave(slave); 1162df828598SMugunthan V N 1163df828598SMugunthan V N /* setup priority mapping */ 1164df828598SMugunthan V N __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map); 11659750a3adSRichard Cochran 11662a05a622SIvan Khoronzhuk switch (cpsw->version) { 11679750a3adSRichard Cochran case CPSW_VERSION_1: 11689750a3adSRichard Cochran slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP); 11699750a3adSRichard Cochran break; 11709750a3adSRichard Cochran case CPSW_VERSION_2: 1171c193f365SMugunthan V N case CPSW_VERSION_3: 1172926489beSMugunthan V N case CPSW_VERSION_4: 11739750a3adSRichard Cochran slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP); 11749750a3adSRichard Cochran break; 11759750a3adSRichard Cochran } 1176df828598SMugunthan V N 1177df828598SMugunthan V N /* setup max packet size, and mac address */ 11782a05a622SIvan Khoronzhuk __raw_writel(cpsw->rx_packet_max, &slave->sliver->rx_maxlen); 1179df828598SMugunthan V N cpsw_set_slave_mac(slave, priv); 1180df828598SMugunthan V N 1181df828598SMugunthan V N slave->mac_control = 0; /* no link yet */ 1182df828598SMugunthan V N 11836f1f5836SIvan Khoronzhuk slave_port = cpsw_get_slave_port(slave->slave_num); 1184df828598SMugunthan V N 1185606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) 1186d9ba8f9eSMugunthan V N cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port); 1187d9ba8f9eSMugunthan V N else 11882a05a622SIvan Khoronzhuk cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, 1189e11b220fSMugunthan V N 1 << slave_port, 0, 0, ALE_MCAST_FWD_2); 1190df828598SMugunthan V N 1191d733f754SDavid Rivshin if (slave->data->phy_node) { 1192552165bcSDavid Rivshin slave->phy = of_phy_connect(priv->ndev, slave->data->phy_node, 11939e42f715SHeiko Schocher &cpsw_adjust_link, 0, slave->data->phy_if); 1194d733f754SDavid Rivshin if (!slave->phy) { 1195d733f754SDavid Rivshin dev_err(priv->dev, "phy \"%s\" not found on slave %d\n", 1196d733f754SDavid Rivshin slave->data->phy_node->full_name, 1197d733f754SDavid Rivshin slave->slave_num); 1198d733f754SDavid Rivshin return; 1199d733f754SDavid Rivshin } 1200d733f754SDavid Rivshin } else { 1201df828598SMugunthan V N slave->phy = phy_connect(priv->ndev, slave->data->phy_id, 1202f9a8f83bSFlorian Fainelli &cpsw_adjust_link, slave->data->phy_if); 1203df828598SMugunthan V N if (IS_ERR(slave->phy)) { 1204d733f754SDavid Rivshin dev_err(priv->dev, 1205d733f754SDavid Rivshin "phy \"%s\" not found on slave %d, err %ld\n", 1206d733f754SDavid Rivshin slave->data->phy_id, slave->slave_num, 1207d733f754SDavid Rivshin PTR_ERR(slave->phy)); 1208df828598SMugunthan V N slave->phy = NULL; 1209d733f754SDavid Rivshin return; 1210d733f754SDavid Rivshin } 1211d733f754SDavid Rivshin } 1212d733f754SDavid Rivshin 12132220943aSAndrew Lunn phy_attached_info(slave->phy); 12142220943aSAndrew Lunn 1215df828598SMugunthan V N phy_start(slave->phy); 1216388367a5SMugunthan V N 1217388367a5SMugunthan V N /* Configure GMII_SEL register */ 121856e31bd8SIvan Khoronzhuk cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num); 1219df828598SMugunthan V N } 1220df828598SMugunthan V N 12213b72c2feSMugunthan V N static inline void cpsw_add_default_vlan(struct cpsw_priv *priv) 12223b72c2feSMugunthan V N { 1223606f3993SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 1224606f3993SIvan Khoronzhuk const int vlan = cpsw->data.default_vlan; 12253b72c2feSMugunthan V N u32 reg; 12263b72c2feSMugunthan V N int i; 12271e5c4bc4SLennart Sorensen int unreg_mcast_mask; 12283b72c2feSMugunthan V N 12292a05a622SIvan Khoronzhuk reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN : 12303b72c2feSMugunthan V N CPSW2_PORT_VLAN; 12313b72c2feSMugunthan V N 12325d8d0d4dSIvan Khoronzhuk writel(vlan, &cpsw->host_port_regs->port_vlan); 12333b72c2feSMugunthan V N 1234606f3993SIvan Khoronzhuk for (i = 0; i < cpsw->data.slaves; i++) 1235606f3993SIvan Khoronzhuk slave_write(cpsw->slaves + i, vlan, reg); 12363b72c2feSMugunthan V N 12371e5c4bc4SLennart Sorensen if (priv->ndev->flags & IFF_ALLMULTI) 12381e5c4bc4SLennart Sorensen unreg_mcast_mask = ALE_ALL_PORTS; 12391e5c4bc4SLennart Sorensen else 12401e5c4bc4SLennart Sorensen unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2; 12411e5c4bc4SLennart Sorensen 12422a05a622SIvan Khoronzhuk cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS, 124361f1cef9SGrygorii Strashko ALE_ALL_PORTS, ALE_ALL_PORTS, 124461f1cef9SGrygorii Strashko unreg_mcast_mask); 12453b72c2feSMugunthan V N } 12463b72c2feSMugunthan V N 1247df828598SMugunthan V N static void cpsw_init_host_port(struct cpsw_priv *priv) 1248df828598SMugunthan V N { 1249d9ba8f9eSMugunthan V N u32 fifo_mode; 12505d8d0d4dSIvan Khoronzhuk u32 control_reg; 12515d8d0d4dSIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 12523b72c2feSMugunthan V N 1253df828598SMugunthan V N /* soft reset the controller and initialize ale */ 12545d8d0d4dSIvan Khoronzhuk soft_reset("cpsw", &cpsw->regs->soft_reset); 12552a05a622SIvan Khoronzhuk cpsw_ale_start(cpsw->ale); 1256df828598SMugunthan V N 1257df828598SMugunthan V N /* switch to vlan unaware mode */ 12582a05a622SIvan Khoronzhuk cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, 12593b72c2feSMugunthan V N CPSW_ALE_VLAN_AWARE); 12605d8d0d4dSIvan Khoronzhuk control_reg = readl(&cpsw->regs->control); 12613b72c2feSMugunthan V N control_reg |= CPSW_VLAN_AWARE; 12625d8d0d4dSIvan Khoronzhuk writel(control_reg, &cpsw->regs->control); 1263606f3993SIvan Khoronzhuk fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE : 1264d9ba8f9eSMugunthan V N CPSW_FIFO_NORMAL_MODE; 12655d8d0d4dSIvan Khoronzhuk writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl); 1266df828598SMugunthan V N 1267df828598SMugunthan V N /* setup host port priority mapping */ 1268df828598SMugunthan V N __raw_writel(CPDMA_TX_PRIORITY_MAP, 12695d8d0d4dSIvan Khoronzhuk &cpsw->host_port_regs->cpdma_tx_pri_map); 12705d8d0d4dSIvan Khoronzhuk __raw_writel(0, &cpsw->host_port_regs->cpdma_rx_chan_map); 1271df828598SMugunthan V N 12722a05a622SIvan Khoronzhuk cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, 1273df828598SMugunthan V N ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 1274df828598SMugunthan V N 1275606f3993SIvan Khoronzhuk if (!cpsw->data.dual_emac) { 12762a05a622SIvan Khoronzhuk cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM, 1277d9ba8f9eSMugunthan V N 0, 0); 12782a05a622SIvan Khoronzhuk cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, 127971a2cbb7SGrygorii Strashko ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2); 1280df828598SMugunthan V N } 1281d9ba8f9eSMugunthan V N } 1282df828598SMugunthan V N 1283*8feb0a19SIvan Khoronzhuk /* split budget depending on channel rates */ 1284*8feb0a19SIvan Khoronzhuk static void cpsw_split_budget(struct net_device *ndev) 1285*8feb0a19SIvan Khoronzhuk { 1286*8feb0a19SIvan Khoronzhuk struct cpsw_priv *priv = netdev_priv(ndev); 1287*8feb0a19SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 1288*8feb0a19SIvan Khoronzhuk struct cpsw_vector *txv = cpsw->txv; 1289*8feb0a19SIvan Khoronzhuk u32 consumed_rate, bigest_rate = 0; 1290*8feb0a19SIvan Khoronzhuk int budget, bigest_rate_ch = 0; 1291*8feb0a19SIvan Khoronzhuk struct cpsw_slave *slave; 1292*8feb0a19SIvan Khoronzhuk int i, rlim_ch_num = 0; 1293*8feb0a19SIvan Khoronzhuk u32 ch_rate, max_rate; 1294*8feb0a19SIvan Khoronzhuk int ch_budget = 0; 1295*8feb0a19SIvan Khoronzhuk 1296*8feb0a19SIvan Khoronzhuk if (cpsw->data.dual_emac) 1297*8feb0a19SIvan Khoronzhuk slave = &cpsw->slaves[priv->emac_port]; 1298*8feb0a19SIvan Khoronzhuk else 1299*8feb0a19SIvan Khoronzhuk slave = &cpsw->slaves[cpsw->data.active_slave]; 1300*8feb0a19SIvan Khoronzhuk 1301*8feb0a19SIvan Khoronzhuk max_rate = slave->phy->speed * 1000; 1302*8feb0a19SIvan Khoronzhuk 1303*8feb0a19SIvan Khoronzhuk consumed_rate = 0; 1304*8feb0a19SIvan Khoronzhuk for (i = 0; i < cpsw->tx_ch_num; i++) { 1305*8feb0a19SIvan Khoronzhuk ch_rate = cpdma_chan_get_rate(txv[i].ch); 1306*8feb0a19SIvan Khoronzhuk if (!ch_rate) 1307*8feb0a19SIvan Khoronzhuk continue; 1308*8feb0a19SIvan Khoronzhuk 1309*8feb0a19SIvan Khoronzhuk rlim_ch_num++; 1310*8feb0a19SIvan Khoronzhuk consumed_rate += ch_rate; 1311*8feb0a19SIvan Khoronzhuk } 1312*8feb0a19SIvan Khoronzhuk 1313*8feb0a19SIvan Khoronzhuk if (cpsw->tx_ch_num == rlim_ch_num) { 1314*8feb0a19SIvan Khoronzhuk max_rate = consumed_rate; 1315*8feb0a19SIvan Khoronzhuk } else { 1316*8feb0a19SIvan Khoronzhuk ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate; 1317*8feb0a19SIvan Khoronzhuk ch_budget = (CPSW_POLL_WEIGHT - ch_budget) / 1318*8feb0a19SIvan Khoronzhuk (cpsw->tx_ch_num - rlim_ch_num); 1319*8feb0a19SIvan Khoronzhuk bigest_rate = (max_rate - consumed_rate) / 1320*8feb0a19SIvan Khoronzhuk (cpsw->tx_ch_num - rlim_ch_num); 1321*8feb0a19SIvan Khoronzhuk } 1322*8feb0a19SIvan Khoronzhuk 1323*8feb0a19SIvan Khoronzhuk /* split tx budget */ 1324*8feb0a19SIvan Khoronzhuk budget = CPSW_POLL_WEIGHT; 1325*8feb0a19SIvan Khoronzhuk for (i = 0; i < cpsw->tx_ch_num; i++) { 1326*8feb0a19SIvan Khoronzhuk ch_rate = cpdma_chan_get_rate(txv[i].ch); 1327*8feb0a19SIvan Khoronzhuk if (ch_rate) { 1328*8feb0a19SIvan Khoronzhuk txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate; 1329*8feb0a19SIvan Khoronzhuk if (!txv[i].budget) 1330*8feb0a19SIvan Khoronzhuk txv[i].budget = 1; 1331*8feb0a19SIvan Khoronzhuk if (ch_rate > bigest_rate) { 1332*8feb0a19SIvan Khoronzhuk bigest_rate_ch = i; 1333*8feb0a19SIvan Khoronzhuk bigest_rate = ch_rate; 1334*8feb0a19SIvan Khoronzhuk } 1335*8feb0a19SIvan Khoronzhuk } else { 1336*8feb0a19SIvan Khoronzhuk txv[i].budget = ch_budget; 1337*8feb0a19SIvan Khoronzhuk if (!bigest_rate_ch) 1338*8feb0a19SIvan Khoronzhuk bigest_rate_ch = i; 1339*8feb0a19SIvan Khoronzhuk } 1340*8feb0a19SIvan Khoronzhuk 1341*8feb0a19SIvan Khoronzhuk budget -= txv[i].budget; 1342*8feb0a19SIvan Khoronzhuk } 1343*8feb0a19SIvan Khoronzhuk 1344*8feb0a19SIvan Khoronzhuk if (budget) 1345*8feb0a19SIvan Khoronzhuk txv[bigest_rate_ch].budget += budget; 1346*8feb0a19SIvan Khoronzhuk 1347*8feb0a19SIvan Khoronzhuk /* split rx budget */ 1348*8feb0a19SIvan Khoronzhuk budget = CPSW_POLL_WEIGHT; 1349*8feb0a19SIvan Khoronzhuk ch_budget = budget / cpsw->rx_ch_num; 1350*8feb0a19SIvan Khoronzhuk for (i = 0; i < cpsw->rx_ch_num; i++) { 1351*8feb0a19SIvan Khoronzhuk cpsw->rxv[i].budget = ch_budget; 1352*8feb0a19SIvan Khoronzhuk budget -= ch_budget; 1353*8feb0a19SIvan Khoronzhuk } 1354*8feb0a19SIvan Khoronzhuk 1355*8feb0a19SIvan Khoronzhuk if (budget) 1356*8feb0a19SIvan Khoronzhuk cpsw->rxv[0].budget += budget; 1357*8feb0a19SIvan Khoronzhuk } 1358*8feb0a19SIvan Khoronzhuk 13593802dce1SIvan Khoronzhuk static int cpsw_fill_rx_channels(struct cpsw_priv *priv) 13603802dce1SIvan Khoronzhuk { 13613802dce1SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 13623802dce1SIvan Khoronzhuk struct sk_buff *skb; 13633802dce1SIvan Khoronzhuk int ch_buf_num; 1364e05107e6SIvan Khoronzhuk int ch, i, ret; 13653802dce1SIvan Khoronzhuk 1366e05107e6SIvan Khoronzhuk for (ch = 0; ch < cpsw->rx_ch_num; ch++) { 1367*8feb0a19SIvan Khoronzhuk ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch); 13683802dce1SIvan Khoronzhuk for (i = 0; i < ch_buf_num; i++) { 13693802dce1SIvan Khoronzhuk skb = __netdev_alloc_skb_ip_align(priv->ndev, 13703802dce1SIvan Khoronzhuk cpsw->rx_packet_max, 13713802dce1SIvan Khoronzhuk GFP_KERNEL); 13723802dce1SIvan Khoronzhuk if (!skb) { 13733802dce1SIvan Khoronzhuk cpsw_err(priv, ifup, "cannot allocate skb\n"); 13743802dce1SIvan Khoronzhuk return -ENOMEM; 13753802dce1SIvan Khoronzhuk } 13763802dce1SIvan Khoronzhuk 1377e05107e6SIvan Khoronzhuk skb_set_queue_mapping(skb, ch); 1378*8feb0a19SIvan Khoronzhuk ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb, 1379*8feb0a19SIvan Khoronzhuk skb->data, skb_tailroom(skb), 1380*8feb0a19SIvan Khoronzhuk 0); 13813802dce1SIvan Khoronzhuk if (ret < 0) { 13823802dce1SIvan Khoronzhuk cpsw_err(priv, ifup, 1383e05107e6SIvan Khoronzhuk "cannot submit skb to channel %d rx, error %d\n", 1384e05107e6SIvan Khoronzhuk ch, ret); 13853802dce1SIvan Khoronzhuk kfree_skb(skb); 13863802dce1SIvan Khoronzhuk return ret; 13873802dce1SIvan Khoronzhuk } 13883802dce1SIvan Khoronzhuk kmemleak_not_leak(skb); 13893802dce1SIvan Khoronzhuk } 13903802dce1SIvan Khoronzhuk 1391e05107e6SIvan Khoronzhuk cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n", 1392e05107e6SIvan Khoronzhuk ch, ch_buf_num); 1393e05107e6SIvan Khoronzhuk } 13943802dce1SIvan Khoronzhuk 1395e05107e6SIvan Khoronzhuk return 0; 13963802dce1SIvan Khoronzhuk } 13973802dce1SIvan Khoronzhuk 13982a05a622SIvan Khoronzhuk static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw) 1399aacebbf8SSebastian Siewior { 14003995d265SSchuyler Patton u32 slave_port; 14013995d265SSchuyler Patton 14026f1f5836SIvan Khoronzhuk slave_port = cpsw_get_slave_port(slave->slave_num); 14033995d265SSchuyler Patton 1404aacebbf8SSebastian Siewior if (!slave->phy) 1405aacebbf8SSebastian Siewior return; 1406aacebbf8SSebastian Siewior phy_stop(slave->phy); 1407aacebbf8SSebastian Siewior phy_disconnect(slave->phy); 1408aacebbf8SSebastian Siewior slave->phy = NULL; 14092a05a622SIvan Khoronzhuk cpsw_ale_control_set(cpsw->ale, slave_port, 14103995d265SSchuyler Patton ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 14111f95ba00SGrygorii Strashko soft_reset_slave(slave); 1412aacebbf8SSebastian Siewior } 1413aacebbf8SSebastian Siewior 1414df828598SMugunthan V N static int cpsw_ndo_open(struct net_device *ndev) 1415df828598SMugunthan V N { 1416df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 1417649a1688SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 14183802dce1SIvan Khoronzhuk int ret; 1419df828598SMugunthan V N u32 reg; 1420df828598SMugunthan V N 142156e31bd8SIvan Khoronzhuk ret = pm_runtime_get_sync(cpsw->dev); 1422108a6537SGrygorii Strashko if (ret < 0) { 142356e31bd8SIvan Khoronzhuk pm_runtime_put_noidle(cpsw->dev); 1424108a6537SGrygorii Strashko return ret; 1425108a6537SGrygorii Strashko } 14263fa88c51SGrygorii Strashko 1427606f3993SIvan Khoronzhuk if (!cpsw_common_res_usage_state(cpsw)) 14282c836bd9SIvan Khoronzhuk cpsw_intr_disable(cpsw); 1429df828598SMugunthan V N netif_carrier_off(ndev); 1430df828598SMugunthan V N 1431e05107e6SIvan Khoronzhuk /* Notify the stack of the actual queue counts. */ 1432e05107e6SIvan Khoronzhuk ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num); 1433e05107e6SIvan Khoronzhuk if (ret) { 1434e05107e6SIvan Khoronzhuk dev_err(priv->dev, "cannot set real number of tx queues\n"); 1435e05107e6SIvan Khoronzhuk goto err_cleanup; 1436e05107e6SIvan Khoronzhuk } 1437e05107e6SIvan Khoronzhuk 1438e05107e6SIvan Khoronzhuk ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num); 1439e05107e6SIvan Khoronzhuk if (ret) { 1440e05107e6SIvan Khoronzhuk dev_err(priv->dev, "cannot set real number of rx queues\n"); 1441e05107e6SIvan Khoronzhuk goto err_cleanup; 1442e05107e6SIvan Khoronzhuk } 1443e05107e6SIvan Khoronzhuk 14442a05a622SIvan Khoronzhuk reg = cpsw->version; 1445df828598SMugunthan V N 1446df828598SMugunthan V N dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n", 1447df828598SMugunthan V N CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg), 1448df828598SMugunthan V N CPSW_RTL_VERSION(reg)); 1449df828598SMugunthan V N 1450df828598SMugunthan V N /* initialize host and slave ports */ 1451606f3993SIvan Khoronzhuk if (!cpsw_common_res_usage_state(cpsw)) 1452df828598SMugunthan V N cpsw_init_host_port(priv); 1453df828598SMugunthan V N for_each_slave(priv, cpsw_slave_open, priv); 1454df828598SMugunthan V N 14553b72c2feSMugunthan V N /* Add default VLAN */ 1456606f3993SIvan Khoronzhuk if (!cpsw->data.dual_emac) 14573b72c2feSMugunthan V N cpsw_add_default_vlan(priv); 1458e6afea0bSMugunthan V N else 14592a05a622SIvan Khoronzhuk cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan, 146061f1cef9SGrygorii Strashko ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0); 14613b72c2feSMugunthan V N 1462606f3993SIvan Khoronzhuk if (!cpsw_common_res_usage_state(cpsw)) { 1463d9ba8f9eSMugunthan V N /* disable priority elevation */ 14645d8d0d4dSIvan Khoronzhuk __raw_writel(0, &cpsw->regs->ptype); 1465df828598SMugunthan V N 1466d9ba8f9eSMugunthan V N /* enable statistics collection only on all ports */ 14675d8d0d4dSIvan Khoronzhuk __raw_writel(0x7, &cpsw->regs->stat_port_en); 1468df828598SMugunthan V N 14691923d6e4SMugunthan V N /* Enable internal fifo flow control */ 14705d8d0d4dSIvan Khoronzhuk writel(0x7, &cpsw->regs->flow_control); 14711923d6e4SMugunthan V N 1472dbc4ec52SIvan Khoronzhuk napi_enable(&cpsw->napi_rx); 1473dbc4ec52SIvan Khoronzhuk napi_enable(&cpsw->napi_tx); 1474d354eb85SMugunthan V N 1475e38b5a3dSIvan Khoronzhuk if (cpsw->tx_irq_disabled) { 1476e38b5a3dSIvan Khoronzhuk cpsw->tx_irq_disabled = false; 1477e38b5a3dSIvan Khoronzhuk enable_irq(cpsw->irqs_table[1]); 14787da11600SMugunthan V N } 14797da11600SMugunthan V N 1480e38b5a3dSIvan Khoronzhuk if (cpsw->rx_irq_disabled) { 1481e38b5a3dSIvan Khoronzhuk cpsw->rx_irq_disabled = false; 1482e38b5a3dSIvan Khoronzhuk enable_irq(cpsw->irqs_table[0]); 14837da11600SMugunthan V N } 14847da11600SMugunthan V N 14853802dce1SIvan Khoronzhuk ret = cpsw_fill_rx_channels(priv); 14863802dce1SIvan Khoronzhuk if (ret < 0) 1487aacebbf8SSebastian Siewior goto err_cleanup; 1488f280e89aSMugunthan V N 14892a05a622SIvan Khoronzhuk if (cpts_register(cpsw->dev, cpsw->cpts, 1490606f3993SIvan Khoronzhuk cpsw->data.cpts_clock_mult, 1491606f3993SIvan Khoronzhuk cpsw->data.cpts_clock_shift)) 1492f280e89aSMugunthan V N dev_err(priv->dev, "error registering cpts device\n"); 1493f280e89aSMugunthan V N 1494d9ba8f9eSMugunthan V N } 1495df828598SMugunthan V N 1496ff5b8ef2SMugunthan V N /* Enable Interrupt pacing if configured */ 14972a05a622SIvan Khoronzhuk if (cpsw->coal_intvl != 0) { 1498ff5b8ef2SMugunthan V N struct ethtool_coalesce coal; 1499ff5b8ef2SMugunthan V N 15002a05a622SIvan Khoronzhuk coal.rx_coalesce_usecs = cpsw->coal_intvl; 1501ff5b8ef2SMugunthan V N cpsw_set_coalesce(ndev, &coal); 1502ff5b8ef2SMugunthan V N } 1503ff5b8ef2SMugunthan V N 1504*8feb0a19SIvan Khoronzhuk cpsw_split_budget(ndev); 15052c836bd9SIvan Khoronzhuk cpdma_ctlr_start(cpsw->dma); 15062c836bd9SIvan Khoronzhuk cpsw_intr_enable(cpsw); 1507f63a975eSMugunthan V N 1508606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) 1509606f3993SIvan Khoronzhuk cpsw->slaves[priv->emac_port].open_stat = true; 1510e05107e6SIvan Khoronzhuk 1511e05107e6SIvan Khoronzhuk netif_tx_start_all_queues(ndev); 1512e05107e6SIvan Khoronzhuk 1513df828598SMugunthan V N return 0; 1514df828598SMugunthan V N 1515aacebbf8SSebastian Siewior err_cleanup: 15162c836bd9SIvan Khoronzhuk cpdma_ctlr_stop(cpsw->dma); 15172a05a622SIvan Khoronzhuk for_each_slave(priv, cpsw_slave_stop, cpsw); 151856e31bd8SIvan Khoronzhuk pm_runtime_put_sync(cpsw->dev); 1519aacebbf8SSebastian Siewior netif_carrier_off(priv->ndev); 1520aacebbf8SSebastian Siewior return ret; 1521df828598SMugunthan V N } 1522df828598SMugunthan V N 1523df828598SMugunthan V N static int cpsw_ndo_stop(struct net_device *ndev) 1524df828598SMugunthan V N { 1525df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 1526649a1688SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 1527df828598SMugunthan V N 1528df828598SMugunthan V N cpsw_info(priv, ifdown, "shutting down cpsw device\n"); 1529e05107e6SIvan Khoronzhuk netif_tx_stop_all_queues(priv->ndev); 1530df828598SMugunthan V N netif_carrier_off(priv->ndev); 1531d9ba8f9eSMugunthan V N 1532606f3993SIvan Khoronzhuk if (cpsw_common_res_usage_state(cpsw) <= 1) { 1533dbc4ec52SIvan Khoronzhuk napi_disable(&cpsw->napi_rx); 1534dbc4ec52SIvan Khoronzhuk napi_disable(&cpsw->napi_tx); 15352a05a622SIvan Khoronzhuk cpts_unregister(cpsw->cpts); 15362c836bd9SIvan Khoronzhuk cpsw_intr_disable(cpsw); 15372c836bd9SIvan Khoronzhuk cpdma_ctlr_stop(cpsw->dma); 15382a05a622SIvan Khoronzhuk cpsw_ale_stop(cpsw->ale); 1539d9ba8f9eSMugunthan V N } 15402a05a622SIvan Khoronzhuk for_each_slave(priv, cpsw_slave_stop, cpsw); 154156e31bd8SIvan Khoronzhuk pm_runtime_put_sync(cpsw->dev); 1542606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) 1543606f3993SIvan Khoronzhuk cpsw->slaves[priv->emac_port].open_stat = false; 1544df828598SMugunthan V N return 0; 1545df828598SMugunthan V N } 1546df828598SMugunthan V N 1547df828598SMugunthan V N static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb, 1548df828598SMugunthan V N struct net_device *ndev) 1549df828598SMugunthan V N { 1550df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 15512c836bd9SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 1552e05107e6SIvan Khoronzhuk struct netdev_queue *txq; 1553e05107e6SIvan Khoronzhuk struct cpdma_chan *txch; 1554e05107e6SIvan Khoronzhuk int ret, q_idx; 1555df828598SMugunthan V N 1556860e9538SFlorian Westphal netif_trans_update(ndev); 1557df828598SMugunthan V N 1558df828598SMugunthan V N if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) { 1559df828598SMugunthan V N cpsw_err(priv, tx_err, "packet pad failed\n"); 15608dc43ddcSTobias Klauser ndev->stats.tx_dropped++; 1561df828598SMugunthan V N return NETDEV_TX_OK; 1562df828598SMugunthan V N } 1563df828598SMugunthan V N 15649232b16dSMugunthan V N if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && 15652a05a622SIvan Khoronzhuk cpsw->cpts->tx_enable) 15662e5b38abSRichard Cochran skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 15672e5b38abSRichard Cochran 15682e5b38abSRichard Cochran skb_tx_timestamp(skb); 15692e5b38abSRichard Cochran 1570e05107e6SIvan Khoronzhuk q_idx = skb_get_queue_mapping(skb); 1571e05107e6SIvan Khoronzhuk if (q_idx >= cpsw->tx_ch_num) 1572e05107e6SIvan Khoronzhuk q_idx = q_idx % cpsw->tx_ch_num; 1573e05107e6SIvan Khoronzhuk 1574*8feb0a19SIvan Khoronzhuk txch = cpsw->txv[q_idx].ch; 1575e05107e6SIvan Khoronzhuk ret = cpsw_tx_packet_submit(priv, skb, txch); 1576df828598SMugunthan V N if (unlikely(ret != 0)) { 1577df828598SMugunthan V N cpsw_err(priv, tx_err, "desc submit failed\n"); 1578df828598SMugunthan V N goto fail; 1579df828598SMugunthan V N } 1580df828598SMugunthan V N 1581fae50823SMugunthan V N /* If there is no more tx desc left free then we need to 1582fae50823SMugunthan V N * tell the kernel to stop sending us tx frames. 1583fae50823SMugunthan V N */ 1584e05107e6SIvan Khoronzhuk if (unlikely(!cpdma_check_free_tx_desc(txch))) { 1585e05107e6SIvan Khoronzhuk txq = netdev_get_tx_queue(ndev, q_idx); 1586e05107e6SIvan Khoronzhuk netif_tx_stop_queue(txq); 1587e05107e6SIvan Khoronzhuk } 1588fae50823SMugunthan V N 1589df828598SMugunthan V N return NETDEV_TX_OK; 1590df828598SMugunthan V N fail: 15918dc43ddcSTobias Klauser ndev->stats.tx_dropped++; 1592e05107e6SIvan Khoronzhuk txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb)); 1593e05107e6SIvan Khoronzhuk netif_tx_stop_queue(txq); 1594df828598SMugunthan V N return NETDEV_TX_BUSY; 1595df828598SMugunthan V N } 1596df828598SMugunthan V N 15972e5b38abSRichard Cochran #ifdef CONFIG_TI_CPTS 15982e5b38abSRichard Cochran 15992a05a622SIvan Khoronzhuk static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw) 16002e5b38abSRichard Cochran { 1601606f3993SIvan Khoronzhuk struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave]; 16022e5b38abSRichard Cochran u32 ts_en, seq_id; 16032e5b38abSRichard Cochran 16042a05a622SIvan Khoronzhuk if (!cpsw->cpts->tx_enable && !cpsw->cpts->rx_enable) { 16052e5b38abSRichard Cochran slave_write(slave, 0, CPSW1_TS_CTL); 16062e5b38abSRichard Cochran return; 16072e5b38abSRichard Cochran } 16082e5b38abSRichard Cochran 16092e5b38abSRichard Cochran seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588; 16102e5b38abSRichard Cochran ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS; 16112e5b38abSRichard Cochran 16122a05a622SIvan Khoronzhuk if (cpsw->cpts->tx_enable) 16132e5b38abSRichard Cochran ts_en |= CPSW_V1_TS_TX_EN; 16142e5b38abSRichard Cochran 16152a05a622SIvan Khoronzhuk if (cpsw->cpts->rx_enable) 16162e5b38abSRichard Cochran ts_en |= CPSW_V1_TS_RX_EN; 16172e5b38abSRichard Cochran 16182e5b38abSRichard Cochran slave_write(slave, ts_en, CPSW1_TS_CTL); 16192e5b38abSRichard Cochran slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE); 16202e5b38abSRichard Cochran } 16212e5b38abSRichard Cochran 16222e5b38abSRichard Cochran static void cpsw_hwtstamp_v2(struct cpsw_priv *priv) 16232e5b38abSRichard Cochran { 1624d9ba8f9eSMugunthan V N struct cpsw_slave *slave; 16255d8d0d4dSIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 16262e5b38abSRichard Cochran u32 ctrl, mtype; 16272e5b38abSRichard Cochran 1628606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) 1629606f3993SIvan Khoronzhuk slave = &cpsw->slaves[priv->emac_port]; 1630d9ba8f9eSMugunthan V N else 1631606f3993SIvan Khoronzhuk slave = &cpsw->slaves[cpsw->data.active_slave]; 1632d9ba8f9eSMugunthan V N 16332e5b38abSRichard Cochran ctrl = slave_read(slave, CPSW2_CONTROL); 16342a05a622SIvan Khoronzhuk switch (cpsw->version) { 163509c55372SGeorge Cherian case CPSW_VERSION_2: 163609c55372SGeorge Cherian ctrl &= ~CTRL_V2_ALL_TS_MASK; 16372e5b38abSRichard Cochran 16382a05a622SIvan Khoronzhuk if (cpsw->cpts->tx_enable) 163909c55372SGeorge Cherian ctrl |= CTRL_V2_TX_TS_BITS; 16402e5b38abSRichard Cochran 16412a05a622SIvan Khoronzhuk if (cpsw->cpts->rx_enable) 164209c55372SGeorge Cherian ctrl |= CTRL_V2_RX_TS_BITS; 164309c55372SGeorge Cherian break; 164409c55372SGeorge Cherian case CPSW_VERSION_3: 164509c55372SGeorge Cherian default: 164609c55372SGeorge Cherian ctrl &= ~CTRL_V3_ALL_TS_MASK; 164709c55372SGeorge Cherian 16482a05a622SIvan Khoronzhuk if (cpsw->cpts->tx_enable) 164909c55372SGeorge Cherian ctrl |= CTRL_V3_TX_TS_BITS; 165009c55372SGeorge Cherian 16512a05a622SIvan Khoronzhuk if (cpsw->cpts->rx_enable) 165209c55372SGeorge Cherian ctrl |= CTRL_V3_RX_TS_BITS; 165309c55372SGeorge Cherian break; 165409c55372SGeorge Cherian } 16552e5b38abSRichard Cochran 16562e5b38abSRichard Cochran mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS; 16572e5b38abSRichard Cochran 16582e5b38abSRichard Cochran slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE); 16592e5b38abSRichard Cochran slave_write(slave, ctrl, CPSW2_CONTROL); 16605d8d0d4dSIvan Khoronzhuk __raw_writel(ETH_P_1588, &cpsw->regs->ts_ltype); 16612e5b38abSRichard Cochran } 16622e5b38abSRichard Cochran 1663a5b4145bSBen Hutchings static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr) 16642e5b38abSRichard Cochran { 16653177bf6fSMugunthan V N struct cpsw_priv *priv = netdev_priv(dev); 16662e5b38abSRichard Cochran struct hwtstamp_config cfg; 16672a05a622SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 16682a05a622SIvan Khoronzhuk struct cpts *cpts = cpsw->cpts; 16692e5b38abSRichard Cochran 16702a05a622SIvan Khoronzhuk if (cpsw->version != CPSW_VERSION_1 && 16712a05a622SIvan Khoronzhuk cpsw->version != CPSW_VERSION_2 && 16722a05a622SIvan Khoronzhuk cpsw->version != CPSW_VERSION_3) 16732ee91e54SBen Hutchings return -EOPNOTSUPP; 16742ee91e54SBen Hutchings 16752e5b38abSRichard Cochran if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 16762e5b38abSRichard Cochran return -EFAULT; 16772e5b38abSRichard Cochran 16782e5b38abSRichard Cochran /* reserved for future extensions */ 16792e5b38abSRichard Cochran if (cfg.flags) 16802e5b38abSRichard Cochran return -EINVAL; 16812e5b38abSRichard Cochran 16822ee91e54SBen Hutchings if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON) 16832e5b38abSRichard Cochran return -ERANGE; 16842e5b38abSRichard Cochran 16852e5b38abSRichard Cochran switch (cfg.rx_filter) { 16862e5b38abSRichard Cochran case HWTSTAMP_FILTER_NONE: 16872e5b38abSRichard Cochran cpts->rx_enable = 0; 16882e5b38abSRichard Cochran break; 16892e5b38abSRichard Cochran case HWTSTAMP_FILTER_ALL: 16902e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 16912e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 16922e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 16932e5b38abSRichard Cochran return -ERANGE; 16942e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 16952e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 16962e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 16972e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 16982e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 16992e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 17002e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_EVENT: 17012e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_SYNC: 17022e5b38abSRichard Cochran case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 17032e5b38abSRichard Cochran cpts->rx_enable = 1; 17042e5b38abSRichard Cochran cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 17052e5b38abSRichard Cochran break; 17062e5b38abSRichard Cochran default: 17072e5b38abSRichard Cochran return -ERANGE; 17082e5b38abSRichard Cochran } 17092e5b38abSRichard Cochran 17102ee91e54SBen Hutchings cpts->tx_enable = cfg.tx_type == HWTSTAMP_TX_ON; 17112ee91e54SBen Hutchings 17122a05a622SIvan Khoronzhuk switch (cpsw->version) { 17132e5b38abSRichard Cochran case CPSW_VERSION_1: 17142a05a622SIvan Khoronzhuk cpsw_hwtstamp_v1(cpsw); 17152e5b38abSRichard Cochran break; 17162e5b38abSRichard Cochran case CPSW_VERSION_2: 1717f7d403cbSGeorge Cherian case CPSW_VERSION_3: 17182e5b38abSRichard Cochran cpsw_hwtstamp_v2(priv); 17192e5b38abSRichard Cochran break; 17202e5b38abSRichard Cochran default: 17212ee91e54SBen Hutchings WARN_ON(1); 17222e5b38abSRichard Cochran } 17232e5b38abSRichard Cochran 17242e5b38abSRichard Cochran return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 17252e5b38abSRichard Cochran } 17262e5b38abSRichard Cochran 1727a5b4145bSBen Hutchings static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr) 1728a5b4145bSBen Hutchings { 17292a05a622SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(dev); 17302a05a622SIvan Khoronzhuk struct cpts *cpts = cpsw->cpts; 1731a5b4145bSBen Hutchings struct hwtstamp_config cfg; 1732a5b4145bSBen Hutchings 17332a05a622SIvan Khoronzhuk if (cpsw->version != CPSW_VERSION_1 && 17342a05a622SIvan Khoronzhuk cpsw->version != CPSW_VERSION_2 && 17352a05a622SIvan Khoronzhuk cpsw->version != CPSW_VERSION_3) 1736a5b4145bSBen Hutchings return -EOPNOTSUPP; 1737a5b4145bSBen Hutchings 1738a5b4145bSBen Hutchings cfg.flags = 0; 1739a5b4145bSBen Hutchings cfg.tx_type = cpts->tx_enable ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 1740a5b4145bSBen Hutchings cfg.rx_filter = (cpts->rx_enable ? 1741a5b4145bSBen Hutchings HWTSTAMP_FILTER_PTP_V2_EVENT : HWTSTAMP_FILTER_NONE); 1742a5b4145bSBen Hutchings 1743a5b4145bSBen Hutchings return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 1744a5b4145bSBen Hutchings } 1745a5b4145bSBen Hutchings 17462e5b38abSRichard Cochran #endif /*CONFIG_TI_CPTS*/ 17472e5b38abSRichard Cochran 17482e5b38abSRichard Cochran static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd) 17492e5b38abSRichard Cochran { 175011f2c988SMugunthan V N struct cpsw_priv *priv = netdev_priv(dev); 1751606f3993SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 1752606f3993SIvan Khoronzhuk int slave_no = cpsw_slave_index(cpsw, priv); 175311f2c988SMugunthan V N 17542e5b38abSRichard Cochran if (!netif_running(dev)) 17552e5b38abSRichard Cochran return -EINVAL; 17562e5b38abSRichard Cochran 175711f2c988SMugunthan V N switch (cmd) { 17582e5b38abSRichard Cochran #ifdef CONFIG_TI_CPTS 175911f2c988SMugunthan V N case SIOCSHWTSTAMP: 1760a5b4145bSBen Hutchings return cpsw_hwtstamp_set(dev, req); 1761a5b4145bSBen Hutchings case SIOCGHWTSTAMP: 1762a5b4145bSBen Hutchings return cpsw_hwtstamp_get(dev, req); 17632e5b38abSRichard Cochran #endif 17642e5b38abSRichard Cochran } 17652e5b38abSRichard Cochran 1766606f3993SIvan Khoronzhuk if (!cpsw->slaves[slave_no].phy) 1767c1b59947SStefan Sørensen return -EOPNOTSUPP; 1768606f3993SIvan Khoronzhuk return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd); 176911f2c988SMugunthan V N } 177011f2c988SMugunthan V N 1771df828598SMugunthan V N static void cpsw_ndo_tx_timeout(struct net_device *ndev) 1772df828598SMugunthan V N { 1773df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 17742c836bd9SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 1775e05107e6SIvan Khoronzhuk int ch; 1776df828598SMugunthan V N 1777df828598SMugunthan V N cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n"); 17788dc43ddcSTobias Klauser ndev->stats.tx_errors++; 17792c836bd9SIvan Khoronzhuk cpsw_intr_disable(cpsw); 1780e05107e6SIvan Khoronzhuk for (ch = 0; ch < cpsw->tx_ch_num; ch++) { 1781*8feb0a19SIvan Khoronzhuk cpdma_chan_stop(cpsw->txv[ch].ch); 1782*8feb0a19SIvan Khoronzhuk cpdma_chan_start(cpsw->txv[ch].ch); 1783e05107e6SIvan Khoronzhuk } 1784e05107e6SIvan Khoronzhuk 17852c836bd9SIvan Khoronzhuk cpsw_intr_enable(cpsw); 1786df828598SMugunthan V N } 1787df828598SMugunthan V N 1788dcfd8d58SMugunthan V N static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p) 1789dcfd8d58SMugunthan V N { 1790dcfd8d58SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 1791dcfd8d58SMugunthan V N struct sockaddr *addr = (struct sockaddr *)p; 1792649a1688SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 1793dcfd8d58SMugunthan V N int flags = 0; 1794dcfd8d58SMugunthan V N u16 vid = 0; 1795a6c5d14fSGrygorii Strashko int ret; 1796dcfd8d58SMugunthan V N 1797dcfd8d58SMugunthan V N if (!is_valid_ether_addr(addr->sa_data)) 1798dcfd8d58SMugunthan V N return -EADDRNOTAVAIL; 1799dcfd8d58SMugunthan V N 180056e31bd8SIvan Khoronzhuk ret = pm_runtime_get_sync(cpsw->dev); 1801a6c5d14fSGrygorii Strashko if (ret < 0) { 180256e31bd8SIvan Khoronzhuk pm_runtime_put_noidle(cpsw->dev); 1803a6c5d14fSGrygorii Strashko return ret; 1804a6c5d14fSGrygorii Strashko } 1805a6c5d14fSGrygorii Strashko 1806606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) { 1807606f3993SIvan Khoronzhuk vid = cpsw->slaves[priv->emac_port].port_vlan; 1808dcfd8d58SMugunthan V N flags = ALE_VLAN; 1809dcfd8d58SMugunthan V N } 1810dcfd8d58SMugunthan V N 18112a05a622SIvan Khoronzhuk cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM, 1812dcfd8d58SMugunthan V N flags, vid); 18132a05a622SIvan Khoronzhuk cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM, 1814dcfd8d58SMugunthan V N flags, vid); 1815dcfd8d58SMugunthan V N 1816dcfd8d58SMugunthan V N memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN); 1817dcfd8d58SMugunthan V N memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN); 1818dcfd8d58SMugunthan V N for_each_slave(priv, cpsw_set_slave_mac, priv); 1819dcfd8d58SMugunthan V N 182056e31bd8SIvan Khoronzhuk pm_runtime_put(cpsw->dev); 1821a6c5d14fSGrygorii Strashko 1822dcfd8d58SMugunthan V N return 0; 1823dcfd8d58SMugunthan V N } 1824dcfd8d58SMugunthan V N 1825df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER 1826df828598SMugunthan V N static void cpsw_ndo_poll_controller(struct net_device *ndev) 1827df828598SMugunthan V N { 1828dbc4ec52SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 1829df828598SMugunthan V N 1830dbc4ec52SIvan Khoronzhuk cpsw_intr_disable(cpsw); 1831dbc4ec52SIvan Khoronzhuk cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw); 1832dbc4ec52SIvan Khoronzhuk cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw); 1833dbc4ec52SIvan Khoronzhuk cpsw_intr_enable(cpsw); 1834df828598SMugunthan V N } 1835df828598SMugunthan V N #endif 1836df828598SMugunthan V N 18373b72c2feSMugunthan V N static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv, 18383b72c2feSMugunthan V N unsigned short vid) 18393b72c2feSMugunthan V N { 18403b72c2feSMugunthan V N int ret; 18419f6bd8faSMugunthan V N int unreg_mcast_mask = 0; 18429f6bd8faSMugunthan V N u32 port_mask; 1843606f3993SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 18449f6bd8faSMugunthan V N 1845606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) { 18469f6bd8faSMugunthan V N port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST; 18479f6bd8faSMugunthan V N 18489f6bd8faSMugunthan V N if (priv->ndev->flags & IFF_ALLMULTI) 18499f6bd8faSMugunthan V N unreg_mcast_mask = port_mask; 18509f6bd8faSMugunthan V N } else { 18519f6bd8faSMugunthan V N port_mask = ALE_ALL_PORTS; 18521e5c4bc4SLennart Sorensen 18531e5c4bc4SLennart Sorensen if (priv->ndev->flags & IFF_ALLMULTI) 18541e5c4bc4SLennart Sorensen unreg_mcast_mask = ALE_ALL_PORTS; 18551e5c4bc4SLennart Sorensen else 18561e5c4bc4SLennart Sorensen unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2; 18579f6bd8faSMugunthan V N } 18583b72c2feSMugunthan V N 18592a05a622SIvan Khoronzhuk ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask, 186061f1cef9SGrygorii Strashko unreg_mcast_mask); 18613b72c2feSMugunthan V N if (ret != 0) 18623b72c2feSMugunthan V N return ret; 18633b72c2feSMugunthan V N 18642a05a622SIvan Khoronzhuk ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, 186571a2cbb7SGrygorii Strashko HOST_PORT_NUM, ALE_VLAN, vid); 18663b72c2feSMugunthan V N if (ret != 0) 18673b72c2feSMugunthan V N goto clean_vid; 18683b72c2feSMugunthan V N 18692a05a622SIvan Khoronzhuk ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, 18709f6bd8faSMugunthan V N port_mask, ALE_VLAN, vid, 0); 18713b72c2feSMugunthan V N if (ret != 0) 18723b72c2feSMugunthan V N goto clean_vlan_ucast; 18733b72c2feSMugunthan V N return 0; 18743b72c2feSMugunthan V N 18753b72c2feSMugunthan V N clean_vlan_ucast: 18762a05a622SIvan Khoronzhuk cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, 187771a2cbb7SGrygorii Strashko HOST_PORT_NUM, ALE_VLAN, vid); 18783b72c2feSMugunthan V N clean_vid: 18792a05a622SIvan Khoronzhuk cpsw_ale_del_vlan(cpsw->ale, vid, 0); 18803b72c2feSMugunthan V N return ret; 18813b72c2feSMugunthan V N } 18823b72c2feSMugunthan V N 18833b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev, 188480d5c368SPatrick McHardy __be16 proto, u16 vid) 18853b72c2feSMugunthan V N { 18863b72c2feSMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 1887649a1688SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 1888a6c5d14fSGrygorii Strashko int ret; 18893b72c2feSMugunthan V N 1890606f3993SIvan Khoronzhuk if (vid == cpsw->data.default_vlan) 18913b72c2feSMugunthan V N return 0; 18923b72c2feSMugunthan V N 189356e31bd8SIvan Khoronzhuk ret = pm_runtime_get_sync(cpsw->dev); 1894a6c5d14fSGrygorii Strashko if (ret < 0) { 189556e31bd8SIvan Khoronzhuk pm_runtime_put_noidle(cpsw->dev); 1896a6c5d14fSGrygorii Strashko return ret; 1897a6c5d14fSGrygorii Strashko } 1898a6c5d14fSGrygorii Strashko 1899606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) { 190002a54164SMugunthan V N /* In dual EMAC, reserved VLAN id should not be used for 190102a54164SMugunthan V N * creating VLAN interfaces as this can break the dual 190202a54164SMugunthan V N * EMAC port separation 190302a54164SMugunthan V N */ 190402a54164SMugunthan V N int i; 190502a54164SMugunthan V N 1906606f3993SIvan Khoronzhuk for (i = 0; i < cpsw->data.slaves; i++) { 1907606f3993SIvan Khoronzhuk if (vid == cpsw->slaves[i].port_vlan) 190802a54164SMugunthan V N return -EINVAL; 190902a54164SMugunthan V N } 191002a54164SMugunthan V N } 191102a54164SMugunthan V N 19123b72c2feSMugunthan V N dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid); 1913a6c5d14fSGrygorii Strashko ret = cpsw_add_vlan_ale_entry(priv, vid); 1914a6c5d14fSGrygorii Strashko 191556e31bd8SIvan Khoronzhuk pm_runtime_put(cpsw->dev); 1916a6c5d14fSGrygorii Strashko return ret; 19173b72c2feSMugunthan V N } 19183b72c2feSMugunthan V N 19193b72c2feSMugunthan V N static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev, 192080d5c368SPatrick McHardy __be16 proto, u16 vid) 19213b72c2feSMugunthan V N { 19223b72c2feSMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 1923649a1688SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 19243b72c2feSMugunthan V N int ret; 19253b72c2feSMugunthan V N 1926606f3993SIvan Khoronzhuk if (vid == cpsw->data.default_vlan) 19273b72c2feSMugunthan V N return 0; 19283b72c2feSMugunthan V N 192956e31bd8SIvan Khoronzhuk ret = pm_runtime_get_sync(cpsw->dev); 1930a6c5d14fSGrygorii Strashko if (ret < 0) { 193156e31bd8SIvan Khoronzhuk pm_runtime_put_noidle(cpsw->dev); 1932a6c5d14fSGrygorii Strashko return ret; 1933a6c5d14fSGrygorii Strashko } 1934a6c5d14fSGrygorii Strashko 1935606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) { 193602a54164SMugunthan V N int i; 193702a54164SMugunthan V N 1938606f3993SIvan Khoronzhuk for (i = 0; i < cpsw->data.slaves; i++) { 1939606f3993SIvan Khoronzhuk if (vid == cpsw->slaves[i].port_vlan) 194002a54164SMugunthan V N return -EINVAL; 194102a54164SMugunthan V N } 194202a54164SMugunthan V N } 194302a54164SMugunthan V N 19443b72c2feSMugunthan V N dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid); 19452a05a622SIvan Khoronzhuk ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0); 19463b72c2feSMugunthan V N if (ret != 0) 19473b72c2feSMugunthan V N return ret; 19483b72c2feSMugunthan V N 19492a05a622SIvan Khoronzhuk ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, 195061f1cef9SGrygorii Strashko HOST_PORT_NUM, ALE_VLAN, vid); 19513b72c2feSMugunthan V N if (ret != 0) 19523b72c2feSMugunthan V N return ret; 19533b72c2feSMugunthan V N 19542a05a622SIvan Khoronzhuk ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast, 19553b72c2feSMugunthan V N 0, ALE_VLAN, vid); 195656e31bd8SIvan Khoronzhuk pm_runtime_put(cpsw->dev); 1957a6c5d14fSGrygorii Strashko return ret; 19583b72c2feSMugunthan V N } 19593b72c2feSMugunthan V N 196083fcad0cSIvan Khoronzhuk static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate) 196183fcad0cSIvan Khoronzhuk { 196283fcad0cSIvan Khoronzhuk struct cpsw_priv *priv = netdev_priv(ndev); 196383fcad0cSIvan Khoronzhuk int tx_ch_num = ndev->real_num_tx_queues; 196483fcad0cSIvan Khoronzhuk u32 consumed_rate, min_rate, max_rate; 196583fcad0cSIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 196683fcad0cSIvan Khoronzhuk struct cpsw_slave *slave; 196783fcad0cSIvan Khoronzhuk int ret, i, weight; 196883fcad0cSIvan Khoronzhuk int rlim_num = 0; 196983fcad0cSIvan Khoronzhuk u32 ch_rate; 197083fcad0cSIvan Khoronzhuk 197183fcad0cSIvan Khoronzhuk ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate; 197283fcad0cSIvan Khoronzhuk if (ch_rate == rate) 197383fcad0cSIvan Khoronzhuk return 0; 197483fcad0cSIvan Khoronzhuk 197583fcad0cSIvan Khoronzhuk if (cpsw->data.dual_emac) 197683fcad0cSIvan Khoronzhuk slave = &cpsw->slaves[priv->emac_port]; 197783fcad0cSIvan Khoronzhuk else 197883fcad0cSIvan Khoronzhuk slave = &cpsw->slaves[cpsw->data.active_slave]; 197983fcad0cSIvan Khoronzhuk max_rate = slave->phy->speed; 198083fcad0cSIvan Khoronzhuk 198183fcad0cSIvan Khoronzhuk consumed_rate = 0; 198283fcad0cSIvan Khoronzhuk for (i = 0; i < tx_ch_num; i++) { 198383fcad0cSIvan Khoronzhuk if (i == queue) 198483fcad0cSIvan Khoronzhuk ch_rate = rate; 198583fcad0cSIvan Khoronzhuk else 198683fcad0cSIvan Khoronzhuk ch_rate = netdev_get_tx_queue(ndev, i)->tx_maxrate; 198783fcad0cSIvan Khoronzhuk if (!ch_rate) 198883fcad0cSIvan Khoronzhuk continue; 198983fcad0cSIvan Khoronzhuk 199083fcad0cSIvan Khoronzhuk rlim_num++; 199183fcad0cSIvan Khoronzhuk consumed_rate += ch_rate; 199283fcad0cSIvan Khoronzhuk } 199383fcad0cSIvan Khoronzhuk 199483fcad0cSIvan Khoronzhuk if (consumed_rate > max_rate) 199583fcad0cSIvan Khoronzhuk dev_info(priv->dev, "The common rate shouldn't be more than %dMbps", 199683fcad0cSIvan Khoronzhuk max_rate); 199783fcad0cSIvan Khoronzhuk 199883fcad0cSIvan Khoronzhuk if (consumed_rate > max_rate) { 199983fcad0cSIvan Khoronzhuk if (max_rate == 10 && consumed_rate <= 100) { 200083fcad0cSIvan Khoronzhuk max_rate = 100; 200183fcad0cSIvan Khoronzhuk } else if (max_rate <= 100 && consumed_rate <= 1000) { 200283fcad0cSIvan Khoronzhuk max_rate = 1000; 200383fcad0cSIvan Khoronzhuk } else { 200483fcad0cSIvan Khoronzhuk dev_err(priv->dev, "The common rate cannot be more than %dMbps", 200583fcad0cSIvan Khoronzhuk max_rate); 200683fcad0cSIvan Khoronzhuk return -EINVAL; 200783fcad0cSIvan Khoronzhuk } 200883fcad0cSIvan Khoronzhuk } 200983fcad0cSIvan Khoronzhuk 201083fcad0cSIvan Khoronzhuk if (consumed_rate > max_rate) { 201183fcad0cSIvan Khoronzhuk dev_err(priv->dev, "The common rate cannot be more than %dMbps", 201283fcad0cSIvan Khoronzhuk max_rate); 201383fcad0cSIvan Khoronzhuk return -EINVAL; 201483fcad0cSIvan Khoronzhuk } 201583fcad0cSIvan Khoronzhuk 201683fcad0cSIvan Khoronzhuk rate *= 1000; 201783fcad0cSIvan Khoronzhuk min_rate = cpdma_chan_get_min_rate(cpsw->dma); 201883fcad0cSIvan Khoronzhuk if ((rate < min_rate && rate)) { 201983fcad0cSIvan Khoronzhuk dev_err(priv->dev, "The common rate cannot be less than %dMbps", 202083fcad0cSIvan Khoronzhuk min_rate); 202183fcad0cSIvan Khoronzhuk return -EINVAL; 202283fcad0cSIvan Khoronzhuk } 202383fcad0cSIvan Khoronzhuk 202483fcad0cSIvan Khoronzhuk ret = pm_runtime_get_sync(cpsw->dev); 202583fcad0cSIvan Khoronzhuk if (ret < 0) { 202683fcad0cSIvan Khoronzhuk pm_runtime_put_noidle(cpsw->dev); 202783fcad0cSIvan Khoronzhuk return ret; 202883fcad0cSIvan Khoronzhuk } 202983fcad0cSIvan Khoronzhuk 203083fcad0cSIvan Khoronzhuk if (rlim_num == tx_ch_num) 203183fcad0cSIvan Khoronzhuk max_rate = consumed_rate; 203283fcad0cSIvan Khoronzhuk 203383fcad0cSIvan Khoronzhuk weight = (rate * 100) / (max_rate * 1000); 2034*8feb0a19SIvan Khoronzhuk cpdma_chan_set_weight(cpsw->txv[queue].ch, weight); 2035*8feb0a19SIvan Khoronzhuk ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, rate); 203683fcad0cSIvan Khoronzhuk 2037*8feb0a19SIvan Khoronzhuk /* re-split budget between channels */ 2038*8feb0a19SIvan Khoronzhuk if (!rate) 2039*8feb0a19SIvan Khoronzhuk cpsw_split_budget(ndev); 204083fcad0cSIvan Khoronzhuk pm_runtime_put(cpsw->dev); 204183fcad0cSIvan Khoronzhuk return ret; 204283fcad0cSIvan Khoronzhuk } 204383fcad0cSIvan Khoronzhuk 2044df828598SMugunthan V N static const struct net_device_ops cpsw_netdev_ops = { 2045df828598SMugunthan V N .ndo_open = cpsw_ndo_open, 2046df828598SMugunthan V N .ndo_stop = cpsw_ndo_stop, 2047df828598SMugunthan V N .ndo_start_xmit = cpsw_ndo_start_xmit, 2048dcfd8d58SMugunthan V N .ndo_set_mac_address = cpsw_ndo_set_mac_address, 20492e5b38abSRichard Cochran .ndo_do_ioctl = cpsw_ndo_ioctl, 2050df828598SMugunthan V N .ndo_validate_addr = eth_validate_addr, 2051df828598SMugunthan V N .ndo_tx_timeout = cpsw_ndo_tx_timeout, 20525c50a856SMugunthan V N .ndo_set_rx_mode = cpsw_ndo_set_rx_mode, 205383fcad0cSIvan Khoronzhuk .ndo_set_tx_maxrate = cpsw_ndo_set_tx_maxrate, 2054df828598SMugunthan V N #ifdef CONFIG_NET_POLL_CONTROLLER 2055df828598SMugunthan V N .ndo_poll_controller = cpsw_ndo_poll_controller, 2056df828598SMugunthan V N #endif 20573b72c2feSMugunthan V N .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid, 20583b72c2feSMugunthan V N .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid, 2059df828598SMugunthan V N }; 2060df828598SMugunthan V N 206152c4f0ecSMugunthan V N static int cpsw_get_regs_len(struct net_device *ndev) 206252c4f0ecSMugunthan V N { 2063606f3993SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 206452c4f0ecSMugunthan V N 2065606f3993SIvan Khoronzhuk return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32); 206652c4f0ecSMugunthan V N } 206752c4f0ecSMugunthan V N 206852c4f0ecSMugunthan V N static void cpsw_get_regs(struct net_device *ndev, 206952c4f0ecSMugunthan V N struct ethtool_regs *regs, void *p) 207052c4f0ecSMugunthan V N { 207152c4f0ecSMugunthan V N u32 *reg = p; 20722a05a622SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 207352c4f0ecSMugunthan V N 207452c4f0ecSMugunthan V N /* update CPSW IP version */ 20752a05a622SIvan Khoronzhuk regs->version = cpsw->version; 207652c4f0ecSMugunthan V N 20772a05a622SIvan Khoronzhuk cpsw_ale_dump(cpsw->ale, reg); 207852c4f0ecSMugunthan V N } 207952c4f0ecSMugunthan V N 2080df828598SMugunthan V N static void cpsw_get_drvinfo(struct net_device *ndev, 2081df828598SMugunthan V N struct ethtool_drvinfo *info) 2082df828598SMugunthan V N { 2083649a1688SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 208456e31bd8SIvan Khoronzhuk struct platform_device *pdev = to_platform_device(cpsw->dev); 20857826d43fSJiri Pirko 208652c4f0ecSMugunthan V N strlcpy(info->driver, "cpsw", sizeof(info->driver)); 20877826d43fSJiri Pirko strlcpy(info->version, "1.0", sizeof(info->version)); 208856e31bd8SIvan Khoronzhuk strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info)); 2089df828598SMugunthan V N } 2090df828598SMugunthan V N 2091df828598SMugunthan V N static u32 cpsw_get_msglevel(struct net_device *ndev) 2092df828598SMugunthan V N { 2093df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 2094df828598SMugunthan V N return priv->msg_enable; 2095df828598SMugunthan V N } 2096df828598SMugunthan V N 2097df828598SMugunthan V N static void cpsw_set_msglevel(struct net_device *ndev, u32 value) 2098df828598SMugunthan V N { 2099df828598SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 2100df828598SMugunthan V N priv->msg_enable = value; 2101df828598SMugunthan V N } 2102df828598SMugunthan V N 21032e5b38abSRichard Cochran static int cpsw_get_ts_info(struct net_device *ndev, 21042e5b38abSRichard Cochran struct ethtool_ts_info *info) 21052e5b38abSRichard Cochran { 21062e5b38abSRichard Cochran #ifdef CONFIG_TI_CPTS 21072a05a622SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 21082e5b38abSRichard Cochran 21092e5b38abSRichard Cochran info->so_timestamping = 21102e5b38abSRichard Cochran SOF_TIMESTAMPING_TX_HARDWARE | 21112e5b38abSRichard Cochran SOF_TIMESTAMPING_TX_SOFTWARE | 21122e5b38abSRichard Cochran SOF_TIMESTAMPING_RX_HARDWARE | 21132e5b38abSRichard Cochran SOF_TIMESTAMPING_RX_SOFTWARE | 21142e5b38abSRichard Cochran SOF_TIMESTAMPING_SOFTWARE | 21152e5b38abSRichard Cochran SOF_TIMESTAMPING_RAW_HARDWARE; 21162a05a622SIvan Khoronzhuk info->phc_index = cpsw->cpts->phc_index; 21172e5b38abSRichard Cochran info->tx_types = 21182e5b38abSRichard Cochran (1 << HWTSTAMP_TX_OFF) | 21192e5b38abSRichard Cochran (1 << HWTSTAMP_TX_ON); 21202e5b38abSRichard Cochran info->rx_filters = 21212e5b38abSRichard Cochran (1 << HWTSTAMP_FILTER_NONE) | 21222e5b38abSRichard Cochran (1 << HWTSTAMP_FILTER_PTP_V2_EVENT); 21232e5b38abSRichard Cochran #else 21242e5b38abSRichard Cochran info->so_timestamping = 21252e5b38abSRichard Cochran SOF_TIMESTAMPING_TX_SOFTWARE | 21262e5b38abSRichard Cochran SOF_TIMESTAMPING_RX_SOFTWARE | 21272e5b38abSRichard Cochran SOF_TIMESTAMPING_SOFTWARE; 21282e5b38abSRichard Cochran info->phc_index = -1; 21292e5b38abSRichard Cochran info->tx_types = 0; 21302e5b38abSRichard Cochran info->rx_filters = 0; 21312e5b38abSRichard Cochran #endif 21322e5b38abSRichard Cochran return 0; 21332e5b38abSRichard Cochran } 21342e5b38abSRichard Cochran 21352479876dSPhilippe Reynes static int cpsw_get_link_ksettings(struct net_device *ndev, 21362479876dSPhilippe Reynes struct ethtool_link_ksettings *ecmd) 2137d3bb9c58SMugunthan V N { 2138d3bb9c58SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 2139606f3993SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 2140606f3993SIvan Khoronzhuk int slave_no = cpsw_slave_index(cpsw, priv); 2141d3bb9c58SMugunthan V N 2142606f3993SIvan Khoronzhuk if (cpsw->slaves[slave_no].phy) 21432479876dSPhilippe Reynes return phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy, 21442479876dSPhilippe Reynes ecmd); 2145d3bb9c58SMugunthan V N else 2146d3bb9c58SMugunthan V N return -EOPNOTSUPP; 2147d3bb9c58SMugunthan V N } 2148d3bb9c58SMugunthan V N 21492479876dSPhilippe Reynes static int cpsw_set_link_ksettings(struct net_device *ndev, 21502479876dSPhilippe Reynes const struct ethtool_link_ksettings *ecmd) 2151d3bb9c58SMugunthan V N { 2152d3bb9c58SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 2153606f3993SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 2154606f3993SIvan Khoronzhuk int slave_no = cpsw_slave_index(cpsw, priv); 2155d3bb9c58SMugunthan V N 2156606f3993SIvan Khoronzhuk if (cpsw->slaves[slave_no].phy) 21572479876dSPhilippe Reynes return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy, 21582479876dSPhilippe Reynes ecmd); 2159d3bb9c58SMugunthan V N else 2160d3bb9c58SMugunthan V N return -EOPNOTSUPP; 2161d3bb9c58SMugunthan V N } 2162d3bb9c58SMugunthan V N 2163d8a64420SMatus Ujhelyi static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol) 2164d8a64420SMatus Ujhelyi { 2165d8a64420SMatus Ujhelyi struct cpsw_priv *priv = netdev_priv(ndev); 2166606f3993SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 2167606f3993SIvan Khoronzhuk int slave_no = cpsw_slave_index(cpsw, priv); 2168d8a64420SMatus Ujhelyi 2169d8a64420SMatus Ujhelyi wol->supported = 0; 2170d8a64420SMatus Ujhelyi wol->wolopts = 0; 2171d8a64420SMatus Ujhelyi 2172606f3993SIvan Khoronzhuk if (cpsw->slaves[slave_no].phy) 2173606f3993SIvan Khoronzhuk phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol); 2174d8a64420SMatus Ujhelyi } 2175d8a64420SMatus Ujhelyi 2176d8a64420SMatus Ujhelyi static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol) 2177d8a64420SMatus Ujhelyi { 2178d8a64420SMatus Ujhelyi struct cpsw_priv *priv = netdev_priv(ndev); 2179606f3993SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 2180606f3993SIvan Khoronzhuk int slave_no = cpsw_slave_index(cpsw, priv); 2181d8a64420SMatus Ujhelyi 2182606f3993SIvan Khoronzhuk if (cpsw->slaves[slave_no].phy) 2183606f3993SIvan Khoronzhuk return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol); 2184d8a64420SMatus Ujhelyi else 2185d8a64420SMatus Ujhelyi return -EOPNOTSUPP; 2186d8a64420SMatus Ujhelyi } 2187d8a64420SMatus Ujhelyi 21881923d6e4SMugunthan V N static void cpsw_get_pauseparam(struct net_device *ndev, 21891923d6e4SMugunthan V N struct ethtool_pauseparam *pause) 21901923d6e4SMugunthan V N { 21911923d6e4SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 21921923d6e4SMugunthan V N 21931923d6e4SMugunthan V N pause->autoneg = AUTONEG_DISABLE; 21941923d6e4SMugunthan V N pause->rx_pause = priv->rx_pause ? true : false; 21951923d6e4SMugunthan V N pause->tx_pause = priv->tx_pause ? true : false; 21961923d6e4SMugunthan V N } 21971923d6e4SMugunthan V N 21981923d6e4SMugunthan V N static int cpsw_set_pauseparam(struct net_device *ndev, 21991923d6e4SMugunthan V N struct ethtool_pauseparam *pause) 22001923d6e4SMugunthan V N { 22011923d6e4SMugunthan V N struct cpsw_priv *priv = netdev_priv(ndev); 22021923d6e4SMugunthan V N bool link; 22031923d6e4SMugunthan V N 22041923d6e4SMugunthan V N priv->rx_pause = pause->rx_pause ? true : false; 22051923d6e4SMugunthan V N priv->tx_pause = pause->tx_pause ? true : false; 22061923d6e4SMugunthan V N 22071923d6e4SMugunthan V N for_each_slave(priv, _cpsw_adjust_link, priv, &link); 22081923d6e4SMugunthan V N return 0; 22091923d6e4SMugunthan V N } 22101923d6e4SMugunthan V N 22117898b1daSGrygorii Strashko static int cpsw_ethtool_op_begin(struct net_device *ndev) 22127898b1daSGrygorii Strashko { 22137898b1daSGrygorii Strashko struct cpsw_priv *priv = netdev_priv(ndev); 2214649a1688SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 22157898b1daSGrygorii Strashko int ret; 22167898b1daSGrygorii Strashko 221756e31bd8SIvan Khoronzhuk ret = pm_runtime_get_sync(cpsw->dev); 22187898b1daSGrygorii Strashko if (ret < 0) { 22197898b1daSGrygorii Strashko cpsw_err(priv, drv, "ethtool begin failed %d\n", ret); 222056e31bd8SIvan Khoronzhuk pm_runtime_put_noidle(cpsw->dev); 22217898b1daSGrygorii Strashko } 22227898b1daSGrygorii Strashko 22237898b1daSGrygorii Strashko return ret; 22247898b1daSGrygorii Strashko } 22257898b1daSGrygorii Strashko 22267898b1daSGrygorii Strashko static void cpsw_ethtool_op_complete(struct net_device *ndev) 22277898b1daSGrygorii Strashko { 22287898b1daSGrygorii Strashko struct cpsw_priv *priv = netdev_priv(ndev); 22297898b1daSGrygorii Strashko int ret; 22307898b1daSGrygorii Strashko 223156e31bd8SIvan Khoronzhuk ret = pm_runtime_put(priv->cpsw->dev); 22327898b1daSGrygorii Strashko if (ret < 0) 22337898b1daSGrygorii Strashko cpsw_err(priv, drv, "ethtool complete failed %d\n", ret); 22347898b1daSGrygorii Strashko } 22357898b1daSGrygorii Strashko 2236ce52c744SIvan Khoronzhuk static void cpsw_get_channels(struct net_device *ndev, 2237ce52c744SIvan Khoronzhuk struct ethtool_channels *ch) 2238ce52c744SIvan Khoronzhuk { 2239ce52c744SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 2240ce52c744SIvan Khoronzhuk 2241ce52c744SIvan Khoronzhuk ch->max_combined = 0; 2242ce52c744SIvan Khoronzhuk ch->max_rx = CPSW_MAX_QUEUES; 2243ce52c744SIvan Khoronzhuk ch->max_tx = CPSW_MAX_QUEUES; 2244ce52c744SIvan Khoronzhuk ch->max_other = 0; 2245ce52c744SIvan Khoronzhuk ch->other_count = 0; 2246ce52c744SIvan Khoronzhuk ch->rx_count = cpsw->rx_ch_num; 2247ce52c744SIvan Khoronzhuk ch->tx_count = cpsw->tx_ch_num; 2248ce52c744SIvan Khoronzhuk ch->combined_count = 0; 2249ce52c744SIvan Khoronzhuk } 2250ce52c744SIvan Khoronzhuk 2251ce52c744SIvan Khoronzhuk static int cpsw_check_ch_settings(struct cpsw_common *cpsw, 2252ce52c744SIvan Khoronzhuk struct ethtool_channels *ch) 2253ce52c744SIvan Khoronzhuk { 2254ce52c744SIvan Khoronzhuk if (ch->combined_count) 2255ce52c744SIvan Khoronzhuk return -EINVAL; 2256ce52c744SIvan Khoronzhuk 2257ce52c744SIvan Khoronzhuk /* verify we have at least one channel in each direction */ 2258ce52c744SIvan Khoronzhuk if (!ch->rx_count || !ch->tx_count) 2259ce52c744SIvan Khoronzhuk return -EINVAL; 2260ce52c744SIvan Khoronzhuk 2261ce52c744SIvan Khoronzhuk if (ch->rx_count > cpsw->data.channels || 2262ce52c744SIvan Khoronzhuk ch->tx_count > cpsw->data.channels) 2263ce52c744SIvan Khoronzhuk return -EINVAL; 2264ce52c744SIvan Khoronzhuk 2265ce52c744SIvan Khoronzhuk return 0; 2266ce52c744SIvan Khoronzhuk } 2267ce52c744SIvan Khoronzhuk 2268ce52c744SIvan Khoronzhuk static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx) 2269ce52c744SIvan Khoronzhuk { 2270ce52c744SIvan Khoronzhuk int (*poll)(struct napi_struct *, int); 2271ce52c744SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 2272ce52c744SIvan Khoronzhuk void (*handler)(void *, int, int); 227383fcad0cSIvan Khoronzhuk struct netdev_queue *queue; 2274*8feb0a19SIvan Khoronzhuk struct cpsw_vector *vec; 2275ce52c744SIvan Khoronzhuk int ret, *ch; 2276ce52c744SIvan Khoronzhuk 2277ce52c744SIvan Khoronzhuk if (rx) { 2278ce52c744SIvan Khoronzhuk ch = &cpsw->rx_ch_num; 2279*8feb0a19SIvan Khoronzhuk vec = cpsw->rxv; 2280ce52c744SIvan Khoronzhuk handler = cpsw_rx_handler; 2281ce52c744SIvan Khoronzhuk poll = cpsw_rx_poll; 2282ce52c744SIvan Khoronzhuk } else { 2283ce52c744SIvan Khoronzhuk ch = &cpsw->tx_ch_num; 2284*8feb0a19SIvan Khoronzhuk vec = cpsw->txv; 2285ce52c744SIvan Khoronzhuk handler = cpsw_tx_handler; 2286ce52c744SIvan Khoronzhuk poll = cpsw_tx_poll; 2287ce52c744SIvan Khoronzhuk } 2288ce52c744SIvan Khoronzhuk 2289ce52c744SIvan Khoronzhuk while (*ch < ch_num) { 2290*8feb0a19SIvan Khoronzhuk vec[*ch].ch = cpdma_chan_create(cpsw->dma, *ch, handler, rx); 229183fcad0cSIvan Khoronzhuk queue = netdev_get_tx_queue(priv->ndev, *ch); 229283fcad0cSIvan Khoronzhuk queue->tx_maxrate = 0; 2293ce52c744SIvan Khoronzhuk 2294*8feb0a19SIvan Khoronzhuk if (IS_ERR(vec[*ch].ch)) 2295*8feb0a19SIvan Khoronzhuk return PTR_ERR(vec[*ch].ch); 2296ce52c744SIvan Khoronzhuk 2297*8feb0a19SIvan Khoronzhuk if (!vec[*ch].ch) 2298ce52c744SIvan Khoronzhuk return -EINVAL; 2299ce52c744SIvan Khoronzhuk 2300ce52c744SIvan Khoronzhuk cpsw_info(priv, ifup, "created new %d %s channel\n", *ch, 2301ce52c744SIvan Khoronzhuk (rx ? "rx" : "tx")); 2302ce52c744SIvan Khoronzhuk (*ch)++; 2303ce52c744SIvan Khoronzhuk } 2304ce52c744SIvan Khoronzhuk 2305ce52c744SIvan Khoronzhuk while (*ch > ch_num) { 2306ce52c744SIvan Khoronzhuk (*ch)--; 2307ce52c744SIvan Khoronzhuk 2308*8feb0a19SIvan Khoronzhuk ret = cpdma_chan_destroy(vec[*ch].ch); 2309ce52c744SIvan Khoronzhuk if (ret) 2310ce52c744SIvan Khoronzhuk return ret; 2311ce52c744SIvan Khoronzhuk 2312ce52c744SIvan Khoronzhuk cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch, 2313ce52c744SIvan Khoronzhuk (rx ? "rx" : "tx")); 2314ce52c744SIvan Khoronzhuk } 2315ce52c744SIvan Khoronzhuk 2316ce52c744SIvan Khoronzhuk return 0; 2317ce52c744SIvan Khoronzhuk } 2318ce52c744SIvan Khoronzhuk 2319ce52c744SIvan Khoronzhuk static int cpsw_update_channels(struct cpsw_priv *priv, 2320ce52c744SIvan Khoronzhuk struct ethtool_channels *ch) 2321ce52c744SIvan Khoronzhuk { 2322ce52c744SIvan Khoronzhuk int ret; 2323ce52c744SIvan Khoronzhuk 2324ce52c744SIvan Khoronzhuk ret = cpsw_update_channels_res(priv, ch->rx_count, 1); 2325ce52c744SIvan Khoronzhuk if (ret) 2326ce52c744SIvan Khoronzhuk return ret; 2327ce52c744SIvan Khoronzhuk 2328ce52c744SIvan Khoronzhuk ret = cpsw_update_channels_res(priv, ch->tx_count, 0); 2329ce52c744SIvan Khoronzhuk if (ret) 2330ce52c744SIvan Khoronzhuk return ret; 2331ce52c744SIvan Khoronzhuk 2332ce52c744SIvan Khoronzhuk return 0; 2333ce52c744SIvan Khoronzhuk } 2334ce52c744SIvan Khoronzhuk 2335ce52c744SIvan Khoronzhuk static int cpsw_set_channels(struct net_device *ndev, 2336ce52c744SIvan Khoronzhuk struct ethtool_channels *chs) 2337ce52c744SIvan Khoronzhuk { 2338ce52c744SIvan Khoronzhuk struct cpsw_priv *priv = netdev_priv(ndev); 2339ce52c744SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 2340ce52c744SIvan Khoronzhuk struct cpsw_slave *slave; 2341ce52c744SIvan Khoronzhuk int i, ret; 2342ce52c744SIvan Khoronzhuk 2343ce52c744SIvan Khoronzhuk ret = cpsw_check_ch_settings(cpsw, chs); 2344ce52c744SIvan Khoronzhuk if (ret < 0) 2345ce52c744SIvan Khoronzhuk return ret; 2346ce52c744SIvan Khoronzhuk 2347ce52c744SIvan Khoronzhuk /* Disable NAPI scheduling */ 2348ce52c744SIvan Khoronzhuk cpsw_intr_disable(cpsw); 2349ce52c744SIvan Khoronzhuk 2350ce52c744SIvan Khoronzhuk /* Stop all transmit queues for every network device. 2351ce52c744SIvan Khoronzhuk * Disable re-using rx descriptors with dormant_on. 2352ce52c744SIvan Khoronzhuk */ 2353ce52c744SIvan Khoronzhuk for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) { 2354ce52c744SIvan Khoronzhuk if (!(slave->ndev && netif_running(slave->ndev))) 2355ce52c744SIvan Khoronzhuk continue; 2356ce52c744SIvan Khoronzhuk 2357ce52c744SIvan Khoronzhuk netif_tx_stop_all_queues(slave->ndev); 2358ce52c744SIvan Khoronzhuk netif_dormant_on(slave->ndev); 2359ce52c744SIvan Khoronzhuk } 2360ce52c744SIvan Khoronzhuk 2361ce52c744SIvan Khoronzhuk /* Handle rest of tx packets and stop cpdma channels */ 2362ce52c744SIvan Khoronzhuk cpdma_ctlr_stop(cpsw->dma); 2363ce52c744SIvan Khoronzhuk ret = cpsw_update_channels(priv, chs); 2364ce52c744SIvan Khoronzhuk if (ret) 2365ce52c744SIvan Khoronzhuk goto err; 2366ce52c744SIvan Khoronzhuk 2367ce52c744SIvan Khoronzhuk for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) { 2368ce52c744SIvan Khoronzhuk if (!(slave->ndev && netif_running(slave->ndev))) 2369ce52c744SIvan Khoronzhuk continue; 2370ce52c744SIvan Khoronzhuk 2371ce52c744SIvan Khoronzhuk /* Inform stack about new count of queues */ 2372ce52c744SIvan Khoronzhuk ret = netif_set_real_num_tx_queues(slave->ndev, 2373ce52c744SIvan Khoronzhuk cpsw->tx_ch_num); 2374ce52c744SIvan Khoronzhuk if (ret) { 2375ce52c744SIvan Khoronzhuk dev_err(priv->dev, "cannot set real number of tx queues\n"); 2376ce52c744SIvan Khoronzhuk goto err; 2377ce52c744SIvan Khoronzhuk } 2378ce52c744SIvan Khoronzhuk 2379ce52c744SIvan Khoronzhuk ret = netif_set_real_num_rx_queues(slave->ndev, 2380ce52c744SIvan Khoronzhuk cpsw->rx_ch_num); 2381ce52c744SIvan Khoronzhuk if (ret) { 2382ce52c744SIvan Khoronzhuk dev_err(priv->dev, "cannot set real number of rx queues\n"); 2383ce52c744SIvan Khoronzhuk goto err; 2384ce52c744SIvan Khoronzhuk } 2385ce52c744SIvan Khoronzhuk 2386ce52c744SIvan Khoronzhuk /* Enable rx packets handling */ 2387ce52c744SIvan Khoronzhuk netif_dormant_off(slave->ndev); 2388ce52c744SIvan Khoronzhuk } 2389ce52c744SIvan Khoronzhuk 2390ce52c744SIvan Khoronzhuk if (cpsw_common_res_usage_state(cpsw)) { 2391e19ac157SWei Yongjun ret = cpsw_fill_rx_channels(priv); 2392e19ac157SWei Yongjun if (ret) 2393ce52c744SIvan Khoronzhuk goto err; 2394ce52c744SIvan Khoronzhuk 2395*8feb0a19SIvan Khoronzhuk cpsw_split_budget(ndev); 2396*8feb0a19SIvan Khoronzhuk 2397ce52c744SIvan Khoronzhuk /* After this receive is started */ 2398ce52c744SIvan Khoronzhuk cpdma_ctlr_start(cpsw->dma); 2399ce52c744SIvan Khoronzhuk cpsw_intr_enable(cpsw); 2400ce52c744SIvan Khoronzhuk } 2401ce52c744SIvan Khoronzhuk 2402ce52c744SIvan Khoronzhuk /* Resume transmit for every affected interface */ 2403ce52c744SIvan Khoronzhuk for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) { 2404ce52c744SIvan Khoronzhuk if (!(slave->ndev && netif_running(slave->ndev))) 2405ce52c744SIvan Khoronzhuk continue; 2406ce52c744SIvan Khoronzhuk netif_tx_start_all_queues(slave->ndev); 2407ce52c744SIvan Khoronzhuk } 2408ce52c744SIvan Khoronzhuk return 0; 2409ce52c744SIvan Khoronzhuk err: 2410ce52c744SIvan Khoronzhuk dev_err(priv->dev, "cannot update channels number, closing device\n"); 2411ce52c744SIvan Khoronzhuk dev_close(ndev); 2412ce52c744SIvan Khoronzhuk return ret; 2413ce52c744SIvan Khoronzhuk } 2414ce52c744SIvan Khoronzhuk 2415a0909949SYegor Yefremov static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata) 2416a0909949SYegor Yefremov { 2417a0909949SYegor Yefremov struct cpsw_priv *priv = netdev_priv(ndev); 2418a0909949SYegor Yefremov struct cpsw_common *cpsw = priv->cpsw; 2419a0909949SYegor Yefremov int slave_no = cpsw_slave_index(cpsw, priv); 2420a0909949SYegor Yefremov 2421a0909949SYegor Yefremov if (cpsw->slaves[slave_no].phy) 2422a0909949SYegor Yefremov return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata); 2423a0909949SYegor Yefremov else 2424a0909949SYegor Yefremov return -EOPNOTSUPP; 2425a0909949SYegor Yefremov } 2426a0909949SYegor Yefremov 2427a0909949SYegor Yefremov static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata) 2428a0909949SYegor Yefremov { 2429a0909949SYegor Yefremov struct cpsw_priv *priv = netdev_priv(ndev); 2430a0909949SYegor Yefremov struct cpsw_common *cpsw = priv->cpsw; 2431a0909949SYegor Yefremov int slave_no = cpsw_slave_index(cpsw, priv); 2432a0909949SYegor Yefremov 2433a0909949SYegor Yefremov if (cpsw->slaves[slave_no].phy) 2434a0909949SYegor Yefremov return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata); 2435a0909949SYegor Yefremov else 2436a0909949SYegor Yefremov return -EOPNOTSUPP; 2437a0909949SYegor Yefremov } 2438a0909949SYegor Yefremov 24396bb10c2bSYegor Yefremov static int cpsw_nway_reset(struct net_device *ndev) 24406bb10c2bSYegor Yefremov { 24416bb10c2bSYegor Yefremov struct cpsw_priv *priv = netdev_priv(ndev); 24426bb10c2bSYegor Yefremov struct cpsw_common *cpsw = priv->cpsw; 24436bb10c2bSYegor Yefremov int slave_no = cpsw_slave_index(cpsw, priv); 24446bb10c2bSYegor Yefremov 24456bb10c2bSYegor Yefremov if (cpsw->slaves[slave_no].phy) 24466bb10c2bSYegor Yefremov return genphy_restart_aneg(cpsw->slaves[slave_no].phy); 24476bb10c2bSYegor Yefremov else 24486bb10c2bSYegor Yefremov return -EOPNOTSUPP; 24496bb10c2bSYegor Yefremov } 24506bb10c2bSYegor Yefremov 2451df828598SMugunthan V N static const struct ethtool_ops cpsw_ethtool_ops = { 2452df828598SMugunthan V N .get_drvinfo = cpsw_get_drvinfo, 2453df828598SMugunthan V N .get_msglevel = cpsw_get_msglevel, 2454df828598SMugunthan V N .set_msglevel = cpsw_set_msglevel, 2455df828598SMugunthan V N .get_link = ethtool_op_get_link, 24562e5b38abSRichard Cochran .get_ts_info = cpsw_get_ts_info, 2457ff5b8ef2SMugunthan V N .get_coalesce = cpsw_get_coalesce, 2458ff5b8ef2SMugunthan V N .set_coalesce = cpsw_set_coalesce, 2459d9718546SMugunthan V N .get_sset_count = cpsw_get_sset_count, 2460d9718546SMugunthan V N .get_strings = cpsw_get_strings, 2461d9718546SMugunthan V N .get_ethtool_stats = cpsw_get_ethtool_stats, 24621923d6e4SMugunthan V N .get_pauseparam = cpsw_get_pauseparam, 24631923d6e4SMugunthan V N .set_pauseparam = cpsw_set_pauseparam, 2464d8a64420SMatus Ujhelyi .get_wol = cpsw_get_wol, 2465d8a64420SMatus Ujhelyi .set_wol = cpsw_set_wol, 246652c4f0ecSMugunthan V N .get_regs_len = cpsw_get_regs_len, 246752c4f0ecSMugunthan V N .get_regs = cpsw_get_regs, 24687898b1daSGrygorii Strashko .begin = cpsw_ethtool_op_begin, 24697898b1daSGrygorii Strashko .complete = cpsw_ethtool_op_complete, 2470ce52c744SIvan Khoronzhuk .get_channels = cpsw_get_channels, 2471ce52c744SIvan Khoronzhuk .set_channels = cpsw_set_channels, 24722479876dSPhilippe Reynes .get_link_ksettings = cpsw_get_link_ksettings, 24732479876dSPhilippe Reynes .set_link_ksettings = cpsw_set_link_ksettings, 2474a0909949SYegor Yefremov .get_eee = cpsw_get_eee, 2475a0909949SYegor Yefremov .set_eee = cpsw_set_eee, 24766bb10c2bSYegor Yefremov .nway_reset = cpsw_nway_reset, 2477df828598SMugunthan V N }; 2478df828598SMugunthan V N 2479606f3993SIvan Khoronzhuk static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw, 2480549985eeSRichard Cochran u32 slave_reg_ofs, u32 sliver_reg_ofs) 2481df828598SMugunthan V N { 24825d8d0d4dSIvan Khoronzhuk void __iomem *regs = cpsw->regs; 2483df828598SMugunthan V N int slave_num = slave->slave_num; 2484606f3993SIvan Khoronzhuk struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num; 2485df828598SMugunthan V N 2486df828598SMugunthan V N slave->data = data; 2487549985eeSRichard Cochran slave->regs = regs + slave_reg_ofs; 2488549985eeSRichard Cochran slave->sliver = regs + sliver_reg_ofs; 2489d9ba8f9eSMugunthan V N slave->port_vlan = data->dual_emac_res_vlan; 2490df828598SMugunthan V N } 2491df828598SMugunthan V N 2492552165bcSDavid Rivshin static int cpsw_probe_dt(struct cpsw_platform_data *data, 24932eb32b0aSMugunthan V N struct platform_device *pdev) 24942eb32b0aSMugunthan V N { 24952eb32b0aSMugunthan V N struct device_node *node = pdev->dev.of_node; 24962eb32b0aSMugunthan V N struct device_node *slave_node; 24972eb32b0aSMugunthan V N int i = 0, ret; 24982eb32b0aSMugunthan V N u32 prop; 24992eb32b0aSMugunthan V N 25002eb32b0aSMugunthan V N if (!node) 25012eb32b0aSMugunthan V N return -EINVAL; 25022eb32b0aSMugunthan V N 25032eb32b0aSMugunthan V N if (of_property_read_u32(node, "slaves", &prop)) { 250488c99ff6SGeorge Cherian dev_err(&pdev->dev, "Missing slaves property in the DT.\n"); 25052eb32b0aSMugunthan V N return -EINVAL; 25062eb32b0aSMugunthan V N } 25072eb32b0aSMugunthan V N data->slaves = prop; 25082eb32b0aSMugunthan V N 2509e86ac13bSMugunthan V N if (of_property_read_u32(node, "active_slave", &prop)) { 251088c99ff6SGeorge Cherian dev_err(&pdev->dev, "Missing active_slave property in the DT.\n"); 2511aa1a15e2SDaniel Mack return -EINVAL; 251278ca0b28SRichard Cochran } 2513e86ac13bSMugunthan V N data->active_slave = prop; 251478ca0b28SRichard Cochran 251500ab94eeSRichard Cochran if (of_property_read_u32(node, "cpts_clock_mult", &prop)) { 251688c99ff6SGeorge Cherian dev_err(&pdev->dev, "Missing cpts_clock_mult property in the DT.\n"); 2517aa1a15e2SDaniel Mack return -EINVAL; 251800ab94eeSRichard Cochran } 251900ab94eeSRichard Cochran data->cpts_clock_mult = prop; 252000ab94eeSRichard Cochran 252100ab94eeSRichard Cochran if (of_property_read_u32(node, "cpts_clock_shift", &prop)) { 252288c99ff6SGeorge Cherian dev_err(&pdev->dev, "Missing cpts_clock_shift property in the DT.\n"); 2523aa1a15e2SDaniel Mack return -EINVAL; 252400ab94eeSRichard Cochran } 252500ab94eeSRichard Cochran data->cpts_clock_shift = prop; 252600ab94eeSRichard Cochran 2527aa1a15e2SDaniel Mack data->slave_data = devm_kzalloc(&pdev->dev, data->slaves 2528aa1a15e2SDaniel Mack * sizeof(struct cpsw_slave_data), 2529b2adaca9SJoe Perches GFP_KERNEL); 2530b2adaca9SJoe Perches if (!data->slave_data) 2531aa1a15e2SDaniel Mack return -ENOMEM; 25322eb32b0aSMugunthan V N 25332eb32b0aSMugunthan V N if (of_property_read_u32(node, "cpdma_channels", &prop)) { 253488c99ff6SGeorge Cherian dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n"); 2535aa1a15e2SDaniel Mack return -EINVAL; 25362eb32b0aSMugunthan V N } 25372eb32b0aSMugunthan V N data->channels = prop; 25382eb32b0aSMugunthan V N 25392eb32b0aSMugunthan V N if (of_property_read_u32(node, "ale_entries", &prop)) { 254088c99ff6SGeorge Cherian dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n"); 2541aa1a15e2SDaniel Mack return -EINVAL; 25422eb32b0aSMugunthan V N } 25432eb32b0aSMugunthan V N data->ale_entries = prop; 25442eb32b0aSMugunthan V N 25452eb32b0aSMugunthan V N if (of_property_read_u32(node, "bd_ram_size", &prop)) { 254688c99ff6SGeorge Cherian dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n"); 2547aa1a15e2SDaniel Mack return -EINVAL; 25482eb32b0aSMugunthan V N } 25492eb32b0aSMugunthan V N data->bd_ram_size = prop; 25502eb32b0aSMugunthan V N 25512eb32b0aSMugunthan V N if (of_property_read_u32(node, "mac_control", &prop)) { 255288c99ff6SGeorge Cherian dev_err(&pdev->dev, "Missing mac_control property in the DT.\n"); 2553aa1a15e2SDaniel Mack return -EINVAL; 25542eb32b0aSMugunthan V N } 25552eb32b0aSMugunthan V N data->mac_control = prop; 25562eb32b0aSMugunthan V N 2557281abd96SMarkus Pargmann if (of_property_read_bool(node, "dual_emac")) 2558281abd96SMarkus Pargmann data->dual_emac = 1; 2559d9ba8f9eSMugunthan V N 25601fb19aa7SVaibhav Hiremath /* 25611fb19aa7SVaibhav Hiremath * Populate all the child nodes here... 25621fb19aa7SVaibhav Hiremath */ 25631fb19aa7SVaibhav Hiremath ret = of_platform_populate(node, NULL, NULL, &pdev->dev); 25641fb19aa7SVaibhav Hiremath /* We do not want to force this, as in some cases may not have child */ 25651fb19aa7SVaibhav Hiremath if (ret) 256688c99ff6SGeorge Cherian dev_warn(&pdev->dev, "Doesn't have any child node\n"); 25671fb19aa7SVaibhav Hiremath 25688658aaf2SBen Hutchings for_each_available_child_of_node(node, slave_node) { 2569549985eeSRichard Cochran struct cpsw_slave_data *slave_data = data->slave_data + i; 2570549985eeSRichard Cochran const void *mac_addr = NULL; 2571549985eeSRichard Cochran int lenp; 2572549985eeSRichard Cochran const __be32 *parp; 2573549985eeSRichard Cochran 2574f468b10eSMarkus Pargmann /* This is no slave child node, continue */ 2575f468b10eSMarkus Pargmann if (strcmp(slave_node->name, "slave")) 2576f468b10eSMarkus Pargmann continue; 2577f468b10eSMarkus Pargmann 2578552165bcSDavid Rivshin slave_data->phy_node = of_parse_phandle(slave_node, 2579552165bcSDavid Rivshin "phy-handle", 0); 2580f1eea5c1SDavid Rivshin parp = of_get_property(slave_node, "phy_id", &lenp); 2581ae092b5bSDavid Rivshin if (slave_data->phy_node) { 2582ae092b5bSDavid Rivshin dev_dbg(&pdev->dev, 2583ae092b5bSDavid Rivshin "slave[%d] using phy-handle=\"%s\"\n", 2584ae092b5bSDavid Rivshin i, slave_data->phy_node->full_name); 2585ae092b5bSDavid Rivshin } else if (of_phy_is_fixed_link(slave_node)) { 2586dfc0a6d3SDavid Rivshin /* In the case of a fixed PHY, the DT node associated 2587dfc0a6d3SDavid Rivshin * to the PHY is the Ethernet MAC DT node. 2588dfc0a6d3SDavid Rivshin */ 25891f71e8c9SMarkus Brunner ret = of_phy_register_fixed_link(slave_node); 259023a09873SJohan Hovold if (ret) { 259123a09873SJohan Hovold if (ret != -EPROBE_DEFER) 259223a09873SJohan Hovold dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret); 25931f71e8c9SMarkus Brunner return ret; 259423a09873SJohan Hovold } 259506cd6d6eSDavid Rivshin slave_data->phy_node = of_node_get(slave_node); 2596f1eea5c1SDavid Rivshin } else if (parp) { 2597f1eea5c1SDavid Rivshin u32 phyid; 2598f1eea5c1SDavid Rivshin struct device_node *mdio_node; 2599f1eea5c1SDavid Rivshin struct platform_device *mdio; 2600f1eea5c1SDavid Rivshin 2601f1eea5c1SDavid Rivshin if (lenp != (sizeof(__be32) * 2)) { 2602f1eea5c1SDavid Rivshin dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i); 260347276fccSMugunthan V N goto no_phy_slave; 2604549985eeSRichard Cochran } 2605549985eeSRichard Cochran mdio_node = of_find_node_by_phandle(be32_to_cpup(parp)); 2606549985eeSRichard Cochran phyid = be32_to_cpup(parp+1); 2607549985eeSRichard Cochran mdio = of_find_device_by_node(mdio_node); 260860e71ab5SJohan Hovold of_node_put(mdio_node); 26096954cc1fSJohan Hovold if (!mdio) { 261056fdb2e0SMarkus Pargmann dev_err(&pdev->dev, "Missing mdio platform device\n"); 26116954cc1fSJohan Hovold return -EINVAL; 26126954cc1fSJohan Hovold } 2613549985eeSRichard Cochran snprintf(slave_data->phy_id, sizeof(slave_data->phy_id), 2614549985eeSRichard Cochran PHY_ID_FMT, mdio->name, phyid); 261586e1d5adSJohan Hovold put_device(&mdio->dev); 2616f1eea5c1SDavid Rivshin } else { 2617ae092b5bSDavid Rivshin dev_err(&pdev->dev, 2618ae092b5bSDavid Rivshin "No slave[%d] phy_id, phy-handle, or fixed-link property\n", 2619ae092b5bSDavid Rivshin i); 2620f1eea5c1SDavid Rivshin goto no_phy_slave; 2621f1eea5c1SDavid Rivshin } 262247276fccSMugunthan V N slave_data->phy_if = of_get_phy_mode(slave_node); 262347276fccSMugunthan V N if (slave_data->phy_if < 0) { 262447276fccSMugunthan V N dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n", 262547276fccSMugunthan V N i); 262647276fccSMugunthan V N return slave_data->phy_if; 262747276fccSMugunthan V N } 262847276fccSMugunthan V N 262947276fccSMugunthan V N no_phy_slave: 2630549985eeSRichard Cochran mac_addr = of_get_mac_address(slave_node); 26310ba517b1SMarkus Pargmann if (mac_addr) { 2632549985eeSRichard Cochran memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN); 26330ba517b1SMarkus Pargmann } else { 2634b6745f6eSMugunthan V N ret = ti_cm_get_macid(&pdev->dev, i, 26350ba517b1SMarkus Pargmann slave_data->mac_addr); 26360ba517b1SMarkus Pargmann if (ret) 26370ba517b1SMarkus Pargmann return ret; 26380ba517b1SMarkus Pargmann } 2639d9ba8f9eSMugunthan V N if (data->dual_emac) { 264091c4166cSMugunthan V N if (of_property_read_u32(slave_node, "dual_emac_res_vlan", 2641d9ba8f9eSMugunthan V N &prop)) { 264288c99ff6SGeorge Cherian dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n"); 2643d9ba8f9eSMugunthan V N slave_data->dual_emac_res_vlan = i+1; 264488c99ff6SGeorge Cherian dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n", 2645d9ba8f9eSMugunthan V N slave_data->dual_emac_res_vlan, i); 2646d9ba8f9eSMugunthan V N } else { 2647d9ba8f9eSMugunthan V N slave_data->dual_emac_res_vlan = prop; 2648d9ba8f9eSMugunthan V N } 2649d9ba8f9eSMugunthan V N } 2650d9ba8f9eSMugunthan V N 2651549985eeSRichard Cochran i++; 26523a27bfacSMugunthan V N if (i == data->slaves) 26533a27bfacSMugunthan V N break; 2654549985eeSRichard Cochran } 2655549985eeSRichard Cochran 26562eb32b0aSMugunthan V N return 0; 26572eb32b0aSMugunthan V N } 26582eb32b0aSMugunthan V N 2659a4e32b0dSJohan Hovold static void cpsw_remove_dt(struct platform_device *pdev) 2660a4e32b0dSJohan Hovold { 26618cbcc466SJohan Hovold struct net_device *ndev = platform_get_drvdata(pdev); 26628cbcc466SJohan Hovold struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 26638cbcc466SJohan Hovold struct cpsw_platform_data *data = &cpsw->data; 26648cbcc466SJohan Hovold struct device_node *node = pdev->dev.of_node; 26658cbcc466SJohan Hovold struct device_node *slave_node; 26668cbcc466SJohan Hovold int i = 0; 26678cbcc466SJohan Hovold 26688cbcc466SJohan Hovold for_each_available_child_of_node(node, slave_node) { 26698cbcc466SJohan Hovold struct cpsw_slave_data *slave_data = &data->slave_data[i]; 26708cbcc466SJohan Hovold 26718cbcc466SJohan Hovold if (strcmp(slave_node->name, "slave")) 26728cbcc466SJohan Hovold continue; 26738cbcc466SJohan Hovold 26748cbcc466SJohan Hovold if (of_phy_is_fixed_link(slave_node)) { 26758cbcc466SJohan Hovold struct phy_device *phydev; 26768cbcc466SJohan Hovold 26778cbcc466SJohan Hovold phydev = of_phy_find_device(slave_node); 26788cbcc466SJohan Hovold if (phydev) { 26798cbcc466SJohan Hovold fixed_phy_unregister(phydev); 26808cbcc466SJohan Hovold /* Put references taken by 26818cbcc466SJohan Hovold * of_phy_find_device() and 26828cbcc466SJohan Hovold * of_phy_register_fixed_link(). 26838cbcc466SJohan Hovold */ 26848cbcc466SJohan Hovold phy_device_free(phydev); 26858cbcc466SJohan Hovold phy_device_free(phydev); 26868cbcc466SJohan Hovold } 26878cbcc466SJohan Hovold } 26888cbcc466SJohan Hovold 26898cbcc466SJohan Hovold of_node_put(slave_data->phy_node); 26908cbcc466SJohan Hovold 26918cbcc466SJohan Hovold i++; 26928cbcc466SJohan Hovold if (i == data->slaves) 26938cbcc466SJohan Hovold break; 26948cbcc466SJohan Hovold } 26958cbcc466SJohan Hovold 2696a4e32b0dSJohan Hovold of_platform_depopulate(&pdev->dev); 2697a4e32b0dSJohan Hovold } 2698a4e32b0dSJohan Hovold 269956e31bd8SIvan Khoronzhuk static int cpsw_probe_dual_emac(struct cpsw_priv *priv) 2700d9ba8f9eSMugunthan V N { 2701606f3993SIvan Khoronzhuk struct cpsw_common *cpsw = priv->cpsw; 2702606f3993SIvan Khoronzhuk struct cpsw_platform_data *data = &cpsw->data; 2703d9ba8f9eSMugunthan V N struct net_device *ndev; 2704d9ba8f9eSMugunthan V N struct cpsw_priv *priv_sl2; 2705e38b5a3dSIvan Khoronzhuk int ret = 0; 2706d9ba8f9eSMugunthan V N 2707e05107e6SIvan Khoronzhuk ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES); 2708d9ba8f9eSMugunthan V N if (!ndev) { 270956e31bd8SIvan Khoronzhuk dev_err(cpsw->dev, "cpsw: error allocating net_device\n"); 2710d9ba8f9eSMugunthan V N return -ENOMEM; 2711d9ba8f9eSMugunthan V N } 2712d9ba8f9eSMugunthan V N 2713d9ba8f9eSMugunthan V N priv_sl2 = netdev_priv(ndev); 2714606f3993SIvan Khoronzhuk priv_sl2->cpsw = cpsw; 2715d9ba8f9eSMugunthan V N priv_sl2->ndev = ndev; 2716d9ba8f9eSMugunthan V N priv_sl2->dev = &ndev->dev; 2717d9ba8f9eSMugunthan V N priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG); 2718d9ba8f9eSMugunthan V N 2719d9ba8f9eSMugunthan V N if (is_valid_ether_addr(data->slave_data[1].mac_addr)) { 2720d9ba8f9eSMugunthan V N memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr, 2721d9ba8f9eSMugunthan V N ETH_ALEN); 272256e31bd8SIvan Khoronzhuk dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n", 272356e31bd8SIvan Khoronzhuk priv_sl2->mac_addr); 2724d9ba8f9eSMugunthan V N } else { 2725d9ba8f9eSMugunthan V N random_ether_addr(priv_sl2->mac_addr); 272656e31bd8SIvan Khoronzhuk dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n", 272756e31bd8SIvan Khoronzhuk priv_sl2->mac_addr); 2728d9ba8f9eSMugunthan V N } 2729d9ba8f9eSMugunthan V N memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN); 2730d9ba8f9eSMugunthan V N 2731d9ba8f9eSMugunthan V N priv_sl2->emac_port = 1; 2732606f3993SIvan Khoronzhuk cpsw->slaves[1].ndev = ndev; 2733f646968fSPatrick McHardy ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 2734d9ba8f9eSMugunthan V N 2735d9ba8f9eSMugunthan V N ndev->netdev_ops = &cpsw_netdev_ops; 27367ad24ea4SWilfried Klaebe ndev->ethtool_ops = &cpsw_ethtool_ops; 2737d9ba8f9eSMugunthan V N 2738d9ba8f9eSMugunthan V N /* register the network device */ 273956e31bd8SIvan Khoronzhuk SET_NETDEV_DEV(ndev, cpsw->dev); 2740d9ba8f9eSMugunthan V N ret = register_netdev(ndev); 2741d9ba8f9eSMugunthan V N if (ret) { 274256e31bd8SIvan Khoronzhuk dev_err(cpsw->dev, "cpsw: error registering net device\n"); 2743d9ba8f9eSMugunthan V N free_netdev(ndev); 2744d9ba8f9eSMugunthan V N ret = -ENODEV; 2745d9ba8f9eSMugunthan V N } 2746d9ba8f9eSMugunthan V N 2747d9ba8f9eSMugunthan V N return ret; 2748d9ba8f9eSMugunthan V N } 2749d9ba8f9eSMugunthan V N 27507da11600SMugunthan V N #define CPSW_QUIRK_IRQ BIT(0) 27517da11600SMugunthan V N 27527da11600SMugunthan V N static struct platform_device_id cpsw_devtype[] = { 27537da11600SMugunthan V N { 27547da11600SMugunthan V N /* keep it for existing comaptibles */ 27557da11600SMugunthan V N .name = "cpsw", 27567da11600SMugunthan V N .driver_data = CPSW_QUIRK_IRQ, 27577da11600SMugunthan V N }, { 27587da11600SMugunthan V N .name = "am335x-cpsw", 27597da11600SMugunthan V N .driver_data = CPSW_QUIRK_IRQ, 27607da11600SMugunthan V N }, { 27617da11600SMugunthan V N .name = "am4372-cpsw", 27627da11600SMugunthan V N .driver_data = 0, 27637da11600SMugunthan V N }, { 27647da11600SMugunthan V N .name = "dra7-cpsw", 27657da11600SMugunthan V N .driver_data = 0, 27667da11600SMugunthan V N }, { 27677da11600SMugunthan V N /* sentinel */ 27687da11600SMugunthan V N } 27697da11600SMugunthan V N }; 27707da11600SMugunthan V N MODULE_DEVICE_TABLE(platform, cpsw_devtype); 27717da11600SMugunthan V N 27727da11600SMugunthan V N enum ti_cpsw_type { 27737da11600SMugunthan V N CPSW = 0, 27747da11600SMugunthan V N AM335X_CPSW, 27757da11600SMugunthan V N AM4372_CPSW, 27767da11600SMugunthan V N DRA7_CPSW, 27777da11600SMugunthan V N }; 27787da11600SMugunthan V N 27797da11600SMugunthan V N static const struct of_device_id cpsw_of_mtable[] = { 27807da11600SMugunthan V N { .compatible = "ti,cpsw", .data = &cpsw_devtype[CPSW], }, 27817da11600SMugunthan V N { .compatible = "ti,am335x-cpsw", .data = &cpsw_devtype[AM335X_CPSW], }, 27827da11600SMugunthan V N { .compatible = "ti,am4372-cpsw", .data = &cpsw_devtype[AM4372_CPSW], }, 27837da11600SMugunthan V N { .compatible = "ti,dra7-cpsw", .data = &cpsw_devtype[DRA7_CPSW], }, 27847da11600SMugunthan V N { /* sentinel */ }, 27857da11600SMugunthan V N }; 27867da11600SMugunthan V N MODULE_DEVICE_TABLE(of, cpsw_of_mtable); 27877da11600SMugunthan V N 2788663e12e6SBill Pemberton static int cpsw_probe(struct platform_device *pdev) 2789df828598SMugunthan V N { 2790ef4183a1SIvan Khoronzhuk struct clk *clk; 2791d1bd9acfSSebastian Siewior struct cpsw_platform_data *data; 2792df828598SMugunthan V N struct net_device *ndev; 2793df828598SMugunthan V N struct cpsw_priv *priv; 2794df828598SMugunthan V N struct cpdma_params dma_params; 2795df828598SMugunthan V N struct cpsw_ale_params ale_params; 2796aa1a15e2SDaniel Mack void __iomem *ss_regs; 2797aa1a15e2SDaniel Mack struct resource *res, *ss_res; 27987da11600SMugunthan V N const struct of_device_id *of_id; 27991d147ccbSMugunthan V N struct gpio_descs *mode; 2800549985eeSRichard Cochran u32 slave_offset, sliver_offset, slave_size; 2801649a1688SIvan Khoronzhuk struct cpsw_common *cpsw; 28025087b915SFelipe Balbi int ret = 0, i; 28035087b915SFelipe Balbi int irq; 2804df828598SMugunthan V N 2805649a1688SIvan Khoronzhuk cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL); 28063420ea88SJohan Hovold if (!cpsw) 28073420ea88SJohan Hovold return -ENOMEM; 28083420ea88SJohan Hovold 280956e31bd8SIvan Khoronzhuk cpsw->dev = &pdev->dev; 2810649a1688SIvan Khoronzhuk 2811e05107e6SIvan Khoronzhuk ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES); 2812df828598SMugunthan V N if (!ndev) { 281388c99ff6SGeorge Cherian dev_err(&pdev->dev, "error allocating net_device\n"); 2814df828598SMugunthan V N return -ENOMEM; 2815df828598SMugunthan V N } 2816df828598SMugunthan V N 2817df828598SMugunthan V N platform_set_drvdata(pdev, ndev); 2818df828598SMugunthan V N priv = netdev_priv(ndev); 2819649a1688SIvan Khoronzhuk priv->cpsw = cpsw; 2820df828598SMugunthan V N priv->ndev = ndev; 2821df828598SMugunthan V N priv->dev = &ndev->dev; 2822df828598SMugunthan V N priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG); 28232a05a622SIvan Khoronzhuk cpsw->rx_packet_max = max(rx_packet_max, 128); 28242a05a622SIvan Khoronzhuk cpsw->cpts = devm_kzalloc(&pdev->dev, sizeof(struct cpts), GFP_KERNEL); 28252a05a622SIvan Khoronzhuk if (!cpsw->cpts) { 282688c99ff6SGeorge Cherian dev_err(&pdev->dev, "error allocating cpts\n"); 28274d507dffSMarkus Pargmann ret = -ENOMEM; 28289232b16dSMugunthan V N goto clean_ndev_ret; 28299232b16dSMugunthan V N } 2830df828598SMugunthan V N 28311d147ccbSMugunthan V N mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW); 28321d147ccbSMugunthan V N if (IS_ERR(mode)) { 28331d147ccbSMugunthan V N ret = PTR_ERR(mode); 28341d147ccbSMugunthan V N dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret); 28351d147ccbSMugunthan V N goto clean_ndev_ret; 28361d147ccbSMugunthan V N } 28371d147ccbSMugunthan V N 28381fb19aa7SVaibhav Hiremath /* 28391fb19aa7SVaibhav Hiremath * This may be required here for child devices. 28401fb19aa7SVaibhav Hiremath */ 28411fb19aa7SVaibhav Hiremath pm_runtime_enable(&pdev->dev); 28421fb19aa7SVaibhav Hiremath 2843739683b4SMugunthan V N /* Select default pin state */ 2844739683b4SMugunthan V N pinctrl_pm_select_default_state(&pdev->dev); 2845739683b4SMugunthan V N 2846a4e32b0dSJohan Hovold /* Need to enable clocks with runtime PM api to access module 2847a4e32b0dSJohan Hovold * registers 2848a4e32b0dSJohan Hovold */ 2849a4e32b0dSJohan Hovold ret = pm_runtime_get_sync(&pdev->dev); 2850a4e32b0dSJohan Hovold if (ret < 0) { 2851a4e32b0dSJohan Hovold pm_runtime_put_noidle(&pdev->dev); 2852aa1a15e2SDaniel Mack goto clean_runtime_disable_ret; 28532eb32b0aSMugunthan V N } 2854a4e32b0dSJohan Hovold 285523a09873SJohan Hovold ret = cpsw_probe_dt(&cpsw->data, pdev); 285623a09873SJohan Hovold if (ret) 2857a4e32b0dSJohan Hovold goto clean_dt_ret; 285823a09873SJohan Hovold 2859606f3993SIvan Khoronzhuk data = &cpsw->data; 2860e05107e6SIvan Khoronzhuk cpsw->rx_ch_num = 1; 2861e05107e6SIvan Khoronzhuk cpsw->tx_ch_num = 1; 28622eb32b0aSMugunthan V N 2863df828598SMugunthan V N if (is_valid_ether_addr(data->slave_data[0].mac_addr)) { 2864df828598SMugunthan V N memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN); 286588c99ff6SGeorge Cherian dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr); 2866df828598SMugunthan V N } else { 28677efd26d0SJoe Perches eth_random_addr(priv->mac_addr); 286888c99ff6SGeorge Cherian dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr); 2869df828598SMugunthan V N } 2870df828598SMugunthan V N 2871df828598SMugunthan V N memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN); 2872df828598SMugunthan V N 2873606f3993SIvan Khoronzhuk cpsw->slaves = devm_kzalloc(&pdev->dev, 2874aa1a15e2SDaniel Mack sizeof(struct cpsw_slave) * data->slaves, 2875df828598SMugunthan V N GFP_KERNEL); 2876606f3993SIvan Khoronzhuk if (!cpsw->slaves) { 2877aa1a15e2SDaniel Mack ret = -ENOMEM; 2878a4e32b0dSJohan Hovold goto clean_dt_ret; 2879df828598SMugunthan V N } 2880df828598SMugunthan V N for (i = 0; i < data->slaves; i++) 2881606f3993SIvan Khoronzhuk cpsw->slaves[i].slave_num = i; 2882df828598SMugunthan V N 2883606f3993SIvan Khoronzhuk cpsw->slaves[0].ndev = ndev; 2884d9ba8f9eSMugunthan V N priv->emac_port = 0; 2885d9ba8f9eSMugunthan V N 2886ef4183a1SIvan Khoronzhuk clk = devm_clk_get(&pdev->dev, "fck"); 2887ef4183a1SIvan Khoronzhuk if (IS_ERR(clk)) { 2888aa1a15e2SDaniel Mack dev_err(priv->dev, "fck is not found\n"); 2889f150bd7fSMugunthan V N ret = -ENODEV; 2890a4e32b0dSJohan Hovold goto clean_dt_ret; 2891df828598SMugunthan V N } 28922a05a622SIvan Khoronzhuk cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000; 2893df828598SMugunthan V N 2894aa1a15e2SDaniel Mack ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2895aa1a15e2SDaniel Mack ss_regs = devm_ioremap_resource(&pdev->dev, ss_res); 2896aa1a15e2SDaniel Mack if (IS_ERR(ss_regs)) { 2897aa1a15e2SDaniel Mack ret = PTR_ERR(ss_regs); 2898a4e32b0dSJohan Hovold goto clean_dt_ret; 2899df828598SMugunthan V N } 29005d8d0d4dSIvan Khoronzhuk cpsw->regs = ss_regs; 2901df828598SMugunthan V N 29022a05a622SIvan Khoronzhuk cpsw->version = readl(&cpsw->regs->id_ver); 2903f280e89aSMugunthan V N 2904aa1a15e2SDaniel Mack res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 29055d8d0d4dSIvan Khoronzhuk cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res); 29065d8d0d4dSIvan Khoronzhuk if (IS_ERR(cpsw->wr_regs)) { 29075d8d0d4dSIvan Khoronzhuk ret = PTR_ERR(cpsw->wr_regs); 2908a4e32b0dSJohan Hovold goto clean_dt_ret; 2909df828598SMugunthan V N } 2910df828598SMugunthan V N 2911df828598SMugunthan V N memset(&dma_params, 0, sizeof(dma_params)); 2912549985eeSRichard Cochran memset(&ale_params, 0, sizeof(ale_params)); 2913549985eeSRichard Cochran 29142a05a622SIvan Khoronzhuk switch (cpsw->version) { 2915549985eeSRichard Cochran case CPSW_VERSION_1: 29165d8d0d4dSIvan Khoronzhuk cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET; 29172a05a622SIvan Khoronzhuk cpsw->cpts->reg = ss_regs + CPSW1_CPTS_OFFSET; 29185d8d0d4dSIvan Khoronzhuk cpsw->hw_stats = ss_regs + CPSW1_HW_STATS; 2919549985eeSRichard Cochran dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET; 2920549985eeSRichard Cochran dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET; 2921549985eeSRichard Cochran ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET; 2922549985eeSRichard Cochran slave_offset = CPSW1_SLAVE_OFFSET; 2923549985eeSRichard Cochran slave_size = CPSW1_SLAVE_SIZE; 2924549985eeSRichard Cochran sliver_offset = CPSW1_SLIVER_OFFSET; 2925549985eeSRichard Cochran dma_params.desc_mem_phys = 0; 2926549985eeSRichard Cochran break; 2927549985eeSRichard Cochran case CPSW_VERSION_2: 2928c193f365SMugunthan V N case CPSW_VERSION_3: 2929926489beSMugunthan V N case CPSW_VERSION_4: 29305d8d0d4dSIvan Khoronzhuk cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET; 29312a05a622SIvan Khoronzhuk cpsw->cpts->reg = ss_regs + CPSW2_CPTS_OFFSET; 29325d8d0d4dSIvan Khoronzhuk cpsw->hw_stats = ss_regs + CPSW2_HW_STATS; 2933549985eeSRichard Cochran dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET; 2934549985eeSRichard Cochran dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET; 2935549985eeSRichard Cochran ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET; 2936549985eeSRichard Cochran slave_offset = CPSW2_SLAVE_OFFSET; 2937549985eeSRichard Cochran slave_size = CPSW2_SLAVE_SIZE; 2938549985eeSRichard Cochran sliver_offset = CPSW2_SLIVER_OFFSET; 2939549985eeSRichard Cochran dma_params.desc_mem_phys = 2940aa1a15e2SDaniel Mack (u32 __force) ss_res->start + CPSW2_BD_OFFSET; 2941549985eeSRichard Cochran break; 2942549985eeSRichard Cochran default: 29432a05a622SIvan Khoronzhuk dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version); 2944549985eeSRichard Cochran ret = -ENODEV; 2945a4e32b0dSJohan Hovold goto clean_dt_ret; 2946549985eeSRichard Cochran } 2947606f3993SIvan Khoronzhuk for (i = 0; i < cpsw->data.slaves; i++) { 2948606f3993SIvan Khoronzhuk struct cpsw_slave *slave = &cpsw->slaves[i]; 2949606f3993SIvan Khoronzhuk 2950606f3993SIvan Khoronzhuk cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset); 2951549985eeSRichard Cochran slave_offset += slave_size; 2952549985eeSRichard Cochran sliver_offset += SLIVER_SIZE; 2953549985eeSRichard Cochran } 2954549985eeSRichard Cochran 2955df828598SMugunthan V N dma_params.dev = &pdev->dev; 2956549985eeSRichard Cochran dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH; 2957549985eeSRichard Cochran dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE; 2958549985eeSRichard Cochran dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP; 2959549985eeSRichard Cochran dma_params.txcp = dma_params.txhdp + CPDMA_TXCP; 2960549985eeSRichard Cochran dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP; 2961df828598SMugunthan V N 2962df828598SMugunthan V N dma_params.num_chan = data->channels; 2963df828598SMugunthan V N dma_params.has_soft_reset = true; 2964df828598SMugunthan V N dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE; 2965df828598SMugunthan V N dma_params.desc_mem_size = data->bd_ram_size; 2966df828598SMugunthan V N dma_params.desc_align = 16; 2967df828598SMugunthan V N dma_params.has_ext_regs = true; 2968549985eeSRichard Cochran dma_params.desc_hw_addr = dma_params.desc_mem_phys; 296983fcad0cSIvan Khoronzhuk dma_params.bus_freq_mhz = cpsw->bus_freq_mhz; 2970df828598SMugunthan V N 29712c836bd9SIvan Khoronzhuk cpsw->dma = cpdma_ctlr_create(&dma_params); 29722c836bd9SIvan Khoronzhuk if (!cpsw->dma) { 2973df828598SMugunthan V N dev_err(priv->dev, "error initializing dma\n"); 2974df828598SMugunthan V N ret = -ENOMEM; 2975a4e32b0dSJohan Hovold goto clean_dt_ret; 2976df828598SMugunthan V N } 2977df828598SMugunthan V N 2978*8feb0a19SIvan Khoronzhuk cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_tx_handler, 0); 2979*8feb0a19SIvan Khoronzhuk cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1); 2980*8feb0a19SIvan Khoronzhuk if (WARN_ON(!cpsw->rxv[0].ch || !cpsw->txv[0].ch)) { 2981df828598SMugunthan V N dev_err(priv->dev, "error initializing dma channels\n"); 2982df828598SMugunthan V N ret = -ENOMEM; 2983df828598SMugunthan V N goto clean_dma_ret; 2984df828598SMugunthan V N } 2985df828598SMugunthan V N 2986df828598SMugunthan V N ale_params.dev = &ndev->dev; 2987df828598SMugunthan V N ale_params.ale_ageout = ale_ageout; 2988df828598SMugunthan V N ale_params.ale_entries = data->ale_entries; 2989df828598SMugunthan V N ale_params.ale_ports = data->slaves; 2990df828598SMugunthan V N 29912a05a622SIvan Khoronzhuk cpsw->ale = cpsw_ale_create(&ale_params); 29922a05a622SIvan Khoronzhuk if (!cpsw->ale) { 2993df828598SMugunthan V N dev_err(priv->dev, "error initializing ale engine\n"); 2994df828598SMugunthan V N ret = -ENODEV; 2995df828598SMugunthan V N goto clean_dma_ret; 2996df828598SMugunthan V N } 2997df828598SMugunthan V N 2998c03abd84SFelipe Balbi ndev->irq = platform_get_irq(pdev, 1); 2999df828598SMugunthan V N if (ndev->irq < 0) { 3000df828598SMugunthan V N dev_err(priv->dev, "error getting irq resource\n"); 3001c1e3334fSJulia Lawall ret = ndev->irq; 3002df828598SMugunthan V N goto clean_ale_ret; 3003df828598SMugunthan V N } 3004df828598SMugunthan V N 30057da11600SMugunthan V N of_id = of_match_device(cpsw_of_mtable, &pdev->dev); 30067da11600SMugunthan V N if (of_id) { 30077da11600SMugunthan V N pdev->id_entry = of_id->data; 30087da11600SMugunthan V N if (pdev->id_entry->driver_data) 3009e38b5a3dSIvan Khoronzhuk cpsw->quirk_irq = true; 30107da11600SMugunthan V N } 30117da11600SMugunthan V N 3012c03abd84SFelipe Balbi /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and 3013c03abd84SFelipe Balbi * MISC IRQs which are always kept disabled with this driver so 3014c03abd84SFelipe Balbi * we will not request them. 3015c03abd84SFelipe Balbi * 3016c03abd84SFelipe Balbi * If anyone wants to implement support for those, make sure to 3017c03abd84SFelipe Balbi * first request and append them to irqs_table array. 3018c03abd84SFelipe Balbi */ 3019c2b32e58SDaniel Mack 3020c03abd84SFelipe Balbi /* RX IRQ */ 30215087b915SFelipe Balbi irq = platform_get_irq(pdev, 1); 3022c1e3334fSJulia Lawall if (irq < 0) { 3023c1e3334fSJulia Lawall ret = irq; 30245087b915SFelipe Balbi goto clean_ale_ret; 3025c1e3334fSJulia Lawall } 30265087b915SFelipe Balbi 3027e38b5a3dSIvan Khoronzhuk cpsw->irqs_table[0] = irq; 3028c03abd84SFelipe Balbi ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt, 3029dbc4ec52SIvan Khoronzhuk 0, dev_name(&pdev->dev), cpsw); 30305087b915SFelipe Balbi if (ret < 0) { 30315087b915SFelipe Balbi dev_err(priv->dev, "error attaching irq (%d)\n", ret); 30325087b915SFelipe Balbi goto clean_ale_ret; 3033df828598SMugunthan V N } 3034df828598SMugunthan V N 3035c03abd84SFelipe Balbi /* TX IRQ */ 30365087b915SFelipe Balbi irq = platform_get_irq(pdev, 2); 3037c1e3334fSJulia Lawall if (irq < 0) { 3038c1e3334fSJulia Lawall ret = irq; 30395087b915SFelipe Balbi goto clean_ale_ret; 3040c1e3334fSJulia Lawall } 30415087b915SFelipe Balbi 3042e38b5a3dSIvan Khoronzhuk cpsw->irqs_table[1] = irq; 3043c03abd84SFelipe Balbi ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt, 3044dbc4ec52SIvan Khoronzhuk 0, dev_name(&pdev->dev), cpsw); 30455087b915SFelipe Balbi if (ret < 0) { 30465087b915SFelipe Balbi dev_err(priv->dev, "error attaching irq (%d)\n", ret); 30475087b915SFelipe Balbi goto clean_ale_ret; 30485087b915SFelipe Balbi } 3049c2b32e58SDaniel Mack 3050f646968fSPatrick McHardy ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 3051df828598SMugunthan V N 3052df828598SMugunthan V N ndev->netdev_ops = &cpsw_netdev_ops; 30537ad24ea4SWilfried Klaebe ndev->ethtool_ops = &cpsw_ethtool_ops; 3054dbc4ec52SIvan Khoronzhuk netif_napi_add(ndev, &cpsw->napi_rx, cpsw_rx_poll, CPSW_POLL_WEIGHT); 3055dbc4ec52SIvan Khoronzhuk netif_tx_napi_add(ndev, &cpsw->napi_tx, cpsw_tx_poll, CPSW_POLL_WEIGHT); 3056df828598SMugunthan V N 3057df828598SMugunthan V N /* register the network device */ 3058df828598SMugunthan V N SET_NETDEV_DEV(ndev, &pdev->dev); 3059df828598SMugunthan V N ret = register_netdev(ndev); 3060df828598SMugunthan V N if (ret) { 3061df828598SMugunthan V N dev_err(priv->dev, "error registering net device\n"); 3062df828598SMugunthan V N ret = -ENODEV; 3063aa1a15e2SDaniel Mack goto clean_ale_ret; 3064df828598SMugunthan V N } 3065df828598SMugunthan V N 30661a3b5056SOlof Johansson cpsw_notice(priv, probe, "initialized device (regs %pa, irq %d)\n", 30671a3b5056SOlof Johansson &ss_res->start, ndev->irq); 3068df828598SMugunthan V N 3069606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) { 307056e31bd8SIvan Khoronzhuk ret = cpsw_probe_dual_emac(priv); 3071d9ba8f9eSMugunthan V N if (ret) { 3072d9ba8f9eSMugunthan V N cpsw_err(priv, probe, "error probe slave 2 emac interface\n"); 3073a7fe9d46SJohan Hovold goto clean_unregister_netdev_ret; 3074d9ba8f9eSMugunthan V N } 3075d9ba8f9eSMugunthan V N } 3076d9ba8f9eSMugunthan V N 3077c46ab7e0SJohan Hovold pm_runtime_put(&pdev->dev); 3078c46ab7e0SJohan Hovold 3079df828598SMugunthan V N return 0; 3080df828598SMugunthan V N 3081a7fe9d46SJohan Hovold clean_unregister_netdev_ret: 3082a7fe9d46SJohan Hovold unregister_netdev(ndev); 3083df828598SMugunthan V N clean_ale_ret: 30842a05a622SIvan Khoronzhuk cpsw_ale_destroy(cpsw->ale); 3085df828598SMugunthan V N clean_dma_ret: 30862c836bd9SIvan Khoronzhuk cpdma_ctlr_destroy(cpsw->dma); 3087a4e32b0dSJohan Hovold clean_dt_ret: 3088a4e32b0dSJohan Hovold cpsw_remove_dt(pdev); 3089c46ab7e0SJohan Hovold pm_runtime_put_sync(&pdev->dev); 3090aa1a15e2SDaniel Mack clean_runtime_disable_ret: 3091f150bd7fSMugunthan V N pm_runtime_disable(&pdev->dev); 3092df828598SMugunthan V N clean_ndev_ret: 3093d1bd9acfSSebastian Siewior free_netdev(priv->ndev); 3094df828598SMugunthan V N return ret; 3095df828598SMugunthan V N } 3096df828598SMugunthan V N 3097663e12e6SBill Pemberton static int cpsw_remove(struct platform_device *pdev) 3098df828598SMugunthan V N { 3099df828598SMugunthan V N struct net_device *ndev = platform_get_drvdata(pdev); 31002a05a622SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 31018a0b6dc9SGrygorii Strashko int ret; 31028a0b6dc9SGrygorii Strashko 31038a0b6dc9SGrygorii Strashko ret = pm_runtime_get_sync(&pdev->dev); 31048a0b6dc9SGrygorii Strashko if (ret < 0) { 31058a0b6dc9SGrygorii Strashko pm_runtime_put_noidle(&pdev->dev); 31068a0b6dc9SGrygorii Strashko return ret; 31078a0b6dc9SGrygorii Strashko } 3108df828598SMugunthan V N 3109606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) 3110606f3993SIvan Khoronzhuk unregister_netdev(cpsw->slaves[1].ndev); 3111d1bd9acfSSebastian Siewior unregister_netdev(ndev); 3112df828598SMugunthan V N 31132a05a622SIvan Khoronzhuk cpsw_ale_destroy(cpsw->ale); 31142c836bd9SIvan Khoronzhuk cpdma_ctlr_destroy(cpsw->dma); 3115a4e32b0dSJohan Hovold cpsw_remove_dt(pdev); 31168a0b6dc9SGrygorii Strashko pm_runtime_put_sync(&pdev->dev); 31178a0b6dc9SGrygorii Strashko pm_runtime_disable(&pdev->dev); 3118606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) 3119606f3993SIvan Khoronzhuk free_netdev(cpsw->slaves[1].ndev); 3120df828598SMugunthan V N free_netdev(ndev); 3121df828598SMugunthan V N return 0; 3122df828598SMugunthan V N } 3123df828598SMugunthan V N 31248963a504SGrygorii Strashko #ifdef CONFIG_PM_SLEEP 3125df828598SMugunthan V N static int cpsw_suspend(struct device *dev) 3126df828598SMugunthan V N { 3127df828598SMugunthan V N struct platform_device *pdev = to_platform_device(dev); 3128df828598SMugunthan V N struct net_device *ndev = platform_get_drvdata(pdev); 3129606f3993SIvan Khoronzhuk struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 3130df828598SMugunthan V N 3131606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) { 3132618073e3SMugunthan V N int i; 3133618073e3SMugunthan V N 3134606f3993SIvan Khoronzhuk for (i = 0; i < cpsw->data.slaves; i++) { 3135606f3993SIvan Khoronzhuk if (netif_running(cpsw->slaves[i].ndev)) 3136606f3993SIvan Khoronzhuk cpsw_ndo_stop(cpsw->slaves[i].ndev); 3137618073e3SMugunthan V N } 3138618073e3SMugunthan V N } else { 3139df828598SMugunthan V N if (netif_running(ndev)) 3140df828598SMugunthan V N cpsw_ndo_stop(ndev); 3141618073e3SMugunthan V N } 31421e7a2e21SDaniel Mack 3143739683b4SMugunthan V N /* Select sleep pin state */ 314456e31bd8SIvan Khoronzhuk pinctrl_pm_select_sleep_state(dev); 3145739683b4SMugunthan V N 3146df828598SMugunthan V N return 0; 3147df828598SMugunthan V N } 3148df828598SMugunthan V N 3149df828598SMugunthan V N static int cpsw_resume(struct device *dev) 3150df828598SMugunthan V N { 3151df828598SMugunthan V N struct platform_device *pdev = to_platform_device(dev); 3152df828598SMugunthan V N struct net_device *ndev = platform_get_drvdata(pdev); 3153606f3993SIvan Khoronzhuk struct cpsw_common *cpsw = netdev_priv(ndev); 3154df828598SMugunthan V N 3155739683b4SMugunthan V N /* Select default pin state */ 315656e31bd8SIvan Khoronzhuk pinctrl_pm_select_default_state(dev); 3157739683b4SMugunthan V N 3158606f3993SIvan Khoronzhuk if (cpsw->data.dual_emac) { 3159618073e3SMugunthan V N int i; 3160618073e3SMugunthan V N 3161606f3993SIvan Khoronzhuk for (i = 0; i < cpsw->data.slaves; i++) { 3162606f3993SIvan Khoronzhuk if (netif_running(cpsw->slaves[i].ndev)) 3163606f3993SIvan Khoronzhuk cpsw_ndo_open(cpsw->slaves[i].ndev); 3164618073e3SMugunthan V N } 3165618073e3SMugunthan V N } else { 3166df828598SMugunthan V N if (netif_running(ndev)) 3167df828598SMugunthan V N cpsw_ndo_open(ndev); 3168618073e3SMugunthan V N } 3169df828598SMugunthan V N return 0; 3170df828598SMugunthan V N } 31718963a504SGrygorii Strashko #endif 3172df828598SMugunthan V N 31738963a504SGrygorii Strashko static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume); 3174df828598SMugunthan V N 3175df828598SMugunthan V N static struct platform_driver cpsw_driver = { 3176df828598SMugunthan V N .driver = { 3177df828598SMugunthan V N .name = "cpsw", 3178df828598SMugunthan V N .pm = &cpsw_pm_ops, 31791e5c76d4SSachin Kamat .of_match_table = cpsw_of_mtable, 3180df828598SMugunthan V N }, 3181df828598SMugunthan V N .probe = cpsw_probe, 3182663e12e6SBill Pemberton .remove = cpsw_remove, 3183df828598SMugunthan V N }; 3184df828598SMugunthan V N 31856fb3b6b5SGrygorii Strashko module_platform_driver(cpsw_driver); 3186df828598SMugunthan V N 3187df828598SMugunthan V N MODULE_LICENSE("GPL"); 3188df828598SMugunthan V N MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>"); 3189df828598SMugunthan V N MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>"); 3190df828598SMugunthan V N MODULE_DESCRIPTION("TI CPSW Ethernet driver"); 3191