Version:  2.0.40 2.2.26 2.4.37 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17

Linux/drivers/net/ethernet/arc/emac_main.c

  1 /*
  2  * Copyright (C) 2004-2013 Synopsys, Inc. (www.synopsys.com)
  3  *
  4  * This program is free software; you can redistribute it and/or modify
  5  * it under the terms of the GNU General Public License version 2 as
  6  * published by the Free Software Foundation.
  7  *
  8  * Driver for the ARC EMAC 10100 (hardware revision 5)
  9  *
 10  * Contributors:
 11  *              Amit Bhor
 12  *              Sameer Dhavale
 13  *              Vineet Gupta
 14  */
 15 
 16 #include <linux/crc32.h>
 17 #include <linux/etherdevice.h>
 18 #include <linux/interrupt.h>
 19 #include <linux/io.h>
 20 #include <linux/module.h>
 21 #include <linux/of_address.h>
 22 #include <linux/of_irq.h>
 23 #include <linux/of_mdio.h>
 24 #include <linux/of_net.h>
 25 #include <linux/of_platform.h>
 26 
 27 #include "emac.h"
 28 
 29 #define DRV_NAME        "arc_emac"
 30 #define DRV_VERSION     "1.0"
 31 
 32 /**
 33  * arc_emac_tx_avail - Return the number of available slots in the tx ring.
 34  * @priv: Pointer to ARC EMAC private data structure.
 35  *
 36  * returns: the number of slots available for transmission in tx the ring.
 37  */
 38 static inline int arc_emac_tx_avail(struct arc_emac_priv *priv)
 39 {
 40         return (priv->txbd_dirty + TX_BD_NUM - priv->txbd_curr - 1) % TX_BD_NUM;
 41 }
 42 
 43 /**
 44  * arc_emac_adjust_link - Adjust the PHY link duplex.
 45  * @ndev:       Pointer to the net_device structure.
 46  *
 47  * This function is called to change the duplex setting after auto negotiation
 48  * is done by the PHY.
 49  */
 50 static void arc_emac_adjust_link(struct net_device *ndev)
 51 {
 52         struct arc_emac_priv *priv = netdev_priv(ndev);
 53         struct phy_device *phy_dev = priv->phy_dev;
 54         unsigned int reg, state_changed = 0;
 55 
 56         if (priv->link != phy_dev->link) {
 57                 priv->link = phy_dev->link;
 58                 state_changed = 1;
 59         }
 60 
 61         if (priv->speed != phy_dev->speed) {
 62                 priv->speed = phy_dev->speed;
 63                 state_changed = 1;
 64         }
 65 
 66         if (priv->duplex != phy_dev->duplex) {
 67                 reg = arc_reg_get(priv, R_CTRL);
 68 
 69                 if (DUPLEX_FULL == phy_dev->duplex)
 70                         reg |= ENFL_MASK;
 71                 else
 72                         reg &= ~ENFL_MASK;
 73 
 74                 arc_reg_set(priv, R_CTRL, reg);
 75                 priv->duplex = phy_dev->duplex;
 76                 state_changed = 1;
 77         }
 78 
 79         if (state_changed)
 80                 phy_print_status(phy_dev);
 81 }
 82 
 83 /**
 84  * arc_emac_get_settings - Get PHY settings.
 85  * @ndev:       Pointer to net_device structure.
 86  * @cmd:        Pointer to ethtool_cmd structure.
 87  *
 88  * This implements ethtool command for getting PHY settings. If PHY could
 89  * not be found, the function returns -ENODEV. This function calls the
 90  * relevant PHY ethtool API to get the PHY settings.
 91  * Issue "ethtool ethX" under linux prompt to execute this function.
 92  */
 93 static int arc_emac_get_settings(struct net_device *ndev,
 94                                  struct ethtool_cmd *cmd)
 95 {
 96         struct arc_emac_priv *priv = netdev_priv(ndev);
 97 
 98         return phy_ethtool_gset(priv->phy_dev, cmd);
 99 }
