1 /* Synopsys DWC Ethernet Quality-of-Service v4.10a linux driver
3 * This is a driver for the Synopsys DWC Ethernet QoS IP version 4.10a (GMAC).
4 * This version introduced a lot of changes which breaks backwards
5 * compatibility the non-QoS IP from Synopsys (used in the ST Micro drivers).
6 * Some fields differ between version 4.00a and 4.10a, mainly the interrupt
7 * bit fields. The driver could be made compatible with 4.00, if all relevant
8 * HW erratas are handled.
10 * The GMAC is highly configurable at synthesis time. This driver has been
11 * developed for a subset of the total available feature set. Currently
14 * - Checksum offload for RX and TX.
15 * - Energy efficient ethernet.
16 * - GMII phy interface.
17 * - The statistics module.
18 * - Single RX and TX queue.
20 * Copyright (C) 2015 Axis Communications AB.
22 * This program is free software; you can redistribute it and/or modify it
23 * under the terms and conditions of the GNU General Public License,
24 * version 2, as published by the Free Software Foundation.
27 #include <linux/clk.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/init.h>
32 #include <linux/ethtool.h>
33 #include <linux/stat.h>
34 #include <linux/types.h>
36 #include <linux/types.h>
37 #include <linux/slab.h>
38 #include <linux/delay.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
42 #include <linux/platform_device.h>
44 #include <linux/phy.h>
45 #include <linux/mii.h>
46 #include <linux/delay.h>
47 #include <linux/dma-mapping.h>
48 #include <linux/vmalloc.h>
50 #include <linux/device.h>
51 #include <linux/bitrev.h>
52 #include <linux/crc32.h>
55 #include <linux/interrupt.h>
56 #include <linux/clocksource.h>
57 #include <linux/net_tstamp.h>
58 #include <linux/pm_runtime.h>
59 #include <linux/of_net.h>
60 #include <linux/of_address.h>
61 #include <linux/of_mdio.h>
62 #include <linux/timer.h>
63 #include <linux/tcp.h>
65 #define DRIVER_NAME "dwceqos"
66 #define DRIVER_DESCRIPTION "Synopsys DWC Ethernet QoS driver"
67 #define DRIVER_VERSION "0.9"
69 #define DWCEQOS_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
70 NETIF_MSG_LINK | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
72 #define DWCEQOS_TX_TIMEOUT 5 /* Seconds */
74 #define DWCEQOS_LPI_TIMER_MIN 8
75 #define DWCEQOS_LPI_TIMER_MAX ((1 << 20) - 1)
77 #define DWCEQOS_RX_BUF_SIZE 2048
79 #define DWCEQOS_RX_DCNT 256
80 #define DWCEQOS_TX_DCNT 256
82 #define DWCEQOS_HASH_TABLE_SIZE 64
84 /* The size field in the DMA descriptor is 14 bits */
85 #define BYTES_PER_DMA_DESC 16376
87 /* Hardware registers */
88 #define START_MAC_REG_OFFSET 0x0000
89 #define MAX_MAC_REG_OFFSET 0x0bd0
90 #define START_MTL_REG_OFFSET 0x0c00
91 #define MAX_MTL_REG_OFFSET 0x0d7c
92 #define START_DMA_REG_OFFSET 0x1000
93 #define MAX_DMA_REG_OFFSET 0x117C
95 #define REG_SPACE_SIZE 0x1800
98 #define REG_DWCEQOS_DMA_MODE 0x1000
99 #define REG_DWCEQOS_DMA_SYSBUS_MODE 0x1004
100 #define REG_DWCEQOS_DMA_IS 0x1008
101 #define REG_DWCEQOS_DMA_DEBUG_ST0 0x100c
103 /* DMA channel registers */
104 #define REG_DWCEQOS_DMA_CH0_CTRL 0x1100
105 #define REG_DWCEQOS_DMA_CH0_TX_CTRL 0x1104
106 #define REG_DWCEQOS_DMA_CH0_RX_CTRL 0x1108
107 #define REG_DWCEQOS_DMA_CH0_TXDESC_LIST 0x1114
108 #define REG_DWCEQOS_DMA_CH0_RXDESC_LIST 0x111c
109 #define REG_DWCEQOS_DMA_CH0_TXDESC_TAIL 0x1120
110 #define REG_DWCEQOS_DMA_CH0_RXDESC_TAIL 0x1128
111 #define REG_DWCEQOS_DMA_CH0_TXDESC_LEN 0x112c
112 #define REG_DWCEQOS_DMA_CH0_RXDESC_LEN 0x1130
113 #define REG_DWCEQOS_DMA_CH0_IE 0x1134
114 #define REG_DWCEQOS_DMA_CH0_CUR_TXDESC 0x1144
115 #define REG_DWCEQOS_DMA_CH0_CUR_RXDESC 0x114c
116 #define REG_DWCEQOS_DMA_CH0_CUR_TXBUF 0x1154
117 #define REG_DWCEQOS_DMA_CH0_CUR_RXBUG 0x115c
118 #define REG_DWCEQOS_DMA_CH0_STA 0x1160
120 #define DWCEQOS_DMA_MODE_TXPR BIT(11)
121 #define DWCEQOS_DMA_MODE_DA BIT(1)
123 #define DWCEQOS_DMA_SYSBUS_MODE_EN_LPI BIT(31)
124 #define DWCEQOS_DMA_SYSBUS_MODE_FB BIT(0)
125 #define DWCEQOS_DMA_SYSBUS_MODE_AAL BIT(12)
127 #define DWCEQOS_DMA_SYSBUS_MODE_RD_OSR_LIMIT(x) \
128 (((x) << 16) & 0x000F0000)
129 #define DWCEQOS_DMA_SYSBUS_MODE_RD_OSR_LIMIT_DEFAULT 3
130 #define DWCEQOS_DMA_SYSBUS_MODE_RD_OSR_LIMIT_MASK GENMASK(19, 16)
132 #define DWCEQOS_DMA_SYSBUS_MODE_WR_OSR_LIMIT(x) \
133 (((x) << 24) & 0x0F000000)
134 #define DWCEQOS_DMA_SYSBUS_MODE_WR_OSR_LIMIT_DEFAULT 3
135 #define DWCEQOS_DMA_SYSBUS_MODE_WR_OSR_LIMIT_MASK GENMASK(27, 24)
137 #define DWCEQOS_DMA_SYSBUS_MODE_BURST_MASK GENMASK(7, 1)
138 #define DWCEQOS_DMA_SYSBUS_MODE_BURST(x) \
139 (((x) << 1) & DWCEQOS_DMA_SYSBUS_MODE_BURST_MASK)
140 #define DWCEQOS_DMA_SYSBUS_MODE_BURST_DEFAULT GENMASK(3, 1)
142 #define DWCEQOS_DMA_CH_CTRL_PBLX8 BIT(16)
143 #define DWCEQOS_DMA_CH_CTRL_DSL(x) ((x) << 18)
145 #define DWCEQOS_DMA_CH_CTRL_PBL(x) ((x) << 16)
146 #define DWCEQOS_DMA_CH_CTRL_START BIT(0)
147 #define DWCEQOS_DMA_CH_RX_CTRL_BUFSIZE(x) ((x) << 1)
148 #define DWCEQOS_DMA_CH_TX_OSP BIT(4)
149 #define DWCEQOS_DMA_CH_TX_TSE BIT(12)
151 #define DWCEQOS_DMA_CH0_IE_NIE BIT(15)
152 #define DWCEQOS_DMA_CH0_IE_AIE BIT(14)
153 #define DWCEQOS_DMA_CH0_IE_RIE BIT(6)
154 #define DWCEQOS_DMA_CH0_IE_TIE BIT(0)
155 #define DWCEQOS_DMA_CH0_IE_FBEE BIT(12)
156 #define DWCEQOS_DMA_CH0_IE_RBUE BIT(7)
158 #define DWCEQOS_DMA_IS_DC0IS BIT(0)
159 #define DWCEQOS_DMA_IS_MTLIS BIT(16)
160 #define DWCEQOS_DMA_IS_MACIS BIT(17)
162 #define DWCEQOS_DMA_CH0_IS_TI BIT(0)
163 #define DWCEQOS_DMA_CH0_IS_RI BIT(6)
164 #define DWCEQOS_DMA_CH0_IS_RBU BIT(7)
165 #define DWCEQOS_DMA_CH0_IS_FBE BIT(12)
166 #define DWCEQOS_DMA_CH0_IS_CDE BIT(13)
167 #define DWCEQOS_DMA_CH0_IS_AIS BIT(14)
169 #define DWCEQOS_DMA_CH0_IS_TEB GENMASK(18, 16)
170 #define DWCEQOS_DMA_CH0_IS_TX_ERR_READ BIT(16)
171 #define DWCEQOS_DMA_CH0_IS_TX_ERR_DESCR BIT(17)
173 #define DWCEQOS_DMA_CH0_IS_REB GENMASK(21, 19)
174 #define DWCEQOS_DMA_CH0_IS_RX_ERR_READ BIT(19)
175 #define DWCEQOS_DMA_CH0_IS_RX_ERR_DESCR BIT(20)
177 /* DMA descriptor bits for RX normal descriptor (read format) */
178 #define DWCEQOS_DMA_RDES3_OWN BIT(31)
179 #define DWCEQOS_DMA_RDES3_INTE BIT(30)
180 #define DWCEQOS_DMA_RDES3_BUF2V BIT(25)
181 #define DWCEQOS_DMA_RDES3_BUF1V BIT(24)
183 /* DMA descriptor bits for RX normal descriptor (write back format) */
184 #define DWCEQOS_DMA_RDES1_IPCE BIT(7)
185 #define DWCEQOS_DMA_RDES3_ES BIT(15)
186 #define DWCEQOS_DMA_RDES3_E_JT BIT(14)
187 #define DWCEQOS_DMA_RDES3_PL(x) ((x) & 0x7fff)
188 #define DWCEQOS_DMA_RDES1_PT 0x00000007
189 #define DWCEQOS_DMA_RDES1_PT_UDP BIT(0)
190 #define DWCEQOS_DMA_RDES1_PT_TCP BIT(1)
191 #define DWCEQOS_DMA_RDES1_PT_ICMP 0x00000003
193 /* DMA descriptor bits for TX normal descriptor (read format) */
194 #define DWCEQOS_DMA_TDES2_IOC BIT(31)
195 #define DWCEQOS_DMA_TDES3_OWN BIT(31)
196 #define DWCEQOS_DMA_TDES3_CTXT BIT(30)
197 #define DWCEQOS_DMA_TDES3_FD BIT(29)
198 #define DWCEQOS_DMA_TDES3_LD BIT(28)
199 #define DWCEQOS_DMA_TDES3_CIPH BIT(16)
200 #define DWCEQOS_DMA_TDES3_CIPP BIT(17)
201 #define DWCEQOS_DMA_TDES3_CA 0x00030000
202 #define DWCEQOS_DMA_TDES3_TSE BIT(18)
203 #define DWCEQOS_DMA_DES3_THL(x) ((x) << 19)
204 #define DWCEQOS_DMA_DES2_B2L(x) ((x) << 16)
206 #define DWCEQOS_DMA_TDES3_TCMSSV BIT(26)
208 /* DMA channel states */
209 #define DMA_TX_CH_STOPPED 0
210 #define DMA_TX_CH_SUSPENDED 6
212 #define DMA_GET_TX_STATE_CH0(status0) ((status0 & 0xF000) >> 12)
215 #define REG_DWCEQOS_MTL_OPER 0x0c00
216 #define REG_DWCEQOS_MTL_DEBUG_ST 0x0c0c
217 #define REG_DWCEQOS_MTL_TXQ0_DEBUG_ST 0x0d08
218 #define REG_DWCEQOS_MTL_RXQ0_DEBUG_ST 0x0d38
220 #define REG_DWCEQOS_MTL_IS 0x0c20
221 #define REG_DWCEQOS_MTL_TXQ0_OPER 0x0d00
222 #define REG_DWCEQOS_MTL_RXQ0_OPER 0x0d30
223 #define REG_DWCEQOS_MTL_RXQ0_MIS_CNT 0x0d34
224 #define REG_DWCEQOS_MTL_RXQ0_CTRL 0x0d3c
226 #define REG_DWCEQOS_MTL_Q0_ISCTRL 0x0d2c
228 #define DWCEQOS_MTL_SCHALG_STRICT 0x00000060
230 #define DWCEQOS_MTL_TXQ_TXQEN BIT(3)
231 #define DWCEQOS_MTL_TXQ_TSF BIT(1)
232 #define DWCEQOS_MTL_TXQ_FTQ BIT(0)
233 #define DWCEQOS_MTL_TXQ_TTC512 0x00000070
235 #define DWCEQOS_MTL_TXQ_SIZE(x) ((((x) - 256) & 0xff00) << 8)
237 #define DWCEQOS_MTL_RXQ_SIZE(x) ((((x) - 256) & 0xff00) << 12)
238 #define DWCEQOS_MTL_RXQ_EHFC BIT(7)
239 #define DWCEQOS_MTL_RXQ_DIS_TCP_EF BIT(6)
240 #define DWCEQOS_MTL_RXQ_FEP BIT(4)
241 #define DWCEQOS_MTL_RXQ_FUP BIT(3)
242 #define DWCEQOS_MTL_RXQ_RSF BIT(5)
243 #define DWCEQOS_MTL_RXQ_RTC32 BIT(0)
246 #define REG_DWCEQOS_MAC_CFG 0x0000
247 #define REG_DWCEQOS_MAC_EXT_CFG 0x0004
248 #define REG_DWCEQOS_MAC_PKT_FILT 0x0008
249 #define REG_DWCEQOS_MAC_WD_TO 0x000c
250 #define REG_DWCEQOS_HASTABLE_LO 0x0010
251 #define REG_DWCEQOS_HASTABLE_HI 0x0014
252 #define REG_DWCEQOS_MAC_IS 0x00b0
253 #define REG_DWCEQOS_MAC_IE 0x00b4
254 #define REG_DWCEQOS_MAC_STAT 0x00b8
255 #define REG_DWCEQOS_MAC_MDIO_ADDR 0x0200
256 #define REG_DWCEQOS_MAC_MDIO_DATA 0x0204
257 #define REG_DWCEQOS_MAC_MAC_ADDR0_HI 0x0300
258 #define REG_DWCEQOS_MAC_MAC_ADDR0_LO 0x0304
259 #define REG_DWCEQOS_MAC_RXQ0_CTRL0 0x00a0
260 #define REG_DWCEQOS_MAC_HW_FEATURE0 0x011c
261 #define REG_DWCEQOS_MAC_HW_FEATURE1 0x0120
262 #define REG_DWCEQOS_MAC_HW_FEATURE2 0x0124
263 #define REG_DWCEQOS_MAC_HASHTABLE_LO 0x0010
264 #define REG_DWCEQOS_MAC_HASHTABLE_HI 0x0014
265 #define REG_DWCEQOS_MAC_LPI_CTRL_STATUS 0x00d0
266 #define REG_DWCEQOS_MAC_LPI_TIMERS_CTRL 0x00d4
267 #define REG_DWCEQOS_MAC_LPI_ENTRY_TIMER 0x00d8
268 #define REG_DWCEQOS_MAC_1US_TIC_COUNTER 0x00dc
269 #define REG_DWCEQOS_MAC_RX_FLOW_CTRL 0x0090
270 #define REG_DWCEQOS_MAC_Q0_TX_FLOW 0x0070
272 #define DWCEQOS_MAC_CFG_ACS BIT(20)
273 #define DWCEQOS_MAC_CFG_JD BIT(17)
274 #define DWCEQOS_MAC_CFG_JE BIT(16)
275 #define DWCEQOS_MAC_CFG_PS BIT(15)
276 #define DWCEQOS_MAC_CFG_FES BIT(14)
277 #define DWCEQOS_MAC_CFG_DM BIT(13)
278 #define DWCEQOS_MAC_CFG_DO BIT(10)
279 #define DWCEQOS_MAC_CFG_TE BIT(1)
280 #define DWCEQOS_MAC_CFG_IPC BIT(27)
281 #define DWCEQOS_MAC_CFG_RE BIT(0)
283 #define DWCEQOS_ADDR_HIGH(reg) (0x00000300 + (reg * 8))
284 #define DWCEQOS_ADDR_LOW(reg) (0x00000304 + (reg * 8))
286 #define DWCEQOS_MAC_IS_LPI_INT BIT(5)
287 #define DWCEQOS_MAC_IS_MMC_INT BIT(8)
289 #define DWCEQOS_MAC_RXQ_EN BIT(1)
290 #define DWCEQOS_MAC_MAC_ADDR_HI_EN BIT(31)
291 #define DWCEQOS_MAC_PKT_FILT_RA BIT(31)
292 #define DWCEQOS_MAC_PKT_FILT_HPF BIT(10)
293 #define DWCEQOS_MAC_PKT_FILT_SAF BIT(9)
294 #define DWCEQOS_MAC_PKT_FILT_SAIF BIT(8)
295 #define DWCEQOS_MAC_PKT_FILT_DBF BIT(5)
296 #define DWCEQOS_MAC_PKT_FILT_PM BIT(4)
297 #define DWCEQOS_MAC_PKT_FILT_DAIF BIT(3)
298 #define DWCEQOS_MAC_PKT_FILT_HMC BIT(2)
299 #define DWCEQOS_MAC_PKT_FILT_HUC BIT(1)
300 #define DWCEQOS_MAC_PKT_FILT_PR BIT(0)
302 #define DWCEQOS_MAC_MDIO_ADDR_CR(x) (((x & 15)) << 8)
303 #define DWCEQOS_MAC_MDIO_ADDR_CR_20 2
304 #define DWCEQOS_MAC_MDIO_ADDR_CR_35 3
305 #define DWCEQOS_MAC_MDIO_ADDR_CR_60 0
306 #define DWCEQOS_MAC_MDIO_ADDR_CR_100 1
307 #define DWCEQOS_MAC_MDIO_ADDR_CR_150 4
308 #define DWCEQOS_MAC_MDIO_ADDR_CR_250 5
309 #define DWCEQOS_MAC_MDIO_ADDR_GOC_READ 0x0000000c
310 #define DWCEQOS_MAC_MDIO_ADDR_GOC_WRITE BIT(2)
311 #define DWCEQOS_MAC_MDIO_ADDR_GB BIT(0)
313 #define DWCEQOS_MAC_LPI_CTRL_STATUS_TLPIEN BIT(0)
314 #define DWCEQOS_MAC_LPI_CTRL_STATUS_TLPIEX BIT(1)
315 #define DWCEQOS_MAC_LPI_CTRL_STATUS_RLPIEN BIT(2)
316 #define DWCEQOS_MAC_LPI_CTRL_STATUS_RLPIEX BIT(3)
317 #define DWCEQOS_MAC_LPI_CTRL_STATUS_TLPIST BIT(8)
318 #define DWCEQOS_MAC_LPI_CTRL_STATUS_RLPIST BIT(9)
319 #define DWCEQOS_MAC_LPI_CTRL_STATUS_LPIEN BIT(16)
320 #define DWCEQOS_MAC_LPI_CTRL_STATUS_PLS BIT(17)
321 #define DWCEQOS_MAC_LPI_CTRL_STATUS_PLSEN BIT(18)
322 #define DWCEQOS_MAC_LPI_CTRL_STATUS_LIPTXA BIT(19)
323 #define DWCEQOS_MAC_LPI_CTRL_STATUS_LPITE BIT(20)
324 #define DWCEQOS_MAC_LPI_CTRL_STATUS_LPITCSE BIT(21)
326 #define DWCEQOS_MAC_1US_TIC_COUNTER_VAL(x) ((x) & GENMASK(11, 0))
328 #define DWCEQOS_LPI_CTRL_ENABLE_EEE (DWCEQOS_MAC_LPI_CTRL_STATUS_LPITE | \
329 DWCEQOS_MAC_LPI_CTRL_STATUS_LIPTXA | \
330 DWCEQOS_MAC_LPI_CTRL_STATUS_LPIEN)
332 #define DWCEQOS_MAC_RX_FLOW_CTRL_RFE BIT(0)
334 #define DWCEQOS_MAC_Q0_TX_FLOW_TFE BIT(1)
335 #define DWCEQOS_MAC_Q0_TX_FLOW_PT(time) ((time) << 16)
336 #define DWCEQOS_MAC_Q0_TX_FLOW_PLT_4_SLOTS (0 << 4)
339 #define DWCEQOS_MAC_HW_FEATURE0_RXCOESEL BIT(16)
340 #define DWCEQOS_MAC_HW_FEATURE0_TXCOESEL BIT(14)
341 #define DWCEQOS_MAC_HW_FEATURE0_HDSEL BIT(2)
342 #define DWCEQOS_MAC_HW_FEATURE0_EEESEL BIT(13)
343 #define DWCEQOS_MAC_HW_FEATURE0_GMIISEL BIT(1)
344 #define DWCEQOS_MAC_HW_FEATURE0_MIISEL BIT(0)
346 #define DWCEQOS_MAC_HW_FEATURE1_TSOEN BIT(18)
347 #define DWCEQOS_MAC_HW_FEATURE1_TXFIFOSIZE(x) ((128 << ((x) & 0x7c0)) >> 6)
348 #define DWCEQOS_MAC_HW_FEATURE1_RXFIFOSIZE(x) (128 << ((x) & 0x1f))
350 #define DWCEQOS_MAX_PERFECT_ADDRESSES(feature1) \
351 (1 + (((feature1) & 0x1fc0000) >> 18))
353 #define DWCEQOS_MDIO_PHYADDR(x) (((x) & 0x1f) << 21)
354 #define DWCEQOS_MDIO_PHYREG(x) (((x) & 0x1f) << 16)
356 #define DWCEQOS_DMA_MODE_SWR BIT(0)
358 #define DWCEQOS_DWCEQOS_RX_BUF_SIZE 2048
360 /* Mac Management Counters */
361 #define REG_DWCEQOS_MMC_CTRL 0x0700
362 #define REG_DWCEQOS_MMC_RXIRQ 0x0704
363 #define REG_DWCEQOS_MMC_TXIRQ 0x0708
364 #define REG_DWCEQOS_MMC_RXIRQMASK 0x070c
365 #define REG_DWCEQOS_MMC_TXIRQMASK 0x0710
367 #define DWCEQOS_MMC_CTRL_CNTRST BIT(0)
368 #define DWCEQOS_MMC_CTRL_RSTONRD BIT(2)
370 #define DWC_MMC_TXLPITRANSCNTR 0x07F0
371 #define DWC_MMC_TXLPIUSCNTR 0x07EC
372 #define DWC_MMC_TXOVERSIZE_G 0x0778
373 #define DWC_MMC_TXVLANPACKETS_G 0x0774
374 #define DWC_MMC_TXPAUSEPACKETS 0x0770
375 #define DWC_MMC_TXEXCESSDEF 0x076C
376 #define DWC_MMC_TXPACKETCOUNT_G 0x0768
377 #define DWC_MMC_TXOCTETCOUNT_G 0x0764
378 #define DWC_MMC_TXCARRIERERROR 0x0760
379 #define DWC_MMC_TXEXCESSCOL 0x075C
380 #define DWC_MMC_TXLATECOL 0x0758
381 #define DWC_MMC_TXDEFERRED 0x0754
382 #define DWC_MMC_TXMULTICOL_G 0x0750
383 #define DWC_MMC_TXSINGLECOL_G 0x074C
384 #define DWC_MMC_TXUNDERFLOWERROR 0x0748
385 #define DWC_MMC_TXBROADCASTPACKETS_GB 0x0744
386 #define DWC_MMC_TXMULTICASTPACKETS_GB 0x0740
387 #define DWC_MMC_TXUNICASTPACKETS_GB 0x073C
388 #define DWC_MMC_TX1024TOMAXOCTETS_GB 0x0738
389 #define DWC_MMC_TX512TO1023OCTETS_GB 0x0734
390 #define DWC_MMC_TX256TO511OCTETS_GB 0x0730
391 #define DWC_MMC_TX128TO255OCTETS_GB 0x072C
392 #define DWC_MMC_TX65TO127OCTETS_GB 0x0728
393 #define DWC_MMC_TX64OCTETS_GB 0x0724
394 #define DWC_MMC_TXMULTICASTPACKETS_G 0x0720
395 #define DWC_MMC_TXBROADCASTPACKETS_G 0x071C
396 #define DWC_MMC_TXPACKETCOUNT_GB 0x0718
397 #define DWC_MMC_TXOCTETCOUNT_GB 0x0714
399 #define DWC_MMC_RXLPITRANSCNTR 0x07F8
400 #define DWC_MMC_RXLPIUSCNTR 0x07F4
401 #define DWC_MMC_RXCTRLPACKETS_G 0x07E4
402 #define DWC_MMC_RXRCVERROR 0x07E0
403 #define DWC_MMC_RXWATCHDOG 0x07DC
404 #define DWC_MMC_RXVLANPACKETS_GB 0x07D8
405 #define DWC_MMC_RXFIFOOVERFLOW 0x07D4
406 #define DWC_MMC_RXPAUSEPACKETS 0x07D0
407 #define DWC_MMC_RXOUTOFRANGETYPE 0x07CC
408 #define DWC_MMC_RXLENGTHERROR 0x07C8
409 #define DWC_MMC_RXUNICASTPACKETS_G 0x07C4
410 #define DWC_MMC_RX1024TOMAXOCTETS_GB 0x07C0
411 #define DWC_MMC_RX512TO1023OCTETS_GB 0x07BC
412 #define DWC_MMC_RX256TO511OCTETS_GB 0x07B8
413 #define DWC_MMC_RX128TO255OCTETS_GB 0x07B4
414 #define DWC_MMC_RX65TO127OCTETS_GB 0x07B0
415 #define DWC_MMC_RX64OCTETS_GB 0x07AC
416 #define DWC_MMC_RXOVERSIZE_G 0x07A8
417 #define DWC_MMC_RXUNDERSIZE_G 0x07A4
418 #define DWC_MMC_RXJABBERERROR 0x07A0
419 #define DWC_MMC_RXRUNTERROR 0x079C
420 #define DWC_MMC_RXALIGNMENTERROR 0x0798
421 #define DWC_MMC_RXCRCERROR 0x0794
422 #define DWC_MMC_RXMULTICASTPACKETS_G 0x0790
423 #define DWC_MMC_RXBROADCASTPACKETS_G 0x078C
424 #define DWC_MMC_RXOCTETCOUNT_G 0x0788
425 #define DWC_MMC_RXOCTETCOUNT_GB 0x0784
426 #define DWC_MMC_RXPACKETCOUNT_GB 0x0780
428 static int debug
= -1;
429 module_param(debug
, int, 0);
430 MODULE_PARM_DESC(debug
, "DWC_eth_qos debug level (0=none,...,16=all)");
432 /* DMA ring descriptor. These are used as support descriptors for the HW DMA */
439 /* DMA hardware descriptor */
440 struct dwceqos_dma_desc
{
445 } ____cacheline_aligned
;
447 struct dwceqos_mmc_counters
{
448 __u64 txlpitranscntr
;
451 __u64 txvlanpackets_g
;
452 __u64 txpausepackets
;
454 __u64 txpacketcount_g
;
455 __u64 txoctetcount_g
;
456 __u64 txcarriererror
;
462 __u64 txunderflowerror
;
463 __u64 txbroadcastpackets_gb
;
464 __u64 txmulticastpackets_gb
;
465 __u64 txunicastpackets_gb
;
466 __u64 tx1024tomaxoctets_gb
;
467 __u64 tx512to1023octets_gb
;
468 __u64 tx256to511octets_gb
;
469 __u64 tx128to255octets_gb
;
470 __u64 tx65to127octets_gb
;
472 __u64 txmulticastpackets_g
;
473 __u64 txbroadcastpackets_g
;
474 __u64 txpacketcount_gb
;
475 __u64 txoctetcount_gb
;
477 __u64 rxlpitranscntr
;
479 __u64 rxctrlpackets_g
;
482 __u64 rxvlanpackets_gb
;
483 __u64 rxfifooverflow
;
484 __u64 rxpausepackets
;
485 __u64 rxoutofrangetype
;
487 __u64 rxunicastpackets_g
;
488 __u64 rx1024tomaxoctets_gb
;
489 __u64 rx512to1023octets_gb
;
490 __u64 rx256to511octets_gb
;
491 __u64 rx128to255octets_gb
;
492 __u64 rx65to127octets_gb
;
498 __u64 rxalignmenterror
;
500 __u64 rxmulticastpackets_g
;
501 __u64 rxbroadcastpackets_g
;
502 __u64 rxoctetcount_g
;
503 __u64 rxoctetcount_gb
;
504 __u64 rxpacketcount_gb
;
507 /* Ethtool statistics */
509 struct dwceqos_stat
{
510 const char stat_name
[ETH_GSTRING_LEN
];
514 #define STAT_ITEM(name, var) \
517 offsetof(struct dwceqos_mmc_counters, var),\
520 static const struct dwceqos_stat dwceqos_ethtool_stats
[] = {
521 STAT_ITEM("tx_bytes", txoctetcount_gb
),
522 STAT_ITEM("tx_packets", txpacketcount_gb
),
523 STAT_ITEM("tx_unicst_packets", txunicastpackets_gb
),
524 STAT_ITEM("tx_broadcast_packets", txbroadcastpackets_gb
),
525 STAT_ITEM("tx_multicast_packets", txmulticastpackets_gb
),
526 STAT_ITEM("tx_pause_packets", txpausepackets
),
527 STAT_ITEM("tx_up_to_64_byte_packets", tx64octets_gb
),
528 STAT_ITEM("tx_65_to_127_byte_packets", tx65to127octets_gb
),
529 STAT_ITEM("tx_128_to_255_byte_packets", tx128to255octets_gb
),
530 STAT_ITEM("tx_256_to_511_byte_packets", tx256to511octets_gb
),
531 STAT_ITEM("tx_512_to_1023_byte_packets", tx512to1023octets_gb
),
532 STAT_ITEM("tx_1024_to_maxsize_packets", tx1024tomaxoctets_gb
),
533 STAT_ITEM("tx_underflow_errors", txunderflowerror
),
534 STAT_ITEM("tx_lpi_count", txlpitranscntr
),
536 STAT_ITEM("rx_bytes", rxoctetcount_gb
),
537 STAT_ITEM("rx_packets", rxpacketcount_gb
),
538 STAT_ITEM("rx_unicast_packets", rxunicastpackets_g
),
539 STAT_ITEM("rx_broadcast_packets", rxbroadcastpackets_g
),
540 STAT_ITEM("rx_multicast_packets", rxmulticastpackets_g
),
541 STAT_ITEM("rx_vlan_packets", rxvlanpackets_gb
),
542 STAT_ITEM("rx_pause_packets", rxpausepackets
),
543 STAT_ITEM("rx_up_to_64_byte_packets", rx64octets_gb
),
544 STAT_ITEM("rx_65_to_127_byte_packets", rx65to127octets_gb
),
545 STAT_ITEM("rx_128_to_255_byte_packets", rx128to255octets_gb
),
546 STAT_ITEM("rx_256_to_511_byte_packets", rx256to511octets_gb
),
547 STAT_ITEM("rx_512_to_1023_byte_packets", rx512to1023octets_gb
),
548 STAT_ITEM("rx_1024_to_maxsize_packets", rx1024tomaxoctets_gb
),
549 STAT_ITEM("rx_fifo_overflow_errors", rxfifooverflow
),
550 STAT_ITEM("rx_oversize_packets", rxoversize_g
),
551 STAT_ITEM("rx_undersize_packets", rxundersize_g
),
552 STAT_ITEM("rx_jabbers", rxjabbererror
),
553 STAT_ITEM("rx_align_errors", rxalignmenterror
),
554 STAT_ITEM("rx_crc_errors", rxcrcerror
),
555 STAT_ITEM("rx_lpi_count", rxlpitranscntr
),
558 /* Configuration of AXI bus parameters.
559 * These values depend on the parameters set on the MAC core as well
560 * as the AXI interconnect.
562 struct dwceqos_bus_cfg
{
563 /* Enable AXI low-power interface. */
565 /* Limit on number of outstanding AXI write requests. */
567 /* Limit on number of outstanding AXI read requests. */
569 /* Bitmap of allowed AXI burst lengths, 4-256 beats. */
571 /* DMA Programmable burst length*/
576 struct dwceqos_flowcontrol
{
585 void __iomem
*baseaddr
;
586 struct clk
*phy_ref_clk
;
587 struct clk
*apb_pclk
;
589 struct device_node
*phy_node
;
590 struct net_device
*ndev
;
591 struct platform_device
*pdev
;
595 struct tasklet_struct tx_bdreclaim_tasklet
;
596 struct workqueue_struct
*txtimeout_handler_wq
;
597 struct work_struct txtimeout_reinit
;
599 phy_interface_t phy_interface
;
600 struct mii_bus
*mii_bus
;
606 struct napi_struct napi
;
608 /* DMA Descriptor Areas */
609 struct ring_desc
*rx_skb
;
610 struct ring_desc
*tx_skb
;
612 struct dwceqos_dma_desc
*tx_descs
;
613 struct dwceqos_dma_desc
*rx_descs
;
615 /* DMA Mapped Descriptor areas*/
616 dma_addr_t tx_descs_addr
;
617 dma_addr_t rx_descs_addr
;
618 dma_addr_t tx_descs_tail_addr
;
619 dma_addr_t rx_descs_tail_addr
;
626 /* Spinlocks for accessing DMA Descriptors */
629 /* Spinlock for register read-modify-writes. */
636 struct dwceqos_bus_cfg bus_cfg
;
637 bool en_tx_lpi_clockgating
;
644 struct dwceqos_mmc_counters mmc_counters
;
645 /* Protect the mmc_counter updates. */
646 spinlock_t stats_lock
;
647 u32 mmc_rx_counters_mask
;
648 u32 mmc_tx_counters_mask
;
650 struct dwceqos_flowcontrol flowcontrol
;
652 /* Tracks the intermediate state of phy started but hardware
653 * init not finished yet.
658 static void dwceqos_read_mmc_counters(struct net_local
*lp
, u32 rx_mask
,
661 static void dwceqos_set_umac_addr(struct net_local
*lp
, unsigned char *addr
,
663 static int dwceqos_stop(struct net_device
*ndev
);
664 static int dwceqos_open(struct net_device
*ndev
);
665 static void dwceqos_tx_poll_demand(struct net_local
*lp
);
667 static void dwceqos_set_rx_flowcontrol(struct net_local
*lp
, bool enable
);
668 static void dwceqos_set_tx_flowcontrol(struct net_local
*lp
, bool enable
);
670 static void dwceqos_reset_state(struct net_local
*lp
);
672 #define dwceqos_read(lp, reg) \
673 readl_relaxed(((void __iomem *)((lp)->baseaddr)) + (reg))
674 #define dwceqos_write(lp, reg, val) \
675 writel_relaxed((val), ((void __iomem *)((lp)->baseaddr)) + (reg))
677 static void dwceqos_reset_state(struct net_local
*lp
)
681 lp
->duplex
= DUPLEX_UNKNOWN
;
682 lp
->flowcontrol
.rx_current
= 0;
683 lp
->flowcontrol
.tx_current
= 0;
688 static void print_descriptor(struct net_local
*lp
, int index
, int tx
)
690 struct dwceqos_dma_desc
*dd
;
693 dd
= (struct dwceqos_dma_desc
*)&lp
->tx_descs
[index
];
695 dd
= (struct dwceqos_dma_desc
*)&lp
->rx_descs
[index
];
697 pr_info("%s DMA Descriptor #%d@%p Contents:\n", tx
? "TX" : "RX",
699 pr_info("0x%08x 0x%08x 0x%08x 0x%08x\n", dd
->des0
, dd
->des1
, dd
->des2
,
703 static void print_status(struct net_local
*lp
)
707 pr_info("tx_free %zu, tx_cur %zu, tx_next %zu\n", lp
->tx_free
,
708 lp
->tx_cur
, lp
->tx_next
);
710 print_descriptor(lp
, lp
->rx_cur
, 0);
712 for (desci
= (lp
->tx_cur
- 10) % DWCEQOS_TX_DCNT
, i
= 0;
715 print_descriptor(lp
, desci
, 1);
716 desci
= (desci
+ 1) % DWCEQOS_TX_DCNT
;
719 pr_info("DMA_Debug_Status0: 0x%08x\n",
720 dwceqos_read(lp
, REG_DWCEQOS_DMA_DEBUG_ST0
));
721 pr_info("DMA_CH0_Status: 0x%08x\n",
722 dwceqos_read(lp
, REG_DWCEQOS_DMA_IS
));
723 pr_info("DMA_CH0_Current_App_TxDesc: 0x%08x\n",
724 dwceqos_read(lp
, 0x1144));
725 pr_info("DMA_CH0_Current_App_TxBuff: 0x%08x\n",
726 dwceqos_read(lp
, 0x1154));
727 pr_info("MTL_Debug_Status: 0x%08x\n",
728 dwceqos_read(lp
, REG_DWCEQOS_MTL_DEBUG_ST
));
729 pr_info("MTL_TXQ0_Debug_Status: 0x%08x\n",
730 dwceqos_read(lp
, REG_DWCEQOS_MTL_TXQ0_DEBUG_ST
));
731 pr_info("MTL_RXQ0_Debug_Status: 0x%08x\n",
732 dwceqos_read(lp
, REG_DWCEQOS_MTL_RXQ0_DEBUG_ST
));
733 pr_info("Current TX DMA: 0x%08x, RX DMA: 0x%08x\n",
734 dwceqos_read(lp
, REG_DWCEQOS_DMA_CH0_CUR_TXDESC
),
735 dwceqos_read(lp
, REG_DWCEQOS_DMA_CH0_CUR_RXDESC
));
738 static void dwceqos_mdio_set_csr(struct net_local
*lp
)
740 int rate
= clk_get_rate(lp
->apb_pclk
);
742 if (rate
<= 20000000)
743 lp
->csr_val
= DWCEQOS_MAC_MDIO_ADDR_CR_20
;
744 else if (rate
<= 35000000)
745 lp
->csr_val
= DWCEQOS_MAC_MDIO_ADDR_CR_35
;
746 else if (rate
<= 60000000)
747 lp
->csr_val
= DWCEQOS_MAC_MDIO_ADDR_CR_60
;
748 else if (rate
<= 100000000)
749 lp
->csr_val
= DWCEQOS_MAC_MDIO_ADDR_CR_100
;
750 else if (rate
<= 150000000)
751 lp
->csr_val
= DWCEQOS_MAC_MDIO_ADDR_CR_150
;
752 else if (rate
<= 250000000)
753 lp
->csr_val
= DWCEQOS_MAC_MDIO_ADDR_CR_250
;
756 /* Simple MDIO functions implementing mii_bus */
757 static int dwceqos_mdio_read(struct mii_bus
*bus
, int mii_id
, int phyreg
)
759 struct net_local
*lp
= bus
->priv
;
764 regval
= DWCEQOS_MDIO_PHYADDR(mii_id
) |
765 DWCEQOS_MDIO_PHYREG(phyreg
) |
766 DWCEQOS_MAC_MDIO_ADDR_CR(lp
->csr_val
) |
767 DWCEQOS_MAC_MDIO_ADDR_GB
|
768 DWCEQOS_MAC_MDIO_ADDR_GOC_READ
;
769 dwceqos_write(lp
, REG_DWCEQOS_MAC_MDIO_ADDR
, regval
);
771 for (i
= 0; i
< 5; ++i
) {
772 usleep_range(64, 128);
773 if (!(dwceqos_read(lp
, REG_DWCEQOS_MAC_MDIO_ADDR
) &
774 DWCEQOS_MAC_MDIO_ADDR_GB
))
778 data
= dwceqos_read(lp
, REG_DWCEQOS_MAC_MDIO_DATA
);
780 netdev_warn(lp
->ndev
, "MDIO read timed out\n");
784 return data
& 0xffff;
787 static int dwceqos_mdio_write(struct mii_bus
*bus
, int mii_id
, int phyreg
,
790 struct net_local
*lp
= bus
->priv
;
794 dwceqos_write(lp
, REG_DWCEQOS_MAC_MDIO_DATA
, value
);
796 regval
= DWCEQOS_MDIO_PHYADDR(mii_id
) |
797 DWCEQOS_MDIO_PHYREG(phyreg
) |
798 DWCEQOS_MAC_MDIO_ADDR_CR(lp
->csr_val
) |
799 DWCEQOS_MAC_MDIO_ADDR_GB
|
800 DWCEQOS_MAC_MDIO_ADDR_GOC_WRITE
;
801 dwceqos_write(lp
, REG_DWCEQOS_MAC_MDIO_ADDR
, regval
);
803 for (i
= 0; i
< 5; ++i
) {
804 usleep_range(64, 128);
805 if (!(dwceqos_read(lp
, REG_DWCEQOS_MAC_MDIO_ADDR
) &
806 DWCEQOS_MAC_MDIO_ADDR_GB
))
810 netdev_warn(lp
->ndev
, "MDIO write timed out\n");
814 static int dwceqos_ioctl(struct net_device
*ndev
, struct ifreq
*rq
, int cmd
)
816 struct net_local
*lp
= netdev_priv(ndev
);
817 struct phy_device
*phydev
= ndev
->phydev
;
819 if (!netif_running(ndev
))
829 return phy_mii_ioctl(phydev
, rq
, cmd
);
831 dev_info(&lp
->pdev
->dev
, "ioctl %X not implemented.\n", cmd
);
836 static void dwceqos_link_down(struct net_local
*lp
)
841 /* Indicate link down to the LPI state machine */
842 spin_lock_irqsave(&lp
->hw_lock
, flags
);
843 regval
= dwceqos_read(lp
, REG_DWCEQOS_MAC_LPI_CTRL_STATUS
);
844 regval
&= ~DWCEQOS_MAC_LPI_CTRL_STATUS_PLS
;
845 dwceqos_write(lp
, REG_DWCEQOS_MAC_LPI_CTRL_STATUS
, regval
);
846 spin_unlock_irqrestore(&lp
->hw_lock
, flags
);
849 static void dwceqos_link_up(struct net_local
*lp
)
851 struct net_device
*ndev
= lp
->ndev
;
855 /* Indicate link up to the LPI state machine */
856 spin_lock_irqsave(&lp
->hw_lock
, flags
);
857 regval
= dwceqos_read(lp
, REG_DWCEQOS_MAC_LPI_CTRL_STATUS
);
858 regval
|= DWCEQOS_MAC_LPI_CTRL_STATUS_PLS
;
859 dwceqos_write(lp
, REG_DWCEQOS_MAC_LPI_CTRL_STATUS
, regval
);
860 spin_unlock_irqrestore(&lp
->hw_lock
, flags
);
862 lp
->eee_active
= !phy_init_eee(ndev
->phydev
, 0);
864 /* Check for changed EEE capability */
865 if (!lp
->eee_active
&& lp
->eee_enabled
) {
868 spin_lock_irqsave(&lp
->hw_lock
, flags
);
869 regval
= dwceqos_read(lp
, REG_DWCEQOS_MAC_LPI_CTRL_STATUS
);
870 regval
&= ~DWCEQOS_LPI_CTRL_ENABLE_EEE
;
871 dwceqos_write(lp
, REG_DWCEQOS_MAC_LPI_CTRL_STATUS
, regval
);
872 spin_unlock_irqrestore(&lp
->hw_lock
, flags
);
876 static void dwceqos_set_speed(struct net_local
*lp
)
878 struct net_device
*ndev
= lp
->ndev
;
879 struct phy_device
*phydev
= ndev
->phydev
;
882 regval
= dwceqos_read(lp
, REG_DWCEQOS_MAC_CFG
);
883 regval
&= ~(DWCEQOS_MAC_CFG_PS
| DWCEQOS_MAC_CFG_FES
|
887 regval
|= DWCEQOS_MAC_CFG_DM
;
888 if (phydev
->speed
== SPEED_10
) {
889 regval
|= DWCEQOS_MAC_CFG_PS
;
890 } else if (phydev
->speed
== SPEED_100
) {
891 regval
|= DWCEQOS_MAC_CFG_PS
|
893 } else if (phydev
->speed
!= SPEED_1000
) {
895 "unknown PHY speed %d\n",
900 dwceqos_write(lp
, REG_DWCEQOS_MAC_CFG
, regval
);
903 static void dwceqos_adjust_link(struct net_device
*ndev
)
905 struct net_local
*lp
= netdev_priv(ndev
);
906 struct phy_device
*phydev
= ndev
->phydev
;
907 int status_change
= 0;
913 if ((lp
->speed
!= phydev
->speed
) ||
914 (lp
->duplex
!= phydev
->duplex
)) {
915 dwceqos_set_speed(lp
);
917 lp
->speed
= phydev
->speed
;
918 lp
->duplex
= phydev
->duplex
;
922 if (lp
->flowcontrol
.autoneg
) {
923 lp
->flowcontrol
.rx
= phydev
->pause
||
925 lp
->flowcontrol
.tx
= phydev
->pause
||
929 if (lp
->flowcontrol
.rx
!= lp
->flowcontrol
.rx_current
) {
930 if (netif_msg_link(lp
))
931 netdev_dbg(ndev
, "set rx flow to %d\n",
933 dwceqos_set_rx_flowcontrol(lp
, lp
->flowcontrol
.rx
);
934 lp
->flowcontrol
.rx_current
= lp
->flowcontrol
.rx
;
936 if (lp
->flowcontrol
.tx
!= lp
->flowcontrol
.tx_current
) {
937 if (netif_msg_link(lp
))
938 netdev_dbg(ndev
, "set tx flow to %d\n",
940 dwceqos_set_tx_flowcontrol(lp
, lp
->flowcontrol
.tx
);
941 lp
->flowcontrol
.tx_current
= lp
->flowcontrol
.tx
;
945 if (phydev
->link
!= lp
->link
) {
946 lp
->link
= phydev
->link
;
952 netif_trans_update(lp
->ndev
);
955 dwceqos_link_down(lp
);
957 phy_print_status(phydev
);
961 static int dwceqos_mii_probe(struct net_device
*ndev
)
963 struct net_local
*lp
= netdev_priv(ndev
);
964 struct phy_device
*phydev
= NULL
;
967 phydev
= of_phy_connect(lp
->ndev
,
969 &dwceqos_adjust_link
,
974 netdev_err(ndev
, "no PHY found\n");
978 netdev_err(ndev
, "no PHY configured\n");
982 if (netif_msg_probe(lp
))
983 phy_attached_info(phydev
);
985 phydev
->supported
&= PHY_GBIT_FEATURES
| SUPPORTED_Pause
|
986 SUPPORTED_Asym_Pause
;
990 lp
->duplex
= DUPLEX_UNKNOWN
;
991 lp
->flowcontrol
.autoneg
= AUTONEG_ENABLE
;
996 static void dwceqos_alloc_rxring_desc(struct net_local
*lp
, int index
)
998 struct sk_buff
*new_skb
;
999 dma_addr_t new_skb_baddr
= 0;
1001 new_skb
= netdev_alloc_skb(lp
->ndev
, DWCEQOS_RX_BUF_SIZE
);
1003 netdev_err(lp
->ndev
, "alloc_skb error for desc %d\n", index
);
1007 new_skb_baddr
= dma_map_single(lp
->ndev
->dev
.parent
,
1008 new_skb
->data
, DWCEQOS_RX_BUF_SIZE
,
1010 if (dma_mapping_error(lp
->ndev
->dev
.parent
, new_skb_baddr
)) {
1011 netdev_err(lp
->ndev
, "DMA map error\n");
1012 dev_kfree_skb(new_skb
);
1017 lp
->rx_descs
[index
].des0
= new_skb_baddr
;
1018 lp
->rx_descs
[index
].des1
= 0;
1019 lp
->rx_descs
[index
].des2
= 0;
1020 lp
->rx_descs
[index
].des3
= DWCEQOS_DMA_RDES3_INTE
|
1021 DWCEQOS_DMA_RDES3_BUF1V
|
1022 DWCEQOS_DMA_RDES3_OWN
;
1024 lp
->rx_skb
[index
].mapping
= new_skb_baddr
;
1025 lp
->rx_skb
[index
].len
= DWCEQOS_RX_BUF_SIZE
;
1028 lp
->rx_skb
[index
].skb
= new_skb
;
1031 static void dwceqos_clean_rings(struct net_local
*lp
)
1036 for (i
= 0; i
< DWCEQOS_RX_DCNT
; i
++) {
1037 if (lp
->rx_skb
[i
].skb
) {
1038 dma_unmap_single(lp
->ndev
->dev
.parent
,
1039 lp
->rx_skb
[i
].mapping
,
1043 dev_kfree_skb(lp
->rx_skb
[i
].skb
);
1044 lp
->rx_skb
[i
].skb
= NULL
;
1045 lp
->rx_skb
[i
].mapping
= 0;
1051 for (i
= 0; i
< DWCEQOS_TX_DCNT
; i
++) {
1052 if (lp
->tx_skb
[i
].skb
) {
1053 dev_kfree_skb(lp
->tx_skb
[i
].skb
);
1054 lp
->tx_skb
[i
].skb
= NULL
;
1056 if (lp
->tx_skb
[i
].mapping
) {
1057 dma_unmap_single(lp
->ndev
->dev
.parent
,
1058 lp
->tx_skb
[i
].mapping
,
1061 lp
->tx_skb
[i
].mapping
= 0;
1067 static void dwceqos_descriptor_free(struct net_local
*lp
)
1071 dwceqos_clean_rings(lp
);
1078 size
= DWCEQOS_RX_DCNT
* sizeof(struct dwceqos_dma_desc
);
1080 dma_free_coherent(lp
->ndev
->dev
.parent
, size
,
1081 (void *)(lp
->rx_descs
), lp
->rx_descs_addr
);
1082 lp
->rx_descs
= NULL
;
1085 size
= DWCEQOS_TX_DCNT
* sizeof(struct dwceqos_dma_desc
);
1087 dma_free_coherent(lp
->ndev
->dev
.parent
, size
,
1088 (void *)(lp
->tx_descs
), lp
->tx_descs_addr
);
1089 lp
->tx_descs
= NULL
;
1093 static int dwceqos_descriptor_init(struct net_local
*lp
)
1102 lp
->rx_descs
= NULL
;
1103 lp
->tx_descs
= NULL
;
1105 /* Reset the DMA indexes */
1109 lp
->tx_free
= DWCEQOS_TX_DCNT
;
1111 /* Allocate Ring descriptors */
1112 size
= DWCEQOS_RX_DCNT
* sizeof(struct ring_desc
);
1113 lp
->rx_skb
= kzalloc(size
, GFP_KERNEL
);
1117 size
= DWCEQOS_TX_DCNT
* sizeof(struct ring_desc
);
1118 lp
->tx_skb
= kzalloc(size
, GFP_KERNEL
);
1122 /* Allocate DMA descriptors */
1123 size
= DWCEQOS_RX_DCNT
* sizeof(struct dwceqos_dma_desc
);
1124 lp
->rx_descs
= dma_alloc_coherent(lp
->ndev
->dev
.parent
, size
,
1125 &lp
->rx_descs_addr
, GFP_KERNEL
);
1128 lp
->rx_descs_tail_addr
= lp
->rx_descs_addr
+
1129 sizeof(struct dwceqos_dma_desc
) * DWCEQOS_RX_DCNT
;
1131 size
= DWCEQOS_TX_DCNT
* sizeof(struct dwceqos_dma_desc
);
1132 lp
->tx_descs
= dma_alloc_coherent(lp
->ndev
->dev
.parent
, size
,
1133 &lp
->tx_descs_addr
, GFP_KERNEL
);
1136 lp
->tx_descs_tail_addr
= lp
->tx_descs_addr
+
1137 sizeof(struct dwceqos_dma_desc
) * DWCEQOS_TX_DCNT
;
1139 /* Initialize RX Ring Descriptors and buffers */
1140 for (i
= 0; i
< DWCEQOS_RX_DCNT
; ++i
) {
1141 dwceqos_alloc_rxring_desc(lp
, i
);
1142 if (!(lp
->rx_skb
[lp
->rx_cur
].skb
))
1146 /* Initialize TX Descriptors */
1147 for (i
= 0; i
< DWCEQOS_TX_DCNT
; ++i
) {
1148 lp
->tx_descs
[i
].des0
= 0;
1149 lp
->tx_descs
[i
].des1
= 0;
1150 lp
->tx_descs
[i
].des2
= 0;
1151 lp
->tx_descs
[i
].des3
= 0;
1154 /* Make descriptor writes visible to the DMA. */
1160 dwceqos_descriptor_free(lp
);
1164 static int dwceqos_packet_avail(struct net_local
*lp
)
1166 return !(lp
->rx_descs
[lp
->rx_cur
].des3
& DWCEQOS_DMA_RDES3_OWN
);
1169 static void dwceqos_get_hwfeatures(struct net_local
*lp
)
1171 lp
->feature0
= dwceqos_read(lp
, REG_DWCEQOS_MAC_HW_FEATURE0
);
1172 lp
->feature1
= dwceqos_read(lp
, REG_DWCEQOS_MAC_HW_FEATURE1
);
1173 lp
->feature2
= dwceqos_read(lp
, REG_DWCEQOS_MAC_HW_FEATURE2
);
1176 static void dwceqos_dma_enable_txirq(struct net_local
*lp
)
1179 unsigned long flags
;
1181 spin_lock_irqsave(&lp
->hw_lock
, flags
);
1182 regval
= dwceqos_read(lp
, REG_DWCEQOS_DMA_CH0_IE
);
1183 regval
|= DWCEQOS_DMA_CH0_IE_TIE
;
1184 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_IE
, regval
);
1185 spin_unlock_irqrestore(&lp
->hw_lock
, flags
);
1188 static void dwceqos_dma_disable_txirq(struct net_local
*lp
)
1191 unsigned long flags
;
1193 spin_lock_irqsave(&lp
->hw_lock
, flags
);
1194 regval
= dwceqos_read(lp
, REG_DWCEQOS_DMA_CH0_IE
);
1195 regval
&= ~DWCEQOS_DMA_CH0_IE_TIE
;
1196 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_IE
, regval
);
1197 spin_unlock_irqrestore(&lp
->hw_lock
, flags
);
1200 static void dwceqos_dma_enable_rxirq(struct net_local
*lp
)
1203 unsigned long flags
;
1205 spin_lock_irqsave(&lp
->hw_lock
, flags
);
1206 regval
= dwceqos_read(lp
, REG_DWCEQOS_DMA_CH0_IE
);
1207 regval
|= DWCEQOS_DMA_CH0_IE_RIE
;
1208 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_IE
, regval
);
1209 spin_unlock_irqrestore(&lp
->hw_lock
, flags
);
1212 static void dwceqos_dma_disable_rxirq(struct net_local
*lp
)
1215 unsigned long flags
;
1217 spin_lock_irqsave(&lp
->hw_lock
, flags
);
1218 regval
= dwceqos_read(lp
, REG_DWCEQOS_DMA_CH0_IE
);
1219 regval
&= ~DWCEQOS_DMA_CH0_IE_RIE
;
1220 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_IE
, regval
);
1221 spin_unlock_irqrestore(&lp
->hw_lock
, flags
);
1224 static void dwceqos_enable_mmc_interrupt(struct net_local
*lp
)
1226 dwceqos_write(lp
, REG_DWCEQOS_MMC_RXIRQMASK
, 0);
1227 dwceqos_write(lp
, REG_DWCEQOS_MMC_TXIRQMASK
, 0);
1230 static int dwceqos_mii_init(struct net_local
*lp
)
1233 struct resource res
;
1234 struct device_node
*mdionode
;
1236 mdionode
= of_get_child_by_name(lp
->pdev
->dev
.of_node
, "mdio");
1241 lp
->mii_bus
= mdiobus_alloc();
1247 lp
->mii_bus
->name
= "DWCEQOS MII bus";
1248 lp
->mii_bus
->read
= &dwceqos_mdio_read
;
1249 lp
->mii_bus
->write
= &dwceqos_mdio_write
;
1250 lp
->mii_bus
->priv
= lp
;
1251 lp
->mii_bus
->parent
= &lp
->pdev
->dev
;
1253 of_address_to_resource(lp
->pdev
->dev
.of_node
, 0, &res
);
1254 snprintf(lp
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%.8llx",
1255 (unsigned long long)res
.start
);
1256 if (of_mdiobus_register(lp
->mii_bus
, mdionode
))
1257 goto err_out_free_mdiobus
;
1261 err_out_free_mdiobus
:
1262 mdiobus_free(lp
->mii_bus
);
1264 of_node_put(mdionode
);
1268 /* DMA reset. When issued also resets all MTL and MAC registers as well */
1269 static void dwceqos_reset_hw(struct net_local
*lp
)
1271 /* Wait (at most) 0.5 seconds for DMA reset*/
1275 /* Force gigabit to guarantee a TX clock for GMII. */
1276 reg
= dwceqos_read(lp
, REG_DWCEQOS_MAC_CFG
);
1277 reg
&= ~(DWCEQOS_MAC_CFG_PS
| DWCEQOS_MAC_CFG_FES
);
1278 reg
|= DWCEQOS_MAC_CFG_DM
;
1279 dwceqos_write(lp
, REG_DWCEQOS_MAC_CFG
, reg
);
1281 dwceqos_write(lp
, REG_DWCEQOS_DMA_MODE
, DWCEQOS_DMA_MODE_SWR
);
1286 reg
= dwceqos_read(lp
, REG_DWCEQOS_DMA_MODE
);
1287 } while ((reg
& DWCEQOS_DMA_MODE_SWR
) && i
);
1288 /* We might experience a timeout if the chip clock mux is broken */
1290 netdev_err(lp
->ndev
, "DMA reset timed out!\n");
1293 static void dwceqos_fatal_bus_error(struct net_local
*lp
, u32 dma_status
)
1295 if (dma_status
& DWCEQOS_DMA_CH0_IS_TEB
) {
1296 netdev_err(lp
->ndev
, "txdma bus error %s %s (status=%08x)\n",
1297 dma_status
& DWCEQOS_DMA_CH0_IS_TX_ERR_READ
?
1299 dma_status
& DWCEQOS_DMA_CH0_IS_TX_ERR_DESCR
?
1305 if (dma_status
& DWCEQOS_DMA_CH0_IS_REB
) {
1306 netdev_err(lp
->ndev
, "rxdma bus error %s %s (status=%08x)\n",
1307 dma_status
& DWCEQOS_DMA_CH0_IS_RX_ERR_READ
?
1309 dma_status
& DWCEQOS_DMA_CH0_IS_RX_ERR_DESCR
?
1317 static void dwceqos_mmc_interrupt(struct net_local
*lp
)
1319 unsigned long flags
;
1321 spin_lock_irqsave(&lp
->stats_lock
, flags
);
1323 /* A latched mmc interrupt can not be masked, we must read
1324 * all the counters with an interrupt pending.
1326 dwceqos_read_mmc_counters(lp
,
1327 dwceqos_read(lp
, REG_DWCEQOS_MMC_RXIRQ
),
1328 dwceqos_read(lp
, REG_DWCEQOS_MMC_TXIRQ
));
1330 spin_unlock_irqrestore(&lp
->stats_lock
, flags
);
1333 static void dwceqos_mac_interrupt(struct net_local
*lp
)
1337 cause
= dwceqos_read(lp
, REG_DWCEQOS_MAC_IS
);
1339 if (cause
& DWCEQOS_MAC_IS_MMC_INT
)
1340 dwceqos_mmc_interrupt(lp
);
1343 static irqreturn_t
dwceqos_interrupt(int irq
, void *dev_id
)
1345 struct net_device
*ndev
= dev_id
;
1346 struct net_local
*lp
= netdev_priv(ndev
);
1350 irqreturn_t ret
= IRQ_NONE
;
1352 cause
= dwceqos_read(lp
, REG_DWCEQOS_DMA_IS
);
1353 /* DMA Channel 0 Interrupt */
1354 if (cause
& DWCEQOS_DMA_IS_DC0IS
) {
1355 dma_status
= dwceqos_read(lp
, REG_DWCEQOS_DMA_CH0_STA
);
1357 /* Transmit Interrupt */
1358 if (dma_status
& DWCEQOS_DMA_CH0_IS_TI
) {
1359 tasklet_schedule(&lp
->tx_bdreclaim_tasklet
);
1360 dwceqos_dma_disable_txirq(lp
);
1363 /* Receive Interrupt */
1364 if (dma_status
& DWCEQOS_DMA_CH0_IS_RI
) {
1365 /* Disable RX IRQs */
1366 dwceqos_dma_disable_rxirq(lp
);
1367 napi_schedule(&lp
->napi
);
1370 /* Fatal Bus Error interrupt */
1371 if (unlikely(dma_status
& DWCEQOS_DMA_CH0_IS_FBE
)) {
1372 dwceqos_fatal_bus_error(lp
, dma_status
);
1374 /* errata 9000831707 */
1375 dma_status
|= DWCEQOS_DMA_CH0_IS_TEB
|
1376 DWCEQOS_DMA_CH0_IS_REB
;
1379 /* Ack all DMA Channel 0 IRQs */
1380 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_STA
, dma_status
);
1384 if (cause
& DWCEQOS_DMA_IS_MTLIS
) {
1385 u32 val
= dwceqos_read(lp
, REG_DWCEQOS_MTL_Q0_ISCTRL
);
1387 dwceqos_write(lp
, REG_DWCEQOS_MTL_Q0_ISCTRL
, val
);
1391 if (cause
& DWCEQOS_DMA_IS_MACIS
) {
1392 dwceqos_mac_interrupt(lp
);
1398 static void dwceqos_set_rx_flowcontrol(struct net_local
*lp
, bool enable
)
1401 unsigned long flags
;
1403 spin_lock_irqsave(&lp
->hw_lock
, flags
);
1405 regval
= dwceqos_read(lp
, REG_DWCEQOS_MAC_RX_FLOW_CTRL
);
1407 regval
|= DWCEQOS_MAC_RX_FLOW_CTRL_RFE
;
1409 regval
&= ~DWCEQOS_MAC_RX_FLOW_CTRL_RFE
;
1410 dwceqos_write(lp
, REG_DWCEQOS_MAC_RX_FLOW_CTRL
, regval
);
1412 spin_unlock_irqrestore(&lp
->hw_lock
, flags
);
1415 static void dwceqos_set_tx_flowcontrol(struct net_local
*lp
, bool enable
)
1418 unsigned long flags
;
1420 spin_lock_irqsave(&lp
->hw_lock
, flags
);
1422 /* MTL flow control */
1423 regval
= dwceqos_read(lp
, REG_DWCEQOS_MTL_RXQ0_OPER
);
1425 regval
|= DWCEQOS_MTL_RXQ_EHFC
;
1427 regval
&= ~DWCEQOS_MTL_RXQ_EHFC
;
1429 dwceqos_write(lp
, REG_DWCEQOS_MTL_RXQ0_OPER
, regval
);
1431 /* MAC flow control */
1432 regval
= dwceqos_read(lp
, REG_DWCEQOS_MAC_Q0_TX_FLOW
);
1434 regval
|= DWCEQOS_MAC_Q0_TX_FLOW_TFE
;
1436 regval
&= ~DWCEQOS_MAC_Q0_TX_FLOW_TFE
;
1437 dwceqos_write(lp
, REG_DWCEQOS_MAC_Q0_TX_FLOW
, regval
);
1439 spin_unlock_irqrestore(&lp
->hw_lock
, flags
);
1442 static void dwceqos_configure_flow_control(struct net_local
*lp
)
1445 unsigned long flags
;
1448 spin_lock_irqsave(&lp
->hw_lock
, flags
);
1450 regval
= dwceqos_read(lp
, REG_DWCEQOS_MTL_RXQ0_OPER
);
1452 /* The queue size is in units of 256 bytes. We want 512 bytes units for
1453 * the threshold fields.
1455 RQS
= ((regval
>> 20) & 0x3FF) + 1;
1458 /* The thresholds are relative to a full queue, with a bias
1459 * of 1 KiByte below full.
1464 regval
= (regval
& 0xFFF000FF) | (RFD
<< 14) | (RFA
<< 8);
1466 if (RFD
>= 0 && RFA
>= 0) {
1467 dwceqos_write(lp
, REG_DWCEQOS_MTL_RXQ0_OPER
, regval
);
1469 netdev_warn(lp
->ndev
,
1470 "FIFO too small for flow control.");
1473 regval
= DWCEQOS_MAC_Q0_TX_FLOW_PT(256) |
1474 DWCEQOS_MAC_Q0_TX_FLOW_PLT_4_SLOTS
;
1476 dwceqos_write(lp
, REG_DWCEQOS_MAC_Q0_TX_FLOW
, regval
);
1478 spin_unlock_irqrestore(&lp
->hw_lock
, flags
);
1481 static void dwceqos_configure_clock(struct net_local
*lp
)
1483 unsigned long rate_mhz
= clk_get_rate(lp
->apb_pclk
) / 1000000;
1488 REG_DWCEQOS_MAC_1US_TIC_COUNTER
,
1489 DWCEQOS_MAC_1US_TIC_COUNTER_VAL(rate_mhz
- 1));
1492 static void dwceqos_configure_bus(struct net_local
*lp
)
1496 /* N.B. We do not support the Fixed Burst mode because it
1497 * opens a race window by making HW access to DMA descriptors
1501 sysbus_reg
= DWCEQOS_DMA_SYSBUS_MODE_AAL
;
1503 if (lp
->bus_cfg
.en_lpi
)
1504 sysbus_reg
|= DWCEQOS_DMA_SYSBUS_MODE_EN_LPI
;
1506 if (lp
->bus_cfg
.burst_map
)
1507 sysbus_reg
|= DWCEQOS_DMA_SYSBUS_MODE_BURST(
1508 lp
->bus_cfg
.burst_map
);
1510 sysbus_reg
|= DWCEQOS_DMA_SYSBUS_MODE_BURST(
1511 DWCEQOS_DMA_SYSBUS_MODE_BURST_DEFAULT
);
1513 if (lp
->bus_cfg
.read_requests
)
1514 sysbus_reg
|= DWCEQOS_DMA_SYSBUS_MODE_RD_OSR_LIMIT(
1515 lp
->bus_cfg
.read_requests
- 1);
1517 sysbus_reg
|= DWCEQOS_DMA_SYSBUS_MODE_RD_OSR_LIMIT(
1518 DWCEQOS_DMA_SYSBUS_MODE_RD_OSR_LIMIT_DEFAULT
);
1520 if (lp
->bus_cfg
.write_requests
)
1521 sysbus_reg
|= DWCEQOS_DMA_SYSBUS_MODE_WR_OSR_LIMIT(
1522 lp
->bus_cfg
.write_requests
- 1);
1524 sysbus_reg
|= DWCEQOS_DMA_SYSBUS_MODE_WR_OSR_LIMIT(
1525 DWCEQOS_DMA_SYSBUS_MODE_WR_OSR_LIMIT_DEFAULT
);
1527 if (netif_msg_hw(lp
))
1528 netdev_dbg(lp
->ndev
, "SysbusMode %#X\n", sysbus_reg
);
1530 dwceqos_write(lp
, REG_DWCEQOS_DMA_SYSBUS_MODE
, sysbus_reg
);
1533 static void dwceqos_init_hw(struct net_local
*lp
)
1535 struct net_device
*ndev
= lp
->ndev
;
1540 /* Software reset */
1541 dwceqos_reset_hw(lp
);
1543 dwceqos_configure_bus(lp
);
1545 /* Probe data bus width, 32/64/128 bits. */
1546 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_TXDESC_TAIL
, 0xF);
1547 regval
= dwceqos_read(lp
, REG_DWCEQOS_DMA_CH0_TXDESC_TAIL
);
1548 buswidth
= (regval
^ 0xF) + 1;
1550 /* Cache-align dma descriptors. */
1551 dma_skip
= (sizeof(struct dwceqos_dma_desc
) - 16) / buswidth
;
1552 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_CTRL
,
1553 DWCEQOS_DMA_CH_CTRL_DSL(dma_skip
) |
1554 DWCEQOS_DMA_CH_CTRL_PBLX8
);
1556 /* Initialize DMA Channel 0 */
1557 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_TXDESC_LEN
, DWCEQOS_TX_DCNT
- 1);
1558 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_RXDESC_LEN
, DWCEQOS_RX_DCNT
- 1);
1559 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_TXDESC_LIST
,
1560 (u32
)lp
->tx_descs_addr
);
1561 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_RXDESC_LIST
,
1562 (u32
)lp
->rx_descs_addr
);
1564 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_TXDESC_TAIL
,
1565 lp
->tx_descs_tail_addr
);
1566 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_RXDESC_TAIL
,
1567 lp
->rx_descs_tail_addr
);
1569 if (lp
->bus_cfg
.tx_pbl
)
1570 regval
= DWCEQOS_DMA_CH_CTRL_PBL(lp
->bus_cfg
.tx_pbl
);
1572 regval
= DWCEQOS_DMA_CH_CTRL_PBL(2);
1574 /* Enable TSO if the HW support it */
1575 if (lp
->feature1
& DWCEQOS_MAC_HW_FEATURE1_TSOEN
)
1576 regval
|= DWCEQOS_DMA_CH_TX_TSE
;
1578 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_TX_CTRL
, regval
);
1580 if (lp
->bus_cfg
.rx_pbl
)
1581 regval
= DWCEQOS_DMA_CH_CTRL_PBL(lp
->bus_cfg
.rx_pbl
);
1583 regval
= DWCEQOS_DMA_CH_CTRL_PBL(2);
1585 regval
|= DWCEQOS_DMA_CH_RX_CTRL_BUFSIZE(DWCEQOS_DWCEQOS_RX_BUF_SIZE
);
1586 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_RX_CTRL
, regval
);
1588 regval
|= DWCEQOS_DMA_CH_CTRL_START
;
1589 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_RX_CTRL
, regval
);
1591 /* Initialize MTL Queues */
1592 regval
= DWCEQOS_MTL_SCHALG_STRICT
;
1593 dwceqos_write(lp
, REG_DWCEQOS_MTL_OPER
, regval
);
1595 regval
= DWCEQOS_MTL_TXQ_SIZE(
1596 DWCEQOS_MAC_HW_FEATURE1_TXFIFOSIZE(lp
->feature1
)) |
1597 DWCEQOS_MTL_TXQ_TXQEN
| DWCEQOS_MTL_TXQ_TSF
|
1598 DWCEQOS_MTL_TXQ_TTC512
;
1599 dwceqos_write(lp
, REG_DWCEQOS_MTL_TXQ0_OPER
, regval
);
1601 regval
= DWCEQOS_MTL_RXQ_SIZE(
1602 DWCEQOS_MAC_HW_FEATURE1_RXFIFOSIZE(lp
->feature1
)) |
1603 DWCEQOS_MTL_RXQ_FUP
| DWCEQOS_MTL_RXQ_FEP
| DWCEQOS_MTL_RXQ_RSF
;
1604 dwceqos_write(lp
, REG_DWCEQOS_MTL_RXQ0_OPER
, regval
);
1606 dwceqos_configure_flow_control(lp
);
1608 /* Initialize MAC */
1609 dwceqos_set_umac_addr(lp
, lp
->ndev
->dev_addr
, 0);
1611 lp
->eee_enabled
= 0;
1613 dwceqos_configure_clock(lp
);
1617 /* probe implemented counters */
1618 dwceqos_write(lp
, REG_DWCEQOS_MMC_RXIRQMASK
, ~0u);
1619 dwceqos_write(lp
, REG_DWCEQOS_MMC_TXIRQMASK
, ~0u);
1620 lp
->mmc_rx_counters_mask
= dwceqos_read(lp
, REG_DWCEQOS_MMC_RXIRQMASK
);
1621 lp
->mmc_tx_counters_mask
= dwceqos_read(lp
, REG_DWCEQOS_MMC_TXIRQMASK
);
1623 dwceqos_write(lp
, REG_DWCEQOS_MMC_CTRL
, DWCEQOS_MMC_CTRL_CNTRST
|
1624 DWCEQOS_MMC_CTRL_RSTONRD
);
1625 dwceqos_enable_mmc_interrupt(lp
);
1627 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_IE
, 0);
1628 dwceqos_write(lp
, REG_DWCEQOS_MAC_IE
, 0);
1630 dwceqos_write(lp
, REG_DWCEQOS_MAC_CFG
, DWCEQOS_MAC_CFG_IPC
|
1631 DWCEQOS_MAC_CFG_DM
| DWCEQOS_MAC_CFG_TE
| DWCEQOS_MAC_CFG_RE
);
1634 regval
= dwceqos_read(lp
, REG_DWCEQOS_DMA_CH0_TX_CTRL
);
1635 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_TX_CTRL
,
1636 regval
| DWCEQOS_DMA_CH_CTRL_START
);
1638 /* Enable MAC TX/RX */
1639 regval
= dwceqos_read(lp
, REG_DWCEQOS_MAC_CFG
);
1640 dwceqos_write(lp
, REG_DWCEQOS_MAC_CFG
,
1641 regval
| DWCEQOS_MAC_CFG_TE
| DWCEQOS_MAC_CFG_RE
);
1643 lp
->phy_defer
= false;
1644 mutex_lock(&ndev
->phydev
->lock
);
1645 phy_read_status(ndev
->phydev
);
1646 dwceqos_adjust_link(lp
->ndev
);
1647 mutex_unlock(&ndev
->phydev
->lock
);
1650 static void dwceqos_tx_reclaim(unsigned long data
)
1652 struct net_device
*ndev
= (struct net_device
*)data
;
1653 struct net_local
*lp
= netdev_priv(ndev
);
1654 unsigned int tx_bytes
= 0;
1655 unsigned int tx_packets
= 0;
1657 spin_lock(&lp
->tx_lock
);
1659 while (lp
->tx_free
< DWCEQOS_TX_DCNT
) {
1660 struct dwceqos_dma_desc
*dd
= &lp
->tx_descs
[lp
->tx_cur
];
1661 struct ring_desc
*rd
= &lp
->tx_skb
[lp
->tx_cur
];
1663 /* Descriptor still being held by DMA ? */
1664 if (dd
->des3
& DWCEQOS_DMA_TDES3_OWN
)
1668 dma_unmap_single(ndev
->dev
.parent
, rd
->mapping
, rd
->len
,
1671 if (unlikely(rd
->skb
)) {
1673 tx_bytes
+= rd
->skb
->len
;
1674 dev_consume_skb_any(rd
->skb
);
1680 lp
->tx_cur
= (lp
->tx_cur
+ 1) % DWCEQOS_TX_DCNT
;
1682 if ((dd
->des3
& DWCEQOS_DMA_TDES3_LD
) &&
1683 (dd
->des3
& DWCEQOS_DMA_RDES3_ES
)) {
1684 if (netif_msg_tx_err(lp
))
1685 netdev_err(ndev
, "TX Error, TDES3 = 0x%x\n",
1687 if (netif_msg_hw(lp
))
1691 spin_unlock(&lp
->tx_lock
);
1693 netdev_completed_queue(ndev
, tx_packets
, tx_bytes
);
1695 dwceqos_dma_enable_txirq(lp
);
1696 netif_wake_queue(ndev
);
1699 static int dwceqos_rx(struct net_local
*lp
, int budget
)
1701 struct sk_buff
*skb
;
1703 unsigned int n_packets
= 0;
1704 unsigned int n_descs
= 0;
1707 struct dwceqos_dma_desc
*dd
;
1708 struct sk_buff
*new_skb
;
1709 dma_addr_t new_skb_baddr
= 0;
1711 while (n_descs
< budget
) {
1712 if (!dwceqos_packet_avail(lp
))
1715 new_skb
= netdev_alloc_skb(lp
->ndev
, DWCEQOS_RX_BUF_SIZE
);
1717 netdev_err(lp
->ndev
, "no memory for new sk_buff\n");
1721 /* Get dma handle of skb->data */
1722 new_skb_baddr
= (u32
)dma_map_single(lp
->ndev
->dev
.parent
,
1724 DWCEQOS_RX_BUF_SIZE
,
1726 if (dma_mapping_error(lp
->ndev
->dev
.parent
, new_skb_baddr
)) {
1727 netdev_err(lp
->ndev
, "DMA map error\n");
1728 dev_kfree_skb(new_skb
);
1732 /* Read descriptor data after reading owner bit. */
1735 dd
= &lp
->rx_descs
[lp
->rx_cur
];
1736 len
= DWCEQOS_DMA_RDES3_PL(dd
->des3
);
1737 skb
= lp
->rx_skb
[lp
->rx_cur
].skb
;
1739 /* Unmap old buffer */
1740 dma_unmap_single(lp
->ndev
->dev
.parent
,
1741 lp
->rx_skb
[lp
->rx_cur
].mapping
,
1742 lp
->rx_skb
[lp
->rx_cur
].len
, DMA_FROM_DEVICE
);
1744 /* Discard packet on reception error or bad checksum */
1745 if ((dd
->des3
& DWCEQOS_DMA_RDES3_ES
) ||
1746 (dd
->des1
& DWCEQOS_DMA_RDES1_IPCE
)) {
1751 skb
->protocol
= eth_type_trans(skb
, lp
->ndev
);
1752 switch (dd
->des1
& DWCEQOS_DMA_RDES1_PT
) {
1753 case DWCEQOS_DMA_RDES1_PT_UDP
:
1754 case DWCEQOS_DMA_RDES1_PT_TCP
:
1755 case DWCEQOS_DMA_RDES1_PT_ICMP
:
1756 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1759 skb
->ip_summed
= CHECKSUM_NONE
;
1764 if (unlikely(!skb
)) {
1765 if (netif_msg_rx_err(lp
))
1766 netdev_dbg(lp
->ndev
, "rx error: des3=%X\n",
1767 lp
->rx_descs
[lp
->rx_cur
].des3
);
1769 tot_size
+= skb
->len
;
1772 netif_receive_skb(skb
);
1775 lp
->rx_descs
[lp
->rx_cur
].des0
= new_skb_baddr
;
1776 lp
->rx_descs
[lp
->rx_cur
].des1
= 0;
1777 lp
->rx_descs
[lp
->rx_cur
].des2
= 0;
1778 /* The DMA must observe des0/1/2 written before des3. */
1780 lp
->rx_descs
[lp
->rx_cur
].des3
= DWCEQOS_DMA_RDES3_INTE
|
1781 DWCEQOS_DMA_RDES3_OWN
|
1782 DWCEQOS_DMA_RDES3_BUF1V
;
1784 lp
->rx_skb
[lp
->rx_cur
].mapping
= new_skb_baddr
;
1785 lp
->rx_skb
[lp
->rx_cur
].len
= DWCEQOS_RX_BUF_SIZE
;
1786 lp
->rx_skb
[lp
->rx_cur
].skb
= new_skb
;
1789 lp
->rx_cur
= (lp
->rx_cur
+ 1) % DWCEQOS_RX_DCNT
;
1792 /* Make sure any ownership update is written to the descriptors before
1797 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_STA
, DWCEQOS_DMA_CH0_IS_RI
);
1798 /* Wake up RX by writing tail pointer */
1799 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_RXDESC_TAIL
,
1800 lp
->rx_descs_tail_addr
);
1805 static int dwceqos_rx_poll(struct napi_struct
*napi
, int budget
)
1807 struct net_local
*lp
= container_of(napi
, struct net_local
, napi
);
1810 work_done
= dwceqos_rx(lp
, budget
- work_done
);
1812 if (!dwceqos_packet_avail(lp
) && work_done
< budget
) {
1813 napi_complete(napi
);
1814 dwceqos_dma_enable_rxirq(lp
);
1822 /* Reinitialize function if a TX timed out */
1823 static void dwceqos_reinit_for_txtimeout(struct work_struct
*data
)
1825 struct net_local
*lp
= container_of(data
, struct net_local
,
1828 netdev_err(lp
->ndev
, "transmit timeout %d s, resetting...\n",
1829 DWCEQOS_TX_TIMEOUT
);
1831 if (netif_msg_hw(lp
))
1835 dwceqos_stop(lp
->ndev
);
1836 dwceqos_open(lp
->ndev
);
1840 /* DT Probing function called by main probe */
1841 static inline int dwceqos_probe_config_dt(struct platform_device
*pdev
)
1843 struct net_device
*ndev
;
1844 struct net_local
*lp
;
1845 const void *mac_address
;
1846 struct dwceqos_bus_cfg
*bus_cfg
;
1847 struct device_node
*np
= pdev
->dev
.of_node
;
1849 ndev
= platform_get_drvdata(pdev
);
1850 lp
= netdev_priv(ndev
);
1851 bus_cfg
= &lp
->bus_cfg
;
1853 /* Set the MAC address. */
1854 mac_address
= of_get_mac_address(pdev
->dev
.of_node
);
1856 ether_addr_copy(ndev
->dev_addr
, mac_address
);
1858 /* These are all optional parameters */
1859 lp
->en_tx_lpi_clockgating
= of_property_read_bool(np
,
1860 "snps,en-tx-lpi-clockgating");
1861 bus_cfg
->en_lpi
= of_property_read_bool(np
, "snps,en-lpi");
1862 of_property_read_u32(np
, "snps,write-requests",
1863 &bus_cfg
->write_requests
);
1864 of_property_read_u32(np
, "snps,read-requests", &bus_cfg
->read_requests
);
1865 of_property_read_u32(np
, "snps,burst-map", &bus_cfg
->burst_map
);
1866 of_property_read_u32(np
, "snps,txpbl", &bus_cfg
->tx_pbl
);
1867 of_property_read_u32(np
, "snps,rxpbl", &bus_cfg
->rx_pbl
);
1869 netdev_dbg(ndev
, "BusCfg: lpi:%u wr:%u rr:%u bm:%X rxpbl:%u txpbl:%d\n",
1871 bus_cfg
->write_requests
,
1872 bus_cfg
->read_requests
,
1880 static int dwceqos_open(struct net_device
*ndev
)
1882 struct net_local
*lp
= netdev_priv(ndev
);
1885 dwceqos_reset_state(lp
);
1886 res
= dwceqos_descriptor_init(lp
);
1888 netdev_err(ndev
, "Unable to allocate DMA memory, rc %d\n", res
);
1891 netdev_reset_queue(ndev
);
1893 /* The dwceqos reset state machine requires all phy clocks to complete,
1894 * hence the unusual init order with phy_start first.
1896 lp
->phy_defer
= true;
1897 phy_start(ndev
->phydev
);
1898 dwceqos_init_hw(lp
);
1899 napi_enable(&lp
->napi
);
1901 netif_start_queue(ndev
);
1902 tasklet_enable(&lp
->tx_bdreclaim_tasklet
);
1904 /* Enable Interrupts -- do this only after we enable NAPI and the
1907 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_IE
,
1908 DWCEQOS_DMA_CH0_IE_NIE
|
1909 DWCEQOS_DMA_CH0_IE_RIE
| DWCEQOS_DMA_CH0_IE_TIE
|
1910 DWCEQOS_DMA_CH0_IE_AIE
|
1911 DWCEQOS_DMA_CH0_IE_FBEE
);
1916 static bool dweqos_is_tx_dma_suspended(struct net_local
*lp
)
1920 reg
= dwceqos_read(lp
, REG_DWCEQOS_DMA_DEBUG_ST0
);
1921 reg
= DMA_GET_TX_STATE_CH0(reg
);
1923 return reg
== DMA_TX_CH_SUSPENDED
;
1926 static void dwceqos_drain_dma(struct net_local
*lp
)
1928 /* Wait for all pending TX buffers to be sent. Upper limit based
1929 * on max frame size on a 10 Mbit link.
1931 size_t limit
= (DWCEQOS_TX_DCNT
* 1250) / 100;
1933 while (!dweqos_is_tx_dma_suspended(lp
) && limit
--)
1934 usleep_range(100, 200);
1937 static int dwceqos_stop(struct net_device
*ndev
)
1939 struct net_local
*lp
= netdev_priv(ndev
);
1941 tasklet_disable(&lp
->tx_bdreclaim_tasklet
);
1942 napi_disable(&lp
->napi
);
1944 /* Stop all tx before we drain the tx dma. */
1945 netif_tx_lock_bh(lp
->ndev
);
1946 netif_stop_queue(ndev
);
1947 netif_tx_unlock_bh(lp
->ndev
);
1949 dwceqos_drain_dma(lp
);
1950 dwceqos_reset_hw(lp
);
1951 phy_stop(ndev
->phydev
);
1953 dwceqos_descriptor_free(lp
);
1958 static void dwceqos_dmadesc_set_ctx(struct net_local
*lp
,
1959 unsigned short gso_size
)
1961 struct dwceqos_dma_desc
*dd
= &lp
->tx_descs
[lp
->tx_next
];
1965 dd
->des2
= gso_size
;
1966 dd
->des3
= DWCEQOS_DMA_TDES3_CTXT
| DWCEQOS_DMA_TDES3_TCMSSV
;
1968 lp
->tx_next
= (lp
->tx_next
+ 1) % DWCEQOS_TX_DCNT
;
1971 static void dwceqos_tx_poll_demand(struct net_local
*lp
)
1973 dwceqos_write(lp
, REG_DWCEQOS_DMA_CH0_TXDESC_TAIL
,
1974 lp
->tx_descs_tail_addr
);
1978 size_t nr_descriptors
;
1979 size_t initial_descriptor
;
1980 size_t last_descriptor
;
1981 size_t prev_gso_size
;
1982 size_t network_header_len
;
1985 static void dwceqos_tx_prepare(struct sk_buff
*skb
, struct net_local
*lp
,
1986 struct dwceqos_tx
*tx
)
1991 if (skb_is_gso(skb
) && skb_shinfo(skb
)->gso_size
!= lp
->gso_size
)
1994 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; ++i
) {
1995 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1997 n
+= (skb_frag_size(frag
) + BYTES_PER_DMA_DESC
- 1) /
2001 tx
->nr_descriptors
= n
;
2002 tx
->initial_descriptor
= lp
->tx_next
;
2003 tx
->last_descriptor
= lp
->tx_next
;
2004 tx
->prev_gso_size
= lp
->gso_size
;
2006 tx
->network_header_len
= skb_transport_offset(skb
);
2007 if (skb_is_gso(skb
))
2008 tx
->network_header_len
+= tcp_hdrlen(skb
);
2011 static int dwceqos_tx_linear(struct sk_buff
*skb
, struct net_local
*lp
,
2012 struct dwceqos_tx
*tx
)
2014 struct ring_desc
*rd
;
2015 struct dwceqos_dma_desc
*dd
;
2017 dma_addr_t dma_handle
;
2019 if (skb_is_gso(skb
) && skb_shinfo(skb
)->gso_size
!= lp
->gso_size
) {
2020 dwceqos_dmadesc_set_ctx(lp
, skb_shinfo(skb
)->gso_size
);
2021 lp
->gso_size
= skb_shinfo(skb
)->gso_size
;
2024 dma_handle
= dma_map_single(lp
->ndev
->dev
.parent
, skb
->data
,
2025 skb_headlen(skb
), DMA_TO_DEVICE
);
2027 if (dma_mapping_error(lp
->ndev
->dev
.parent
, dma_handle
)) {
2028 netdev_err(lp
->ndev
, "TX DMA Mapping error\n");
2032 rd
= &lp
->tx_skb
[lp
->tx_next
];
2033 dd
= &lp
->tx_descs
[lp
->tx_next
];
2036 rd
->len
= skb_headlen(skb
);
2037 rd
->mapping
= dma_handle
;
2039 /* Set up DMA Descriptor */
2040 dd
->des0
= dma_handle
;
2042 if (skb_is_gso(skb
)) {
2043 payload_len
= skb_headlen(skb
) - tx
->network_header_len
;
2046 dd
->des1
= dma_handle
+ tx
->network_header_len
;
2047 dd
->des2
= tx
->network_header_len
|
2048 DWCEQOS_DMA_DES2_B2L(payload_len
);
2049 dd
->des3
= DWCEQOS_DMA_TDES3_TSE
|
2050 DWCEQOS_DMA_DES3_THL((tcp_hdrlen(skb
) / 4)) |
2051 (skb
->len
- tx
->network_header_len
);
2054 dd
->des2
= skb_headlen(skb
);
2055 dd
->des3
= skb
->len
;
2057 switch (skb
->ip_summed
) {
2058 case CHECKSUM_PARTIAL
:
2059 dd
->des3
|= DWCEQOS_DMA_TDES3_CA
;
2061 case CHECKSUM_UNNECESSARY
:
2062 case CHECKSUM_COMPLETE
:
2068 dd
->des3
|= DWCEQOS_DMA_TDES3_FD
;
2069 if (lp
->tx_next
!= tx
->initial_descriptor
)
2070 dd
->des3
|= DWCEQOS_DMA_TDES3_OWN
;
2072 tx
->last_descriptor
= lp
->tx_next
;
2073 lp
->tx_next
= (lp
->tx_next
+ 1) % DWCEQOS_TX_DCNT
;
2078 static int dwceqos_tx_frags(struct sk_buff
*skb
, struct net_local
*lp
,
2079 struct dwceqos_tx
*tx
)
2081 struct ring_desc
*rd
= NULL
;
2082 struct dwceqos_dma_desc
*dd
;
2083 dma_addr_t dma_handle
;
2086 /* Setup more ring and DMA descriptor if the packet is fragmented */
2087 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; ++i
) {
2088 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
2090 size_t consumed_size
;
2093 dma_handle
= skb_frag_dma_map(lp
->ndev
->dev
.parent
, frag
, 0,
2094 skb_frag_size(frag
),
2096 if (dma_mapping_error(lp
->ndev
->dev
.parent
, dma_handle
)) {
2097 netdev_err(lp
->ndev
, "DMA Mapping error\n");
2101 /* order-3 fragments span more than one descriptor. */
2102 frag_size
= skb_frag_size(frag
);
2104 while (consumed_size
< frag_size
) {
2105 size_t dma_size
= min_t(size_t, 16376,
2106 frag_size
- consumed_size
);
2108 rd
= &lp
->tx_skb
[lp
->tx_next
];
2109 memset(rd
, 0, sizeof(*rd
));
2111 dd
= &lp
->tx_descs
[lp
->tx_next
];
2113 /* Set DMA Descriptor fields */
2114 dd
->des0
= dma_handle
+ consumed_size
;
2116 dd
->des2
= dma_size
;
2118 if (skb_is_gso(skb
))
2119 dd
->des3
= (skb
->len
- tx
->network_header_len
);
2121 dd
->des3
= skb
->len
;
2123 dd
->des3
|= DWCEQOS_DMA_TDES3_OWN
;
2125 tx
->last_descriptor
= lp
->tx_next
;
2126 lp
->tx_next
= (lp
->tx_next
+ 1) % DWCEQOS_TX_DCNT
;
2127 consumed_size
+= dma_size
;
2130 rd
->len
= skb_frag_size(frag
);
2131 rd
->mapping
= dma_handle
;
2137 static void dwceqos_tx_finalize(struct sk_buff
*skb
, struct net_local
*lp
,
2138 struct dwceqos_tx
*tx
)
2140 lp
->tx_descs
[tx
->last_descriptor
].des3
|= DWCEQOS_DMA_TDES3_LD
;
2141 lp
->tx_descs
[tx
->last_descriptor
].des2
|= DWCEQOS_DMA_TDES2_IOC
;
2143 lp
->tx_skb
[tx
->last_descriptor
].skb
= skb
;
2145 /* Make all descriptor updates visible to the DMA before setting the
2150 lp
->tx_descs
[tx
->initial_descriptor
].des3
|= DWCEQOS_DMA_TDES3_OWN
;
2152 /* Make the owner bit visible before TX wakeup. */
2155 dwceqos_tx_poll_demand(lp
);
2158 static void dwceqos_tx_rollback(struct net_local
*lp
, struct dwceqos_tx
*tx
)
2160 size_t i
= tx
->initial_descriptor
;
2162 while (i
!= lp
->tx_next
) {
2163 if (lp
->tx_skb
[i
].mapping
)
2164 dma_unmap_single(lp
->ndev
->dev
.parent
,
2165 lp
->tx_skb
[i
].mapping
,
2169 lp
->tx_skb
[i
].mapping
= 0;
2170 lp
->tx_skb
[i
].skb
= NULL
;
2172 memset(&lp
->tx_descs
[i
], 0, sizeof(lp
->tx_descs
[i
]));
2174 i
= (i
+ 1) % DWCEQOS_TX_DCNT
;
2177 lp
->tx_next
= tx
->initial_descriptor
;
2178 lp
->gso_size
= tx
->prev_gso_size
;
2181 static int dwceqos_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
2183 struct net_local
*lp
= netdev_priv(ndev
);
2184 struct dwceqos_tx trans
;
2187 dwceqos_tx_prepare(skb
, lp
, &trans
);
2188 if (lp
->tx_free
< trans
.nr_descriptors
) {
2189 netif_stop_queue(ndev
);
2190 return NETDEV_TX_BUSY
;
2193 err
= dwceqos_tx_linear(skb
, lp
, &trans
);
2197 err
= dwceqos_tx_frags(skb
, lp
, &trans
);
2201 WARN_ON(lp
->tx_next
!=
2202 ((trans
.initial_descriptor
+ trans
.nr_descriptors
) %
2205 spin_lock_bh(&lp
->tx_lock
);
2206 lp
->tx_free
-= trans
.nr_descriptors
;
2207 dwceqos_tx_finalize(skb
, lp
, &trans
);
2208 netdev_sent_queue(ndev
, skb
->len
);
2209 spin_unlock_bh(&lp
->tx_lock
);
2211 netif_trans_update(ndev
);
2215 dwceqos_tx_rollback(lp
, &trans
);
2220 /* Set MAC address and then update HW accordingly */
2221 static int dwceqos_set_mac_address(struct net_device
*ndev
, void *addr
)
2223 struct net_local
*lp
= netdev_priv(ndev
);
2224 struct sockaddr
*hwaddr
= (struct sockaddr
*)addr
;
2226 if (netif_running(ndev
))
2229 if (!is_valid_ether_addr(hwaddr
->sa_data
))
2230 return -EADDRNOTAVAIL
;
2232 memcpy(ndev
->dev_addr
, hwaddr
->sa_data
, ndev
->addr_len
);
2234 dwceqos_set_umac_addr(lp
, lp
->ndev
->dev_addr
, 0);
2238 static void dwceqos_tx_timeout(struct net_device
*ndev
)
2240 struct net_local
*lp
= netdev_priv(ndev
);
2242 queue_work(lp
->txtimeout_handler_wq
, &lp
->txtimeout_reinit
);
2245 static void dwceqos_set_umac_addr(struct net_local
*lp
, unsigned char *addr
,
2250 data
= (addr
[5] << 8) | addr
[4];
2251 dwceqos_write(lp
, DWCEQOS_ADDR_HIGH(reg_n
),
2252 data
| DWCEQOS_MAC_MAC_ADDR_HI_EN
);
2253 data
= (addr
[3] << 24) | (addr
[2] << 16) | (addr
[1] << 8) | addr
[0];
2254 dwceqos_write(lp
, DWCEQOS_ADDR_LOW(reg_n
), data
);
2257 static void dwceqos_disable_umac_addr(struct net_local
*lp
, unsigned int reg_n
)
2259 /* Do not disable MAC address 0 */
2261 dwceqos_write(lp
, DWCEQOS_ADDR_HIGH(reg_n
), 0);
2264 static void dwceqos_set_rx_mode(struct net_device
*ndev
)
2266 struct net_local
*lp
= netdev_priv(ndev
);
2270 struct netdev_hw_addr
*ha
;
2271 unsigned int max_mac_addr
;
2273 max_mac_addr
= DWCEQOS_MAX_PERFECT_ADDRESSES(lp
->feature1
);
2275 if (ndev
->flags
& IFF_PROMISC
) {
2276 regval
= DWCEQOS_MAC_PKT_FILT_PR
;
2277 } else if (((netdev_mc_count(ndev
) > DWCEQOS_HASH_TABLE_SIZE
) ||
2278 (ndev
->flags
& IFF_ALLMULTI
))) {
2279 regval
= DWCEQOS_MAC_PKT_FILT_PM
;
2280 dwceqos_write(lp
, REG_DWCEQOS_HASTABLE_LO
, 0xffffffff);
2281 dwceqos_write(lp
, REG_DWCEQOS_HASTABLE_HI
, 0xffffffff);
2282 } else if (!netdev_mc_empty(ndev
)) {
2283 regval
= DWCEQOS_MAC_PKT_FILT_HMC
;
2284 memset(mc_filter
, 0, sizeof(mc_filter
));
2285 netdev_for_each_mc_addr(ha
, ndev
) {
2286 /* The upper 6 bits of the calculated CRC are used to
2287 * index the contens of the hash table
2289 int bit_nr
= bitrev32(~crc32_le(~0, ha
->addr
, 6)) >> 26;
2290 /* The most significant bit determines the register
2291 * to use (H/L) while the other 5 bits determine
2292 * the bit within the register.
2294 mc_filter
[bit_nr
>> 5] |= 1 << (bit_nr
& 31);
2296 dwceqos_write(lp
, REG_DWCEQOS_HASTABLE_LO
, mc_filter
[0]);
2297 dwceqos_write(lp
, REG_DWCEQOS_HASTABLE_HI
, mc_filter
[1]);
2299 if (netdev_uc_count(ndev
) > max_mac_addr
) {
2300 regval
|= DWCEQOS_MAC_PKT_FILT_PR
;
2302 netdev_for_each_uc_addr(ha
, ndev
) {
2303 dwceqos_set_umac_addr(lp
, ha
->addr
, reg
);
2306 for (; reg
< DWCEQOS_MAX_PERFECT_ADDRESSES(lp
->feature1
); reg
++)
2307 dwceqos_disable_umac_addr(lp
, reg
);
2309 dwceqos_write(lp
, REG_DWCEQOS_MAC_PKT_FILT
, regval
);
2312 #ifdef CONFIG_NET_POLL_CONTROLLER
2313 static void dwceqos_poll_controller(struct net_device
*ndev
)
2315 disable_irq(ndev
->irq
);
2316 dwceqos_interrupt(ndev
->irq
, ndev
);
2317 enable_irq(ndev
->irq
);
2321 static void dwceqos_read_mmc_counters(struct net_local
*lp
, u32 rx_mask
,
2324 if (tx_mask
& BIT(27))
2325 lp
->mmc_counters
.txlpitranscntr
+=
2326 dwceqos_read(lp
, DWC_MMC_TXLPITRANSCNTR
);
2327 if (tx_mask
& BIT(26))
2328 lp
->mmc_counters
.txpiuscntr
+=
2329 dwceqos_read(lp
, DWC_MMC_TXLPIUSCNTR
);
2330 if (tx_mask
& BIT(25))
2331 lp
->mmc_counters
.txoversize_g
+=
2332 dwceqos_read(lp
, DWC_MMC_TXOVERSIZE_G
);
2333 if (tx_mask
& BIT(24))
2334 lp
->mmc_counters
.txvlanpackets_g
+=
2335 dwceqos_read(lp
, DWC_MMC_TXVLANPACKETS_G
);
2336 if (tx_mask
& BIT(23))
2337 lp
->mmc_counters
.txpausepackets
+=
2338 dwceqos_read(lp
, DWC_MMC_TXPAUSEPACKETS
);
2339 if (tx_mask
& BIT(22))
2340 lp
->mmc_counters
.txexcessdef
+=
2341 dwceqos_read(lp
, DWC_MMC_TXEXCESSDEF
);
2342 if (tx_mask
& BIT(21))
2343 lp
->mmc_counters
.txpacketcount_g
+=
2344 dwceqos_read(lp
, DWC_MMC_TXPACKETCOUNT_G
);
2345 if (tx_mask
& BIT(20))
2346 lp
->mmc_counters
.txoctetcount_g
+=
2347 dwceqos_read(lp
, DWC_MMC_TXOCTETCOUNT_G
);
2348 if (tx_mask
& BIT(19))
2349 lp
->mmc_counters
.txcarriererror
+=
2350 dwceqos_read(lp
, DWC_MMC_TXCARRIERERROR
);
2351 if (tx_mask
& BIT(18))
2352 lp
->mmc_counters
.txexcesscol
+=
2353 dwceqos_read(lp
, DWC_MMC_TXEXCESSCOL
);
2354 if (tx_mask
& BIT(17))
2355 lp
->mmc_counters
.txlatecol
+=
2356 dwceqos_read(lp
, DWC_MMC_TXLATECOL
);
2357 if (tx_mask
& BIT(16))
2358 lp
->mmc_counters
.txdeferred
+=
2359 dwceqos_read(lp
, DWC_MMC_TXDEFERRED
);
2360 if (tx_mask
& BIT(15))
2361 lp
->mmc_counters
.txmulticol_g
+=
2362 dwceqos_read(lp
, DWC_MMC_TXMULTICOL_G
);
2363 if (tx_mask
& BIT(14))
2364 lp
->mmc_counters
.txsinglecol_g
+=
2365 dwceqos_read(lp
, DWC_MMC_TXSINGLECOL_G
);
2366 if (tx_mask
& BIT(13))
2367 lp
->mmc_counters
.txunderflowerror
+=
2368 dwceqos_read(lp
, DWC_MMC_TXUNDERFLOWERROR
);
2369 if (tx_mask
& BIT(12))
2370 lp
->mmc_counters
.txbroadcastpackets_gb
+=
2371 dwceqos_read(lp
, DWC_MMC_TXBROADCASTPACKETS_GB
);
2372 if (tx_mask
& BIT(11))
2373 lp
->mmc_counters
.txmulticastpackets_gb
+=
2374 dwceqos_read(lp
, DWC_MMC_TXMULTICASTPACKETS_GB
);
2375 if (tx_mask
& BIT(10))
2376 lp
->mmc_counters
.txunicastpackets_gb
+=
2377 dwceqos_read(lp
, DWC_MMC_TXUNICASTPACKETS_GB
);
2378 if (tx_mask
& BIT(9))
2379 lp
->mmc_counters
.tx1024tomaxoctets_gb
+=
2380 dwceqos_read(lp
, DWC_MMC_TX1024TOMAXOCTETS_GB
);
2381 if (tx_mask
& BIT(8))
2382 lp
->mmc_counters
.tx512to1023octets_gb
+=
2383 dwceqos_read(lp
, DWC_MMC_TX512TO1023OCTETS_GB
);
2384 if (tx_mask
& BIT(7))
2385 lp
->mmc_counters
.tx256to511octets_gb
+=
2386 dwceqos_read(lp
, DWC_MMC_TX256TO511OCTETS_GB
);
2387 if (tx_mask
& BIT(6))
2388 lp
->mmc_counters
.tx128to255octets_gb
+=
2389 dwceqos_read(lp
, DWC_MMC_TX128TO255OCTETS_GB
);
2390 if (tx_mask
& BIT(5))
2391 lp
->mmc_counters
.tx65to127octets_gb
+=
2392 dwceqos_read(lp
, DWC_MMC_TX65TO127OCTETS_GB
);
2393 if (tx_mask
& BIT(4))
2394 lp
->mmc_counters
.tx64octets_gb
+=
2395 dwceqos_read(lp
, DWC_MMC_TX64OCTETS_GB
);
2396 if (tx_mask
& BIT(3))
2397 lp
->mmc_counters
.txmulticastpackets_g
+=
2398 dwceqos_read(lp
, DWC_MMC_TXMULTICASTPACKETS_G
);
2399 if (tx_mask
& BIT(2))
2400 lp
->mmc_counters
.txbroadcastpackets_g
+=
2401 dwceqos_read(lp
, DWC_MMC_TXBROADCASTPACKETS_G
);
2402 if (tx_mask
& BIT(1))
2403 lp
->mmc_counters
.txpacketcount_gb
+=
2404 dwceqos_read(lp
, DWC_MMC_TXPACKETCOUNT_GB
);
2405 if (tx_mask
& BIT(0))
2406 lp
->mmc_counters
.txoctetcount_gb
+=
2407 dwceqos_read(lp
, DWC_MMC_TXOCTETCOUNT_GB
);
2409 if (rx_mask
& BIT(27))
2410 lp
->mmc_counters
.rxlpitranscntr
+=
2411 dwceqos_read(lp
, DWC_MMC_RXLPITRANSCNTR
);
2412 if (rx_mask
& BIT(26))
2413 lp
->mmc_counters
.rxlpiuscntr
+=
2414 dwceqos_read(lp
, DWC_MMC_RXLPIUSCNTR
);
2415 if (rx_mask
& BIT(25))
2416 lp
->mmc_counters
.rxctrlpackets_g
+=
2417 dwceqos_read(lp
, DWC_MMC_RXCTRLPACKETS_G
);
2418 if (rx_mask
& BIT(24))
2419 lp
->mmc_counters
.rxrcverror
+=
2420 dwceqos_read(lp
, DWC_MMC_RXRCVERROR
);
2421 if (rx_mask
& BIT(23))
2422 lp
->mmc_counters
.rxwatchdog
+=
2423 dwceqos_read(lp
, DWC_MMC_RXWATCHDOG
);
2424 if (rx_mask
& BIT(22))
2425 lp
->mmc_counters
.rxvlanpackets_gb
+=
2426 dwceqos_read(lp
, DWC_MMC_RXVLANPACKETS_GB
);
2427 if (rx_mask
& BIT(21))
2428 lp
->mmc_counters
.rxfifooverflow
+=
2429 dwceqos_read(lp
, DWC_MMC_RXFIFOOVERFLOW
);
2430 if (rx_mask
& BIT(20))
2431 lp
->mmc_counters
.rxpausepackets
+=
2432 dwceqos_read(lp
, DWC_MMC_RXPAUSEPACKETS
);
2433 if (rx_mask
& BIT(19))
2434 lp
->mmc_counters
.rxoutofrangetype
+=
2435 dwceqos_read(lp
, DWC_MMC_RXOUTOFRANGETYPE
);
2436 if (rx_mask
& BIT(18))
2437 lp
->mmc_counters
.rxlengtherror
+=
2438 dwceqos_read(lp
, DWC_MMC_RXLENGTHERROR
);
2439 if (rx_mask
& BIT(17))
2440 lp
->mmc_counters
.rxunicastpackets_g
+=
2441 dwceqos_read(lp
, DWC_MMC_RXUNICASTPACKETS_G
);
2442 if (rx_mask
& BIT(16))
2443 lp
->mmc_counters
.rx1024tomaxoctets_gb
+=
2444 dwceqos_read(lp
, DWC_MMC_RX1024TOMAXOCTETS_GB
);
2445 if (rx_mask
& BIT(15))
2446 lp
->mmc_counters
.rx512to1023octets_gb
+=
2447 dwceqos_read(lp
, DWC_MMC_RX512TO1023OCTETS_GB
);
2448 if (rx_mask
& BIT(14))
2449 lp
->mmc_counters
.rx256to511octets_gb
+=
2450 dwceqos_read(lp
, DWC_MMC_RX256TO511OCTETS_GB
);
2451 if (rx_mask
& BIT(13))
2452 lp
->mmc_counters
.rx128to255octets_gb
+=
2453 dwceqos_read(lp
, DWC_MMC_RX128TO255OCTETS_GB
);
2454 if (rx_mask
& BIT(12))
2455 lp
->mmc_counters
.rx65to127octets_gb
+=
2456 dwceqos_read(lp
, DWC_MMC_RX65TO127OCTETS_GB
);
2457 if (rx_mask
& BIT(11))
2458 lp
->mmc_counters
.rx64octets_gb
+=
2459 dwceqos_read(lp
, DWC_MMC_RX64OCTETS_GB
);
2460 if (rx_mask
& BIT(10))
2461 lp
->mmc_counters
.rxoversize_g
+=
2462 dwceqos_read(lp
, DWC_MMC_RXOVERSIZE_G
);
2463 if (rx_mask
& BIT(9))
2464 lp
->mmc_counters
.rxundersize_g
+=
2465 dwceqos_read(lp
, DWC_MMC_RXUNDERSIZE_G
);
2466 if (rx_mask
& BIT(8))
2467 lp
->mmc_counters
.rxjabbererror
+=
2468 dwceqos_read(lp
, DWC_MMC_RXJABBERERROR
);
2469 if (rx_mask
& BIT(7))
2470 lp
->mmc_counters
.rxrunterror
+=
2471 dwceqos_read(lp
, DWC_MMC_RXRUNTERROR
);
2472 if (rx_mask
& BIT(6))
2473 lp
->mmc_counters
.rxalignmenterror
+=
2474 dwceqos_read(lp
, DWC_MMC_RXALIGNMENTERROR
);
2475 if (rx_mask
& BIT(5))
2476 lp
->mmc_counters
.rxcrcerror
+=
2477 dwceqos_read(lp
, DWC_MMC_RXCRCERROR
);
2478 if (rx_mask
& BIT(4))
2479 lp
->mmc_counters
.rxmulticastpackets_g
+=
2480 dwceqos_read(lp
, DWC_MMC_RXMULTICASTPACKETS_G
);
2481 if (rx_mask
& BIT(3))
2482 lp
->mmc_counters
.rxbroadcastpackets_g
+=
2483 dwceqos_read(lp
, DWC_MMC_RXBROADCASTPACKETS_G
);
2484 if (rx_mask
& BIT(2))
2485 lp
->mmc_counters
.rxoctetcount_g
+=
2486 dwceqos_read(lp
, DWC_MMC_RXOCTETCOUNT_G
);
2487 if (rx_mask
& BIT(1))
2488 lp
->mmc_counters
.rxoctetcount_gb
+=
2489 dwceqos_read(lp
, DWC_MMC_RXOCTETCOUNT_GB
);
2490 if (rx_mask
& BIT(0))
2491 lp
->mmc_counters
.rxpacketcount_gb
+=
2492 dwceqos_read(lp
, DWC_MMC_RXPACKETCOUNT_GB
);
2495 static struct rtnl_link_stats64
*
2496 dwceqos_get_stats64(struct net_device
*ndev
, struct rtnl_link_stats64
*s
)
2498 unsigned long flags
;
2499 struct net_local
*lp
= netdev_priv(ndev
);
2500 struct dwceqos_mmc_counters
*hwstats
= &lp
->mmc_counters
;
2502 spin_lock_irqsave(&lp
->stats_lock
, flags
);
2503 dwceqos_read_mmc_counters(lp
, lp
->mmc_rx_counters_mask
,
2504 lp
->mmc_tx_counters_mask
);
2505 spin_unlock_irqrestore(&lp
->stats_lock
, flags
);
2507 s
->rx_packets
= hwstats
->rxpacketcount_gb
;
2508 s
->rx_bytes
= hwstats
->rxoctetcount_gb
;
2509 s
->rx_errors
= hwstats
->rxpacketcount_gb
-
2510 hwstats
->rxbroadcastpackets_g
-
2511 hwstats
->rxmulticastpackets_g
-
2512 hwstats
->rxunicastpackets_g
;
2513 s
->multicast
= hwstats
->rxmulticastpackets_g
;
2514 s
->rx_length_errors
= hwstats
->rxlengtherror
;
2515 s
->rx_crc_errors
= hwstats
->rxcrcerror
;
2516 s
->rx_fifo_errors
= hwstats
->rxfifooverflow
;
2518 s
->tx_packets
= hwstats
->txpacketcount_gb
;
2519 s
->tx_bytes
= hwstats
->txoctetcount_gb
;
2521 if (lp
->mmc_tx_counters_mask
& BIT(21))
2522 s
->tx_errors
= hwstats
->txpacketcount_gb
-
2523 hwstats
->txpacketcount_g
;
2525 s
->tx_errors
= hwstats
->txunderflowerror
+
2526 hwstats
->txcarriererror
;
2532 dwceqos_get_drvinfo(struct net_device
*ndev
, struct ethtool_drvinfo
*ed
)
2534 const struct net_local
*lp
= netdev_priv(ndev
);
2536 strcpy(ed
->driver
, lp
->pdev
->dev
.driver
->name
);
2537 strcpy(ed
->version
, DRIVER_VERSION
);
2540 static void dwceqos_get_pauseparam(struct net_device
*ndev
,
2541 struct ethtool_pauseparam
*pp
)
2543 const struct net_local
*lp
= netdev_priv(ndev
);
2545 pp
->autoneg
= lp
->flowcontrol
.autoneg
;
2546 pp
->tx_pause
= lp
->flowcontrol
.tx
;
2547 pp
->rx_pause
= lp
->flowcontrol
.rx
;
2550 static int dwceqos_set_pauseparam(struct net_device
*ndev
,
2551 struct ethtool_pauseparam
*pp
)
2553 struct net_local
*lp
= netdev_priv(ndev
);
2556 lp
->flowcontrol
.autoneg
= pp
->autoneg
;
2558 ndev
->phydev
->advertising
|= ADVERTISED_Pause
;
2559 ndev
->phydev
->advertising
|= ADVERTISED_Asym_Pause
;
2561 ndev
->phydev
->advertising
&= ~ADVERTISED_Pause
;
2562 ndev
->phydev
->advertising
&= ~ADVERTISED_Asym_Pause
;
2563 lp
->flowcontrol
.rx
= pp
->rx_pause
;
2564 lp
->flowcontrol
.tx
= pp
->tx_pause
;
2567 if (netif_running(ndev
))
2568 ret
= phy_start_aneg(ndev
->phydev
);
2573 static void dwceqos_get_strings(struct net_device
*ndev
, u32 stringset
,
2578 if (stringset
!= ETH_SS_STATS
)
2581 for (i
= 0; i
< ARRAY_SIZE(dwceqos_ethtool_stats
); ++i
) {
2582 memcpy(data
, dwceqos_ethtool_stats
[i
].stat_name
,
2584 data
+= ETH_GSTRING_LEN
;
2588 static void dwceqos_get_ethtool_stats(struct net_device
*ndev
,
2589 struct ethtool_stats
*stats
, u64
*data
)
2591 struct net_local
*lp
= netdev_priv(ndev
);
2592 unsigned long flags
;
2594 u8
*mmcstat
= (u8
*)&lp
->mmc_counters
;
2596 spin_lock_irqsave(&lp
->stats_lock
, flags
);
2597 dwceqos_read_mmc_counters(lp
, lp
->mmc_rx_counters_mask
,
2598 lp
->mmc_tx_counters_mask
);
2599 spin_unlock_irqrestore(&lp
->stats_lock
, flags
);
2601 for (i
= 0; i
< ARRAY_SIZE(dwceqos_ethtool_stats
); ++i
) {
2603 mmcstat
+ dwceqos_ethtool_stats
[i
].offset
,
2609 static int dwceqos_get_sset_count(struct net_device
*ndev
, int sset
)
2611 if (sset
== ETH_SS_STATS
)
2612 return ARRAY_SIZE(dwceqos_ethtool_stats
);
2617 static void dwceqos_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
,
2620 const struct net_local
*lp
= netdev_priv(dev
);
2621 u32
*reg_space
= (u32
*)space
;
2626 for (reg_offset
= START_MAC_REG_OFFSET
;
2627 reg_offset
<= MAX_DMA_REG_OFFSET
; reg_offset
+= 4) {
2628 reg_space
[reg_ix
] = dwceqos_read(lp
, reg_offset
);
2632 for (reg_offset
= START_MTL_REG_OFFSET
;
2633 reg_offset
<= MAX_MTL_REG_OFFSET
; reg_offset
+= 4) {
2634 reg_space
[reg_ix
] = dwceqos_read(lp
, reg_offset
);
2639 for (reg_offset
= START_DMA_REG_OFFSET
;
2640 reg_offset
<= MAX_DMA_REG_OFFSET
; reg_offset
+= 4) {
2641 reg_space
[reg_ix
] = dwceqos_read(lp
, reg_offset
);
2645 BUG_ON(4 * reg_ix
> REG_SPACE_SIZE
);
2648 static int dwceqos_get_regs_len(struct net_device
*dev
)
2650 return REG_SPACE_SIZE
;
2653 static inline const char *dwceqos_get_rx_lpi_state(u32 lpi_ctrl
)
2655 return (lpi_ctrl
& DWCEQOS_MAC_LPI_CTRL_STATUS_RLPIST
) ? "on" : "off";
2658 static inline const char *dwceqos_get_tx_lpi_state(u32 lpi_ctrl
)
2660 return (lpi_ctrl
& DWCEQOS_MAC_LPI_CTRL_STATUS_TLPIST
) ? "on" : "off";
2663 static int dwceqos_get_eee(struct net_device
*ndev
, struct ethtool_eee
*edata
)
2665 struct net_local
*lp
= netdev_priv(ndev
);
2669 if (!(lp
->feature0
& DWCEQOS_MAC_HW_FEATURE0_EEESEL
))
2672 edata
->eee_active
= lp
->eee_active
;
2673 edata
->eee_enabled
= lp
->eee_enabled
;
2674 edata
->tx_lpi_timer
= dwceqos_read(lp
, REG_DWCEQOS_MAC_LPI_ENTRY_TIMER
);
2675 lpi_status
= dwceqos_read(lp
, REG_DWCEQOS_MAC_LPI_CTRL_STATUS
);
2676 lpi_enabled
= !!(lpi_status
& DWCEQOS_MAC_LPI_CTRL_STATUS_LIPTXA
);
2677 edata
->tx_lpi_enabled
= lpi_enabled
;
2679 if (netif_msg_hw(lp
)) {
2682 regval
= dwceqos_read(lp
, REG_DWCEQOS_MAC_LPI_CTRL_STATUS
);
2684 netdev_info(lp
->ndev
, "MAC LPI State: RX:%s TX:%s\n",
2685 dwceqos_get_rx_lpi_state(regval
),
2686 dwceqos_get_tx_lpi_state(regval
));
2689 return phy_ethtool_get_eee(ndev
->phydev
, edata
);
2692 static int dwceqos_set_eee(struct net_device
*ndev
, struct ethtool_eee
*edata
)
2694 struct net_local
*lp
= netdev_priv(ndev
);
2696 unsigned long flags
;
2698 if (!(lp
->feature0
& DWCEQOS_MAC_HW_FEATURE0_EEESEL
))
2701 if (edata
->eee_enabled
&& !lp
->eee_active
)
2704 if (edata
->tx_lpi_enabled
) {
2705 if (edata
->tx_lpi_timer
< DWCEQOS_LPI_TIMER_MIN
||
2706 edata
->tx_lpi_timer
> DWCEQOS_LPI_TIMER_MAX
)
2710 lp
->eee_enabled
= edata
->eee_enabled
;
2712 if (edata
->eee_enabled
&& edata
->tx_lpi_enabled
) {
2713 dwceqos_write(lp
, REG_DWCEQOS_MAC_LPI_ENTRY_TIMER
,
2714 edata
->tx_lpi_timer
);
2716 spin_lock_irqsave(&lp
->hw_lock
, flags
);
2717 regval
= dwceqos_read(lp
, REG_DWCEQOS_MAC_LPI_CTRL_STATUS
);
2718 regval
|= DWCEQOS_LPI_CTRL_ENABLE_EEE
;
2719 if (lp
->en_tx_lpi_clockgating
)
2720 regval
|= DWCEQOS_MAC_LPI_CTRL_STATUS_LPITCSE
;
2721 dwceqos_write(lp
, REG_DWCEQOS_MAC_LPI_CTRL_STATUS
, regval
);
2722 spin_unlock_irqrestore(&lp
->hw_lock
, flags
);
2724 spin_lock_irqsave(&lp
->hw_lock
, flags
);
2725 regval
= dwceqos_read(lp
, REG_DWCEQOS_MAC_LPI_CTRL_STATUS
);
2726 regval
&= ~DWCEQOS_LPI_CTRL_ENABLE_EEE
;
2727 dwceqos_write(lp
, REG_DWCEQOS_MAC_LPI_CTRL_STATUS
, regval
);
2728 spin_unlock_irqrestore(&lp
->hw_lock
, flags
);
2731 return phy_ethtool_set_eee(ndev
->phydev
, edata
);
2734 static u32
dwceqos_get_msglevel(struct net_device
*ndev
)
2736 const struct net_local
*lp
= netdev_priv(ndev
);
2738 return lp
->msg_enable
;
2741 static void dwceqos_set_msglevel(struct net_device
*ndev
, u32 msglevel
)
2743 struct net_local
*lp
= netdev_priv(ndev
);
2745 lp
->msg_enable
= msglevel
;
2748 static const struct ethtool_ops dwceqos_ethtool_ops
= {
2749 .get_drvinfo
= dwceqos_get_drvinfo
,
2750 .get_link
= ethtool_op_get_link
,
2751 .get_pauseparam
= dwceqos_get_pauseparam
,
2752 .set_pauseparam
= dwceqos_set_pauseparam
,
2753 .get_strings
= dwceqos_get_strings
,
2754 .get_ethtool_stats
= dwceqos_get_ethtool_stats
,
2755 .get_sset_count
= dwceqos_get_sset_count
,
2756 .get_regs
= dwceqos_get_regs
,
2757 .get_regs_len
= dwceqos_get_regs_len
,
2758 .get_eee
= dwceqos_get_eee
,
2759 .set_eee
= dwceqos_set_eee
,
2760 .get_msglevel
= dwceqos_get_msglevel
,
2761 .set_msglevel
= dwceqos_set_msglevel
,
2762 .get_link_ksettings
= phy_ethtool_get_link_ksettings
,
2763 .set_link_ksettings
= phy_ethtool_set_link_ksettings
,
2766 static const struct net_device_ops netdev_ops
= {
2767 .ndo_open
= dwceqos_open
,
2768 .ndo_stop
= dwceqos_stop
,
2769 .ndo_start_xmit
= dwceqos_start_xmit
,
2770 .ndo_set_rx_mode
= dwceqos_set_rx_mode
,
2771 .ndo_set_mac_address
= dwceqos_set_mac_address
,
2772 #ifdef CONFIG_NET_POLL_CONTROLLER
2773 .ndo_poll_controller
= dwceqos_poll_controller
,
2775 .ndo_do_ioctl
= dwceqos_ioctl
,
2776 .ndo_tx_timeout
= dwceqos_tx_timeout
,
2777 .ndo_get_stats64
= dwceqos_get_stats64
,
2780 static const struct of_device_id dwceq_of_match
[] = {
2781 { .compatible
= "snps,dwc-qos-ethernet-4.10", },
2784 MODULE_DEVICE_TABLE(of
, dwceq_of_match
);
2786 static int dwceqos_probe(struct platform_device
*pdev
)
2788 struct resource
*r_mem
= NULL
;
2789 struct net_device
*ndev
;
2790 struct net_local
*lp
;
2793 r_mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2795 dev_err(&pdev
->dev
, "no IO resource defined.\n");
2799 ndev
= alloc_etherdev(sizeof(*lp
));
2801 dev_err(&pdev
->dev
, "etherdev allocation failed.\n");
2805 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
2807 lp
= netdev_priv(ndev
);
2810 lp
->msg_enable
= netif_msg_init(debug
, DWCEQOS_MSG_DEFAULT
);
2812 spin_lock_init(&lp
->tx_lock
);
2813 spin_lock_init(&lp
->hw_lock
);
2814 spin_lock_init(&lp
->stats_lock
);
2816 lp
->apb_pclk
= devm_clk_get(&pdev
->dev
, "apb_pclk");
2817 if (IS_ERR(lp
->apb_pclk
)) {
2818 dev_err(&pdev
->dev
, "apb_pclk clock not found.\n");
2819 ret
= PTR_ERR(lp
->apb_pclk
);
2820 goto err_out_free_netdev
;
2823 ret
= clk_prepare_enable(lp
->apb_pclk
);
2825 dev_err(&pdev
->dev
, "Unable to enable APER clock.\n");
2826 goto err_out_free_netdev
;
2829 lp
->baseaddr
= devm_ioremap_resource(&pdev
->dev
, r_mem
);
2830 if (IS_ERR(lp
->baseaddr
)) {
2831 dev_err(&pdev
->dev
, "failed to map baseaddress.\n");
2832 ret
= PTR_ERR(lp
->baseaddr
);
2833 goto err_out_clk_dis_aper
;
2836 ndev
->irq
= platform_get_irq(pdev
, 0);
2837 ndev
->watchdog_timeo
= DWCEQOS_TX_TIMEOUT
* HZ
;
2838 ndev
->netdev_ops
= &netdev_ops
;
2839 ndev
->ethtool_ops
= &dwceqos_ethtool_ops
;
2840 ndev
->base_addr
= r_mem
->start
;
2842 dwceqos_get_hwfeatures(lp
);
2843 dwceqos_mdio_set_csr(lp
);
2845 ndev
->hw_features
= NETIF_F_SG
;
2847 if (lp
->feature1
& DWCEQOS_MAC_HW_FEATURE1_TSOEN
)
2848 ndev
->hw_features
|= NETIF_F_TSO
| NETIF_F_TSO6
;
2850 if (lp
->feature0
& DWCEQOS_MAC_HW_FEATURE0_TXCOESEL
)
2851 ndev
->hw_features
|= NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
;
2853 if (lp
->feature0
& DWCEQOS_MAC_HW_FEATURE0_RXCOESEL
)
2854 ndev
->hw_features
|= NETIF_F_RXCSUM
;
2856 ndev
->features
= ndev
->hw_features
;
2858 lp
->phy_ref_clk
= devm_clk_get(&pdev
->dev
, "phy_ref_clk");
2859 if (IS_ERR(lp
->phy_ref_clk
)) {
2860 dev_err(&pdev
->dev
, "phy_ref_clk clock not found.\n");
2861 ret
= PTR_ERR(lp
->phy_ref_clk
);
2862 goto err_out_clk_dis_aper
;
2865 ret
= clk_prepare_enable(lp
->phy_ref_clk
);
2867 dev_err(&pdev
->dev
, "Unable to enable device clock.\n");
2868 goto err_out_clk_dis_aper
;
2871 lp
->phy_node
= of_parse_phandle(lp
->pdev
->dev
.of_node
,
2873 if (!lp
->phy_node
&& of_phy_is_fixed_link(lp
->pdev
->dev
.of_node
)) {
2874 ret
= of_phy_register_fixed_link(lp
->pdev
->dev
.of_node
);
2876 dev_err(&pdev
->dev
, "invalid fixed-link");
2877 goto err_out_clk_dis_phy
;
2880 lp
->phy_node
= of_node_get(lp
->pdev
->dev
.of_node
);
2883 ret
= of_get_phy_mode(lp
->pdev
->dev
.of_node
);
2885 dev_err(&lp
->pdev
->dev
, "error in getting phy i/f\n");
2886 goto err_out_clk_dis_phy
;
2889 lp
->phy_interface
= ret
;
2891 ret
= dwceqos_mii_init(lp
);
2893 dev_err(&lp
->pdev
->dev
, "error in dwceqos_mii_init\n");
2894 goto err_out_clk_dis_phy
;
2897 ret
= dwceqos_mii_probe(ndev
);
2899 netdev_err(ndev
, "mii_probe fail.\n");
2901 goto err_out_clk_dis_phy
;
2904 dwceqos_set_umac_addr(lp
, lp
->ndev
->dev_addr
, 0);
2906 tasklet_init(&lp
->tx_bdreclaim_tasklet
, dwceqos_tx_reclaim
,
2907 (unsigned long)ndev
);
2908 tasklet_disable(&lp
->tx_bdreclaim_tasklet
);
2910 lp
->txtimeout_handler_wq
= alloc_workqueue(DRIVER_NAME
,
2912 INIT_WORK(&lp
->txtimeout_reinit
, dwceqos_reinit_for_txtimeout
);
2914 platform_set_drvdata(pdev
, ndev
);
2915 ret
= dwceqos_probe_config_dt(pdev
);
2917 dev_err(&lp
->pdev
->dev
, "Unable to retrieve DT, error %d\n",
2919 goto err_out_clk_dis_phy
;
2921 dev_info(&lp
->pdev
->dev
, "pdev->id %d, baseaddr 0x%08lx, irq %d\n",
2922 pdev
->id
, ndev
->base_addr
, ndev
->irq
);
2924 ret
= devm_request_irq(&pdev
->dev
, ndev
->irq
, &dwceqos_interrupt
, 0,
2927 dev_err(&lp
->pdev
->dev
, "Unable to request IRQ %d, error %d\n",
2929 goto err_out_clk_dis_phy
;
2932 if (netif_msg_probe(lp
))
2933 netdev_dbg(ndev
, "net_local@%p\n", lp
);
2935 netif_napi_add(ndev
, &lp
->napi
, dwceqos_rx_poll
, NAPI_POLL_WEIGHT
);
2937 ret
= register_netdev(ndev
);
2939 dev_err(&pdev
->dev
, "Cannot register net device, aborting.\n");
2940 goto err_out_clk_dis_phy
;
2945 err_out_clk_dis_phy
:
2946 clk_disable_unprepare(lp
->phy_ref_clk
);
2947 err_out_clk_dis_aper
:
2948 clk_disable_unprepare(lp
->apb_pclk
);
2949 err_out_free_netdev
:
2950 of_node_put(lp
->phy_node
);
2952 platform_set_drvdata(pdev
, NULL
);
2956 static int dwceqos_remove(struct platform_device
*pdev
)
2958 struct net_device
*ndev
= platform_get_drvdata(pdev
);
2959 struct net_local
*lp
;
2962 lp
= netdev_priv(ndev
);
2965 phy_disconnect(ndev
->phydev
);
2966 mdiobus_unregister(lp
->mii_bus
);
2967 mdiobus_free(lp
->mii_bus
);
2969 unregister_netdev(ndev
);
2971 clk_disable_unprepare(lp
->phy_ref_clk
);
2972 clk_disable_unprepare(lp
->apb_pclk
);
2980 static struct platform_driver dwceqos_driver
= {
2981 .probe
= dwceqos_probe
,
2982 .remove
= dwceqos_remove
,
2984 .name
= DRIVER_NAME
,
2985 .of_match_table
= dwceq_of_match
,
2989 module_platform_driver(dwceqos_driver
);
2991 MODULE_DESCRIPTION("DWC Ethernet QoS v4.10a driver");
2992 MODULE_LICENSE("GPL v2");
2993 MODULE_AUTHOR("Andreas Irestaal <andreas.irestal@axis.com>");
2994 MODULE_AUTHOR("Lars Persson <lars.persson@axis.com>");