1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
5 /* Qualcomm Technologies, Inc. EMAC Gigabit Ethernet Driver */
7 #include <linux/if_ether.h>
8 #include <linux/if_vlan.h>
9 #include <linux/interrupt.h>
11 #include <linux/module.h>
13 #include <linux/of_net.h>
14 #include <linux/of_device.h>
15 #include <linux/phy.h>
16 #include <linux/platform_device.h>
17 #include <linux/acpi.h>
21 #include "emac-sgmii.h"
23 #define EMAC_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
24 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
26 #define EMAC_RRD_SIZE 4
27 /* The RRD size if timestamping is enabled: */
28 #define EMAC_TS_RRD_SIZE 6
29 #define EMAC_TPD_SIZE 4
30 #define EMAC_RFD_SIZE 2
32 #define REG_MAC_RX_STATUS_BIN EMAC_RXMAC_STATC_REG0
33 #define REG_MAC_RX_STATUS_END EMAC_RXMAC_STATC_REG22
34 #define REG_MAC_TX_STATUS_BIN EMAC_TXMAC_STATC_REG0
35 #define REG_MAC_TX_STATUS_END EMAC_TXMAC_STATC_REG24
37 #define RXQ0_NUM_RFD_PREF_DEF 8
38 #define TXQ0_NUM_TPD_PREF_DEF 5
40 #define EMAC_PREAMBLE_DEF 7
42 #define DMAR_DLY_CNT_DEF 15
43 #define DMAW_DLY_CNT_DEF 4
45 #define IMR_NORMAL_MASK (ISR_ERROR | ISR_OVER | ISR_TX_PKT)
67 /* in sync with enum emac_clk_id */
68 static const char * const emac_clk_name
[] = {
69 "axi_clk", "cfg_ahb_clk", "high_speed_clk", "mdio_clk", "tx_clk",
73 void emac_reg_update32(void __iomem
*addr
, u32 mask
, u32 val
)
75 u32 data
= readl(addr
);
77 writel(((data
& ~mask
) | val
), addr
);
81 int emac_reinit_locked(struct emac_adapter
*adpt
)
85 mutex_lock(&adpt
->reset_lock
);
88 emac_sgmii_reset(adpt
);
89 ret
= emac_mac_up(adpt
);
91 mutex_unlock(&adpt
->reset_lock
);
97 static int emac_napi_rtx(struct napi_struct
*napi
, int budget
)
99 struct emac_rx_queue
*rx_q
=
100 container_of(napi
, struct emac_rx_queue
, napi
);
101 struct emac_adapter
*adpt
= netdev_priv(rx_q
->netdev
);
102 struct emac_irq
*irq
= rx_q
->irq
;
105 emac_mac_rx_process(adpt
, rx_q
, &work_done
, budget
);
107 if (work_done
< budget
) {
108 napi_complete_done(napi
, work_done
);
110 irq
->mask
|= rx_q
->intr
;
111 writel(irq
->mask
, adpt
->base
+ EMAC_INT_MASK
);
117 /* Transmit the packet */
118 static int emac_start_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
120 struct emac_adapter
*adpt
= netdev_priv(netdev
);
122 return emac_mac_tx_buf_send(adpt
, &adpt
->tx_q
, skb
);
125 static irqreturn_t
emac_isr(int _irq
, void *data
)
127 struct emac_irq
*irq
= data
;
128 struct emac_adapter
*adpt
=
129 container_of(irq
, struct emac_adapter
, irq
);
130 struct emac_rx_queue
*rx_q
= &adpt
->rx_q
;
133 /* disable the interrupt */
134 writel(0, adpt
->base
+ EMAC_INT_MASK
);
136 isr
= readl_relaxed(adpt
->base
+ EMAC_INT_STATUS
);
138 status
= isr
& irq
->mask
;
142 if (status
& ISR_ERROR
) {
143 net_err_ratelimited("%s: error interrupt 0x%lx\n",
144 adpt
->netdev
->name
, status
& ISR_ERROR
);
146 schedule_work(&adpt
->work_thread
);
149 /* Schedule the napi for receive queue with interrupt
152 if (status
& rx_q
->intr
) {
153 if (napi_schedule_prep(&rx_q
->napi
)) {
154 irq
->mask
&= ~rx_q
->intr
;
155 __napi_schedule(&rx_q
->napi
);
159 if (status
& TX_PKT_INT
)
160 emac_mac_tx_process(adpt
, &adpt
->tx_q
);
162 if (status
& ISR_OVER
)
163 net_warn_ratelimited("%s: TX/RX overflow interrupt\n",
167 /* enable the interrupt */
168 writel(irq
->mask
, adpt
->base
+ EMAC_INT_MASK
);
173 /* Configure VLAN tag strip/insert feature */
174 static int emac_set_features(struct net_device
*netdev
,
175 netdev_features_t features
)
177 netdev_features_t changed
= features
^ netdev
->features
;
178 struct emac_adapter
*adpt
= netdev_priv(netdev
);
180 /* We only need to reprogram the hardware if the VLAN tag features
181 * have changed, and if it's already running.
183 if (!(changed
& (NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_CTAG_RX
)))
186 if (!netif_running(netdev
))
189 /* emac_mac_mode_config() uses netdev->features to configure the EMAC,
190 * so make sure it's set first.
192 netdev
->features
= features
;
194 return emac_reinit_locked(adpt
);
197 /* Configure Multicast and Promiscuous modes */
198 static void emac_rx_mode_set(struct net_device
*netdev
)
200 struct emac_adapter
*adpt
= netdev_priv(netdev
);
201 struct netdev_hw_addr
*ha
;
203 emac_mac_mode_config(adpt
);
205 /* update multicast address filtering */
206 emac_mac_multicast_addr_clear(adpt
);
207 netdev_for_each_mc_addr(ha
, netdev
)
208 emac_mac_multicast_addr_set(adpt
, ha
->addr
);
211 /* Change the Maximum Transfer Unit (MTU) */
212 static int emac_change_mtu(struct net_device
*netdev
, int new_mtu
)
214 struct emac_adapter
*adpt
= netdev_priv(netdev
);
216 netif_dbg(adpt
, hw
, adpt
->netdev
,
217 "changing MTU from %d to %d\n", netdev
->mtu
,
219 netdev
->mtu
= new_mtu
;
221 if (netif_running(netdev
))
222 return emac_reinit_locked(adpt
);
227 /* Called when the network interface is made active */
228 static int emac_open(struct net_device
*netdev
)
230 struct emac_adapter
*adpt
= netdev_priv(netdev
);
231 struct emac_irq
*irq
= &adpt
->irq
;
234 ret
= request_irq(irq
->irq
, emac_isr
, 0, "emac-core0", irq
);
236 netdev_err(adpt
->netdev
, "could not request emac-core0 irq\n");
240 /* allocate rx/tx dma buffer & descriptors */
241 ret
= emac_mac_rx_tx_rings_alloc_all(adpt
);
243 netdev_err(adpt
->netdev
, "error allocating rx/tx rings\n");
244 free_irq(irq
->irq
, irq
);
248 ret
= emac_sgmii_open(adpt
);
250 emac_mac_rx_tx_rings_free_all(adpt
);
251 free_irq(irq
->irq
, irq
);
255 ret
= emac_mac_up(adpt
);
257 emac_mac_rx_tx_rings_free_all(adpt
);
258 free_irq(irq
->irq
, irq
);
259 emac_sgmii_close(adpt
);
266 /* Called when the network interface is disabled */
267 static int emac_close(struct net_device
*netdev
)
269 struct emac_adapter
*adpt
= netdev_priv(netdev
);
271 mutex_lock(&adpt
->reset_lock
);
273 emac_sgmii_close(adpt
);
275 emac_mac_rx_tx_rings_free_all(adpt
);
277 free_irq(adpt
->irq
.irq
, &adpt
->irq
);
279 mutex_unlock(&adpt
->reset_lock
);
284 /* Respond to a TX hang */
285 static void emac_tx_timeout(struct net_device
*netdev
, unsigned int txqueue
)
287 struct emac_adapter
*adpt
= netdev_priv(netdev
);
289 schedule_work(&adpt
->work_thread
);
293 * emac_update_hw_stats - read the EMAC stat registers
295 * Reads the stats registers and write the values to adpt->stats.
297 * adpt->stats.lock must be held while calling this function,
298 * and while reading from adpt->stats.
300 void emac_update_hw_stats(struct emac_adapter
*adpt
)
302 struct emac_stats
*stats
= &adpt
->stats
;
303 u64
*stats_itr
= &adpt
->stats
.rx_ok
;
304 void __iomem
*base
= adpt
->base
;
307 addr
= REG_MAC_RX_STATUS_BIN
;
308 while (addr
<= REG_MAC_RX_STATUS_END
) {
309 *stats_itr
+= readl_relaxed(base
+ addr
);
314 /* additional rx status */
315 stats
->rx_crc_align
+= readl_relaxed(base
+ EMAC_RXMAC_STATC_REG23
);
316 stats
->rx_jabbers
+= readl_relaxed(base
+ EMAC_RXMAC_STATC_REG24
);
318 /* update tx status */
319 addr
= REG_MAC_TX_STATUS_BIN
;
320 stats_itr
= &stats
->tx_ok
;
322 while (addr
<= REG_MAC_TX_STATUS_END
) {
323 *stats_itr
+= readl_relaxed(base
+ addr
);
328 /* additional tx status */
329 stats
->tx_col
+= readl_relaxed(base
+ EMAC_TXMAC_STATC_REG25
);
332 /* Provide network statistics info for the interface */
333 static void emac_get_stats64(struct net_device
*netdev
,
334 struct rtnl_link_stats64
*net_stats
)
336 struct emac_adapter
*adpt
= netdev_priv(netdev
);
337 struct emac_stats
*stats
= &adpt
->stats
;
339 spin_lock(&stats
->lock
);
341 emac_update_hw_stats(adpt
);
343 /* return parsed statistics */
344 net_stats
->rx_packets
= stats
->rx_ok
;
345 net_stats
->tx_packets
= stats
->tx_ok
;
346 net_stats
->rx_bytes
= stats
->rx_byte_cnt
;
347 net_stats
->tx_bytes
= stats
->tx_byte_cnt
;
348 net_stats
->multicast
= stats
->rx_mcast
;
349 net_stats
->collisions
= stats
->tx_1_col
+ stats
->tx_2_col
* 2 +
350 stats
->tx_late_col
+ stats
->tx_abort_col
;
352 net_stats
->rx_errors
= stats
->rx_frag
+ stats
->rx_fcs_err
+
353 stats
->rx_len_err
+ stats
->rx_sz_ov
+
355 net_stats
->rx_fifo_errors
= stats
->rx_rxf_ov
;
356 net_stats
->rx_length_errors
= stats
->rx_len_err
;
357 net_stats
->rx_crc_errors
= stats
->rx_fcs_err
;
358 net_stats
->rx_frame_errors
= stats
->rx_align_err
;
359 net_stats
->rx_over_errors
= stats
->rx_rxf_ov
;
360 net_stats
->rx_missed_errors
= stats
->rx_rxf_ov
;
362 net_stats
->tx_errors
= stats
->tx_late_col
+ stats
->tx_abort_col
+
363 stats
->tx_underrun
+ stats
->tx_trunc
;
364 net_stats
->tx_fifo_errors
= stats
->tx_underrun
;
365 net_stats
->tx_aborted_errors
= stats
->tx_abort_col
;
366 net_stats
->tx_window_errors
= stats
->tx_late_col
;
368 spin_unlock(&stats
->lock
);
371 static const struct net_device_ops emac_netdev_ops
= {
372 .ndo_open
= emac_open
,
373 .ndo_stop
= emac_close
,
374 .ndo_validate_addr
= eth_validate_addr
,
375 .ndo_start_xmit
= emac_start_xmit
,
376 .ndo_set_mac_address
= eth_mac_addr
,
377 .ndo_change_mtu
= emac_change_mtu
,
378 .ndo_do_ioctl
= phy_do_ioctl_running
,
379 .ndo_tx_timeout
= emac_tx_timeout
,
380 .ndo_get_stats64
= emac_get_stats64
,
381 .ndo_set_features
= emac_set_features
,
382 .ndo_set_rx_mode
= emac_rx_mode_set
,
385 /* Watchdog task routine, called to reinitialize the EMAC */
386 static void emac_work_thread(struct work_struct
*work
)
388 struct emac_adapter
*adpt
=
389 container_of(work
, struct emac_adapter
, work_thread
);
391 emac_reinit_locked(adpt
);
394 /* Initialize various data structures */
395 static void emac_init_adapter(struct emac_adapter
*adpt
)
399 adpt
->rrd_size
= EMAC_RRD_SIZE
;
400 adpt
->tpd_size
= EMAC_TPD_SIZE
;
401 adpt
->rfd_size
= EMAC_RFD_SIZE
;
404 adpt
->tx_desc_cnt
= EMAC_DEF_TX_DESCS
;
405 adpt
->rx_desc_cnt
= EMAC_DEF_RX_DESCS
;
408 adpt
->dma_order
= emac_dma_ord_out
;
409 adpt
->dmar_block
= emac_dma_req_4096
;
410 adpt
->dmaw_block
= emac_dma_req_128
;
411 adpt
->dmar_dly_cnt
= DMAR_DLY_CNT_DEF
;
412 adpt
->dmaw_dly_cnt
= DMAW_DLY_CNT_DEF
;
413 adpt
->tpd_burst
= TXQ0_NUM_TPD_PREF_DEF
;
414 adpt
->rfd_burst
= RXQ0_NUM_RFD_PREF_DEF
;
417 reg
= ((EMAC_DEF_RX_IRQ_MOD
>> 1) << IRQ_MODERATOR2_INIT_SHFT
) |
418 ((EMAC_DEF_TX_IRQ_MOD
>> 1) << IRQ_MODERATOR_INIT_SHFT
);
422 adpt
->preamble
= EMAC_PREAMBLE_DEF
;
424 /* default to automatic flow control */
425 adpt
->automatic
= true;
427 /* Disable single-pause-frame mode by default */
428 adpt
->single_pause_mode
= false;
432 static int emac_clks_get(struct platform_device
*pdev
,
433 struct emac_adapter
*adpt
)
437 for (i
= 0; i
< EMAC_CLK_CNT
; i
++) {
438 struct clk
*clk
= devm_clk_get(&pdev
->dev
, emac_clk_name
[i
]);
442 "could not claim clock %s (error=%li)\n",
443 emac_clk_name
[i
], PTR_ERR(clk
));
454 /* Initialize clocks */
455 static int emac_clks_phase1_init(struct platform_device
*pdev
,
456 struct emac_adapter
*adpt
)
460 /* On ACPI platforms, clocks are controlled by firmware and/or
461 * ACPI, not by drivers.
463 if (has_acpi_companion(&pdev
->dev
))
466 ret
= emac_clks_get(pdev
, adpt
);
470 ret
= clk_prepare_enable(adpt
->clk
[EMAC_CLK_AXI
]);
474 ret
= clk_prepare_enable(adpt
->clk
[EMAC_CLK_CFG_AHB
]);
478 ret
= clk_set_rate(adpt
->clk
[EMAC_CLK_HIGH_SPEED
], 19200000);
482 return clk_prepare_enable(adpt
->clk
[EMAC_CLK_HIGH_SPEED
]);
485 /* Enable clocks; needs emac_clks_phase1_init to be called before */
486 static int emac_clks_phase2_init(struct platform_device
*pdev
,
487 struct emac_adapter
*adpt
)
491 if (has_acpi_companion(&pdev
->dev
))
494 ret
= clk_set_rate(adpt
->clk
[EMAC_CLK_TX
], 125000000);
498 ret
= clk_prepare_enable(adpt
->clk
[EMAC_CLK_TX
]);
502 ret
= clk_set_rate(adpt
->clk
[EMAC_CLK_HIGH_SPEED
], 125000000);
506 ret
= clk_set_rate(adpt
->clk
[EMAC_CLK_MDIO
], 25000000);
510 ret
= clk_prepare_enable(adpt
->clk
[EMAC_CLK_MDIO
]);
514 ret
= clk_prepare_enable(adpt
->clk
[EMAC_CLK_RX
]);
518 return clk_prepare_enable(adpt
->clk
[EMAC_CLK_SYS
]);
521 static void emac_clks_teardown(struct emac_adapter
*adpt
)
526 for (i
= 0; i
< EMAC_CLK_CNT
; i
++)
527 clk_disable_unprepare(adpt
->clk
[i
]);
530 /* Get the resources */
531 static int emac_probe_resources(struct platform_device
*pdev
,
532 struct emac_adapter
*adpt
)
534 struct net_device
*netdev
= adpt
->netdev
;
535 char maddr
[ETH_ALEN
];
538 /* get mac address */
539 if (device_get_mac_address(&pdev
->dev
, maddr
, ETH_ALEN
))
540 ether_addr_copy(netdev
->dev_addr
, maddr
);
542 eth_hw_addr_random(netdev
);
544 /* Core 0 interrupt */
545 ret
= platform_get_irq(pdev
, 0);
550 /* base register address */
551 adpt
->base
= devm_platform_ioremap_resource(pdev
, 0);
552 if (IS_ERR(adpt
->base
))
553 return PTR_ERR(adpt
->base
);
555 /* CSR register address */
556 adpt
->csr
= devm_platform_ioremap_resource(pdev
, 1);
557 if (IS_ERR(adpt
->csr
))
558 return PTR_ERR(adpt
->csr
);
560 netdev
->base_addr
= (unsigned long)adpt
->base
;
565 static const struct of_device_id emac_dt_match
[] = {
567 .compatible
= "qcom,fsm9900-emac",
571 MODULE_DEVICE_TABLE(of
, emac_dt_match
);
573 #if IS_ENABLED(CONFIG_ACPI)
574 static const struct acpi_device_id emac_acpi_match
[] = {
580 MODULE_DEVICE_TABLE(acpi
, emac_acpi_match
);
583 static int emac_probe(struct platform_device
*pdev
)
585 struct net_device
*netdev
;
586 struct emac_adapter
*adpt
;
587 struct emac_sgmii
*phy
;
592 /* The TPD buffer address is limited to:
593 * 1. PTP: 45bits. (Driver doesn't support yet.)
594 * 2. NON-PTP: 46bits.
596 ret
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(46));
598 dev_err(&pdev
->dev
, "could not set DMA mask\n");
602 netdev
= alloc_etherdev(sizeof(struct emac_adapter
));
606 dev_set_drvdata(&pdev
->dev
, netdev
);
607 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
608 emac_set_ethtool_ops(netdev
);
610 adpt
= netdev_priv(netdev
);
611 adpt
->netdev
= netdev
;
612 adpt
->msg_enable
= EMAC_MSG_DEFAULT
;
615 atomic_set(&phy
->decode_error_count
, 0);
617 mutex_init(&adpt
->reset_lock
);
618 spin_lock_init(&adpt
->stats
.lock
);
620 adpt
->irq
.mask
= RX_PKT_INT0
| IMR_NORMAL_MASK
;
622 ret
= emac_probe_resources(pdev
, adpt
);
624 goto err_undo_netdev
;
626 /* initialize clocks */
627 ret
= emac_clks_phase1_init(pdev
, adpt
);
629 dev_err(&pdev
->dev
, "could not initialize clocks\n");
630 goto err_undo_netdev
;
633 netdev
->watchdog_timeo
= EMAC_WATCHDOG_TIME
;
634 netdev
->irq
= adpt
->irq
.irq
;
636 netdev
->netdev_ops
= &emac_netdev_ops
;
638 emac_init_adapter(adpt
);
640 /* init external phy */
641 ret
= emac_phy_config(pdev
, adpt
);
643 goto err_undo_clocks
;
645 /* init internal sgmii phy */
646 ret
= emac_sgmii_config(pdev
, adpt
);
648 goto err_undo_mdiobus
;
651 ret
= emac_clks_phase2_init(pdev
, adpt
);
653 dev_err(&pdev
->dev
, "could not initialize clocks\n");
654 goto err_undo_mdiobus
;
657 /* set hw features */
658 netdev
->features
= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
|
659 NETIF_F_TSO
| NETIF_F_TSO6
| NETIF_F_HW_VLAN_CTAG_RX
|
660 NETIF_F_HW_VLAN_CTAG_TX
;
661 netdev
->hw_features
= netdev
->features
;
663 netdev
->vlan_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
|
664 NETIF_F_TSO
| NETIF_F_TSO6
;
666 /* MTU range: 46 - 9194 */
667 netdev
->min_mtu
= EMAC_MIN_ETH_FRAME_SIZE
-
668 (ETH_HLEN
+ ETH_FCS_LEN
+ VLAN_HLEN
);
669 netdev
->max_mtu
= EMAC_MAX_ETH_FRAME_SIZE
-
670 (ETH_HLEN
+ ETH_FCS_LEN
+ VLAN_HLEN
);
672 INIT_WORK(&adpt
->work_thread
, emac_work_thread
);
674 /* Initialize queues */
675 emac_mac_rx_tx_ring_init_all(pdev
, adpt
);
677 netif_napi_add(netdev
, &adpt
->rx_q
.napi
, emac_napi_rtx
,
680 ret
= register_netdev(netdev
);
682 dev_err(&pdev
->dev
, "could not register net device\n");
686 reg
= readl_relaxed(adpt
->base
+ EMAC_DMA_MAS_CTRL
);
687 devid
= (reg
& DEV_ID_NUM_BMSK
) >> DEV_ID_NUM_SHFT
;
688 revid
= (reg
& DEV_REV_NUM_BMSK
) >> DEV_REV_NUM_SHFT
;
689 reg
= readl_relaxed(adpt
->base
+ EMAC_CORE_HW_VERSION
);
691 netif_info(adpt
, probe
, netdev
,
692 "hardware id %d.%d, hardware version %d.%d.%d\n",
694 (reg
& MAJOR_BMSK
) >> MAJOR_SHFT
,
695 (reg
& MINOR_BMSK
) >> MINOR_SHFT
,
696 (reg
& STEP_BMSK
) >> STEP_SHFT
);
701 netif_napi_del(&adpt
->rx_q
.napi
);
703 put_device(&adpt
->phydev
->mdio
.dev
);
704 mdiobus_unregister(adpt
->mii_bus
);
706 emac_clks_teardown(adpt
);
713 static int emac_remove(struct platform_device
*pdev
)
715 struct net_device
*netdev
= dev_get_drvdata(&pdev
->dev
);
716 struct emac_adapter
*adpt
= netdev_priv(netdev
);
718 unregister_netdev(netdev
);
719 netif_napi_del(&adpt
->rx_q
.napi
);
721 emac_clks_teardown(adpt
);
723 put_device(&adpt
->phydev
->mdio
.dev
);
724 mdiobus_unregister(adpt
->mii_bus
);
727 if (adpt
->phy
.digital
)
728 iounmap(adpt
->phy
.digital
);
729 iounmap(adpt
->phy
.base
);
734 static void emac_shutdown(struct platform_device
*pdev
)
736 struct net_device
*netdev
= dev_get_drvdata(&pdev
->dev
);
737 struct emac_adapter
*adpt
= netdev_priv(netdev
);
739 if (netdev
->flags
& IFF_UP
) {
740 /* Closing the SGMII turns off its interrupts */
741 emac_sgmii_close(adpt
);
743 /* Resetting the MAC turns off all DMA and its interrupts */
744 emac_mac_reset(adpt
);
748 static struct platform_driver emac_platform_driver
= {
750 .remove
= emac_remove
,
753 .of_match_table
= emac_dt_match
,
754 .acpi_match_table
= ACPI_PTR(emac_acpi_match
),
756 .shutdown
= emac_shutdown
,
759 module_platform_driver(emac_platform_driver
);
761 MODULE_LICENSE("GPL v2");
762 MODULE_ALIAS("platform:qcom-emac");