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