1 /* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
13 /* Qualcomm Technologies, Inc. EMAC Gigabit Ethernet Driver */
15 #include <linux/if_ether.h>
16 #include <linux/if_vlan.h>
17 #include <linux/interrupt.h>
19 #include <linux/module.h>
21 #include <linux/of_net.h>
22 #include <linux/of_device.h>
23 #include <linux/phy.h>
24 #include <linux/platform_device.h>
25 #include <linux/acpi.h>
29 #include "emac-sgmii.h"
31 #define EMAC_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK | \
32 NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
34 #define EMAC_RRD_SIZE 4
35 /* The RRD size if timestamping is enabled: */
36 #define EMAC_TS_RRD_SIZE 6
37 #define EMAC_TPD_SIZE 4
38 #define EMAC_RFD_SIZE 2
40 #define REG_MAC_RX_STATUS_BIN EMAC_RXMAC_STATC_REG0
41 #define REG_MAC_RX_STATUS_END EMAC_RXMAC_STATC_REG22
42 #define REG_MAC_TX_STATUS_BIN EMAC_TXMAC_STATC_REG0
43 #define REG_MAC_TX_STATUS_END EMAC_TXMAC_STATC_REG24
45 #define RXQ0_NUM_RFD_PREF_DEF 8
46 #define TXQ0_NUM_TPD_PREF_DEF 5
48 #define EMAC_PREAMBLE_DEF 7
50 #define DMAR_DLY_CNT_DEF 15
51 #define DMAW_DLY_CNT_DEF 4
53 #define IMR_NORMAL_MASK (\
59 #define IMR_EXTENDED_MASK (\
73 #define ISR_GPHY_LINK (\
91 /* in sync with enum emac_clk_id */
92 static const char * const emac_clk_name
[] = {
93 "axi_clk", "cfg_ahb_clk", "high_speed_clk", "mdio_clk", "tx_clk",
97 void emac_reg_update32(void __iomem
*addr
, u32 mask
, u32 val
)
99 u32 data
= readl(addr
);
101 writel(((data
& ~mask
) | val
), addr
);
105 int emac_reinit_locked(struct emac_adapter
*adpt
)
109 mutex_lock(&adpt
->reset_lock
);
112 emac_sgmii_reset(adpt
);
113 ret
= emac_mac_up(adpt
);
115 mutex_unlock(&adpt
->reset_lock
);
121 static int emac_napi_rtx(struct napi_struct
*napi
, int budget
)
123 struct emac_rx_queue
*rx_q
=
124 container_of(napi
, struct emac_rx_queue
, napi
);
125 struct emac_adapter
*adpt
= netdev_priv(rx_q
->netdev
);
126 struct emac_irq
*irq
= rx_q
->irq
;
129 emac_mac_rx_process(adpt
, rx_q
, &work_done
, budget
);
131 if (work_done
< budget
) {
134 irq
->mask
|= rx_q
->intr
;
135 writel(irq
->mask
, adpt
->base
+ EMAC_INT_MASK
);
141 /* Transmit the packet */
142 static int emac_start_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
144 struct emac_adapter
*adpt
= netdev_priv(netdev
);
146 return emac_mac_tx_buf_send(adpt
, &adpt
->tx_q
, skb
);
149 irqreturn_t
emac_isr(int _irq
, void *data
)
151 struct emac_irq
*irq
= data
;
152 struct emac_adapter
*adpt
=
153 container_of(irq
, struct emac_adapter
, irq
);
154 struct emac_rx_queue
*rx_q
= &adpt
->rx_q
;
157 /* disable the interrupt */
158 writel(0, adpt
->base
+ EMAC_INT_MASK
);
160 isr
= readl_relaxed(adpt
->base
+ EMAC_INT_STATUS
);
162 status
= isr
& irq
->mask
;
166 if (status
& ISR_ERROR
) {
167 netif_warn(adpt
, intr
, adpt
->netdev
,
168 "warning: error irq status 0x%lx\n",
171 schedule_work(&adpt
->work_thread
);
174 /* Schedule the napi for receive queue with interrupt
177 if (status
& rx_q
->intr
) {
178 if (napi_schedule_prep(&rx_q
->napi
)) {
179 irq
->mask
&= ~rx_q
->intr
;
180 __napi_schedule(&rx_q
->napi
);
184 if (status
& TX_PKT_INT
)
185 emac_mac_tx_process(adpt
, &adpt
->tx_q
);
187 if (status
& ISR_OVER
)
188 net_warn_ratelimited("warning: TX/RX overflow\n");
191 if (status
& ISR_GPHY_LINK
)
192 phy_mac_interrupt(adpt
->phydev
, !!(status
& GPHY_LINK_UP_INT
));
195 /* enable the interrupt */
196 writel(irq
->mask
, adpt
->base
+ EMAC_INT_MASK
);
201 /* Configure VLAN tag strip/insert feature */
202 static int emac_set_features(struct net_device
*netdev
,
203 netdev_features_t features
)
205 netdev_features_t changed
= features
^ netdev
->features
;
206 struct emac_adapter
*adpt
= netdev_priv(netdev
);
208 /* We only need to reprogram the hardware if the VLAN tag features
209 * have changed, and if it's already running.
211 if (!(changed
& (NETIF_F_HW_VLAN_CTAG_TX
| NETIF_F_HW_VLAN_CTAG_RX
)))
214 if (!netif_running(netdev
))
217 /* emac_mac_mode_config() uses netdev->features to configure the EMAC,
218 * so make sure it's set first.
220 netdev
->features
= features
;
222 return emac_reinit_locked(adpt
);
225 /* Configure Multicast and Promiscuous modes */
226 static void emac_rx_mode_set(struct net_device
*netdev
)
228 struct emac_adapter
*adpt
= netdev_priv(netdev
);
229 struct netdev_hw_addr
*ha
;
231 emac_mac_mode_config(adpt
);
233 /* update multicast address filtering */
234 emac_mac_multicast_addr_clear(adpt
);
235 netdev_for_each_mc_addr(ha
, netdev
)
236 emac_mac_multicast_addr_set(adpt
, ha
->addr
);
239 /* Change the Maximum Transfer Unit (MTU) */
240 static int emac_change_mtu(struct net_device
*netdev
, int new_mtu
)
242 struct emac_adapter
*adpt
= netdev_priv(netdev
);
244 netif_info(adpt
, hw
, adpt
->netdev
,
245 "changing MTU from %d to %d\n", netdev
->mtu
,
247 netdev
->mtu
= new_mtu
;
249 if (netif_running(netdev
))
250 return emac_reinit_locked(adpt
);
255 /* Called when the network interface is made active */
256 static int emac_open(struct net_device
*netdev
)
258 struct emac_adapter
*adpt
= netdev_priv(netdev
);
261 /* allocate rx/tx dma buffer & descriptors */
262 ret
= emac_mac_rx_tx_rings_alloc_all(adpt
);
264 netdev_err(adpt
->netdev
, "error allocating rx/tx rings\n");
268 ret
= emac_mac_up(adpt
);
270 emac_mac_rx_tx_rings_free_all(adpt
);
274 emac_mac_start(adpt
);
279 /* Called when the network interface is disabled */
280 static int emac_close(struct net_device
*netdev
)
282 struct emac_adapter
*adpt
= netdev_priv(netdev
);
284 mutex_lock(&adpt
->reset_lock
);
287 emac_mac_rx_tx_rings_free_all(adpt
);
289 mutex_unlock(&adpt
->reset_lock
);
294 /* Respond to a TX hang */
295 static void emac_tx_timeout(struct net_device
*netdev
)
297 struct emac_adapter
*adpt
= netdev_priv(netdev
);
299 schedule_work(&adpt
->work_thread
);
302 /* IOCTL support for the interface */
303 static int emac_ioctl(struct net_device
*netdev
, struct ifreq
*ifr
, int cmd
)
305 if (!netif_running(netdev
))
311 return phy_mii_ioctl(netdev
->phydev
, ifr
, cmd
);
314 /* Provide network statistics info for the interface */
315 static struct rtnl_link_stats64
*emac_get_stats64(struct net_device
*netdev
,
316 struct rtnl_link_stats64
*net_stats
)
318 struct emac_adapter
*adpt
= netdev_priv(netdev
);
319 unsigned int addr
= REG_MAC_RX_STATUS_BIN
;
320 struct emac_stats
*stats
= &adpt
->stats
;
321 u64
*stats_itr
= &adpt
->stats
.rx_ok
;
324 spin_lock(&stats
->lock
);
326 while (addr
<= REG_MAC_RX_STATUS_END
) {
327 val
= readl_relaxed(adpt
->base
+ addr
);
333 /* additional rx status */
334 val
= readl_relaxed(adpt
->base
+ EMAC_RXMAC_STATC_REG23
);
335 adpt
->stats
.rx_crc_align
+= val
;
336 val
= readl_relaxed(adpt
->base
+ EMAC_RXMAC_STATC_REG24
);
337 adpt
->stats
.rx_jabbers
+= val
;
339 /* update tx status */
340 addr
= REG_MAC_TX_STATUS_BIN
;
341 stats_itr
= &adpt
->stats
.tx_ok
;
343 while (addr
<= REG_MAC_TX_STATUS_END
) {
344 val
= readl_relaxed(adpt
->base
+ addr
);
350 /* additional tx status */
351 val
= readl_relaxed(adpt
->base
+ EMAC_TXMAC_STATC_REG25
);
352 adpt
->stats
.tx_col
+= val
;
354 /* return parsed statistics */
355 net_stats
->rx_packets
= stats
->rx_ok
;
356 net_stats
->tx_packets
= stats
->tx_ok
;
357 net_stats
->rx_bytes
= stats
->rx_byte_cnt
;
358 net_stats
->tx_bytes
= stats
->tx_byte_cnt
;
359 net_stats
->multicast
= stats
->rx_mcast
;
360 net_stats
->collisions
= stats
->tx_1_col
+ stats
->tx_2_col
* 2 +
361 stats
->tx_late_col
+ stats
->tx_abort_col
;
363 net_stats
->rx_errors
= stats
->rx_frag
+ stats
->rx_fcs_err
+
364 stats
->rx_len_err
+ stats
->rx_sz_ov
+
366 net_stats
->rx_fifo_errors
= stats
->rx_rxf_ov
;
367 net_stats
->rx_length_errors
= stats
->rx_len_err
;
368 net_stats
->rx_crc_errors
= stats
->rx_fcs_err
;
369 net_stats
->rx_frame_errors
= stats
->rx_align_err
;
370 net_stats
->rx_over_errors
= stats
->rx_rxf_ov
;
371 net_stats
->rx_missed_errors
= stats
->rx_rxf_ov
;
373 net_stats
->tx_errors
= stats
->tx_late_col
+ stats
->tx_abort_col
+
374 stats
->tx_underrun
+ stats
->tx_trunc
;
375 net_stats
->tx_fifo_errors
= stats
->tx_underrun
;
376 net_stats
->tx_aborted_errors
= stats
->tx_abort_col
;
377 net_stats
->tx_window_errors
= stats
->tx_late_col
;
379 spin_unlock(&stats
->lock
);
384 static const struct net_device_ops emac_netdev_ops
= {
385 .ndo_open
= emac_open
,
386 .ndo_stop
= emac_close
,
387 .ndo_validate_addr
= eth_validate_addr
,
388 .ndo_start_xmit
= emac_start_xmit
,
389 .ndo_set_mac_address
= eth_mac_addr
,
390 .ndo_change_mtu
= emac_change_mtu
,
391 .ndo_do_ioctl
= emac_ioctl
,
392 .ndo_tx_timeout
= emac_tx_timeout
,
393 .ndo_get_stats64
= emac_get_stats64
,
394 .ndo_set_features
= emac_set_features
,
395 .ndo_set_rx_mode
= emac_rx_mode_set
,
398 /* Watchdog task routine, called to reinitialize the EMAC */
399 static void emac_work_thread(struct work_struct
*work
)
401 struct emac_adapter
*adpt
=
402 container_of(work
, struct emac_adapter
, work_thread
);
404 emac_reinit_locked(adpt
);
407 /* Initialize various data structures */
408 static void emac_init_adapter(struct emac_adapter
*adpt
)
413 adpt
->tx_desc_cnt
= EMAC_DEF_TX_DESCS
;
414 adpt
->rx_desc_cnt
= EMAC_DEF_RX_DESCS
;
417 adpt
->dma_order
= emac_dma_ord_out
;
418 adpt
->dmar_block
= emac_dma_req_4096
;
419 adpt
->dmaw_block
= emac_dma_req_128
;
420 adpt
->dmar_dly_cnt
= DMAR_DLY_CNT_DEF
;
421 adpt
->dmaw_dly_cnt
= DMAW_DLY_CNT_DEF
;
422 adpt
->tpd_burst
= TXQ0_NUM_TPD_PREF_DEF
;
423 adpt
->rfd_burst
= RXQ0_NUM_RFD_PREF_DEF
;
426 reg
= ((EMAC_DEF_RX_IRQ_MOD
>> 1) << IRQ_MODERATOR2_INIT_SHFT
) |
427 ((EMAC_DEF_TX_IRQ_MOD
>> 1) << IRQ_MODERATOR_INIT_SHFT
);
431 adpt
->preamble
= EMAC_PREAMBLE_DEF
;
435 static int emac_clks_get(struct platform_device
*pdev
,
436 struct emac_adapter
*adpt
)
440 for (i
= 0; i
< EMAC_CLK_CNT
; i
++) {
441 struct clk
*clk
= devm_clk_get(&pdev
->dev
, emac_clk_name
[i
]);
445 "could not claim clock %s (error=%li)\n",
446 emac_clk_name
[i
], PTR_ERR(clk
));
457 /* Initialize clocks */
458 static int emac_clks_phase1_init(struct platform_device
*pdev
,
459 struct emac_adapter
*adpt
)
463 /* On ACPI platforms, clocks are controlled by firmware and/or
464 * ACPI, not by drivers.
466 if (has_acpi_companion(&pdev
->dev
))
469 ret
= emac_clks_get(pdev
, adpt
);
473 ret
= clk_prepare_enable(adpt
->clk
[EMAC_CLK_AXI
]);
477 ret
= clk_prepare_enable(adpt
->clk
[EMAC_CLK_CFG_AHB
]);
481 ret
= clk_set_rate(adpt
->clk
[EMAC_CLK_HIGH_SPEED
], 19200000);
485 return clk_prepare_enable(adpt
->clk
[EMAC_CLK_HIGH_SPEED
]);
488 /* Enable clocks; needs emac_clks_phase1_init to be called before */
489 static int emac_clks_phase2_init(struct platform_device
*pdev
,
490 struct emac_adapter
*adpt
)
494 if (has_acpi_companion(&pdev
->dev
))
497 ret
= clk_set_rate(adpt
->clk
[EMAC_CLK_TX
], 125000000);
501 ret
= clk_prepare_enable(adpt
->clk
[EMAC_CLK_TX
]);
505 ret
= clk_set_rate(adpt
->clk
[EMAC_CLK_HIGH_SPEED
], 125000000);
509 ret
= clk_set_rate(adpt
->clk
[EMAC_CLK_MDIO
], 25000000);
513 ret
= clk_prepare_enable(adpt
->clk
[EMAC_CLK_MDIO
]);
517 ret
= clk_prepare_enable(adpt
->clk
[EMAC_CLK_RX
]);
521 return clk_prepare_enable(adpt
->clk
[EMAC_CLK_SYS
]);
524 static void emac_clks_teardown(struct emac_adapter
*adpt
)
529 for (i
= 0; i
< EMAC_CLK_CNT
; i
++)
530 clk_disable_unprepare(adpt
->clk
[i
]);
533 /* Get the resources */
534 static int emac_probe_resources(struct platform_device
*pdev
,
535 struct emac_adapter
*adpt
)
537 struct net_device
*netdev
= adpt
->netdev
;
538 struct resource
*res
;
539 char maddr
[ETH_ALEN
];
542 /* get mac address */
543 if (device_get_mac_address(&pdev
->dev
, maddr
, ETH_ALEN
))
544 ether_addr_copy(netdev
->dev_addr
, maddr
);
546 eth_hw_addr_random(netdev
);
548 /* Core 0 interrupt */
549 ret
= platform_get_irq(pdev
, 0);
552 "error: missing core0 irq resource (error=%i)\n", ret
);
557 /* base register address */
558 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
559 adpt
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
560 if (IS_ERR(adpt
->base
))
561 return PTR_ERR(adpt
->base
);
563 /* CSR register address */
564 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
565 adpt
->csr
= devm_ioremap_resource(&pdev
->dev
, res
);
566 if (IS_ERR(adpt
->csr
))
567 return PTR_ERR(adpt
->csr
);
569 netdev
->base_addr
= (unsigned long)adpt
->base
;
574 static const struct of_device_id emac_dt_match
[] = {
576 .compatible
= "qcom,fsm9900-emac",
580 MODULE_DEVICE_TABLE(of
, emac_dt_match
);
582 #if IS_ENABLED(CONFIG_ACPI)
583 static const struct acpi_device_id emac_acpi_match
[] = {
589 MODULE_DEVICE_TABLE(acpi
, emac_acpi_match
);
592 static int emac_probe(struct platform_device
*pdev
)
594 struct net_device
*netdev
;
595 struct emac_adapter
*adpt
;
596 struct emac_phy
*phy
;
601 /* The EMAC itself is capable of 64-bit DMA, so try that first. */
602 ret
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(64));
604 /* Some platforms may restrict the EMAC's address bus to less
605 * then the size of DDR. In this case, we need to try a
606 * smaller mask. We could try every possible smaller mask,
607 * but that's overkill. Instead, just fall to 32-bit, which
608 * should always work.
610 ret
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(32));
612 dev_err(&pdev
->dev
, "could not set DMA mask\n");
617 netdev
= alloc_etherdev(sizeof(struct emac_adapter
));
621 dev_set_drvdata(&pdev
->dev
, netdev
);
622 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
624 adpt
= netdev_priv(netdev
);
625 adpt
->netdev
= netdev
;
626 adpt
->msg_enable
= EMAC_MSG_DEFAULT
;
630 mutex_init(&adpt
->reset_lock
);
631 spin_lock_init(&adpt
->stats
.lock
);
633 adpt
->irq
.mask
= RX_PKT_INT0
| IMR_NORMAL_MASK
;
635 ret
= emac_probe_resources(pdev
, adpt
);
637 goto err_undo_netdev
;
639 /* initialize clocks */
640 ret
= emac_clks_phase1_init(pdev
, adpt
);
642 dev_err(&pdev
->dev
, "could not initialize clocks\n");
643 goto err_undo_netdev
;
646 netdev
->watchdog_timeo
= EMAC_WATCHDOG_TIME
;
647 netdev
->irq
= adpt
->irq
.irq
;
649 adpt
->rrd_size
= EMAC_RRD_SIZE
;
650 adpt
->tpd_size
= EMAC_TPD_SIZE
;
651 adpt
->rfd_size
= EMAC_RFD_SIZE
;
653 netdev
->netdev_ops
= &emac_netdev_ops
;
655 emac_init_adapter(adpt
);
657 /* init external phy */
658 ret
= emac_phy_config(pdev
, adpt
);
660 goto err_undo_clocks
;
662 /* init internal sgmii phy */
663 ret
= emac_sgmii_config(pdev
, adpt
);
665 goto err_undo_mdiobus
;
668 ret
= emac_clks_phase2_init(pdev
, adpt
);
670 dev_err(&pdev
->dev
, "could not initialize clocks\n");
671 goto err_undo_mdiobus
;
674 emac_mac_reset(adpt
);
676 /* set hw features */
677 netdev
->features
= NETIF_F_SG
| NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
|
678 NETIF_F_TSO
| NETIF_F_TSO6
| NETIF_F_HW_VLAN_CTAG_RX
|
679 NETIF_F_HW_VLAN_CTAG_TX
;
680 netdev
->hw_features
= netdev
->features
;
682 netdev
->vlan_features
|= NETIF_F_SG
| NETIF_F_HW_CSUM
|
683 NETIF_F_TSO
| NETIF_F_TSO6
;
685 /* MTU range: 46 - 9194 */
686 netdev
->min_mtu
= EMAC_MIN_ETH_FRAME_SIZE
-
687 (ETH_HLEN
+ ETH_FCS_LEN
+ VLAN_HLEN
);
688 netdev
->max_mtu
= EMAC_MAX_ETH_FRAME_SIZE
-
689 (ETH_HLEN
+ ETH_FCS_LEN
+ VLAN_HLEN
);
691 INIT_WORK(&adpt
->work_thread
, emac_work_thread
);
693 /* Initialize queues */
694 emac_mac_rx_tx_ring_init_all(pdev
, adpt
);
696 netif_napi_add(netdev
, &adpt
->rx_q
.napi
, emac_napi_rtx
,
699 ret
= register_netdev(netdev
);
701 dev_err(&pdev
->dev
, "could not register net device\n");
705 reg
= readl_relaxed(adpt
->base
+ EMAC_DMA_MAS_CTRL
);
706 devid
= (reg
& DEV_ID_NUM_BMSK
) >> DEV_ID_NUM_SHFT
;
707 revid
= (reg
& DEV_REV_NUM_BMSK
) >> DEV_REV_NUM_SHFT
;
708 reg
= readl_relaxed(adpt
->base
+ EMAC_CORE_HW_VERSION
);
710 netif_info(adpt
, probe
, netdev
,
711 "hardware id %d.%d, hardware version %d.%d.%d\n",
713 (reg
& MAJOR_BMSK
) >> MAJOR_SHFT
,
714 (reg
& MINOR_BMSK
) >> MINOR_SHFT
,
715 (reg
& STEP_BMSK
) >> STEP_SHFT
);
720 netif_napi_del(&adpt
->rx_q
.napi
);
722 if (!has_acpi_companion(&pdev
->dev
))
723 put_device(&adpt
->phydev
->mdio
.dev
);
724 mdiobus_unregister(adpt
->mii_bus
);
726 emac_clks_teardown(adpt
);
733 static int emac_remove(struct platform_device
*pdev
)
735 struct net_device
*netdev
= dev_get_drvdata(&pdev
->dev
);
736 struct emac_adapter
*adpt
= netdev_priv(netdev
);
738 unregister_netdev(netdev
);
739 netif_napi_del(&adpt
->rx_q
.napi
);
741 emac_clks_teardown(adpt
);
743 if (!has_acpi_companion(&pdev
->dev
))
744 put_device(&adpt
->phydev
->mdio
.dev
);
745 mdiobus_unregister(adpt
->mii_bus
);
748 if (adpt
->phy
.digital
)
749 iounmap(adpt
->phy
.digital
);
750 iounmap(adpt
->phy
.base
);
755 static struct platform_driver emac_platform_driver
= {
757 .remove
= emac_remove
,
760 .of_match_table
= emac_dt_match
,
761 .acpi_match_table
= ACPI_PTR(emac_acpi_match
),
765 module_platform_driver(emac_platform_driver
);
767 MODULE_LICENSE("GPL v2");
768 MODULE_ALIAS("platform:qcom-emac");