2 * This is the driver for the GMAC on-chip Ethernet controller for ST SoCs.
3 * DWC Ether MAC version 4.00 has been used for developing this code.
5 * This only implements the mac core functions for this chip.
7 * Copyright (C) 2015 STMicroelectronics Ltd
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
13 * Author: Alexandre Torgue <alexandre.torgue@st.com>
16 #include <linux/crc32.h>
17 #include <linux/slab.h>
18 #include <linux/ethtool.h>
20 #include "stmmac_pcs.h"
23 static void dwmac4_core_init(struct mac_device_info
*hw
, int mtu
)
25 void __iomem
*ioaddr
= hw
->pcsr
;
26 u32 value
= readl(ioaddr
+ GMAC_CONFIG
);
28 value
|= GMAC_CORE_INIT
;
31 value
|= GMAC_CONFIG_2K
;
33 value
|= GMAC_CONFIG_JE
;
36 value
|= GMAC_CONFIG_TE
;
38 if (hw
->ps
== SPEED_1000
) {
39 value
&= ~GMAC_CONFIG_PS
;
41 value
|= GMAC_CONFIG_PS
;
43 if (hw
->ps
== SPEED_10
)
44 value
&= ~GMAC_CONFIG_FES
;
46 value
|= GMAC_CONFIG_FES
;
50 writel(value
, ioaddr
+ GMAC_CONFIG
);
52 /* Mask GMAC interrupts */
53 value
= GMAC_INT_DEFAULT_MASK
;
55 value
|= GMAC_INT_PMT_EN
;
57 value
|= GMAC_PCS_IRQ_DEFAULT
;
59 writel(value
, ioaddr
+ GMAC_INT_EN
);
62 static void dwmac4_rx_queue_enable(struct mac_device_info
*hw
, u32 queue
)
64 void __iomem
*ioaddr
= hw
->pcsr
;
65 u32 value
= readl(ioaddr
+ GMAC_RXQ_CTRL0
);
67 value
&= GMAC_RX_QUEUE_CLEAR(queue
);
68 value
|= GMAC_RX_AV_QUEUE_ENABLE(queue
);
70 writel(value
, ioaddr
+ GMAC_RXQ_CTRL0
);
73 static void dwmac4_dump_regs(struct mac_device_info
*hw
, u32
*reg_space
)
75 void __iomem
*ioaddr
= hw
->pcsr
;
78 for (i
= 0; i
< GMAC_REG_NUM
; i
++)
79 reg_space
[i
] = readl(ioaddr
+ i
* 4);
82 static int dwmac4_rx_ipc_enable(struct mac_device_info
*hw
)
84 void __iomem
*ioaddr
= hw
->pcsr
;
85 u32 value
= readl(ioaddr
+ GMAC_CONFIG
);
88 value
|= GMAC_CONFIG_IPC
;
90 value
&= ~GMAC_CONFIG_IPC
;
92 writel(value
, ioaddr
+ GMAC_CONFIG
);
94 value
= readl(ioaddr
+ GMAC_CONFIG
);
96 return !!(value
& GMAC_CONFIG_IPC
);
99 static void dwmac4_pmt(struct mac_device_info
*hw
, unsigned long mode
)
101 void __iomem
*ioaddr
= hw
->pcsr
;
102 unsigned int pmt
= 0;
104 if (mode
& WAKE_MAGIC
) {
105 pr_debug("GMAC: WOL Magic frame\n");
106 pmt
|= power_down
| magic_pkt_en
;
108 if (mode
& WAKE_UCAST
) {
109 pr_debug("GMAC: WOL on global unicast\n");
110 pmt
|= power_down
| global_unicast
| wake_up_frame_en
;
113 writel(pmt
, ioaddr
+ GMAC_PMT
);
116 static void dwmac4_set_umac_addr(struct mac_device_info
*hw
,
117 unsigned char *addr
, unsigned int reg_n
)
119 void __iomem
*ioaddr
= hw
->pcsr
;
121 stmmac_dwmac4_set_mac_addr(ioaddr
, addr
, GMAC_ADDR_HIGH(reg_n
),
122 GMAC_ADDR_LOW(reg_n
));
125 static void dwmac4_get_umac_addr(struct mac_device_info
*hw
,
126 unsigned char *addr
, unsigned int reg_n
)
128 void __iomem
*ioaddr
= hw
->pcsr
;
130 stmmac_dwmac4_get_mac_addr(ioaddr
, addr
, GMAC_ADDR_HIGH(reg_n
),
131 GMAC_ADDR_LOW(reg_n
));
134 static void dwmac4_set_eee_mode(struct mac_device_info
*hw
,
135 bool en_tx_lpi_clockgating
)
137 void __iomem
*ioaddr
= hw
->pcsr
;
140 /* Enable the link status receive on RGMII, SGMII ore SMII
141 * receive path and instruct the transmit to enter in LPI
144 value
= readl(ioaddr
+ GMAC4_LPI_CTRL_STATUS
);
145 value
|= GMAC4_LPI_CTRL_STATUS_LPIEN
| GMAC4_LPI_CTRL_STATUS_LPITXA
;
147 if (en_tx_lpi_clockgating
)
148 value
|= GMAC4_LPI_CTRL_STATUS_LPITCSE
;
150 writel(value
, ioaddr
+ GMAC4_LPI_CTRL_STATUS
);
153 static void dwmac4_reset_eee_mode(struct mac_device_info
*hw
)
155 void __iomem
*ioaddr
= hw
->pcsr
;
158 value
= readl(ioaddr
+ GMAC4_LPI_CTRL_STATUS
);
159 value
&= ~(GMAC4_LPI_CTRL_STATUS_LPIEN
| GMAC4_LPI_CTRL_STATUS_LPITXA
);
160 writel(value
, ioaddr
+ GMAC4_LPI_CTRL_STATUS
);
163 static void dwmac4_set_eee_pls(struct mac_device_info
*hw
, int link
)
165 void __iomem
*ioaddr
= hw
->pcsr
;
168 value
= readl(ioaddr
+ GMAC4_LPI_CTRL_STATUS
);
171 value
|= GMAC4_LPI_CTRL_STATUS_PLS
;
173 value
&= ~GMAC4_LPI_CTRL_STATUS_PLS
;
175 writel(value
, ioaddr
+ GMAC4_LPI_CTRL_STATUS
);
178 static void dwmac4_set_eee_timer(struct mac_device_info
*hw
, int ls
, int tw
)
180 void __iomem
*ioaddr
= hw
->pcsr
;
181 int value
= ((tw
& 0xffff)) | ((ls
& 0x3ff) << 16);
183 /* Program the timers in the LPI timer control register:
184 * LS: minimum time (ms) for which the link
185 * status from PHY should be ok before transmitting
187 * TW: minimum time (us) for which the core waits
188 * after it has stopped transmitting the LPI pattern.
190 writel(value
, ioaddr
+ GMAC4_LPI_TIMER_CTRL
);
193 static void dwmac4_set_filter(struct mac_device_info
*hw
,
194 struct net_device
*dev
)
196 void __iomem
*ioaddr
= (void __iomem
*)dev
->base_addr
;
197 unsigned int value
= 0;
199 if (dev
->flags
& IFF_PROMISC
) {
200 value
= GMAC_PACKET_FILTER_PR
;
201 } else if ((dev
->flags
& IFF_ALLMULTI
) ||
202 (netdev_mc_count(dev
) > HASH_TABLE_SIZE
)) {
204 value
= GMAC_PACKET_FILTER_PM
;
205 /* Set the 64 bits of the HASH tab. To be updated if taller
208 writel(0xffffffff, ioaddr
+ GMAC_HASH_TAB_0_31
);
209 writel(0xffffffff, ioaddr
+ GMAC_HASH_TAB_32_63
);
210 } else if (!netdev_mc_empty(dev
)) {
212 struct netdev_hw_addr
*ha
;
214 /* Hash filter for multicast */
215 value
= GMAC_PACKET_FILTER_HMC
;
217 memset(mc_filter
, 0, sizeof(mc_filter
));
218 netdev_for_each_mc_addr(ha
, dev
) {
219 /* The upper 6 bits of the calculated CRC are used to
220 * index the content of the Hash Table Reg 0 and 1.
223 (bitrev32(~crc32_le(~0, ha
->addr
, 6)) >> 26);
224 /* The most significant bit determines the register
225 * to use while the other 5 bits determines the bit
226 * within the selected register
228 mc_filter
[bit_nr
>> 5] |= (1 << (bit_nr
& 0x1F));
230 writel(mc_filter
[0], ioaddr
+ GMAC_HASH_TAB_0_31
);
231 writel(mc_filter
[1], ioaddr
+ GMAC_HASH_TAB_32_63
);
234 /* Handle multiple unicast addresses */
235 if (netdev_uc_count(dev
) > GMAC_MAX_PERFECT_ADDRESSES
) {
236 /* Switch to promiscuous mode if more than 128 addrs
239 value
|= GMAC_PACKET_FILTER_PR
;
240 } else if (!netdev_uc_empty(dev
)) {
242 struct netdev_hw_addr
*ha
;
244 netdev_for_each_uc_addr(ha
, dev
) {
245 dwmac4_set_umac_addr(hw
, ha
->addr
, reg
);
250 writel(value
, ioaddr
+ GMAC_PACKET_FILTER
);
253 static void dwmac4_flow_ctrl(struct mac_device_info
*hw
, unsigned int duplex
,
254 unsigned int fc
, unsigned int pause_time
)
256 void __iomem
*ioaddr
= hw
->pcsr
;
257 u32 channel
= STMMAC_CHAN0
; /* FIXME */
258 unsigned int flow
= 0;
260 pr_debug("GMAC Flow-Control:\n");
262 pr_debug("\tReceive Flow-Control ON\n");
263 flow
|= GMAC_RX_FLOW_CTRL_RFE
;
264 writel(flow
, ioaddr
+ GMAC_RX_FLOW_CTRL
);
267 pr_debug("\tTransmit Flow-Control ON\n");
268 flow
|= GMAC_TX_FLOW_CTRL_TFE
;
269 writel(flow
, ioaddr
+ GMAC_QX_TX_FLOW_CTRL(channel
));
272 pr_debug("\tduplex mode: PAUSE %d\n", pause_time
);
273 flow
|= (pause_time
<< GMAC_TX_FLOW_CTRL_PT_SHIFT
);
274 writel(flow
, ioaddr
+ GMAC_QX_TX_FLOW_CTRL(channel
));
279 static void dwmac4_ctrl_ane(void __iomem
*ioaddr
, bool ane
, bool srgmi_ral
,
282 dwmac_ctrl_ane(ioaddr
, GMAC_PCS_BASE
, ane
, srgmi_ral
, loopback
);
285 static void dwmac4_rane(void __iomem
*ioaddr
, bool restart
)
287 dwmac_rane(ioaddr
, GMAC_PCS_BASE
, restart
);
290 static void dwmac4_get_adv_lp(void __iomem
*ioaddr
, struct rgmii_adv
*adv
)
292 dwmac_get_adv_lp(ioaddr
, GMAC_PCS_BASE
, adv
);
295 /* RGMII or SMII interface */
296 static void dwmac4_phystatus(void __iomem
*ioaddr
, struct stmmac_extra_stats
*x
)
300 status
= readl(ioaddr
+ GMAC_PHYIF_CONTROL_STATUS
);
303 /* Check the link status */
304 if (status
& GMAC_PHYIF_CTRLSTATUS_LNKSTS
) {
309 speed_value
= ((status
& GMAC_PHYIF_CTRLSTATUS_SPEED
) >>
310 GMAC_PHYIF_CTRLSTATUS_SPEED_SHIFT
);
311 if (speed_value
== GMAC_PHYIF_CTRLSTATUS_SPEED_125
)
312 x
->pcs_speed
= SPEED_1000
;
313 else if (speed_value
== GMAC_PHYIF_CTRLSTATUS_SPEED_25
)
314 x
->pcs_speed
= SPEED_100
;
316 x
->pcs_speed
= SPEED_10
;
318 x
->pcs_duplex
= (status
& GMAC_PHYIF_CTRLSTATUS_LNKMOD_MASK
);
320 pr_info("Link is Up - %d/%s\n", (int)x
->pcs_speed
,
321 x
->pcs_duplex
? "Full" : "Half");
324 pr_info("Link is Down\n");
328 static int dwmac4_irq_status(struct mac_device_info
*hw
,
329 struct stmmac_extra_stats
*x
)
331 void __iomem
*ioaddr
= hw
->pcsr
;
332 u32 mtl_int_qx_status
;
336 intr_status
= readl(ioaddr
+ GMAC_INT_STATUS
);
338 /* Not used events (e.g. MMC interrupts) are not handled. */
339 if ((intr_status
& mmc_tx_irq
))
341 if (unlikely(intr_status
& mmc_rx_irq
))
343 if (unlikely(intr_status
& mmc_rx_csum_offload_irq
))
344 x
->mmc_rx_csum_offload_irq_n
++;
345 /* Clear the PMT bits 5 and 6 by reading the PMT status reg */
346 if (unlikely(intr_status
& pmt_irq
)) {
347 readl(ioaddr
+ GMAC_PMT
);
348 x
->irq_receive_pmt_irq_n
++;
351 mtl_int_qx_status
= readl(ioaddr
+ MTL_INT_STATUS
);
352 /* Check MTL Interrupt: Currently only one queue is used: Q0. */
353 if (mtl_int_qx_status
& MTL_INT_Q0
) {
354 /* read Queue 0 Interrupt status */
355 u32 status
= readl(ioaddr
+ MTL_CHAN_INT_CTRL(STMMAC_CHAN0
));
357 if (status
& MTL_RX_OVERFLOW_INT
) {
358 /* clear Interrupt */
359 writel(status
| MTL_RX_OVERFLOW_INT
,
360 ioaddr
+ MTL_CHAN_INT_CTRL(STMMAC_CHAN0
));
361 ret
= CORE_IRQ_MTL_RX_OVERFLOW
;
365 dwmac_pcs_isr(ioaddr
, GMAC_PCS_BASE
, intr_status
, x
);
366 if (intr_status
& PCS_RGSMIIIS_IRQ
)
367 dwmac4_phystatus(ioaddr
, x
);
372 static void dwmac4_debug(void __iomem
*ioaddr
, struct stmmac_extra_stats
*x
)
376 /* Currently only channel 0 is supported */
377 value
= readl(ioaddr
+ MTL_CHAN_TX_DEBUG(STMMAC_CHAN0
));
379 if (value
& MTL_DEBUG_TXSTSFSTS
)
380 x
->mtl_tx_status_fifo_full
++;
381 if (value
& MTL_DEBUG_TXFSTS
)
382 x
->mtl_tx_fifo_not_empty
++;
383 if (value
& MTL_DEBUG_TWCSTS
)
385 if (value
& MTL_DEBUG_TRCSTS_MASK
) {
386 u32 trcsts
= (value
& MTL_DEBUG_TRCSTS_MASK
)
387 >> MTL_DEBUG_TRCSTS_SHIFT
;
388 if (trcsts
== MTL_DEBUG_TRCSTS_WRITE
)
389 x
->mtl_tx_fifo_read_ctrl_write
++;
390 else if (trcsts
== MTL_DEBUG_TRCSTS_TXW
)
391 x
->mtl_tx_fifo_read_ctrl_wait
++;
392 else if (trcsts
== MTL_DEBUG_TRCSTS_READ
)
393 x
->mtl_tx_fifo_read_ctrl_read
++;
395 x
->mtl_tx_fifo_read_ctrl_idle
++;
397 if (value
& MTL_DEBUG_TXPAUSED
)
398 x
->mac_tx_in_pause
++;
400 value
= readl(ioaddr
+ MTL_CHAN_RX_DEBUG(STMMAC_CHAN0
));
402 if (value
& MTL_DEBUG_RXFSTS_MASK
) {
403 u32 rxfsts
= (value
& MTL_DEBUG_RXFSTS_MASK
)
404 >> MTL_DEBUG_RRCSTS_SHIFT
;
406 if (rxfsts
== MTL_DEBUG_RXFSTS_FULL
)
407 x
->mtl_rx_fifo_fill_level_full
++;
408 else if (rxfsts
== MTL_DEBUG_RXFSTS_AT
)
409 x
->mtl_rx_fifo_fill_above_thresh
++;
410 else if (rxfsts
== MTL_DEBUG_RXFSTS_BT
)
411 x
->mtl_rx_fifo_fill_below_thresh
++;
413 x
->mtl_rx_fifo_fill_level_empty
++;
415 if (value
& MTL_DEBUG_RRCSTS_MASK
) {
416 u32 rrcsts
= (value
& MTL_DEBUG_RRCSTS_MASK
) >>
417 MTL_DEBUG_RRCSTS_SHIFT
;
419 if (rrcsts
== MTL_DEBUG_RRCSTS_FLUSH
)
420 x
->mtl_rx_fifo_read_ctrl_flush
++;
421 else if (rrcsts
== MTL_DEBUG_RRCSTS_RSTAT
)
422 x
->mtl_rx_fifo_read_ctrl_read_data
++;
423 else if (rrcsts
== MTL_DEBUG_RRCSTS_RDATA
)
424 x
->mtl_rx_fifo_read_ctrl_status
++;
426 x
->mtl_rx_fifo_read_ctrl_idle
++;
428 if (value
& MTL_DEBUG_RWCSTS
)
429 x
->mtl_rx_fifo_ctrl_active
++;
432 value
= readl(ioaddr
+ GMAC_DEBUG
);
434 if (value
& GMAC_DEBUG_TFCSTS_MASK
) {
435 u32 tfcsts
= (value
& GMAC_DEBUG_TFCSTS_MASK
)
436 >> GMAC_DEBUG_TFCSTS_SHIFT
;
438 if (tfcsts
== GMAC_DEBUG_TFCSTS_XFER
)
439 x
->mac_tx_frame_ctrl_xfer
++;
440 else if (tfcsts
== GMAC_DEBUG_TFCSTS_GEN_PAUSE
)
441 x
->mac_tx_frame_ctrl_pause
++;
442 else if (tfcsts
== GMAC_DEBUG_TFCSTS_WAIT
)
443 x
->mac_tx_frame_ctrl_wait
++;
445 x
->mac_tx_frame_ctrl_idle
++;
447 if (value
& GMAC_DEBUG_TPESTS
)
448 x
->mac_gmii_tx_proto_engine
++;
449 if (value
& GMAC_DEBUG_RFCFCSTS_MASK
)
450 x
->mac_rx_frame_ctrl_fifo
= (value
& GMAC_DEBUG_RFCFCSTS_MASK
)
451 >> GMAC_DEBUG_RFCFCSTS_SHIFT
;
452 if (value
& GMAC_DEBUG_RPESTS
)
453 x
->mac_gmii_rx_proto_engine
++;
456 static const struct stmmac_ops dwmac4_ops
= {
457 .core_init
= dwmac4_core_init
,
458 .rx_ipc
= dwmac4_rx_ipc_enable
,
459 .rx_queue_enable
= dwmac4_rx_queue_enable
,
460 .dump_regs
= dwmac4_dump_regs
,
461 .host_irq_status
= dwmac4_irq_status
,
462 .flow_ctrl
= dwmac4_flow_ctrl
,
464 .set_umac_addr
= dwmac4_set_umac_addr
,
465 .get_umac_addr
= dwmac4_get_umac_addr
,
466 .set_eee_mode
= dwmac4_set_eee_mode
,
467 .reset_eee_mode
= dwmac4_reset_eee_mode
,
468 .set_eee_timer
= dwmac4_set_eee_timer
,
469 .set_eee_pls
= dwmac4_set_eee_pls
,
470 .pcs_ctrl_ane
= dwmac4_ctrl_ane
,
471 .pcs_rane
= dwmac4_rane
,
472 .pcs_get_adv_lp
= dwmac4_get_adv_lp
,
473 .debug
= dwmac4_debug
,
474 .set_filter
= dwmac4_set_filter
,
477 struct mac_device_info
*dwmac4_setup(void __iomem
*ioaddr
, int mcbins
,
478 int perfect_uc_entries
, int *synopsys_id
)
480 struct mac_device_info
*mac
;
481 u32 hwid
= readl(ioaddr
+ GMAC_VERSION
);
483 mac
= kzalloc(sizeof(const struct mac_device_info
), GFP_KERNEL
);
488 mac
->multicast_filter_bins
= mcbins
;
489 mac
->unicast_filter_entries
= perfect_uc_entries
;
490 mac
->mcast_bits_log2
= 0;
492 if (mac
->multicast_filter_bins
)
493 mac
->mcast_bits_log2
= ilog2(mac
->multicast_filter_bins
);
495 mac
->mac
= &dwmac4_ops
;
497 mac
->link
.port
= GMAC_CONFIG_PS
;
498 mac
->link
.duplex
= GMAC_CONFIG_DM
;
499 mac
->link
.speed
= GMAC_CONFIG_FES
;
500 mac
->mii
.addr
= GMAC_MDIO_ADDR
;
501 mac
->mii
.data
= GMAC_MDIO_DATA
;
502 mac
->mii
.addr_shift
= 21;
503 mac
->mii
.addr_mask
= GENMASK(25, 21);
504 mac
->mii
.reg_shift
= 16;
505 mac
->mii
.reg_mask
= GENMASK(20, 16);
506 mac
->mii
.clk_csr_shift
= 8;
507 mac
->mii
.clk_csr_mask
= GENMASK(11, 8);
509 /* Get and dump the chip ID */
510 *synopsys_id
= stmmac_get_synopsys_id(hwid
);
512 if (*synopsys_id
> DWMAC_CORE_4_00
)
513 mac
->dma
= &dwmac410_dma_ops
;
515 mac
->dma
= &dwmac4_dma_ops
;