blob: dbea8472bfb47ab6293d2cb9277b342c15ccaff6 [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
Alexey Brodkine4f23792013-06-24 09:54:27 +040029
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 */
37static 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;
Romain Perier6eacf312014-09-08 17:14:47 +000051 if (priv->set_mac_speed)
52 priv->set_mac_speed(priv, priv->speed);
Alexey Brodkine4f23792013-06-24 09:54:27 +040053 }
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 */
82static 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 */
101static 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 */
120static void arc_emac_get_drvinfo(struct net_device *ndev,
121 struct ethtool_drvinfo *info)
122{
Romain Perier23d2d9a2014-08-26 13:14:51 +0000123 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));
Alexey Brodkine4f23792013-06-24 09:54:27 +0400127}
128
129static 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 */
142static void arc_emac_tx_clean(struct net_device *ndev)
143{
144 struct arc_emac_priv *priv = netdev_priv(ndev);
Tobias Klauserff458f62014-07-09 11:07:37 +0200145 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400146 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
Alexey Brodkine4f23792013-06-24 09:54:27 +0400155 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
Alexey Brodkina4a11392013-06-26 11:49:26 +0400175 dma_unmap_single(&ndev->dev, dma_unmap_addr(tx_buff, addr),
176 dma_unmap_len(tx_buff, len), DMA_TO_DEVICE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400177
178 /* return the sk_buff to system */
179 dev_kfree_skb_irq(skb);
180
181 txbd->data = 0;
182 txbd->info = 0;
183
Vineet Gupta27082ee2013-09-04 17:17:15 +0530184 *txbd_dirty = (*txbd_dirty + 1) % TX_BD_NUM;
185
Alexey Brodkine4f23792013-06-24 09:54:27 +0400186 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 */
200static 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
Alexey Brodkin9cff8662013-08-13 17:04:36 +0400205 for (work_done = 0; work_done < budget; work_done++) {
Alexey Brodkine4f23792013-06-24 09:54:27 +0400206 unsigned int *last_rx_bd = &priv->last_rx_bd;
Tobias Klauserff458f62014-07-09 11:07:37 +0200207 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400208 struct buffer_state *rx_buff = &priv->rx_buff[*last_rx_bd];
209 struct arc_emac_bd *rxbd = &priv->rxbd[*last_rx_bd];
Alexey Brodkine4f23792013-06-24 09:54:27 +0400210 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 */
Alexey Brodkina4a11392013-06-26 11:49:26 +0400231 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400232 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
Alexey Brodkina4a11392013-06-26 11:49:26 +0400245 dma_unmap_single(&ndev->dev, dma_unmap_addr(rx_buff, addr),
246 dma_unmap_len(rx_buff, len), DMA_FROM_DEVICE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400247
248 /* Prepare the BD for next cycle */
Alexey Brodkina4a11392013-06-26 11:49:26 +0400249 rx_buff->skb = netdev_alloc_skb_ip_align(ndev,
250 EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400251 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,
Alexey Brodkina4a11392013-06-26 11:49:26 +0400262 EMAC_BUFFER_SIZE, DMA_FROM_DEVICE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400263 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 }
Alexey Brodkina4a11392013-06-26 11:49:26 +0400270 dma_unmap_addr_set(rx_buff, addr, addr);
271 dma_unmap_len_set(rx_buff, len, EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400272
Alexey Brodkina4a11392013-06-26 11:49:26 +0400273 rxbd->data = cpu_to_le32(addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400274
275 /* Make sure pointer to data buffer is set */
276 wmb();
277
278 /* Return ownership to EMAC */
Alexey Brodkina4a11392013-06-26 11:49:26 +0400279 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400280 }
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 */
292static 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 */
319static 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);
Tobias Klauserff458f62014-07-09 11:07:37 +0200323 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400324 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
Beniamino Galvani5a45e572014-05-11 18:11:48 +0200368#ifdef CONFIG_NET_POLL_CONTROLLER
369static 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
Alexey Brodkine4f23792013-06-24 09:54:27 +0400377/**
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 */
387static 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;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400391 int i;
392
393 phy_dev->autoneg = AUTONEG_ENABLE;
394 phy_dev->speed = 0;
395 phy_dev->duplex = 0;
Florian Fainellib0ac9562013-12-05 14:52:15 -0800396 phy_dev->advertising &= phy_dev->supported;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400397
Alexey Brodkina4a11392013-06-26 11:49:26 +0400398 priv->last_rx_bd = 0;
399
Alexey Brodkine4f23792013-06-24 09:54:27 +0400400 /* Allocate and set buffers for Rx BD's */
Alexey Brodkine4f23792013-06-24 09:54:27 +0400401 for (i = 0; i < RX_BD_NUM; i++) {
Alexey Brodkina4a11392013-06-26 11:49:26 +0400402 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))
Alexey Brodkine4f23792013-06-24 09:54:27 +0400410 return -ENOMEM;
411
Alexey Brodkina4a11392013-06-26 11:49:26 +0400412 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);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400423
424 /* Make sure pointer to data buffer is set */
425 wmb();
426
Alexey Brodkina4a11392013-06-26 11:49:26 +0400427 /* Return ownership to EMAC */
428 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400429
Alexey Brodkina4a11392013-06-26 11:49:26 +0400430 *last_rx_bd = (*last_rx_bd + 1) % RX_BD_NUM;
431 }
Alexey Brodkine4f23792013-06-24 09:54:27 +0400432
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/**
Beniamino Galvani775dd682014-05-11 18:11:47 +0200466 * 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 */
472static 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/**
Alexey Brodkine4f23792013-06-24 09:54:27 +0400501 * 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 */
508static 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 */
531static struct net_device_stats *arc_emac_stats(struct net_device *ndev)
532{
533 struct arc_emac_priv *priv = netdev_priv(ndev);
Tobias Klauserff458f62014-07-09 11:07:37 +0200534 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400535 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 */
566static 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;
Tobias Klauserff458f62014-07-09 11:07:37 +0200570 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400571 __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 }
Alexey Brodkina4a11392013-06-26 11:49:26 +0400596 dma_unmap_addr_set(&priv->tx_buff[*txbd_curr], addr, addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400597 dma_unmap_len_set(&priv->tx_buff[*txbd_curr], len, len);
598
599 priv->tx_buff[*txbd_curr].skb = skb;
Alexey Brodkina4a11392013-06-26 11:49:26 +0400600 priv->txbd[*txbd_curr].data = cpu_to_le32(addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400601
602 /* Make sure pointer to data buffer is set */
603 wmb();
604
Eric Dumazet37ec2742013-12-19 18:10:40 -0800605 skb_tx_timestamp(skb);
606
Alexey Brodkine4f23792013-06-24 09:54:27 +0400607 *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
Alexey Brodkine4f23792013-06-24 09:54:27 +0400621 return NETDEV_TX_OK;
622}
623
Max Schwarz235a2512014-04-18 02:17:32 +0200624static 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
Alexey Brodkine4f23792013-06-24 09:54:27 +0400636/**
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 */
647static int arc_emac_set_address(struct net_device *ndev, void *p)
648{
Alexey Brodkine4f23792013-06-24 09:54:27 +0400649 struct sockaddr *addr = p;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400650
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
Max Schwarz235a2512014-04-18 02:17:32 +0200659 arc_emac_set_address_internal(ndev);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400660
661 return 0;
662}
663
664static 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,
Beniamino Galvani775dd682014-05-11 18:11:47 +0200670 .ndo_set_rx_mode = arc_emac_set_rx_mode,
Beniamino Galvani5a45e572014-05-11 18:11:48 +0200671#ifdef CONFIG_NET_POLL_CONTROLLER
672 .ndo_poll_controller = arc_emac_poll_controller,
673#endif
Alexey Brodkine4f23792013-06-24 09:54:27 +0400674};
675
Romain Perier23d2d9a2014-08-26 13:14:51 +0000676int arc_emac_probe(struct net_device *ndev, int interface)
Alexey Brodkine4f23792013-06-24 09:54:27 +0400677{
Romain Perier23d2d9a2014-08-26 13:14:51 +0000678 struct device *dev = ndev->dev.parent;
Thierry Redingf7578492013-09-18 15:24:44 +0200679 struct resource res_regs;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400680 struct device_node *phy_node;
681 struct arc_emac_priv *priv;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400682 const char *mac_addr;
Thierry Redingf7578492013-09-18 15:24:44 +0200683 unsigned int id, clock_frequency, irq;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400684 int err;
685
Alexey Brodkine4f23792013-06-24 09:54:27 +0400686
687 /* Get PHY from device tree */
Romain Perierf15f44e2014-08-26 13:14:49 +0000688 phy_node = of_parse_phandle(dev->of_node, "phy", 0);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400689 if (!phy_node) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000690 dev_err(dev, "failed to retrieve phy description from device tree\n");
Alexey Brodkine4f23792013-06-24 09:54:27 +0400691 return -ENODEV;
692 }
693
694 /* Get EMAC registers base address from device tree */
Romain Perierf15f44e2014-08-26 13:14:49 +0000695 err = of_address_to_resource(dev->of_node, 0, &res_regs);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400696 if (err) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000697 dev_err(dev, "failed to retrieve registers base from device tree\n");
Alexey Brodkine4f23792013-06-24 09:54:27 +0400698 return -ENODEV;
699 }
700
Alexey Brodkine4f23792013-06-24 09:54:27 +0400701 /* Get IRQ from device tree */
Romain Perierf15f44e2014-08-26 13:14:49 +0000702 irq = irq_of_parse_and_map(dev->of_node, 0);
Thierry Redingf7578492013-09-18 15:24:44 +0200703 if (!irq) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000704 dev_err(dev, "failed to retrieve <irq> value from device tree\n");
Alexey Brodkine4f23792013-06-24 09:54:27 +0400705 return -ENODEV;
706 }
707
Alexey Brodkine4f23792013-06-24 09:54:27 +0400708
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);
Romain Perierf15f44e2014-08-26 13:14:49 +0000716 priv->dev = dev;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400717
Romain Perierf15f44e2014-08-26 13:14:49 +0000718 priv->regs = devm_ioremap_resource(dev, &res_regs);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400719 if (IS_ERR(priv->regs)) {
Romain Perier23d2d9a2014-08-26 13:14:51 +0000720 return PTR_ERR(priv->regs);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400721 }
Romain Perierf15f44e2014-08-26 13:14:49 +0000722 dev_dbg(dev, "Registers base address is 0x%p\n", priv->regs);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400723
Romain Perier23d2d9a2014-08-26 13:14:51 +0000724 if (priv->clk) {
Heiko Stübner88154c92014-04-25 10:06:13 +0200725 err = clk_prepare_enable(priv->clk);
726 if (err) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000727 dev_err(dev, "failed to enable clock\n");
Romain Perier23d2d9a2014-08-26 13:14:51 +0000728 return err;
Heiko Stübner88154c92014-04-25 10:06:13 +0200729 }
730
731 clock_frequency = clk_get_rate(priv->clk);
Romain Perier23d2d9a2014-08-26 13:14:51 +0000732 } 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 }
Heiko Stübner88154c92014-04-25 10:06:13 +0200739 }
740
Alexey Brodkine4f23792013-06-24 09:54:27 +0400741 id = arc_reg_get(priv, R_ID);
742
743 /* Check for EMAC revision 5 or 7, magic number */
744 if (!(id == 0x0005fd02 || id == 0x0007fd02)) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000745 dev_err(dev, "ARC EMAC not detected, id=0x%x\n", id);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400746 err = -ENODEV;
Heiko Stübner88154c92014-04-25 10:06:13 +0200747 goto out_clken;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400748 }
Romain Perierf15f44e2014-08-26 13:14:49 +0000749 dev_info(dev, "ARC EMAC detected with id: 0x%x\n", id);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400750
751 /* Set poll rate so that it polls every 1 ms */
752 arc_reg_set(priv, R_POLLRATE, clock_frequency / 1000000);
753
Thierry Redingf7578492013-09-18 15:24:44 +0200754 ndev->irq = irq;
Romain Perierf15f44e2014-08-26 13:14:49 +0000755 dev_info(dev, "IRQ is %d\n", ndev->irq);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400756
757 /* Register interrupt handler for device */
Romain Perierf15f44e2014-08-26 13:14:49 +0000758 err = devm_request_irq(dev, ndev->irq, arc_emac_intr, 0,
Alexey Brodkine4f23792013-06-24 09:54:27 +0400759 ndev->name, ndev);
760 if (err) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000761 dev_err(dev, "could not allocate IRQ\n");
Heiko Stübner88154c92014-04-25 10:06:13 +0200762 goto out_clken;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400763 }
764
765 /* Get MAC address from device tree */
Romain Perierf15f44e2014-08-26 13:14:49 +0000766 mac_addr = of_get_mac_address(dev->of_node);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400767
Luka Perkov99470812013-10-30 00:11:00 +0100768 if (mac_addr)
Alexey Brodkine4f23792013-06-24 09:54:27 +0400769 memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
Luka Perkov99470812013-10-30 00:11:00 +0100770 else
771 eth_hw_addr_random(ndev);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400772
Max Schwarz235a2512014-04-18 02:17:32 +0200773 arc_emac_set_address_internal(ndev);
Romain Perierf15f44e2014-08-26 13:14:49 +0000774 dev_info(dev, "MAC address is now %pM\n", ndev->dev_addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400775
776 /* Do 1 allocation instead of 2 separate ones for Rx and Tx BD rings */
Romain Perierf15f44e2014-08-26 13:14:49 +0000777 priv->rxbd = dmam_alloc_coherent(dev, RX_RING_SZ + TX_RING_SZ,
Alexey Brodkine4f23792013-06-24 09:54:27 +0400778 &priv->rxbd_dma, GFP_KERNEL);
779
780 if (!priv->rxbd) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000781 dev_err(dev, "failed to allocate data buffers\n");
Alexey Brodkine4f23792013-06-24 09:54:27 +0400782 err = -ENOMEM;
Heiko Stübner88154c92014-04-25 10:06:13 +0200783 goto out_clken;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400784 }
785
786 priv->txbd = priv->rxbd + RX_BD_NUM;
787
788 priv->txbd_dma = priv->rxbd_dma + RX_RING_SZ;
Romain Perierf15f44e2014-08-26 13:14:49 +0000789 dev_dbg(dev, "EMAC Device addr: Rx Ring [0x%x], Tx Ring[%x]\n",
Alexey Brodkine4f23792013-06-24 09:54:27 +0400790 (unsigned int)priv->rxbd_dma, (unsigned int)priv->txbd_dma);
791
Romain Perier93e91b32014-08-26 13:14:50 +0000792 err = arc_mdio_probe(priv);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400793 if (err) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000794 dev_err(dev, "failed to probe MII bus\n");
Heiko Stübner88154c92014-04-25 10:06:13 +0200795 goto out_clken;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400796 }
797
798 priv->phy_dev = of_phy_connect(ndev, phy_node, arc_emac_adjust_link, 0,
Romain Perier23d2d9a2014-08-26 13:14:51 +0000799 interface);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400800 if (!priv->phy_dev) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000801 dev_err(dev, "of_phy_connect() failed\n");
Alexey Brodkine4f23792013-06-24 09:54:27 +0400802 err = -ENODEV;
Heiko Stübner796bec12014-04-25 10:03:29 +0200803 goto out_mdio;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400804 }
805
Romain Perierf15f44e2014-08-26 13:14:49 +0000806 dev_info(dev, "connected to %s phy with id 0x%x\n",
Alexey Brodkine4f23792013-06-24 09:54:27 +0400807 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) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000813 dev_err(dev, "failed to register network device\n");
Heiko Stübner796bec12014-04-25 10:03:29 +0200814 goto out_netif_api;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400815 }
816
817 return 0;
818
Heiko Stübner796bec12014-04-25 10:03:29 +0200819out_netif_api:
820 netif_napi_del(&priv->napi);
821 phy_disconnect(priv->phy_dev);
822 priv->phy_dev = NULL;
823out_mdio:
824 arc_mdio_remove(priv);
Heiko Stübner88154c92014-04-25 10:06:13 +0200825out_clken:
Romain Perier23d2d9a2014-08-26 13:14:51 +0000826 if (priv->clk)
Heiko Stübner88154c92014-04-25 10:06:13 +0200827 clk_disable_unprepare(priv->clk);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400828 return err;
829}
Romain Perier23d2d9a2014-08-26 13:14:51 +0000830EXPORT_SYMBOL_GPL(arc_emac_probe);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400831
Romain Perier23d2d9a2014-08-26 13:14:51 +0000832int arc_emac_remove(struct net_device *ndev)
Alexey Brodkine4f23792013-06-24 09:54:27 +0400833{
Alexey Brodkine4f23792013-06-24 09:54:27 +0400834 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);
Heiko Stübner88154c92014-04-25 10:06:13 +0200841
842 if (!IS_ERR(priv->clk)) {
843 clk_disable_unprepare(priv->clk);
Heiko Stübner88154c92014-04-25 10:06:13 +0200844 }
845
Alexey Brodkine4f23792013-06-24 09:54:27 +0400846
847 return 0;
848}
Romain Perier23d2d9a2014-08-26 13:14:51 +0000849EXPORT_SYMBOL_GPL(arc_emac_remove);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400850
851MODULE_AUTHOR("Alexey Brodkin <abrodkin@synopsys.com>");
852MODULE_DESCRIPTION("ARC EMAC driver");
853MODULE_LICENSE("GPL");