1 /******************************************************************************* 2 STMMAC Common Header File 3 4 Copyright (C) 2007-2009 STMicroelectronics Ltd 5 6 This program is free software; you can redistribute it and/or modify it 7 under the terms and conditions of the GNU General Public License, 8 version 2, as published by the Free Software Foundation. 9 10 This program is distributed in the hope it will be useful, but WITHOUT 11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 more details. 14 15 You should have received a copy of the GNU General Public License along with 16 this program; if not, write to the Free Software Foundation, Inc., 17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 18 19 The full GNU General Public License is included in this distribution in 20 the file called "COPYING". 21 22 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com> 23 *******************************************************************************/ 24 25 #include <linux/etherdevice.h> 26 #include <linux/netdevice.h> 27 #include <linux/phy.h> 28 #include <linux/module.h> 29 #include <linux/init.h> 30 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE) 31 #define STMMAC_VLAN_TAG_USED 32 #include <linux/if_vlan.h> 33 #endif 34 35 #include "descs.h" 36 #include "mmc.h" 37 38 #undef CHIP_DEBUG_PRINT 39 /* Turn-on extra printk debug for MAC core, dma and descriptors */ 40 /* #define CHIP_DEBUG_PRINT */ 41 42 #ifdef CHIP_DEBUG_PRINT 43 #define CHIP_DBG(fmt, args...) printk(fmt, ## args) 44 #else 45 #define CHIP_DBG(fmt, args...) do { } while (0) 46 #endif 47 48 #undef FRAME_FILTER_DEBUG 49 /* #define FRAME_FILTER_DEBUG */ 50 51 struct stmmac_extra_stats { 52 /* Transmit errors */ 53 unsigned long tx_underflow ____cacheline_aligned; 54 unsigned long tx_carrier; 55 unsigned long tx_losscarrier; 56 unsigned long vlan_tag; 57 unsigned long tx_deferred; 58 unsigned long tx_vlan; 59 unsigned long tx_jabber; 60 unsigned long tx_frame_flushed; 61 unsigned long tx_payload_error; 62 unsigned long tx_ip_header_error; 63 /* Receive errors */ 64 unsigned long rx_desc; 65 unsigned long sa_filter_fail; 66 unsigned long overflow_error; 67 unsigned long ipc_csum_error; 68 unsigned long rx_collision; 69 unsigned long rx_crc; 70 unsigned long rx_length; 71 unsigned long rx_mii; 72 unsigned long rx_multicast; 73 unsigned long rx_gmac_overflow; 74 unsigned long rx_watchdog; 75 unsigned long da_rx_filter_fail; 76 unsigned long sa_rx_filter_fail; 77 unsigned long rx_missed_cntr; 78 unsigned long rx_overflow_cntr; 79 unsigned long rx_vlan; 80 /* Tx/Rx IRQ errors */ 81 unsigned long tx_undeflow_irq; 82 unsigned long tx_process_stopped_irq; 83 unsigned long tx_jabber_irq; 84 unsigned long rx_overflow_irq; 85 unsigned long rx_buf_unav_irq; 86 unsigned long rx_process_stopped_irq; 87 unsigned long rx_watchdog_irq; 88 unsigned long tx_early_irq; 89 unsigned long fatal_bus_error_irq; 90 /* Extra info */ 91 unsigned long threshold; 92 unsigned long tx_pkt_n; 93 unsigned long rx_pkt_n; 94 unsigned long poll_n; 95 unsigned long sched_timer_n; 96 unsigned long normal_irq_n; 97 }; 98 99 #define HASH_TABLE_SIZE 64 100 #define PAUSE_TIME 0x200 101 102 /* Flow Control defines */ 103 #define FLOW_OFF 0 104 #define FLOW_RX 1 105 #define FLOW_TX 2 106 #define FLOW_AUTO (FLOW_TX | FLOW_RX) 107 108 #define SF_DMA_MODE 1 /* DMA STORE-AND-FORWARD Operation Mode */ 109 110 /* DAM HW feature register fields */ 111 #define DMA_HW_FEAT_MIISEL 0x00000001 /* 10/100 Mbps Support */ 112 #define DMA_HW_FEAT_GMIISEL 0x00000002 /* 1000 Mbps Support */ 113 #define DMA_HW_FEAT_HDSEL 0x00000004 /* Half-Duplex Support */ 114 #define DMA_HW_FEAT_EXTHASHEN 0x00000008 /* Expanded DA Hash Filter */ 115 #define DMA_HW_FEAT_HASHSEL 0x00000010 /* HASH Filter */ 116 #define DMA_HW_FEAT_ADDMACADRSEL 0x00000020 /* Multiple MAC Addr Reg */ 117 #define DMA_HW_FEAT_PCSSEL 0x00000040 /* PCS registers */ 118 #define DMA_HW_FEAT_L3L4FLTREN 0x00000080 /* Layer 3 & Layer 4 Feature */ 119 #define DMA_HW_FEAT_SMASEL 0x00000100 /* SMA(MDIO) Interface */ 120 #define DMA_HW_FEAT_RWKSEL 0x00000200 /* PMT Remote Wakeup */ 121 #define DMA_HW_FEAT_MGKSEL 0x00000400 /* PMT Magic Packet */ 122 #define DMA_HW_FEAT_MMCSEL 0x00000800 /* RMON Module */ 123 #define DMA_HW_FEAT_TSVER1SEL 0x00001000 /* Only IEEE 1588-2002 Timestamp */ 124 #define DMA_HW_FEAT_TSVER2SEL 0x00002000 /* IEEE 1588-2008 Adv Timestamp */ 125 #define DMA_HW_FEAT_EEESEL 0x00004000 /* Energy Efficient Ethernet */ 126 #define DMA_HW_FEAT_AVSEL 0x00008000 /* AV Feature */ 127 #define DMA_HW_FEAT_TXCOESEL 0x00010000 /* Checksum Offload in Tx */ 128 #define DMA_HW_FEAT_RXTYP1COE 0x00020000 /* IP csum Offload(Type 1) in Rx */ 129 #define DMA_HW_FEAT_RXTYP2COE 0x00040000 /* IP csum Offload(Type 2) in Rx */ 130 #define DMA_HW_FEAT_RXFIFOSIZE 0x00080000 /* Rx FIFO > 2048 Bytes */ 131 #define DMA_HW_FEAT_RXCHCNT 0x00300000 /* No. of additional Rx Channels */ 132 #define DMA_HW_FEAT_TXCHCNT 0x00c00000 /* No. of additional Tx Channels */ 133 #define DMA_HW_FEAT_ENHDESSEL 0x01000000 /* Alternate (Enhanced Descriptor) */ 134 #define DMA_HW_FEAT_INTTSEN 0x02000000 /* Timestamping with Internal 135 System Time */ 136 #define DMA_HW_FEAT_FLEXIPPSEN 0x04000000 /* Flexible PPS Output */ 137 #define DMA_HW_FEAT_SAVLANINS 0x08000000 /* Source Addr or VLAN Insertion */ 138 #define DMA_HW_FEAT_ACTPHYIF 0x70000000 /* Active/selected PHY interface */ 139 140 enum rx_frame_status { /* IPC status */ 141 good_frame = 0, 142 discard_frame = 1, 143 csum_none = 2, 144 llc_snap = 4, 145 }; 146 147 enum tx_dma_irq_status { 148 tx_hard_error = 1, 149 tx_hard_error_bump_tc = 2, 150 handle_tx_rx = 3, 151 }; 152 153 /* DMA HW capabilities */ 154 struct dma_features { 155 unsigned int mbps_10_100; 156 unsigned int mbps_1000; 157 unsigned int half_duplex; 158 unsigned int hash_filter; 159 unsigned int multi_addr; 160 unsigned int pcs; 161 unsigned int sma_mdio; 162 unsigned int pmt_remote_wake_up; 163 unsigned int pmt_magic_frame; 164 unsigned int rmon; 165 /* IEEE 1588-2002*/ 166 unsigned int time_stamp; 167 /* IEEE 1588-2008*/ 168 unsigned int atime_stamp; 169 /* 802.3az - Energy-Efficient Ethernet (EEE) */ 170 unsigned int eee; 171 unsigned int av; 172 /* TX and RX csum */ 173 unsigned int tx_coe; 174 unsigned int rx_coe_type1; 175 unsigned int rx_coe_type2; 176 unsigned int rxfifo_over_2048; 177 /* TX and RX number of channels */ 178 unsigned int number_rx_channel; 179 unsigned int number_tx_channel; 180 /* Alternate (enhanced) DESC mode*/ 181 unsigned int enh_desc; 182 }; 183 184 /* GMAC TX FIFO is 8K, Rx FIFO is 16K */ 185 #define BUF_SIZE_16KiB 16384 186 #define BUF_SIZE_8KiB 8192 187 #define BUF_SIZE_4KiB 4096 188 #define BUF_SIZE_2KiB 2048 189 190 /* Power Down and WOL */ 191 #define PMT_NOT_SUPPORTED 0 192 #define PMT_SUPPORTED 1 193 194 /* Common MAC defines */ 195 #define MAC_CTRL_REG 0x00000000 /* MAC Control */ 196 #define MAC_ENABLE_TX 0x00000008 /* Transmitter Enable */ 197 #define MAC_RNABLE_RX 0x00000004 /* Receiver Enable */ 198 199 struct stmmac_desc_ops { 200 /* DMA RX descriptor ring initialization */ 201 void (*init_rx_desc) (struct dma_desc *p, unsigned int ring_size, 202 int disable_rx_ic); 203 /* DMA TX descriptor ring initialization */ 204 void (*init_tx_desc) (struct dma_desc *p, unsigned int ring_size); 205 206 /* Invoked by the xmit function to prepare the tx descriptor */ 207 void (*prepare_tx_desc) (struct dma_desc *p, int is_fs, int len, 208 int csum_flag); 209 /* Set/get the owner of the descriptor */ 210 void (*set_tx_owner) (struct dma_desc *p); 211 int (*get_tx_owner) (struct dma_desc *p); 212 /* Invoked by the xmit function to close the tx descriptor */ 213 void (*close_tx_desc) (struct dma_desc *p); 214 /* Clean the tx descriptor as soon as the tx irq is received */ 215 void (*release_tx_desc) (struct dma_desc *p); 216 /* Clear interrupt on tx frame completion. When this bit is 217 * set an interrupt happens as soon as the frame is transmitted */ 218 void (*clear_tx_ic) (struct dma_desc *p); 219 /* Last tx segment reports the transmit status */ 220 int (*get_tx_ls) (struct dma_desc *p); 221 /* Return the transmit status looking at the TDES1 */ 222 int (*tx_status) (void *data, struct stmmac_extra_stats *x, 223 struct dma_desc *p, void __iomem *ioaddr); 224 /* Get the buffer size from the descriptor */ 225 int (*get_tx_len) (struct dma_desc *p); 226 /* Handle extra events on specific interrupts hw dependent */ 227 int (*get_rx_owner) (struct dma_desc *p); 228 void (*set_rx_owner) (struct dma_desc *p); 229 /* Get the receive frame size */ 230 int (*get_rx_frame_len) (struct dma_desc *p); 231 /* Return the reception status looking at the RDES1 */ 232 int (*rx_status) (void *data, struct stmmac_extra_stats *x, 233 struct dma_desc *p); 234 }; 235 236 struct stmmac_dma_ops { 237 /* DMA core initialization */ 238 int (*init) (void __iomem *ioaddr, int pbl, u32 dma_tx, u32 dma_rx); 239 /* Dump DMA registers */ 240 void (*dump_regs) (void __iomem *ioaddr); 241 /* Set tx/rx threshold in the csr6 register 242 * An invalid value enables the store-and-forward mode */ 243 void (*dma_mode) (void __iomem *ioaddr, int txmode, int rxmode); 244 /* To track extra statistic (if supported) */ 245 void (*dma_diagnostic_fr) (void *data, struct stmmac_extra_stats *x, 246 void __iomem *ioaddr); 247 void (*enable_dma_transmission) (void __iomem *ioaddr); 248 void (*enable_dma_irq) (void __iomem *ioaddr); 249 void (*disable_dma_irq) (void __iomem *ioaddr); 250 void (*start_tx) (void __iomem *ioaddr); 251 void (*stop_tx) (void __iomem *ioaddr); 252 void (*start_rx) (void __iomem *ioaddr); 253 void (*stop_rx) (void __iomem *ioaddr); 254 int (*dma_interrupt) (void __iomem *ioaddr, 255 struct stmmac_extra_stats *x); 256 /* If supported then get the optional core features */ 257 unsigned int (*get_hw_feature) (void __iomem *ioaddr); 258 }; 259 260 struct stmmac_ops { 261 /* MAC core initialization */ 262 void (*core_init) (void __iomem *ioaddr) ____cacheline_aligned; 263 /* Support checksum offload engine */ 264 int (*rx_coe) (void __iomem *ioaddr); 265 /* Dump MAC registers */ 266 void (*dump_regs) (void __iomem *ioaddr); 267 /* Handle extra events on specific interrupts hw dependent */ 268 void (*host_irq_status) (void __iomem *ioaddr); 269 /* Multicast filter setting */ 270 void (*set_filter) (struct net_device *dev); 271 /* Flow control setting */ 272 void (*flow_ctrl) (void __iomem *ioaddr, unsigned int duplex, 273 unsigned int fc, unsigned int pause_time); 274 /* Set power management mode (e.g. magic frame) */ 275 void (*pmt) (void __iomem *ioaddr, unsigned long mode); 276 /* Set/Get Unicast MAC addresses */ 277 void (*set_umac_addr) (void __iomem *ioaddr, unsigned char *addr, 278 unsigned int reg_n); 279 void (*get_umac_addr) (void __iomem *ioaddr, unsigned char *addr, 280 unsigned int reg_n); 281 }; 282 283 struct mac_link { 284 int port; 285 int duplex; 286 int speed; 287 }; 288 289 struct mii_regs { 290 unsigned int addr; /* MII Address */ 291 unsigned int data; /* MII Data */ 292 }; 293 294 struct stmmac_ring_mode_ops { 295 unsigned int (*is_jumbo_frm) (int len, int ehn_desc); 296 unsigned int (*jumbo_frm) (void *priv, struct sk_buff *skb, int csum); 297 void (*refill_desc3) (int bfsize, struct dma_desc *p); 298 void (*init_desc3) (int des3_as_data_buf, struct dma_desc *p); 299 void (*init_dma_chain) (struct dma_desc *des, dma_addr_t phy_addr, 300 unsigned int size); 301 void (*clean_desc3) (struct dma_desc *p); 302 int (*set_16kib_bfsize) (int mtu); 303 }; 304 305 struct mac_device_info { 306 const struct stmmac_ops *mac; 307 const struct stmmac_desc_ops *desc; 308 const struct stmmac_dma_ops *dma; 309 const struct stmmac_ring_mode_ops *ring; 310 struct mii_regs mii; /* MII register Addresses */ 311 struct mac_link link; 312 unsigned int synopsys_uid; 313 }; 314 315 struct mac_device_info *dwmac1000_setup(void __iomem *ioaddr); 316 struct mac_device_info *dwmac100_setup(void __iomem *ioaddr); 317 318 extern void stmmac_set_mac_addr(void __iomem *ioaddr, u8 addr[6], 319 unsigned int high, unsigned int low); 320 extern void stmmac_get_mac_addr(void __iomem *ioaddr, unsigned char *addr, 321 unsigned int high, unsigned int low); 322 323 extern void stmmac_set_mac(void __iomem *ioaddr, bool enable); 324 325 extern void dwmac_dma_flush_tx_fifo(void __iomem *ioaddr); 326 extern const struct stmmac_ring_mode_ops ring_mode_ops; 327