1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004-2013 Synopsys, Inc. (www.synopsys.com)
5 * Driver for the ARC EMAC 10100 (hardware revision 5)
13 #include <linux/crc32.h>
14 #include <linux/etherdevice.h>
15 #include <linux/interrupt.h>
17 #include <linux/module.h>
19 #include <linux/of_address.h>
20 #include <linux/of_irq.h>
21 #include <linux/of_mdio.h>
22 #include <linux/of_net.h>
26 static void arc_emac_restart(struct net_device *ndev);
29 * arc_emac_tx_avail - Return the number of available slots in the tx ring.
30 * @priv: Pointer to ARC EMAC private data structure.
32 * returns: the number of slots available for transmission in tx the ring.
34 static inline int arc_emac_tx_avail(struct arc_emac_priv *priv)
36 return (priv->txbd_dirty + TX_BD_NUM - priv->txbd_curr - 1) % TX_BD_NUM;
40 * arc_emac_adjust_link - Adjust the PHY link duplex.
41 * @ndev: Pointer to the net_device structure.
43 * This function is called to change the duplex setting after auto negotiation
46 static void arc_emac_adjust_link(struct net_device *ndev)
48 struct arc_emac_priv *priv = netdev_priv(ndev);
49 struct phy_device *phy_dev = ndev->phydev;
50 unsigned int reg, state_changed = 0;
52 if (priv->link != phy_dev->link) {
53 priv->link = phy_dev->link;
57 if (priv->speed != phy_dev->speed) {
58 priv->speed = phy_dev->speed;
60 if (priv->set_mac_speed)
61 priv->set_mac_speed(priv, priv->speed);
64 if (priv->duplex != phy_dev->duplex) {
65 reg = arc_reg_get(priv, R_CTRL);
67 if (phy_dev->duplex == DUPLEX_FULL)
72 arc_reg_set(priv, R_CTRL, reg);
73 priv->duplex = phy_dev->duplex;
78 phy_print_status(phy_dev);
82 * arc_emac_get_drvinfo - Get EMAC driver information.
83 * @ndev: Pointer to net_device structure.
84 * @info: Pointer to ethtool_drvinfo structure.
86 * This implements ethtool command for getting the driver information.
87 * Issue "ethtool -i ethX" under linux prompt to execute this function.
89 static void arc_emac_get_drvinfo(struct net_device *ndev,
90 struct ethtool_drvinfo *info)
92 struct arc_emac_priv *priv = netdev_priv(ndev);
94 strscpy(info->driver, priv->drv_name, sizeof(info->driver));
97 static const struct ethtool_ops arc_emac_ethtool_ops = {
98 .get_drvinfo = arc_emac_get_drvinfo,
99 .get_link = ethtool_op_get_link,
100 .get_link_ksettings = phy_ethtool_get_link_ksettings,
101 .set_link_ksettings = phy_ethtool_set_link_ksettings,
104 #define FIRST_OR_LAST_MASK (FIRST_MASK | LAST_MASK)
107 * arc_emac_tx_clean - clears processed by EMAC Tx BDs.
108 * @ndev: Pointer to the network device.
110 static void arc_emac_tx_clean(struct net_device *ndev)
112 struct arc_emac_priv *priv = netdev_priv(ndev);
113 struct net_device_stats *stats = &ndev->stats;
114 struct device *dev = ndev->dev.parent;
117 for (i = 0; i < TX_BD_NUM; i++) {
118 unsigned int *txbd_dirty = &priv->txbd_dirty;
119 struct arc_emac_bd *txbd = &priv->txbd[*txbd_dirty];
120 struct buffer_state *tx_buff = &priv->tx_buff[*txbd_dirty];
121 struct sk_buff *skb = tx_buff->skb;
122 unsigned int info = le32_to_cpu(txbd->info);
124 if ((info & FOR_EMAC) || !txbd->data || !skb)
127 if (unlikely(info & (DROP | DEFR | LTCL | UFLO))) {
132 stats->tx_carrier_errors++;
138 stats->tx_fifo_errors++;
139 } else if (likely(info & FIRST_OR_LAST_MASK)) {
141 stats->tx_bytes += skb->len;
144 dma_unmap_single(dev, dma_unmap_addr(tx_buff, addr),
145 dma_unmap_len(tx_buff, len), DMA_TO_DEVICE);
147 /* return the sk_buff to system */
148 dev_consume_skb_irq(skb);
154 *txbd_dirty = (*txbd_dirty + 1) % TX_BD_NUM;
157 /* Ensure that txbd_dirty is visible to tx() before checking
162 if (netif_queue_stopped(ndev) && arc_emac_tx_avail(priv))
163 netif_wake_queue(ndev);
167 * arc_emac_rx - processing of Rx packets.
168 * @ndev: Pointer to the network device.
169 * @budget: How many BDs to process on 1 call.
171 * returns: Number of processed BDs
173 * Iterate through Rx BDs and deliver received packages to upper layer.
175 static int arc_emac_rx(struct net_device *ndev, int budget)
177 struct arc_emac_priv *priv = netdev_priv(ndev);
178 struct device *dev = ndev->dev.parent;
179 unsigned int work_done;
181 for (work_done = 0; work_done < budget; work_done++) {
182 unsigned int *last_rx_bd = &priv->last_rx_bd;
183 struct net_device_stats *stats = &ndev->stats;
184 struct buffer_state *rx_buff = &priv->rx_buff[*last_rx_bd];
185 struct arc_emac_bd *rxbd = &priv->rxbd[*last_rx_bd];
186 unsigned int pktlen, info = le32_to_cpu(rxbd->info);
190 if (unlikely((info & OWN_MASK) == FOR_EMAC))
193 /* Make a note that we saw a packet at this BD.
194 * So next time, driver starts from this + 1
196 *last_rx_bd = (*last_rx_bd + 1) % RX_BD_NUM;
198 if (unlikely((info & FIRST_OR_LAST_MASK) !=
199 FIRST_OR_LAST_MASK)) {
200 /* We pre-allocate buffers of MTU size so incoming
201 * packets won't be split/chained.
204 netdev_err(ndev, "incomplete packet received\n");
206 /* Return ownership to EMAC */
207 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
209 stats->rx_length_errors++;
213 /* Prepare the BD for next cycle. netif_receive_skb()
214 * only if new skb was allocated and mapped to avoid holes
217 skb = netdev_alloc_skb_ip_align(ndev, EMAC_BUFFER_SIZE);
218 if (unlikely(!skb)) {
220 netdev_err(ndev, "cannot allocate skb\n");
221 /* Return ownership to EMAC */
222 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
228 addr = dma_map_single(dev, (void *)skb->data,
229 EMAC_BUFFER_SIZE, DMA_FROM_DEVICE);
230 if (dma_mapping_error(dev, addr)) {
232 netdev_err(ndev, "cannot map dma buffer\n");
234 /* Return ownership to EMAC */
235 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
241 /* unmap previosly mapped skb */
242 dma_unmap_single(dev, dma_unmap_addr(rx_buff, addr),
243 dma_unmap_len(rx_buff, len), DMA_FROM_DEVICE);
245 pktlen = info & LEN_MASK;
247 stats->rx_bytes += pktlen;
248 skb_put(rx_buff->skb, pktlen);
249 rx_buff->skb->dev = ndev;
250 rx_buff->skb->protocol = eth_type_trans(rx_buff->skb, ndev);
252 netif_receive_skb(rx_buff->skb);
255 dma_unmap_addr_set(rx_buff, addr, addr);
256 dma_unmap_len_set(rx_buff, len, EMAC_BUFFER_SIZE);
258 rxbd->data = cpu_to_le32(addr);
260 /* Make sure pointer to data buffer is set */
263 /* Return ownership to EMAC */
264 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
271 * arc_emac_rx_miss_handle - handle R_MISS register
272 * @ndev: Pointer to the net_device structure.
274 static void arc_emac_rx_miss_handle(struct net_device *ndev)
276 struct arc_emac_priv *priv = netdev_priv(ndev);
277 struct net_device_stats *stats = &ndev->stats;
280 miss = arc_reg_get(priv, R_MISS);
282 stats->rx_errors += miss;
283 stats->rx_missed_errors += miss;
284 priv->rx_missed_errors += miss;
289 * arc_emac_rx_stall_check - check RX stall
290 * @ndev: Pointer to the net_device structure.
291 * @budget: How many BDs requested to process on 1 call.
292 * @work_done: How many BDs processed
294 * Under certain conditions EMAC stop reception of incoming packets and
295 * continuously increment R_MISS register instead of saving data into
296 * provided buffer. This function detect that condition and restart
299 static void arc_emac_rx_stall_check(struct net_device *ndev,
300 int budget, unsigned int work_done)
302 struct arc_emac_priv *priv = netdev_priv(ndev);
303 struct arc_emac_bd *rxbd;
306 priv->rx_missed_errors = 0;
308 if (priv->rx_missed_errors && budget) {
309 rxbd = &priv->rxbd[priv->last_rx_bd];
310 if (le32_to_cpu(rxbd->info) & FOR_EMAC) {
311 arc_emac_restart(ndev);
312 priv->rx_missed_errors = 0;
318 * arc_emac_poll - NAPI poll handler.
319 * @napi: Pointer to napi_struct structure.
320 * @budget: How many BDs to process on 1 call.
322 * returns: Number of processed BDs
324 static int arc_emac_poll(struct napi_struct *napi, int budget)
326 struct net_device *ndev = napi->dev;
327 struct arc_emac_priv *priv = netdev_priv(ndev);
328 unsigned int work_done;
330 arc_emac_tx_clean(ndev);
331 arc_emac_rx_miss_handle(ndev);
333 work_done = arc_emac_rx(ndev, budget);
334 if (work_done < budget) {
335 napi_complete_done(napi, work_done);
336 arc_reg_or(priv, R_ENABLE, RXINT_MASK | TXINT_MASK);
339 arc_emac_rx_stall_check(ndev, budget, work_done);
345 * arc_emac_intr - Global interrupt handler for EMAC.
347 * @dev_instance: device instance.
349 * returns: IRQ_HANDLED for all cases.
351 * ARC EMAC has only 1 interrupt line, and depending on bits raised in
352 * STATUS register we may tell what is a reason for interrupt to fire.
354 static irqreturn_t arc_emac_intr(int irq, void *dev_instance)
356 struct net_device *ndev = dev_instance;
357 struct arc_emac_priv *priv = netdev_priv(ndev);
358 struct net_device_stats *stats = &ndev->stats;
361 status = arc_reg_get(priv, R_STATUS);
362 status &= ~MDIO_MASK;
364 /* Reset all flags except "MDIO complete" */
365 arc_reg_set(priv, R_STATUS, status);
367 if (status & (RXINT_MASK | TXINT_MASK)) {
368 if (likely(napi_schedule_prep(&priv->napi))) {
369 arc_reg_clr(priv, R_ENABLE, RXINT_MASK | TXINT_MASK);
370 __napi_schedule(&priv->napi);
374 if (status & ERR_MASK) {
375 /* MSER/RXCR/RXFR/RXFL interrupt fires on corresponding
376 * 8-bit error counter overrun.
379 if (status & MSER_MASK) {
380 stats->rx_missed_errors += 0x100;
381 stats->rx_errors += 0x100;
382 priv->rx_missed_errors += 0x100;
383 napi_schedule(&priv->napi);
386 if (status & RXCR_MASK) {
387 stats->rx_crc_errors += 0x100;
388 stats->rx_errors += 0x100;
391 if (status & RXFR_MASK) {
392 stats->rx_frame_errors += 0x100;
393 stats->rx_errors += 0x100;
396 if (status & RXFL_MASK) {
397 stats->rx_over_errors += 0x100;
398 stats->rx_errors += 0x100;
405 #ifdef CONFIG_NET_POLL_CONTROLLER
406 static void arc_emac_poll_controller(struct net_device *dev)
408 disable_irq(dev->irq);
409 arc_emac_intr(dev->irq, dev);
410 enable_irq(dev->irq);
415 * arc_emac_open - Open the network device.
416 * @ndev: Pointer to the network device.
418 * returns: 0, on success or non-zero error value on failure.
420 * This function sets the MAC address, requests and enables an IRQ
421 * for the EMAC device and starts the Tx queue.
422 * It also connects to the phy device.
424 static int arc_emac_open(struct net_device *ndev)
426 struct arc_emac_priv *priv = netdev_priv(ndev);
427 struct phy_device *phy_dev = ndev->phydev;
428 struct device *dev = ndev->dev.parent;
431 phy_dev->autoneg = AUTONEG_ENABLE;
434 linkmode_and(phy_dev->advertising, phy_dev->advertising,
437 priv->last_rx_bd = 0;
439 /* Allocate and set buffers for Rx BD's */
440 for (i = 0; i < RX_BD_NUM; i++) {
442 unsigned int *last_rx_bd = &priv->last_rx_bd;
443 struct arc_emac_bd *rxbd = &priv->rxbd[*last_rx_bd];
444 struct buffer_state *rx_buff = &priv->rx_buff[*last_rx_bd];
446 rx_buff->skb = netdev_alloc_skb_ip_align(ndev,
448 if (unlikely(!rx_buff->skb))
451 addr = dma_map_single(dev, (void *)rx_buff->skb->data,
452 EMAC_BUFFER_SIZE, DMA_FROM_DEVICE);
453 if (dma_mapping_error(dev, addr)) {
454 netdev_err(ndev, "cannot dma map\n");
455 dev_kfree_skb(rx_buff->skb);
458 dma_unmap_addr_set(rx_buff, addr, addr);
459 dma_unmap_len_set(rx_buff, len, EMAC_BUFFER_SIZE);
461 rxbd->data = cpu_to_le32(addr);
463 /* Make sure pointer to data buffer is set */
466 /* Return ownership to EMAC */
467 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
469 *last_rx_bd = (*last_rx_bd + 1) % RX_BD_NUM;
473 priv->txbd_dirty = 0;
476 memset(priv->txbd, 0, TX_RING_SZ);
478 /* Initialize logical address filter */
479 arc_reg_set(priv, R_LAFL, 0);
480 arc_reg_set(priv, R_LAFH, 0);
482 /* Set BD ring pointers for device side */
483 arc_reg_set(priv, R_RX_RING, (unsigned int)priv->rxbd_dma);
484 arc_reg_set(priv, R_TX_RING, (unsigned int)priv->txbd_dma);
486 /* Enable interrupts */
487 arc_reg_set(priv, R_ENABLE, RXINT_MASK | TXINT_MASK | ERR_MASK);
490 arc_reg_set(priv, R_CTRL,
491 (RX_BD_NUM << 24) | /* RX BD table length */
492 (TX_BD_NUM << 16) | /* TX BD table length */
493 TXRN_MASK | RXRN_MASK);
495 napi_enable(&priv->napi);
498 arc_reg_or(priv, R_CTRL, EN_MASK);
500 phy_start(ndev->phydev);
502 netif_start_queue(ndev);
508 * arc_emac_set_rx_mode - Change the receive filtering mode.
509 * @ndev: Pointer to the network device.
511 * This function enables/disables promiscuous or all-multicast mode
512 * and updates the multicast filtering list of the network device.
514 static void arc_emac_set_rx_mode(struct net_device *ndev)
516 struct arc_emac_priv *priv = netdev_priv(ndev);
518 if (ndev->flags & IFF_PROMISC) {
519 arc_reg_or(priv, R_CTRL, PROM_MASK);
521 arc_reg_clr(priv, R_CTRL, PROM_MASK);
523 if (ndev->flags & IFF_ALLMULTI) {
524 arc_reg_set(priv, R_LAFL, ~0);
525 arc_reg_set(priv, R_LAFH, ~0);
526 } else if (ndev->flags & IFF_MULTICAST) {
527 struct netdev_hw_addr *ha;
528 unsigned int filter[2] = { 0, 0 };
531 netdev_for_each_mc_addr(ha, ndev) {
532 bit = ether_crc_le(ETH_ALEN, ha->addr) >> 26;
533 filter[bit >> 5] |= 1 << (bit & 31);
536 arc_reg_set(priv, R_LAFL, filter[0]);
537 arc_reg_set(priv, R_LAFH, filter[1]);
539 arc_reg_set(priv, R_LAFL, 0);
540 arc_reg_set(priv, R_LAFH, 0);
546 * arc_free_tx_queue - free skb from tx queue
547 * @ndev: Pointer to the network device.
549 * This function must be called while EMAC disable
551 static void arc_free_tx_queue(struct net_device *ndev)
553 struct arc_emac_priv *priv = netdev_priv(ndev);
554 struct device *dev = ndev->dev.parent;
557 for (i = 0; i < TX_BD_NUM; i++) {
558 struct arc_emac_bd *txbd = &priv->txbd[i];
559 struct buffer_state *tx_buff = &priv->tx_buff[i];
562 dma_unmap_single(dev,
563 dma_unmap_addr(tx_buff, addr),
564 dma_unmap_len(tx_buff, len),
567 /* return the sk_buff to system */
568 dev_kfree_skb_irq(tx_buff->skb);
578 * arc_free_rx_queue - free skb from rx queue
579 * @ndev: Pointer to the network device.
581 * This function must be called while EMAC disable
583 static void arc_free_rx_queue(struct net_device *ndev)
585 struct arc_emac_priv *priv = netdev_priv(ndev);
586 struct device *dev = ndev->dev.parent;
589 for (i = 0; i < RX_BD_NUM; i++) {
590 struct arc_emac_bd *rxbd = &priv->rxbd[i];
591 struct buffer_state *rx_buff = &priv->rx_buff[i];
594 dma_unmap_single(dev,
595 dma_unmap_addr(rx_buff, addr),
596 dma_unmap_len(rx_buff, len),
599 /* return the sk_buff to system */
600 dev_kfree_skb_irq(rx_buff->skb);
610 * arc_emac_stop - Close the network device.
611 * @ndev: Pointer to the network device.
613 * This function stops the Tx queue, disables interrupts and frees the IRQ for
615 * It also disconnects the PHY device associated with the EMAC device.
617 static int arc_emac_stop(struct net_device *ndev)
619 struct arc_emac_priv *priv = netdev_priv(ndev);
621 napi_disable(&priv->napi);
622 netif_stop_queue(ndev);
624 phy_stop(ndev->phydev);
626 /* Disable interrupts */
627 arc_reg_clr(priv, R_ENABLE, RXINT_MASK | TXINT_MASK | ERR_MASK);
630 arc_reg_clr(priv, R_CTRL, EN_MASK);
632 /* Return the sk_buff to system */
633 arc_free_tx_queue(ndev);
634 arc_free_rx_queue(ndev);
640 * arc_emac_stats - Get system network statistics.
641 * @ndev: Pointer to net_device structure.
643 * Returns the address of the device statistics structure.
644 * Statistics are updated in interrupt handler.
646 static struct net_device_stats *arc_emac_stats(struct net_device *ndev)
648 struct arc_emac_priv *priv = netdev_priv(ndev);
649 struct net_device_stats *stats = &ndev->stats;
650 unsigned long miss, rxerr;
651 u8 rxcrc, rxfram, rxoflow;
653 rxerr = arc_reg_get(priv, R_RXERR);
654 miss = arc_reg_get(priv, R_MISS);
658 rxoflow = rxerr >> 16;
660 stats->rx_errors += miss;
661 stats->rx_errors += rxcrc + rxfram + rxoflow;
663 stats->rx_over_errors += rxoflow;
664 stats->rx_frame_errors += rxfram;
665 stats->rx_crc_errors += rxcrc;
666 stats->rx_missed_errors += miss;
672 * arc_emac_tx - Starts the data transmission.
673 * @skb: sk_buff pointer that contains data to be Transmitted.
674 * @ndev: Pointer to net_device structure.
676 * returns: NETDEV_TX_OK, on success
677 * NETDEV_TX_BUSY, if any of the descriptors are not free.
679 * This function is invoked from upper layers to initiate transmission.
681 static netdev_tx_t arc_emac_tx(struct sk_buff *skb, struct net_device *ndev)
683 struct arc_emac_priv *priv = netdev_priv(ndev);
684 unsigned int len, *txbd_curr = &priv->txbd_curr;
685 struct net_device_stats *stats = &ndev->stats;
686 __le32 *info = &priv->txbd[*txbd_curr].info;
687 struct device *dev = ndev->dev.parent;
690 if (skb_padto(skb, ETH_ZLEN))
693 len = max_t(unsigned int, ETH_ZLEN, skb->len);
695 if (unlikely(!arc_emac_tx_avail(priv))) {
696 netif_stop_queue(ndev);
697 netdev_err(ndev, "BUG! Tx Ring full when queue awake!\n");
698 return NETDEV_TX_BUSY;
701 addr = dma_map_single(dev, (void *)skb->data, len, DMA_TO_DEVICE);
703 if (unlikely(dma_mapping_error(dev, addr))) {
706 dev_kfree_skb_any(skb);
709 dma_unmap_addr_set(&priv->tx_buff[*txbd_curr], addr, addr);
710 dma_unmap_len_set(&priv->tx_buff[*txbd_curr], len, len);
712 priv->txbd[*txbd_curr].data = cpu_to_le32(addr);
714 /* Make sure pointer to data buffer is set */
717 skb_tx_timestamp(skb);
719 *info = cpu_to_le32(FOR_EMAC | FIRST_OR_LAST_MASK | len);
721 /* Make sure info word is set */
724 priv->tx_buff[*txbd_curr].skb = skb;
726 /* Increment index to point to the next BD */
727 *txbd_curr = (*txbd_curr + 1) % TX_BD_NUM;
729 /* Ensure that tx_clean() sees the new txbd_curr before
730 * checking the queue status. This prevents an unneeded wake
731 * of the queue in tx_clean().
735 if (!arc_emac_tx_avail(priv)) {
736 netif_stop_queue(ndev);
737 /* Refresh tx_dirty */
739 if (arc_emac_tx_avail(priv))
740 netif_start_queue(ndev);
743 arc_reg_set(priv, R_STATUS, TXPL_MASK);
748 static void arc_emac_set_address_internal(struct net_device *ndev)
750 struct arc_emac_priv *priv = netdev_priv(ndev);
751 unsigned int addr_low, addr_hi;
753 addr_low = le32_to_cpu(*(__le32 *)&ndev->dev_addr[0]);
754 addr_hi = le16_to_cpu(*(__le16 *)&ndev->dev_addr[4]);
756 arc_reg_set(priv, R_ADDRL, addr_low);
757 arc_reg_set(priv, R_ADDRH, addr_hi);
761 * arc_emac_set_address - Set the MAC address for this device.
762 * @ndev: Pointer to net_device structure.
763 * @p: 6 byte Address to be written as MAC address.
765 * This function copies the HW address from the sockaddr structure to the
766 * net_device structure and updates the address in HW.
768 * returns: -EBUSY if the net device is busy or 0 if the address is set
771 static int arc_emac_set_address(struct net_device *ndev, void *p)
773 struct sockaddr *addr = p;
775 if (netif_running(ndev))
778 if (!is_valid_ether_addr(addr->sa_data))
779 return -EADDRNOTAVAIL;
781 eth_hw_addr_set(ndev, addr->sa_data);
783 arc_emac_set_address_internal(ndev);
789 * arc_emac_restart - Restart EMAC
790 * @ndev: Pointer to net_device structure.
792 * This function do hardware reset of EMAC in order to restore
793 * network packets reception.
795 static void arc_emac_restart(struct net_device *ndev)
797 struct arc_emac_priv *priv = netdev_priv(ndev);
798 struct net_device_stats *stats = &ndev->stats;
802 netdev_warn(ndev, "restarting stalled EMAC\n");
804 netif_stop_queue(ndev);
806 /* Disable interrupts */
807 arc_reg_clr(priv, R_ENABLE, RXINT_MASK | TXINT_MASK | ERR_MASK);
810 arc_reg_clr(priv, R_CTRL, EN_MASK);
812 /* Return the sk_buff to system */
813 arc_free_tx_queue(ndev);
817 priv->txbd_dirty = 0;
818 memset(priv->txbd, 0, TX_RING_SZ);
820 for (i = 0; i < RX_BD_NUM; i++) {
821 struct arc_emac_bd *rxbd = &priv->rxbd[i];
822 unsigned int info = le32_to_cpu(rxbd->info);
824 if (!(info & FOR_EMAC)) {
828 /* Return ownership to EMAC */
829 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
831 priv->last_rx_bd = 0;
833 /* Make sure info is visible to EMAC before enable */
836 /* Enable interrupts */
837 arc_reg_set(priv, R_ENABLE, RXINT_MASK | TXINT_MASK | ERR_MASK);
840 arc_reg_or(priv, R_CTRL, EN_MASK);
842 netif_start_queue(ndev);
845 static const struct net_device_ops arc_emac_netdev_ops = {
846 .ndo_open = arc_emac_open,
847 .ndo_stop = arc_emac_stop,
848 .ndo_start_xmit = arc_emac_tx,
849 .ndo_set_mac_address = arc_emac_set_address,
850 .ndo_get_stats = arc_emac_stats,
851 .ndo_set_rx_mode = arc_emac_set_rx_mode,
852 .ndo_eth_ioctl = phy_do_ioctl_running,
853 #ifdef CONFIG_NET_POLL_CONTROLLER
854 .ndo_poll_controller = arc_emac_poll_controller,
858 int arc_emac_probe(struct net_device *ndev, int interface)
860 struct device *dev = ndev->dev.parent;
861 struct resource res_regs;
862 struct device_node *phy_node;
863 struct phy_device *phydev = NULL;
864 struct arc_emac_priv *priv;
865 unsigned int id, clock_frequency, irq;
868 /* Get PHY from device tree */
869 phy_node = of_parse_phandle(dev->of_node, "phy", 0);
871 dev_err(dev, "failed to retrieve phy description from device tree\n");
875 /* Get EMAC registers base address from device tree */
876 err = of_address_to_resource(dev->of_node, 0, &res_regs);
878 dev_err(dev, "failed to retrieve registers base from device tree\n");
883 /* Get IRQ from device tree */
884 irq = irq_of_parse_and_map(dev->of_node, 0);
886 dev_err(dev, "failed to retrieve <irq> value from device tree\n");
891 ndev->netdev_ops = &arc_emac_netdev_ops;
892 ndev->ethtool_ops = &arc_emac_ethtool_ops;
893 ndev->watchdog_timeo = TX_TIMEOUT;
895 priv = netdev_priv(ndev);
898 priv->regs = devm_ioremap_resource(dev, &res_regs);
899 if (IS_ERR(priv->regs)) {
900 err = PTR_ERR(priv->regs);
904 dev_dbg(dev, "Registers base address is 0x%p\n", priv->regs);
907 err = clk_prepare_enable(priv->clk);
909 dev_err(dev, "failed to enable clock\n");
913 clock_frequency = clk_get_rate(priv->clk);
915 /* Get CPU clock frequency from device tree */
916 if (of_property_read_u32(dev->of_node, "clock-frequency",
918 dev_err(dev, "failed to retrieve <clock-frequency> from device tree\n");
924 id = arc_reg_get(priv, R_ID);
926 /* Check for EMAC revision 5 or 7, magic number */
927 if (!(id == 0x0005fd02 || id == 0x0007fd02)) {
928 dev_err(dev, "ARC EMAC not detected, id=0x%x\n", id);
932 dev_info(dev, "ARC EMAC detected with id: 0x%x\n", id);
934 /* Set poll rate so that it polls every 1 ms */
935 arc_reg_set(priv, R_POLLRATE, clock_frequency / 1000000);
938 dev_info(dev, "IRQ is %d\n", ndev->irq);
940 /* Register interrupt handler for device */
941 err = devm_request_irq(dev, ndev->irq, arc_emac_intr, 0,
944 dev_err(dev, "could not allocate IRQ\n");
948 /* Get MAC address from device tree */
949 err = of_get_ethdev_address(dev->of_node, ndev);
951 eth_hw_addr_random(ndev);
953 arc_emac_set_address_internal(ndev);
954 dev_info(dev, "MAC address is now %pM\n", ndev->dev_addr);
956 /* Do 1 allocation instead of 2 separate ones for Rx and Tx BD rings */
957 priv->rxbd = dmam_alloc_coherent(dev, RX_RING_SZ + TX_RING_SZ,
958 &priv->rxbd_dma, GFP_KERNEL);
961 dev_err(dev, "failed to allocate data buffers\n");
966 priv->txbd = priv->rxbd + RX_BD_NUM;
968 priv->txbd_dma = priv->rxbd_dma + RX_RING_SZ;
969 dev_dbg(dev, "EMAC Device addr: Rx Ring [0x%x], Tx Ring[%x]\n",
970 (unsigned int)priv->rxbd_dma, (unsigned int)priv->txbd_dma);
972 err = arc_mdio_probe(priv);
974 dev_err(dev, "failed to probe MII bus\n");
978 phydev = of_phy_connect(ndev, phy_node, arc_emac_adjust_link, 0,
981 dev_err(dev, "of_phy_connect() failed\n");
986 dev_info(dev, "connected to %s phy with id 0x%x\n",
987 phydev->drv->name, phydev->phy_id);
989 netif_napi_add_weight(ndev, &priv->napi, arc_emac_poll,
990 ARC_EMAC_NAPI_WEIGHT);
992 err = register_netdev(ndev);
994 dev_err(dev, "failed to register network device\n");
998 of_node_put(phy_node);
1002 netif_napi_del(&priv->napi);
1003 phy_disconnect(phydev);
1005 arc_mdio_remove(priv);
1008 clk_disable_unprepare(priv->clk);
1010 of_node_put(phy_node);
1014 EXPORT_SYMBOL_GPL(arc_emac_probe);
1016 void arc_emac_remove(struct net_device *ndev)
1018 struct arc_emac_priv *priv = netdev_priv(ndev);
1020 phy_disconnect(ndev->phydev);
1021 arc_mdio_remove(priv);
1022 unregister_netdev(ndev);
1023 netif_napi_del(&priv->napi);
1025 if (!IS_ERR(priv->clk))
1026 clk_disable_unprepare(priv->clk);
1028 EXPORT_SYMBOL_GPL(arc_emac_remove);
1031 MODULE_DESCRIPTION("ARC EMAC driver");
1032 MODULE_LICENSE("GPL");