blob: 5919394d9f585f4c635b27dc985919b25d250646 [file] [log] [blame]
Alexey Brodkine4f23792013-06-24 09:54:27 +04001/*
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
Beniamino Galvani775dd682014-05-11 18:11:47 +020016#include <linux/crc32.h>
Alexey Brodkine4f23792013-06-24 09:54:27 +040017#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/**
Beniamino Galvani74dd40b2014-09-10 22:50:03 +020033 * 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 */
38static 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/**
Alexey Brodkine4f23792013-06-24 09:54:27 +040044 * 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 */
50static 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 */
93static 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 */
112static 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 */
131static 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
138static 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 */
151static void arc_emac_tx_clean(struct net_device *ndev)
152{
153 struct arc_emac_priv *priv = netdev_priv(ndev);
Tobias Klauserff458f62014-07-09 11:07:37 +0200154 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400155 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
Alexey Brodkine4f23792013-06-24 09:54:27 +0400164 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
Alexey Brodkina4a11392013-06-26 11:49:26 +0400184 dma_unmap_single(&ndev->dev, dma_unmap_addr(tx_buff, addr),
185 dma_unmap_len(tx_buff, len), DMA_TO_DEVICE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400186
187 /* return the sk_buff to system */
188 dev_kfree_skb_irq(skb);
189
190 txbd->data = 0;
191 txbd->info = 0;
192
Vineet Gupta27082ee2013-09-04 17:17:15 +0530193 *txbd_dirty = (*txbd_dirty + 1) % TX_BD_NUM;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400194 }
Beniamino Galvani74dd40b2014-09-10 22:50:03 +0200195
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);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400203}
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 */
214static 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
Alexey Brodkin9cff8662013-08-13 17:04:36 +0400219 for (work_done = 0; work_done < budget; work_done++) {
Alexey Brodkine4f23792013-06-24 09:54:27 +0400220 unsigned int *last_rx_bd = &priv->last_rx_bd;
Tobias Klauserff458f62014-07-09 11:07:37 +0200221 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400222 struct buffer_state *rx_buff = &priv->rx_buff[*last_rx_bd];
223 struct arc_emac_bd *rxbd = &priv->rxbd[*last_rx_bd];
Alexey Brodkine4f23792013-06-24 09:54:27 +0400224 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 */
Alexey Brodkina4a11392013-06-26 11:49:26 +0400245 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400246 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
Alexey Brodkina4a11392013-06-26 11:49:26 +0400259 dma_unmap_single(&ndev->dev, dma_unmap_addr(rx_buff, addr),
260 dma_unmap_len(rx_buff, len), DMA_FROM_DEVICE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400261
262 /* Prepare the BD for next cycle */
Alexey Brodkina4a11392013-06-26 11:49:26 +0400263 rx_buff->skb = netdev_alloc_skb_ip_align(ndev,
264 EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400265 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,
Alexey Brodkina4a11392013-06-26 11:49:26 +0400276 EMAC_BUFFER_SIZE, DMA_FROM_DEVICE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400277 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 }
Alexey Brodkina4a11392013-06-26 11:49:26 +0400284 dma_unmap_addr_set(rx_buff, addr, addr);
285 dma_unmap_len_set(rx_buff, len, EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400286
Alexey Brodkina4a11392013-06-26 11:49:26 +0400287 rxbd->data = cpu_to_le32(addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400288
289 /* Make sure pointer to data buffer is set */
290 wmb();
291
292 /* Return ownership to EMAC */
Alexey Brodkina4a11392013-06-26 11:49:26 +0400293 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400294 }
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 */
306static 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);
Beniamino Galvani7ce76792014-09-10 22:50:02 +0200317 arc_reg_or(priv, R_ENABLE, RXINT_MASK | TXINT_MASK);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400318 }
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 */
333static 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);
Tobias Klauserff458f62014-07-09 11:07:37 +0200337 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400338 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
Beniamino Galvani7ce76792014-09-10 22:50:02 +0200346 if (status & (RXINT_MASK | TXINT_MASK)) {
Alexey Brodkine4f23792013-06-24 09:54:27 +0400347 if (likely(napi_schedule_prep(&priv->napi))) {
Beniamino Galvani7ce76792014-09-10 22:50:02 +0200348 arc_reg_clr(priv, R_ENABLE, RXINT_MASK | TXINT_MASK);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400349 __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
Beniamino Galvani5a45e572014-05-11 18:11:48 +0200382#ifdef CONFIG_NET_POLL_CONTROLLER
383static 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
Alexey Brodkine4f23792013-06-24 09:54:27 +0400391/**
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 */
401static 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;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400405 int i;
406
407 phy_dev->autoneg = AUTONEG_ENABLE;
408 phy_dev->speed = 0;
409 phy_dev->duplex = 0;
Florian Fainellib0ac9562013-12-05 14:52:15 -0800410 phy_dev->advertising &= phy_dev->supported;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400411
Alexey Brodkina4a11392013-06-26 11:49:26 +0400412 priv->last_rx_bd = 0;
413
Alexey Brodkine4f23792013-06-24 09:54:27 +0400414 /* Allocate and set buffers for Rx BD's */
Alexey Brodkine4f23792013-06-24 09:54:27 +0400415 for (i = 0; i < RX_BD_NUM; i++) {
Alexey Brodkina4a11392013-06-26 11:49:26 +0400416 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))
Alexey Brodkine4f23792013-06-24 09:54:27 +0400424 return -ENOMEM;
425
Alexey Brodkina4a11392013-06-26 11:49:26 +0400426 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);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400437
438 /* Make sure pointer to data buffer is set */
439 wmb();
440
Alexey Brodkina4a11392013-06-26 11:49:26 +0400441 /* Return ownership to EMAC */
442 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400443
Alexey Brodkina4a11392013-06-26 11:49:26 +0400444 *last_rx_bd = (*last_rx_bd + 1) % RX_BD_NUM;
445 }
Alexey Brodkine4f23792013-06-24 09:54:27 +0400446
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 */
Beniamino Galvani7ce76792014-09-10 22:50:02 +0200459 arc_reg_set(priv, R_ENABLE, RXINT_MASK | TXINT_MASK | ERR_MASK);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400460
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/**
Beniamino Galvani775dd682014-05-11 18:11:47 +0200480 * 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 */
486static 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/**
Alexey Brodkine4f23792013-06-24 09:54:27 +0400515 * 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 */
522static 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 */
Beniamino Galvani7ce76792014-09-10 22:50:02 +0200530 arc_reg_clr(priv, R_ENABLE, RXINT_MASK | TXINT_MASK | ERR_MASK);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400531
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 */
545static struct net_device_stats *arc_emac_stats(struct net_device *ndev)
546{
547 struct arc_emac_priv *priv = netdev_priv(ndev);
Tobias Klauserff458f62014-07-09 11:07:37 +0200548 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400549 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 */
580static 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;
Tobias Klauserff458f62014-07-09 11:07:37 +0200584 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400585 __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
Beniamino Galvani74dd40b2014-09-10 22:50:03 +0200593 if (unlikely(!arc_emac_tx_avail(priv))) {
Alexey Brodkine4f23792013-06-24 09:54:27 +0400594 netif_stop_queue(ndev);
Beniamino Galvani74dd40b2014-09-10 22:50:03 +0200595 netdev_err(ndev, "BUG! Tx Ring full when queue awake!\n");
Alexey Brodkine4f23792013-06-24 09:54:27 +0400596 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 }
Alexey Brodkina4a11392013-06-26 11:49:26 +0400608 dma_unmap_addr_set(&priv->tx_buff[*txbd_curr], addr, addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400609 dma_unmap_len_set(&priv->tx_buff[*txbd_curr], len, len);
610
611 priv->tx_buff[*txbd_curr].skb = skb;
Alexey Brodkina4a11392013-06-26 11:49:26 +0400612 priv->txbd[*txbd_curr].data = cpu_to_le32(addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400613
614 /* Make sure pointer to data buffer is set */
615 wmb();
616
Eric Dumazet37ec2742013-12-19 18:10:40 -0800617 skb_tx_timestamp(skb);
618
Alexey Brodkine4f23792013-06-24 09:54:27 +0400619 *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
Beniamino Galvani74dd40b2014-09-10 22:50:03 +0200624 /* 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();
Alexey Brodkine4f23792013-06-24 09:54:27 +0400629
Beniamino Galvani74dd40b2014-09-10 22:50:03 +0200630 if (!arc_emac_tx_avail(priv)) {
Alexey Brodkine4f23792013-06-24 09:54:27 +0400631 netif_stop_queue(ndev);
Beniamino Galvani74dd40b2014-09-10 22:50:03 +0200632 /* Refresh tx_dirty */
633 smp_mb();
634 if (arc_emac_tx_avail(priv))
635 netif_start_queue(ndev);
636 }
Alexey Brodkine4f23792013-06-24 09:54:27 +0400637
638 arc_reg_set(priv, R_STATUS, TXPL_MASK);
639
Alexey Brodkine4f23792013-06-24 09:54:27 +0400640 return NETDEV_TX_OK;
641}
642
Max Schwarz235a2512014-04-18 02:17:32 +0200643static 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
Alexey Brodkine4f23792013-06-24 09:54:27 +0400655/**
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 */
666static int arc_emac_set_address(struct net_device *ndev, void *p)
667{
Alexey Brodkine4f23792013-06-24 09:54:27 +0400668 struct sockaddr *addr = p;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400669
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
Max Schwarz235a2512014-04-18 02:17:32 +0200678 arc_emac_set_address_internal(ndev);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400679
680 return 0;
681}
682
683static 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,
Beniamino Galvani775dd682014-05-11 18:11:47 +0200689 .ndo_set_rx_mode = arc_emac_set_rx_mode,
Beniamino Galvani5a45e572014-05-11 18:11:48 +0200690#ifdef CONFIG_NET_POLL_CONTROLLER
691 .ndo_poll_controller = arc_emac_poll_controller,
692#endif
Alexey Brodkine4f23792013-06-24 09:54:27 +0400693};
694
695static int arc_emac_probe(struct platform_device *pdev)
696{
Thierry Redingf7578492013-09-18 15:24:44 +0200697 struct resource res_regs;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400698 struct device_node *phy_node;
699 struct arc_emac_priv *priv;
700 struct net_device *ndev;
701 const char *mac_addr;
Thierry Redingf7578492013-09-18 15:24:44 +0200702 unsigned int id, clock_frequency, irq;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400703 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
Alexey Brodkine4f23792013-06-24 09:54:27 +0400722 /* Get IRQ from device tree */
Thierry Redingf7578492013-09-18 15:24:44 +0200723 irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
724 if (!irq) {
Alexey Brodkine4f23792013-06-24 09:54:27 +0400725 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
Wei Yongjun45f1b022013-11-11 14:15:12 +0800733 platform_set_drvdata(pdev, ndev);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400734 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;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400744
745 priv->regs = devm_ioremap_resource(&pdev->dev, &res_regs);
746 if (IS_ERR(priv->regs)) {
747 err = PTR_ERR(priv->regs);
Heiko Stübner796bec12014-04-25 10:03:29 +0200748 goto out_netdev;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400749 }
750 dev_dbg(&pdev->dev, "Registers base address is 0x%p\n", priv->regs);
751
Heiko Stübner88154c92014-04-25 10:06:13 +0200752 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
Alexey Brodkine4f23792013-06-24 09:54:27 +0400771 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;
Heiko Stübner88154c92014-04-25 10:06:13 +0200777 goto out_clken;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400778 }
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
Thierry Redingf7578492013-09-18 15:24:44 +0200784 ndev->irq = irq;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400785 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");
Heiko Stübner88154c92014-04-25 10:06:13 +0200792 goto out_clken;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400793 }
794
795 /* Get MAC address from device tree */
796 mac_addr = of_get_mac_address(pdev->dev.of_node);
797
Luka Perkov99470812013-10-30 00:11:00 +0100798 if (mac_addr)
Alexey Brodkine4f23792013-06-24 09:54:27 +0400799 memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
Luka Perkov99470812013-10-30 00:11:00 +0100800 else
801 eth_hw_addr_random(ndev);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400802
Max Schwarz235a2512014-04-18 02:17:32 +0200803 arc_emac_set_address_internal(ndev);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400804 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;
Heiko Stübner88154c92014-04-25 10:06:13 +0200813 goto out_clken;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400814 }
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");
Heiko Stübner88154c92014-04-25 10:06:13 +0200825 goto out_clken;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400826 }
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;
Heiko Stübner796bec12014-04-25 10:03:29 +0200833 goto out_mdio;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400834 }
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) {
Alexey Brodkine4f23792013-06-24 09:54:27 +0400843 dev_err(&pdev->dev, "failed to register network device\n");
Heiko Stübner796bec12014-04-25 10:03:29 +0200844 goto out_netif_api;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400845 }
846
847 return 0;
848
Heiko Stübner796bec12014-04-25 10:03:29 +0200849out_netif_api:
850 netif_napi_del(&priv->napi);
851 phy_disconnect(priv->phy_dev);
852 priv->phy_dev = NULL;
853out_mdio:
854 arc_mdio_remove(priv);
Heiko Stübner88154c92014-04-25 10:06:13 +0200855out_clken:
856 if (!IS_ERR(priv->clk))
857 clk_disable_unprepare(priv->clk);
858out_clkget:
859 if (!IS_ERR(priv->clk))
860 clk_put(priv->clk);
Heiko Stübner796bec12014-04-25 10:03:29 +0200861out_netdev:
Alexey Brodkine4f23792013-06-24 09:54:27 +0400862 free_netdev(ndev);
863 return err;
864}
865
866static 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);
Heiko Stübner88154c92014-04-25 10:06:13 +0200876
877 if (!IS_ERR(priv->clk)) {
878 clk_disable_unprepare(priv->clk);
879 clk_put(priv->clk);
880 }
881
Alexey Brodkine4f23792013-06-24 09:54:27 +0400882 free_netdev(ndev);
883
884 return 0;
885}
886
887static const struct of_device_id arc_emac_dt_ids[] = {
888 { .compatible = "snps,arc-emac" },
889 { /* Sentinel */ }
890};
891MODULE_DEVICE_TABLE(of, arc_emac_dt_ids);
892
893static 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
903module_platform_driver(arc_emac_driver);
904
905MODULE_AUTHOR("Alexey Brodkin <abrodkin@synopsys.com>");
906MODULE_DESCRIPTION("ARC EMAC driver");
907MODULE_LICENSE("GPL");