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