100 
101 /**
102  * arc_emac_set_settings - Set PHY settings as passed in the argument.
103  * @ndev:       Pointer to net_device structure.
104  * @cmd:        Pointer to ethtool_cmd structure.
105  *
106  * This implements ethtool command for setting various PHY settings. If PHY
107  * could not be found, the function returns -ENODEV. This function calls the
108  * relevant PHY ethtool API to set the PHY.
109  * Issue e.g. "ethtool -s ethX speed 1000" under linux prompt to execute this
110  * function.
111  */
112 static int arc_emac_set_settings(struct net_device *ndev,
113                                  struct ethtool_cmd *cmd)
114 {
115         struct arc_emac_priv *priv = netdev_priv(ndev);
116 
117         if (!capable(CAP_NET_ADMIN))
118                 return -EPERM;
119 
120         return phy_ethtool_sset(priv->phy_dev, cmd);
121 }
122 
123 /**
124  * arc_emac_get_drvinfo - Get EMAC driver information.
125  * @ndev:       Pointer to net_device structure.
126  * @info:       Pointer to ethtool_drvinfo structure.
127  *
128  * This implements ethtool command for getting the driver information.
129  * Issue "ethtool -i ethX" under linux prompt to execute this function.
130  */
131 static void arc_emac_get_drvinfo(struct net_device *ndev,
132                                  struct ethtool_drvinfo *info)
133 {
134         strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
135         strlcpy(info->version, DRV_VERSION, sizeof(info->version));
136 }
137 
138 static const struct ethtool_ops arc_emac_ethtool_ops = {
139         .get_settings   = arc_emac_get_settings,
140         .set_settings   = arc_emac_set_settings,
141         .get_drvinfo    = arc_emac_get_drvinfo,
142         .get_link       = ethtool_op_get_link,
143 };
144 
145 #define FIRST_OR_LAST_MASK      (FIRST_MASK | LAST_MASK)
146 
147 /**
148  * arc_emac_tx_clean - clears processed by EMAC Tx BDs.
149  * @ndev:       Pointer to the network device.
150  */
151 static void arc_emac_tx_clean(struct net_device *ndev)
152 {
153         struct arc_emac_priv *priv = netdev_priv(ndev);
154         struct net_device_stats *stats = &ndev->stats;
155         unsigned int i;
156 
157         for (i = 0; i < TX_BD_NUM; i++) {
158                 unsigned int *txbd_dirty = &priv->txbd_dirty;
159                 struct arc_emac_bd *txbd = &priv->txbd[*txbd_dirty];
160                 struct buffer_state *tx_buff = &priv->tx_buff[*txbd_dirty];
161                 struct sk_buff *skb = tx_buff->skb;
162                 unsigned int info = le32_to_cpu(txbd->info);
163 
164                 if ((info & FOR_EMAC) || !txbd->data)
165                         break;
166 
167                 if (unlikely(info & (DROP | DEFR | LTCL | UFLO))) {
168                         stats->tx_errors++;
169                         stats->tx_dropped++;
170 
171                         if (info & DEFR)
172                                 stats->tx_carrier_errors++;
173 
174                         if (info & LTCL)
175                                 stats->collisions++;
176 
177                         if (info & UFLO)
178                                 stats->tx_fifo_errors++;
179                 } else if (likely(info & FIRST_OR_LAST_MASK)) {
180                         stats->tx_packets++;
181                         stats->tx_bytes += skb->len;
182                 }
183 
184                 dma_unmap_single(&ndev->dev, dma_unmap_addr(tx_buff, addr),
185                                  dma_unmap_len(tx_buff, len), DMA_TO_DEVICE);
186 
187                 /* return the sk_buff to system */
188                 dev_kfree_skb_irq(skb);
189 
190                 txbd->data = 0;
191                 txbd->info = 0;
192 
193                 *txbd_dirty = (*txbd_dirty + 1) % TX_BD_NUM;
194         }
195 
196         /* Ensure that txbd_dirty is visible to tx() before checking
197          * for queue stopped.
198          */
199         smp_mb();
200 
201         if (netif_queue_stopped(ndev) && arc_emac_tx_avail(priv))
202                 netif_wake_queue(ndev);
203 }
204 
205 /**
206  * arc_emac_rx - processing of Rx packets.
207  * @ndev:       Pointer to the network device.
208  * @budget:     How many BDs to process on 1 call.
209  *
210  * returns:     Number of processed BDs
211  *
212  * Iterate through Rx BDs and deliver received packages to upper layer.
213  */
214 static int arc_emac_rx(struct net_device *ndev, int budget)
215 {
216         struct arc_emac_priv *priv = netdev_priv(ndev);
217         unsigned int work_done;
218 
219         for (work_done = 0; work_done < budget; work_done++) {
220                 unsigned int *last_rx_bd = &priv->last_rx_bd;
221                 struct net_device_stats *stats = &ndev->stats;
222                 struct buffer_state *rx_buff = &priv->rx_buff[*last_rx_bd];
223                 struct arc_emac_bd *rxbd = &priv->rxbd[*last_rx_bd];
224                 unsigned int pktlen, info = le32_to_cpu(rxbd->info);
225                 struct sk_buff *skb;
226                 dma_addr_t addr;
227 
228                 if (unlikely((info & OWN_MASK) == FOR_EMAC))
229                         break;
230 
231                 /* Make a note that we saw a packet at this BD.
232                  * So next time, driver starts from this + 1
233                  */
234                 *last_rx_bd = (*last_rx_bd + 1) % RX_BD_NUM;
235 
236                 if (unlikely((info & FIRST_OR_LAST_MASK) !=
237                              FIRST_OR_LAST_MASK)) {
238                         /* We pre-allocate buffers of MTU size so incoming
239                          * packets won't be split/chained.
240                          */
241                         if (net_ratelimit())
242                                 netdev_err(ndev, "incomplete packet received\n");
243 
244                         /* Return ownership to EMAC */
245                         rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
246                         stats->rx_errors++;
247                         stats->rx_length_errors++;
248                         continue;
249                 }
250 
251                 pktlen = info & LEN_MASK;
252                 stats->rx_packets++;
253                 stats->rx_bytes += pktlen;
254                 skb = rx_buff->skb;
255                 skb_put(skb, pktlen);
256                 skb->dev = ndev;
257                 skb->protocol = eth_type_trans(skb, ndev);
258 
259                 dma_unmap_single(&ndev->dev, dma_unmap_addr(rx_buff, addr),
260                                  dma_unmap_len(rx_buff, len), DMA_FROM_DEVICE);
261 
262                 /* Prepare the BD for next cycle */
263                 rx_buff->skb = netdev_alloc_skb_ip_align(ndev,
264                                                          EMAC_BUFFER_SIZE);
265                 if (unlikely(!rx_buff->skb)) {
266                         stats->rx_errors++;
267                         /* Because receive_skb is below, increment rx_dropped */
268                         stats->rx_dropped++;
269                         continue;
270                 }
271 
272                 /* receive_skb only if new skb was allocated to avoid holes */
273                 netif_receive_skb(skb);
274 
275                 addr = dma_map_single(&ndev->dev, (void *)rx_buff->skb->data,
276                                       EMAC_BUFFER_SIZE, DMA_FROM_DEVICE);
277                 if (dma_mapping_error(&ndev->dev, addr)) {
278                         if (net_ratelimit())
279                                 netdev_err(ndev, "cannot dma map\n");
280                         dev_kfree_skb(rx_buff->skb);
281                         stats->rx_errors++;
282                         continue;
283                 }
284                 dma_unmap_addr_set(rx_buff, addr, addr);
285                 dma_unmap_len_set(rx_buff, len, EMAC_BUFFER_SIZE);
286 
287                 rxbd->data = cpu_to_le32(addr);
288 
289                 /* Make sure pointer to data buffer is set */
290                 wmb();
291 
292                 /* Return ownership to EMAC */
293                 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
294         }
295 
296         return work_done;
297 }
298 
299 /**
300  * arc_emac_poll - NAPI poll handler.
301  * @napi:       Pointer to napi_struct structure.
302  * @budget:     How many BDs to process on 1 call.
303  *
304  * returns:     Number of processed BDs
305  */
306 static int arc_emac_poll(struct napi_struct *napi, int budget)
307 {
308         struct net_device *ndev = napi->dev;
309         struct arc_emac_priv *priv = netdev_priv(ndev);
310         unsigned int work_done;
311 
312         arc_emac_tx_clean(ndev);
313 
314         work_done = arc_emac_rx(ndev, budget);
315         if (work_done < budget) {
316                 napi_complete(napi);
317                 arc_reg_or(priv, R_ENABLE, RXINT_MASK | TXINT_MASK);
318         }
319 
320         return work_done;
321 }
322 
323 /**
324  * arc_emac_intr - Global interrupt handler for EMAC.
325  * @irq:                irq number.
326  * @dev_instance:       device instance.
327  *
328  * returns: IRQ_HANDLED for all cases.
329  *
330  * ARC EMAC has only 1 interrupt line, and depending on bits raised in
331  * STATUS register we may tell what is a reason for interrupt to fire.
332  */
333 static irqreturn_t arc_emac_intr(int irq, void *dev_instance)
334 {
335         struct net_device *ndev = dev_instance;
336         struct arc_emac_priv *priv = netdev_priv(ndev);
337         struct net_device_stats *stats = &ndev->stats;
338         unsigned int status;
339 
340         status = arc_reg_get(priv, R_STATUS);
341         status &= ~MDIO_MASK;
342 
343         /* Reset all flags except "MDIO complete" */
344         arc_reg_set(priv, R_STATUS, status);
345 
346         if (status & (RXINT_MASK | TXINT_MASK)) {
347                 if (likely(napi_schedule_prep(&priv->napi))) {
348                         arc_reg_clr(priv, R_ENABLE, RXINT_MASK | TXINT_MASK);
349                         __napi_schedule(&priv->napi);
350                 }
351         }
352 
353         if (status & ERR_MASK) {
354                 /* MSER/RXCR/RXFR/RXFL interrupt fires on corresponding
355                  * 8-bit error counter overrun.
356                  */
357 
358                 if (status & MSER_MASK) {
359                         stats->rx_missed_errors += 0x100;
360                         stats->rx_errors += 0x100;
361                 }
362 
363                 if (status & RXCR_MASK) {
364                         stats->rx_crc_errors += 0x100;
365                         stats->rx_errors += 0x100;
366                 }
367 
368                 if (status & RXFR_MASK) {
369                         stats->rx_frame_errors += 0x100;
370                         stats->rx_errors += 0x100;
371                 }
372 
373                 if (status & RXFL_MASK) {
374                         stats->rx_over_errors += 0x100;
375                         stats->rx_errors += 0x100;
376                 }
377         }
378 
379         return IRQ_HANDLED;
380 }
381 
382 #ifdef CONFIG_NET_POLL_CONTROLLER
383 static void arc_emac_poll_controller(struct net_device *dev)
384 {
385         disable_irq(dev->irq);
386         arc_emac_intr(dev->irq, dev);
387         enable_irq(dev->irq);
388 }
389 #endif
390 
391 /**
392  * arc_emac_open - Open the network device.
393  * @ndev:       Pointer to the network device.
394  *
395  * returns: 0, on success or non-zero error value on failure.
396  *
397  * This function sets the MAC address, requests and enables an IRQ
398  * for the EMAC device and starts the Tx queue.
399  * It also connects to the phy device.
400  */
401 static int arc_emac_open(struct net_device *ndev)
402 {
403         struct arc_emac_priv *priv = netdev_priv(ndev);
404         struct phy_device *phy_dev = priv->phy_dev;
405         int i;
406 
407         phy_dev->autoneg = AUTONEG_ENABLE;
408         phy_dev->speed = 0;
409         phy_dev->duplex = 0;
410         phy_dev->advertising &= phy_dev->supported;
411 
412         priv->last_rx_bd = 0;
413 
414         /* Allocate and set buffers for Rx BD's */
415         for (i = 0; i < RX_BD_NUM; i++) {
416                 dma_addr_t addr;
417                 unsigned int *last_rx_bd = &priv->last_rx_bd;
418                 struct arc_emac_bd *rxbd = &priv->rxbd[*last_rx_bd];
419                 struct buffer_state *rx_buff = &priv->rx_buff[*last_rx_bd];
420 
421                 rx_buff->skb = netdev_alloc_skb_ip_align(ndev,
422                                                          EMAC_BUFFER_SIZE);
423                 if (unlikely(!rx_buff->skb))
424                         return -ENOMEM;
425 
426                 addr = dma_map_single(&ndev->dev, (void *)rx_buff->skb->data,
427                                       EMAC_BUFFER_SIZE, DMA_FROM_DEVICE);
428                 if (dma_mapping_error(&ndev->dev, addr)) {
429                         netdev_err(ndev, "cannot dma map\n");
430                         dev_kfree_skb(rx_buff->skb);
431                         return -ENOMEM;
432                 }
433                 dma_unmap_addr_set(rx_buff, addr, addr);
434                 dma_unmap_len_set(rx_buff, len, EMAC_BUFFER_SIZE);
435 
436                 rxbd->data = cpu_to_le32(addr);
437 
438                 /* Make sure pointer to data buffer is set */
439                 wmb();
440 
441                 /* Return ownership to EMAC */
442                 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
443 
444                 *last_rx_bd = (*last_rx_bd + 1) % RX_BD_NUM;
445         }
446 
447         /* Clean Tx BD's */
448         memset(priv->txbd, 0, TX_RING_SZ);
449 
450         /* Initialize logical address filter */
451         arc_reg_set(priv, R_LAFL, 0);
452         arc_reg_set(priv, R_LAFH, 0);
453 
454         /* Set BD ring pointers for device side */
455         arc_reg_set(priv, R_RX_RING, (unsigned int)priv->rxbd_dma);
456         arc_reg_set(priv, R_TX_RING, (unsigned int)priv->txbd_dma);
457 
458         /* Enable interrupts */
459         arc_reg_set(priv, R_ENABLE, RXINT_MASK | TXINT_MASK | ERR_MASK);
460 
461         /* Set CONTROL */
462         arc_reg_set(priv, R_CTRL,
463                      (RX_BD_NUM << 24) |        /* RX BD table length */
464                      (TX_BD_NUM << 16) |        /* TX BD table length */
465                      TXRN_MASK | RXRN_MASK);
466 
467         napi_enable(&priv->napi);
468 
469         /* Enable EMAC */
470         arc_reg_or(priv, R_CTRL, EN_MASK);
471 
472         phy_start_aneg(priv->phy_dev);
473 
474         netif_start_queue(ndev);
475 
476         return 0;
477 }
478 
479 /**
480  * arc_emac_set_rx_mode - Change the receive filtering mode.
481  * @ndev:       Pointer to the network device.
482  *
483  * This function enables/disables promiscuous or all-multicast mode
484  * and updates the multicast filtering list of the network device.
485  */
486 static void arc_emac_set_rx_mode(struct net_device *ndev)
487 {
488         struct arc_emac_priv *priv = netdev_priv(ndev);
489 
490         if (ndev->flags & IFF_PROMISC) {
491                 arc_reg_or(priv, R_CTRL, PROM_MASK);
492         } else {
493                 arc_reg_clr(priv, R_CTRL, PROM_MASK);
494 
495                 if (ndev->flags & IFF_ALLMULTI) {
496                         arc_reg_set(priv, R_LAFL, ~0);
497                         arc_reg_set(priv, R_LAFH, ~0);
498                 } else {
499                         struct netdev_hw_addr *ha;
500                         unsigned int filter[2] = { 0, 0 };
501                         int bit;
502 
503                         netdev_for_each_mc_addr(ha, ndev) {
504                                 bit = ether_crc_le(ETH_ALEN, ha->addr) >> 26;
505                                 filter[bit >> 5] |= 1 << (bit & 31);
506                         }
507 
508                         arc_reg_set(priv, R_LAFL, filter[0]);
509                         arc_reg_set(priv, R_LAFH, filter[1]);
510                 }
511         }
512 }
513 
514 /**
515  * arc_emac_stop - Close the network device.
516  * @ndev:       Pointer to the network device.
517  *
518  * This function stops the Tx queue, disables interrupts and frees the IRQ for
519  * the EMAC device.
520  * It also disconnects the PHY device associated with the EMAC device.
521  */
522 static int arc_emac_stop(struct net_device *ndev)
523 {
524         struct arc_emac_priv *priv = netdev_priv(ndev);
525 
526         napi_disable(&priv->napi);
527         netif_stop_queue(ndev);
528 
529         /* Disable interrupts */
530         arc_reg_clr(priv, R_ENABLE, RXINT_MASK | TXINT_MASK | ERR_MASK);
531 
532         /* Disable EMAC */
533         arc_reg_clr(priv, R_CTRL, EN_MASK);
534 
535         return 0;
536 }
537 
538 /**
539  * arc_emac_stats - Get system network statistics.
540  * @ndev:       Pointer to net_device structure.
541  *
542  * Returns the address of the device statistics structure.
543  * Statistics are updated in interrupt handler.
544  */
545 static struct net_device_stats *arc_emac_stats(struct net_device *ndev)
546 {
547         struct arc_emac_priv *priv = netdev_priv(ndev);
548         struct net_device_stats *stats = &ndev->stats;
549         unsigned long miss, rxerr;
550         u8 rxcrc, rxfram, rxoflow;
551 
552         rxerr = arc_reg_get(priv, R_RXERR);
553         miss = arc_reg_get(priv, R_MISS);
554 
555         rxcrc = rxerr;
556         rxfram = rxerr >> 8;
557         rxoflow = rxerr >> 16;
558 
559         stats->rx_errors += miss;
560         stats->rx_errors += rxcrc + rxfram + rxoflow;
561 
562         stats->rx_over_errors += rxoflow;
563         stats->rx_frame_errors += rxfram;
564         stats->rx_crc_errors += rxcrc;
565         stats->rx_missed_errors += miss;
566 
567         return stats;
568 }
569 
570 /**
571  * arc_emac_tx - Starts the data transmission.
572  * @skb:        sk_buff pointer that contains data to be Transmitted.
573  * @ndev:       Pointer to net_device structure.
574  *
575  * returns: NETDEV_TX_OK, on success
576  *              NETDEV_TX_BUSY, if any of the descriptors are not free.
577  *
578  * This function is invoked from upper layers to initiate transmission.
579  */
580 static int arc_emac_tx(struct sk_buff *skb, struct net_device *ndev)
581 {
582         struct arc_emac_priv *priv = netdev_priv(ndev);
583         unsigned int len, *txbd_curr = &priv->txbd_curr;
584         struct net_device_stats *stats = &ndev->stats;
585         __le32 *info = &priv->txbd[*txbd_curr].info;
586         dma_addr_t addr;
587 
588         if (skb_padto(skb, ETH_ZLEN))
589                 return NETDEV_TX_OK;
590 
591         len = max_t(unsigned int, ETH_ZLEN, skb->len);
592 
593         if (unlikely(!arc_emac_tx_avail(priv))) {
594                 netif_stop_queue(ndev);
595                 netdev_err(ndev, "BUG! Tx Ring full when queue awake!\n");
596                 return NETDEV_TX_BUSY;
597         }
598 
599         addr = dma_map_single(&ndev->dev, (void *)skb->data, len,
600                               DMA_TO_DEVICE);
601 
602         if (unlikely(dma_mapping_error(&ndev->dev, addr))) {
603                 stats->tx_dropped++;
604                 stats->tx_errors++;
605                 dev_kfree_skb(skb);
606                 return NETDEV_TX_OK;
607         }
608         dma_unmap_addr_set(&priv->tx_buff[*txbd_curr], addr, addr);
609         dma_unmap_len_set(&priv->tx_buff[*txbd_curr], len, len);
610 
611         priv->tx_buff[*txbd_curr].skb = skb;
612         priv->txbd[*txbd_curr].data = cpu_to_le32(addr);
613 
614         /* Make sure pointer to data buffer is set */
615         wmb();
616 
617         skb_tx_timestamp(skb);
618 
619         *info = cpu_to_le32(FOR_EMAC | FIRST_OR_LAST_MASK | len);
620 
621         /* Increment index to point to the next BD */
622         *txbd_curr = (*txbd_curr + 1) % TX_BD_NUM;
623 
624         /* Ensure that tx_clean() sees the new txbd_curr before
625          * checking the queue status. This prevents an unneeded wake
626          * of the queue in tx_clean().
627          */
628         smp_mb();
629 
630         if (!arc_emac_tx_avail(priv)) {
631                 netif_stop_queue(ndev);
632                 /* Refresh tx_dirty */
633                 smp_mb();
634                 if (arc_emac_tx_avail(priv))
635                         netif_start_queue(ndev);
636         }
637 
638         arc_reg_set(priv, R_STATUS, TXPL_MASK);
639 
640         return NETDEV_TX_OK;
641 }
642 
643 static void arc_emac_set_address_internal(struct net_device *ndev)
644 {
645         struct arc_emac_priv *priv = netdev_priv(ndev);
646         unsigned int addr_low, addr_hi;
647 
648         addr_low = le32_to_cpu(*(__le32 *) &ndev->dev_addr[0]);
649         addr_hi = le16_to_cpu(*(__le16 *) &ndev->dev_addr[4]);
650 
651         arc_reg_set(priv, R_ADDRL, addr_low);
652         arc_reg_set(priv, R_ADDRH, addr_hi);
653 }
654 
655 /**
656  * arc_emac_set_address - Set the MAC address for this device.
657  * @ndev:       Pointer to net_device structure.
658  * @p:          6 byte Address to be written as MAC address.
659  *
660  * This function copies the HW address from the sockaddr structure to the
661  * net_device structure and updates the address in HW.
662  *
663  * returns:     -EBUSY if the net device is busy or 0 if the address is set
664  *              successfully.
665  */
666 static int arc_emac_set_address(struct net_device *ndev, void *p)
667 {
668         struct sockaddr *addr = p;
669 
670         if (netif_running(ndev))
671                 return -EBUSY;
672 
673         if (!is_valid_ether_addr(addr->sa_data))
674                 return -EADDRNOTAVAIL;
675 
676         memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
677 
678         arc_emac_set_address_internal(ndev);
679 
680         return 0;
681 }
682 
683 static const struct net_device_ops arc_emac_netdev_ops = {
684         .ndo_open               = arc_emac_open,
685         .ndo_stop               = arc_emac_stop,
686         .ndo_start_xmit         = arc_emac_tx,
687         .ndo_set_mac_address    = arc_emac_set_address,
688         .ndo_get_stats          = arc_emac_stats,
689         .ndo_set_rx_mode        = arc_emac_set_rx_mode,
690 #ifdef CONFIG_NET_POLL_CONTROLLER
691         .ndo_poll_controller    = arc_emac_poll_controller,
692 #endif
693 };
694 
695 static int arc_emac_probe(struct platform_device *pdev)
696 {
697         struct resource res_regs;
698         struct device_node *phy_node;
699         struct arc_emac_priv *priv;
700         struct net_device *ndev;
701         const char *mac_addr;
702         unsigned int id, clock_frequency, irq;
703         int err;
704 
705         if (!pdev->dev.of_node)
706                 return -ENODEV;
707 
708         /* Get PHY from device tree */
709         phy_node = of_parse_phandle(pdev->dev.of_node, "phy", 0);
710         if (!phy_node) {
711                 dev_err(&pdev->dev, "failed to retrieve phy description from device tree\n");
712                 return -ENODEV;
713         }
714 
715         /* Get EMAC registers base address from device tree */
716         err = of_address_to_resource(pdev->dev.of_node, 0, &res_regs);
717         if (err) {
718                 dev_err(&pdev->dev, "failed to retrieve registers base from device tree\n");
719                 return -ENODEV;
720         }
721 
722         /* Get IRQ from device tree */
723         irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
724         if (!irq) {
725                 dev_err(&pdev->dev, "failed to retrieve <irq> value from device tree\n");
726                 return -ENODEV;
727         }
728 
729         ndev = alloc_etherdev(sizeof(struct arc_emac_priv));
730         if (!ndev)
731                 return -ENOMEM;
732 
733         platform_set_drvdata(pdev, ndev);
734         SET_NETDEV_DEV(ndev, &pdev->dev);
735 
736         ndev->netdev_ops = &arc_emac_netdev_ops;
737         ndev->ethtool_ops = &arc_emac_ethtool_ops;
738         ndev->watchdog_timeo = TX_TIMEOUT;
739         /* FIXME :: no multicast support yet */
740         ndev->flags &= ~IFF_MULTICAST;
741 
742         priv = netdev_priv(ndev);
743         priv->dev = &pdev->dev;
744 
745         priv->regs = devm_ioremap_resource(&pdev->dev, &res_regs);
746         if (IS_ERR(priv->regs)) {
747                 err = PTR_ERR(priv->regs);
748                 goto out_netdev;
749         }
750         dev_dbg(&pdev->dev, "Registers base address is 0x%p\n", priv->regs);
751 
752         priv->clk = of_clk_get(pdev->dev.of_node, 0);
753         if (IS_ERR(priv->clk)) {
754                 /* Get CPU clock frequency from device tree */
755                 if (of_property_read_u32(pdev->dev.of_node, "clock-frequency",
756                                         &clock_frequency)) {
757                         dev_err(&pdev->dev, "failed to retrieve <clock-frequency> from device tree\n");
758                         err = -EINVAL;
759                         goto out_netdev;
760                 }
761         } else {
762                 err = clk_prepare_enable(priv->clk);
763                 if (err) {
764                         dev_err(&pdev->dev, "failed to enable clock\n");
765                         goto out_clkget;
766                 }
767 
768                 clock_frequency = clk_get_rate(priv->clk);
769         }
770 
771         id = arc_reg_get(priv, R_ID);
772 
773         /* Check for EMAC revision 5 or 7, magic number */
774         if (!(id == 0x0005fd02 || id == 0x0007fd02)) {
775                 dev_err(&pdev->dev, "ARC EMAC not detected, id=0x%x\n", id);
776                 err = -ENODEV;
777                 goto out_clken;
778         }
779         dev_info(&pdev->dev, "ARC EMAC detected with id: 0x%x\n", id);
780 
781         /* Set poll rate so that it polls every 1 ms */
782         arc_reg_set(priv, R_POLLRATE, clock_frequency / 1000000);
783 
784         ndev->irq = irq;
785         dev_info(&pdev->dev, "IRQ is %d\n", ndev->irq);
786 
787         /* Register interrupt handler for device */
788         err = devm_request_irq(&pdev->dev, ndev->irq, arc_emac_intr, 0,
789                                ndev->name, ndev);
790         if (err) {
791                 dev_err(&pdev->dev, "could not allocate IRQ\n");
792                 goto out_clken;
793         }
794 
795         /* Get MAC address from device tree */
796         mac_addr = of_get_mac_address(pdev->dev.of_node);
797 
798         if (mac_addr)
799                 memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
800         else
801                 eth_hw_addr_random(ndev);
802 
803         arc_emac_set_address_internal(ndev);
804         dev_info(&pdev->dev, "MAC address is now %pM\n", ndev->dev_addr);
805 
806         /* Do 1 allocation instead of 2 separate ones for Rx and Tx BD rings */
807         priv->rxbd = dmam_alloc_coherent(&pdev->dev, RX_RING_SZ + TX_RING_SZ,
808                                          &priv->rxbd_dma, GFP_KERNEL);
809 
810         if (!priv->rxbd) {
811                 dev_err(&pdev->dev, "failed to allocate data buffers\n");
812                 err = -ENOMEM;
813                 goto out_clken;
814         }
815 
816         priv->txbd = priv->rxbd + RX_BD_NUM;
817 
818         priv->txbd_dma = priv->rxbd_dma + RX_RING_SZ;
819         dev_dbg(&pdev->dev, "EMAC Device addr: Rx Ring [0x%x], Tx Ring[%x]\n",
820                 (unsigned int)priv->rxbd_dma, (unsigned int)priv->txbd_dma);
821 
822         err = arc_mdio_probe(pdev, priv);
823         if (err) {
824                 dev_err(&pdev->dev, "failed to probe MII bus\n");
825                 goto out_clken;
826         }
827 
828         priv->phy_dev = of_phy_connect(ndev, phy_node, arc_emac_adjust_link, 0,
829                                        PHY_INTERFACE_MODE_MII);
830         if (!priv->phy_dev) {
831                 dev_err(&pdev->dev, "of_phy_connect() failed\n");
832                 err = -ENODEV;
833                 goto out_mdio;
834         }
835 
836         dev_info(&pdev->dev, "connected to %s phy with id 0x%x\n",
837                  priv->phy_dev->drv->name, priv->phy_dev->phy_id);
838 
839         netif_napi_add(ndev, &priv->napi, arc_emac_poll, ARC_EMAC_NAPI_WEIGHT);
840 
841         err = register_netdev(ndev);
842         if (err) {
843                 dev_err(&pdev->dev, "failed to register network device\n");
844                 goto out_netif_api;
845         }
846 
847         return 0;
848 
849 out_netif_api:
850         netif_napi_del(&priv->napi);
851         phy_disconnect(priv->phy_dev);
852         priv->phy_dev = NULL;
853 out_mdio:
854         arc_mdio_remove(priv);
855 out_clken:
856         if (!IS_ERR(priv->clk))
857                 clk_disable_unprepare(priv->clk);
858 out_clkget:
859         if (!IS_ERR(priv->clk))
860                 clk_put(priv->clk);
861 out_netdev:
862         free_netdev(ndev);
863         return err;
864 }
865 
866 static int arc_emac_remove(struct platform_device *pdev)
867 {
868         struct net_device *ndev = platform_get_drvdata(pdev);
869         struct arc_emac_priv *priv = netdev_priv(ndev);
870 
871         phy_disconnect(priv->phy_dev);
872         priv->phy_dev = NULL;
873         arc_mdio_remove(priv);
874         unregister_netdev(ndev);
875         netif_napi_del(&priv->napi);
876 
877         if (!IS_ERR(priv->clk)) {
878                 clk_disable_unprepare(priv->clk);
879                 clk_put(priv->clk);
880         }
881 
882         free_netdev(ndev);
883 
884         return 0;
885 }
886 
887 static const struct of_device_id arc_emac_dt_ids[] = {
888         { .compatible = "snps,arc-emac" },
889         { /* Sentinel */ }
890 };
891 MODULE_DEVICE_TABLE(of, arc_emac_dt_ids);
892 
893 static struct platform_driver arc_emac_driver = {
894         .probe = arc_emac_probe,
895         .remove = arc_emac_remove,
896         .driver = {
897                 .name = DRV_NAME,
898                 .owner = THIS_MODULE,
899                 .of_match_table  = arc_emac_dt_ids,
900                 },
901 };
902 
903 module_platform_driver(arc_emac_driver);
904 
905 MODULE_AUTHOR("Alexey Brodkin <abrodkin@synopsys.com>");
906 MODULE_DESCRIPTION("ARC EMAC driver");
907 MODULE_LICENSE("GPL");
908 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us