blob: a7773923a7a06472bd889e9d3cfe59c048d6e8d4 [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;
51 }
52
53 if (priv->duplex != phy_dev->duplex) {
54 reg = arc_reg_get(priv, R_CTRL);
55
56 if (DUPLEX_FULL == phy_dev->duplex)
57 reg |= ENFL_MASK;
58 else
59 reg &= ~ENFL_MASK;
60
61 arc_reg_set(priv, R_CTRL, reg);
62 priv->duplex = phy_dev->duplex;
63 state_changed = 1;
64 }
65
66 if (state_changed)
67 phy_print_status(phy_dev);
68}
69
70/**
71 * arc_emac_get_settings - Get PHY settings.
72 * @ndev: Pointer to net_device structure.
73 * @cmd: Pointer to ethtool_cmd structure.
74 *
75 * This implements ethtool command for getting PHY settings. If PHY could
76 * not be found, the function returns -ENODEV. This function calls the
77 * relevant PHY ethtool API to get the PHY settings.
78 * Issue "ethtool ethX" under linux prompt to execute this function.
79 */
80static int arc_emac_get_settings(struct net_device *ndev,
81 struct ethtool_cmd *cmd)
82{
83 struct arc_emac_priv *priv = netdev_priv(ndev);
84
85 return phy_ethtool_gset(priv->phy_dev, cmd);
86}
87
88/**
89 * arc_emac_set_settings - Set PHY settings as passed in the argument.
90 * @ndev: Pointer to net_device structure.
91 * @cmd: Pointer to ethtool_cmd structure.
92 *
93 * This implements ethtool command for setting various PHY settings. If PHY
94 * could not be found, the function returns -ENODEV. This function calls the
95 * relevant PHY ethtool API to set the PHY.
96 * Issue e.g. "ethtool -s ethX speed 1000" under linux prompt to execute this
97 * function.
98 */
99static int arc_emac_set_settings(struct net_device *ndev,
100 struct ethtool_cmd *cmd)
101{
102 struct arc_emac_priv *priv = netdev_priv(ndev);
103
104 if (!capable(CAP_NET_ADMIN))
105 return -EPERM;
106
107 return phy_ethtool_sset(priv->phy_dev, cmd);
108}
109
110/**
111 * arc_emac_get_drvinfo - Get EMAC driver information.
112 * @ndev: Pointer to net_device structure.
113 * @info: Pointer to ethtool_drvinfo structure.
114 *
115 * This implements ethtool command for getting the driver information.
116 * Issue "ethtool -i ethX" under linux prompt to execute this function.
117 */
118static void arc_emac_get_drvinfo(struct net_device *ndev,
119 struct ethtool_drvinfo *info)
120{
Romain Perier23d2d9a2014-08-26 13:14:51 +0000121 struct arc_emac_priv *priv = netdev_priv(ndev);
122
123 strlcpy(info->driver, priv->drv_name, sizeof(info->driver));
124 strlcpy(info->version, priv->drv_version, sizeof(info->version));
Alexey Brodkine4f23792013-06-24 09:54:27 +0400125}
126
127static const struct ethtool_ops arc_emac_ethtool_ops = {
128 .get_settings = arc_emac_get_settings,
129 .set_settings = arc_emac_set_settings,
130 .get_drvinfo = arc_emac_get_drvinfo,
131 .get_link = ethtool_op_get_link,
132};
133
134#define FIRST_OR_LAST_MASK (FIRST_MASK | LAST_MASK)
135
136/**
137 * arc_emac_tx_clean - clears processed by EMAC Tx BDs.
138 * @ndev: Pointer to the network device.
139 */
140static void arc_emac_tx_clean(struct net_device *ndev)
141{
142 struct arc_emac_priv *priv = netdev_priv(ndev);
Tobias Klauserff458f62014-07-09 11:07:37 +0200143 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400144 unsigned int i;
145
146 for (i = 0; i < TX_BD_NUM; i++) {
147 unsigned int *txbd_dirty = &priv->txbd_dirty;
148 struct arc_emac_bd *txbd = &priv->txbd[*txbd_dirty];
149 struct buffer_state *tx_buff = &priv->tx_buff[*txbd_dirty];
150 struct sk_buff *skb = tx_buff->skb;
151 unsigned int info = le32_to_cpu(txbd->info);
152
Alexey Brodkine4f23792013-06-24 09:54:27 +0400153 if ((info & FOR_EMAC) || !txbd->data)
154 break;
155
156 if (unlikely(info & (DROP | DEFR | LTCL | UFLO))) {
157 stats->tx_errors++;
158 stats->tx_dropped++;
159
160 if (info & DEFR)
161 stats->tx_carrier_errors++;
162
163 if (info & LTCL)
164 stats->collisions++;
165
166 if (info & UFLO)
167 stats->tx_fifo_errors++;
168 } else if (likely(info & FIRST_OR_LAST_MASK)) {
169 stats->tx_packets++;
170 stats->tx_bytes += skb->len;
171 }
172
Alexey Brodkina4a11392013-06-26 11:49:26 +0400173 dma_unmap_single(&ndev->dev, dma_unmap_addr(tx_buff, addr),
174 dma_unmap_len(tx_buff, len), DMA_TO_DEVICE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400175
176 /* return the sk_buff to system */
177 dev_kfree_skb_irq(skb);
178
179 txbd->data = 0;
180 txbd->info = 0;
181
Vineet Gupta27082ee2013-09-04 17:17:15 +0530182 *txbd_dirty = (*txbd_dirty + 1) % TX_BD_NUM;
183
Alexey Brodkine4f23792013-06-24 09:54:27 +0400184 if (netif_queue_stopped(ndev))
185 netif_wake_queue(ndev);
186 }
187}
188
189/**
190 * arc_emac_rx - processing of Rx packets.
191 * @ndev: Pointer to the network device.
192 * @budget: How many BDs to process on 1 call.
193 *
194 * returns: Number of processed BDs
195 *
196 * Iterate through Rx BDs and deliver received packages to upper layer.
197 */
198static int arc_emac_rx(struct net_device *ndev, int budget)
199{
200 struct arc_emac_priv *priv = netdev_priv(ndev);
201 unsigned int work_done;
202
Alexey Brodkin9cff8662013-08-13 17:04:36 +0400203 for (work_done = 0; work_done < budget; work_done++) {
Alexey Brodkine4f23792013-06-24 09:54:27 +0400204 unsigned int *last_rx_bd = &priv->last_rx_bd;
Tobias Klauserff458f62014-07-09 11:07:37 +0200205 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400206 struct buffer_state *rx_buff = &priv->rx_buff[*last_rx_bd];
207 struct arc_emac_bd *rxbd = &priv->rxbd[*last_rx_bd];
Alexey Brodkine4f23792013-06-24 09:54:27 +0400208 unsigned int pktlen, info = le32_to_cpu(rxbd->info);
209 struct sk_buff *skb;
210 dma_addr_t addr;
211
212 if (unlikely((info & OWN_MASK) == FOR_EMAC))
213 break;
214
215 /* Make a note that we saw a packet at this BD.
216 * So next time, driver starts from this + 1
217 */
218 *last_rx_bd = (*last_rx_bd + 1) % RX_BD_NUM;
219
220 if (unlikely((info & FIRST_OR_LAST_MASK) !=
221 FIRST_OR_LAST_MASK)) {
222 /* We pre-allocate buffers of MTU size so incoming
223 * packets won't be split/chained.
224 */
225 if (net_ratelimit())
226 netdev_err(ndev, "incomplete packet received\n");
227
228 /* Return ownership to EMAC */
Alexey Brodkina4a11392013-06-26 11:49:26 +0400229 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400230 stats->rx_errors++;
231 stats->rx_length_errors++;
232 continue;
233 }
234
235 pktlen = info & LEN_MASK;
236 stats->rx_packets++;
237 stats->rx_bytes += pktlen;
238 skb = rx_buff->skb;
239 skb_put(skb, pktlen);
240 skb->dev = ndev;
241 skb->protocol = eth_type_trans(skb, ndev);
242
Alexey Brodkina4a11392013-06-26 11:49:26 +0400243 dma_unmap_single(&ndev->dev, dma_unmap_addr(rx_buff, addr),
244 dma_unmap_len(rx_buff, len), DMA_FROM_DEVICE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400245
246 /* Prepare the BD for next cycle */
Alexey Brodkina4a11392013-06-26 11:49:26 +0400247 rx_buff->skb = netdev_alloc_skb_ip_align(ndev,
248 EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400249 if (unlikely(!rx_buff->skb)) {
250 stats->rx_errors++;
251 /* Because receive_skb is below, increment rx_dropped */
252 stats->rx_dropped++;
253 continue;
254 }
255
256 /* receive_skb only if new skb was allocated to avoid holes */
257 netif_receive_skb(skb);
258
259 addr = dma_map_single(&ndev->dev, (void *)rx_buff->skb->data,
Alexey Brodkina4a11392013-06-26 11:49:26 +0400260 EMAC_BUFFER_SIZE, DMA_FROM_DEVICE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400261 if (dma_mapping_error(&ndev->dev, addr)) {
262 if (net_ratelimit())
263 netdev_err(ndev, "cannot dma map\n");
264 dev_kfree_skb(rx_buff->skb);
265 stats->rx_errors++;
266 continue;
267 }
Alexey Brodkina4a11392013-06-26 11:49:26 +0400268 dma_unmap_addr_set(rx_buff, addr, addr);
269 dma_unmap_len_set(rx_buff, len, EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400270
Alexey Brodkina4a11392013-06-26 11:49:26 +0400271 rxbd->data = cpu_to_le32(addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400272
273 /* Make sure pointer to data buffer is set */
274 wmb();
275
276 /* Return ownership to EMAC */
Alexey Brodkina4a11392013-06-26 11:49:26 +0400277 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400278 }
279
280 return work_done;
281}
282
283/**
284 * arc_emac_poll - NAPI poll handler.
285 * @napi: Pointer to napi_struct structure.
286 * @budget: How many BDs to process on 1 call.
287 *
288 * returns: Number of processed BDs
289 */
290static int arc_emac_poll(struct napi_struct *napi, int budget)
291{
292 struct net_device *ndev = napi->dev;
293 struct arc_emac_priv *priv = netdev_priv(ndev);
294 unsigned int work_done;
295
296 arc_emac_tx_clean(ndev);
297
298 work_done = arc_emac_rx(ndev, budget);
299 if (work_done < budget) {
300 napi_complete(napi);
301 arc_reg_or(priv, R_ENABLE, RXINT_MASK);
302 }
303
304 return work_done;
305}
306
307/**
308 * arc_emac_intr - Global interrupt handler for EMAC.
309 * @irq: irq number.
310 * @dev_instance: device instance.
311 *
312 * returns: IRQ_HANDLED for all cases.
313 *
314 * ARC EMAC has only 1 interrupt line, and depending on bits raised in
315 * STATUS register we may tell what is a reason for interrupt to fire.
316 */
317static irqreturn_t arc_emac_intr(int irq, void *dev_instance)
318{
319 struct net_device *ndev = dev_instance;
320 struct arc_emac_priv *priv = netdev_priv(ndev);
Tobias Klauserff458f62014-07-09 11:07:37 +0200321 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400322 unsigned int status;
323
324 status = arc_reg_get(priv, R_STATUS);
325 status &= ~MDIO_MASK;
326
327 /* Reset all flags except "MDIO complete" */
328 arc_reg_set(priv, R_STATUS, status);
329
330 if (status & RXINT_MASK) {
331 if (likely(napi_schedule_prep(&priv->napi))) {
332 arc_reg_clr(priv, R_ENABLE, RXINT_MASK);
333 __napi_schedule(&priv->napi);
334 }
335 }
336
337 if (status & ERR_MASK) {
338 /* MSER/RXCR/RXFR/RXFL interrupt fires on corresponding
339 * 8-bit error counter overrun.
340 */
341
342 if (status & MSER_MASK) {
343 stats->rx_missed_errors += 0x100;
344 stats->rx_errors += 0x100;
345 }
346
347 if (status & RXCR_MASK) {
348 stats->rx_crc_errors += 0x100;
349 stats->rx_errors += 0x100;
350 }
351
352 if (status & RXFR_MASK) {
353 stats->rx_frame_errors += 0x100;
354 stats->rx_errors += 0x100;
355 }
356
357 if (status & RXFL_MASK) {
358 stats->rx_over_errors += 0x100;
359 stats->rx_errors += 0x100;
360 }
361 }
362
363 return IRQ_HANDLED;
364}
365
Beniamino Galvani5a45e572014-05-11 18:11:48 +0200366#ifdef CONFIG_NET_POLL_CONTROLLER
367static void arc_emac_poll_controller(struct net_device *dev)
368{
369 disable_irq(dev->irq);
370 arc_emac_intr(dev->irq, dev);
371 enable_irq(dev->irq);
372}
373#endif
374
Alexey Brodkine4f23792013-06-24 09:54:27 +0400375/**
376 * arc_emac_open - Open the network device.
377 * @ndev: Pointer to the network device.
378 *
379 * returns: 0, on success or non-zero error value on failure.
380 *
381 * This function sets the MAC address, requests and enables an IRQ
382 * for the EMAC device and starts the Tx queue.
383 * It also connects to the phy device.
384 */
385static int arc_emac_open(struct net_device *ndev)
386{
387 struct arc_emac_priv *priv = netdev_priv(ndev);
388 struct phy_device *phy_dev = priv->phy_dev;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400389 int i;
390
391 phy_dev->autoneg = AUTONEG_ENABLE;
392 phy_dev->speed = 0;
393 phy_dev->duplex = 0;
Florian Fainellib0ac9562013-12-05 14:52:15 -0800394 phy_dev->advertising &= phy_dev->supported;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400395
Alexey Brodkina4a11392013-06-26 11:49:26 +0400396 priv->last_rx_bd = 0;
397
Alexey Brodkine4f23792013-06-24 09:54:27 +0400398 /* Allocate and set buffers for Rx BD's */
Alexey Brodkine4f23792013-06-24 09:54:27 +0400399 for (i = 0; i < RX_BD_NUM; i++) {
Alexey Brodkina4a11392013-06-26 11:49:26 +0400400 dma_addr_t addr;
401 unsigned int *last_rx_bd = &priv->last_rx_bd;
402 struct arc_emac_bd *rxbd = &priv->rxbd[*last_rx_bd];
403 struct buffer_state *rx_buff = &priv->rx_buff[*last_rx_bd];
404
405 rx_buff->skb = netdev_alloc_skb_ip_align(ndev,
406 EMAC_BUFFER_SIZE);
407 if (unlikely(!rx_buff->skb))
Alexey Brodkine4f23792013-06-24 09:54:27 +0400408 return -ENOMEM;
409
Alexey Brodkina4a11392013-06-26 11:49:26 +0400410 addr = dma_map_single(&ndev->dev, (void *)rx_buff->skb->data,
411 EMAC_BUFFER_SIZE, DMA_FROM_DEVICE);
412 if (dma_mapping_error(&ndev->dev, addr)) {
413 netdev_err(ndev, "cannot dma map\n");
414 dev_kfree_skb(rx_buff->skb);
415 return -ENOMEM;
416 }
417 dma_unmap_addr_set(rx_buff, addr, addr);
418 dma_unmap_len_set(rx_buff, len, EMAC_BUFFER_SIZE);
419
420 rxbd->data = cpu_to_le32(addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400421
422 /* Make sure pointer to data buffer is set */
423 wmb();
424
Alexey Brodkina4a11392013-06-26 11:49:26 +0400425 /* Return ownership to EMAC */
426 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400427
Alexey Brodkina4a11392013-06-26 11:49:26 +0400428 *last_rx_bd = (*last_rx_bd + 1) % RX_BD_NUM;
429 }
Alexey Brodkine4f23792013-06-24 09:54:27 +0400430
431 /* Clean Tx BD's */
432 memset(priv->txbd, 0, TX_RING_SZ);
433
434 /* Initialize logical address filter */
435 arc_reg_set(priv, R_LAFL, 0);
436 arc_reg_set(priv, R_LAFH, 0);
437
438 /* Set BD ring pointers for device side */
439 arc_reg_set(priv, R_RX_RING, (unsigned int)priv->rxbd_dma);
440 arc_reg_set(priv, R_TX_RING, (unsigned int)priv->txbd_dma);
441
442 /* Enable interrupts */
443 arc_reg_set(priv, R_ENABLE, RXINT_MASK | ERR_MASK);
444
445 /* Set CONTROL */
446 arc_reg_set(priv, R_CTRL,
447 (RX_BD_NUM << 24) | /* RX BD table length */
448 (TX_BD_NUM << 16) | /* TX BD table length */
449 TXRN_MASK | RXRN_MASK);
450
451 napi_enable(&priv->napi);
452
453 /* Enable EMAC */
454 arc_reg_or(priv, R_CTRL, EN_MASK);
455
456 phy_start_aneg(priv->phy_dev);
457
458 netif_start_queue(ndev);
459
460 return 0;
461}
462
463/**
Beniamino Galvani775dd682014-05-11 18:11:47 +0200464 * arc_emac_set_rx_mode - Change the receive filtering mode.
465 * @ndev: Pointer to the network device.
466 *
467 * This function enables/disables promiscuous or all-multicast mode
468 * and updates the multicast filtering list of the network device.
469 */
470static void arc_emac_set_rx_mode(struct net_device *ndev)
471{
472 struct arc_emac_priv *priv = netdev_priv(ndev);
473
474 if (ndev->flags & IFF_PROMISC) {
475 arc_reg_or(priv, R_CTRL, PROM_MASK);
476 } else {
477 arc_reg_clr(priv, R_CTRL, PROM_MASK);
478
479 if (ndev->flags & IFF_ALLMULTI) {
480 arc_reg_set(priv, R_LAFL, ~0);
481 arc_reg_set(priv, R_LAFH, ~0);
482 } else {
483 struct netdev_hw_addr *ha;
484 unsigned int filter[2] = { 0, 0 };
485 int bit;
486
487 netdev_for_each_mc_addr(ha, ndev) {
488 bit = ether_crc_le(ETH_ALEN, ha->addr) >> 26;
489 filter[bit >> 5] |= 1 << (bit & 31);
490 }
491
492 arc_reg_set(priv, R_LAFL, filter[0]);
493 arc_reg_set(priv, R_LAFH, filter[1]);
494 }
495 }
496}
497
498/**
Alexey Brodkine4f23792013-06-24 09:54:27 +0400499 * arc_emac_stop - Close the network device.
500 * @ndev: Pointer to the network device.
501 *
502 * This function stops the Tx queue, disables interrupts and frees the IRQ for
503 * the EMAC device.
504 * It also disconnects the PHY device associated with the EMAC device.
505 */
506static int arc_emac_stop(struct net_device *ndev)
507{
508 struct arc_emac_priv *priv = netdev_priv(ndev);
509
510 napi_disable(&priv->napi);
511 netif_stop_queue(ndev);
512
513 /* Disable interrupts */
514 arc_reg_clr(priv, R_ENABLE, RXINT_MASK | ERR_MASK);
515
516 /* Disable EMAC */
517 arc_reg_clr(priv, R_CTRL, EN_MASK);
518
519 return 0;
520}
521
522/**
523 * arc_emac_stats - Get system network statistics.
524 * @ndev: Pointer to net_device structure.
525 *
526 * Returns the address of the device statistics structure.
527 * Statistics are updated in interrupt handler.
528 */
529static struct net_device_stats *arc_emac_stats(struct net_device *ndev)
530{
531 struct arc_emac_priv *priv = netdev_priv(ndev);
Tobias Klauserff458f62014-07-09 11:07:37 +0200532 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400533 unsigned long miss, rxerr;
534 u8 rxcrc, rxfram, rxoflow;
535
536 rxerr = arc_reg_get(priv, R_RXERR);
537 miss = arc_reg_get(priv, R_MISS);
538
539 rxcrc = rxerr;
540 rxfram = rxerr >> 8;
541 rxoflow = rxerr >> 16;
542
543 stats->rx_errors += miss;
544 stats->rx_errors += rxcrc + rxfram + rxoflow;
545
546 stats->rx_over_errors += rxoflow;
547 stats->rx_frame_errors += rxfram;
548 stats->rx_crc_errors += rxcrc;
549 stats->rx_missed_errors += miss;
550
551 return stats;
552}
553
554/**
555 * arc_emac_tx - Starts the data transmission.
556 * @skb: sk_buff pointer that contains data to be Transmitted.
557 * @ndev: Pointer to net_device structure.
558 *
559 * returns: NETDEV_TX_OK, on success
560 * NETDEV_TX_BUSY, if any of the descriptors are not free.
561 *
562 * This function is invoked from upper layers to initiate transmission.
563 */
564static int arc_emac_tx(struct sk_buff *skb, struct net_device *ndev)
565{
566 struct arc_emac_priv *priv = netdev_priv(ndev);
567 unsigned int len, *txbd_curr = &priv->txbd_curr;
Tobias Klauserff458f62014-07-09 11:07:37 +0200568 struct net_device_stats *stats = &ndev->stats;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400569 __le32 *info = &priv->txbd[*txbd_curr].info;
570 dma_addr_t addr;
571
572 if (skb_padto(skb, ETH_ZLEN))
573 return NETDEV_TX_OK;
574
575 len = max_t(unsigned int, ETH_ZLEN, skb->len);
576
577 /* EMAC still holds this buffer in its possession.
578 * CPU must not modify this buffer descriptor
579 */
580 if (unlikely((le32_to_cpu(*info) & OWN_MASK) == FOR_EMAC)) {
581 netif_stop_queue(ndev);
582 return NETDEV_TX_BUSY;
583 }
584
585 addr = dma_map_single(&ndev->dev, (void *)skb->data, len,
586 DMA_TO_DEVICE);
587
588 if (unlikely(dma_mapping_error(&ndev->dev, addr))) {
589 stats->tx_dropped++;
590 stats->tx_errors++;
591 dev_kfree_skb(skb);
592 return NETDEV_TX_OK;
593 }
Alexey Brodkina4a11392013-06-26 11:49:26 +0400594 dma_unmap_addr_set(&priv->tx_buff[*txbd_curr], addr, addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400595 dma_unmap_len_set(&priv->tx_buff[*txbd_curr], len, len);
596
597 priv->tx_buff[*txbd_curr].skb = skb;
Alexey Brodkina4a11392013-06-26 11:49:26 +0400598 priv->txbd[*txbd_curr].data = cpu_to_le32(addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400599
600 /* Make sure pointer to data buffer is set */
601 wmb();
602
Eric Dumazet37ec2742013-12-19 18:10:40 -0800603 skb_tx_timestamp(skb);
604
Alexey Brodkine4f23792013-06-24 09:54:27 +0400605 *info = cpu_to_le32(FOR_EMAC | FIRST_OR_LAST_MASK | len);
606
607 /* Increment index to point to the next BD */
608 *txbd_curr = (*txbd_curr + 1) % TX_BD_NUM;
609
610 /* Get "info" of the next BD */
611 info = &priv->txbd[*txbd_curr].info;
612
613 /* Check if if Tx BD ring is full - next BD is still owned by EMAC */
614 if (unlikely((le32_to_cpu(*info) & OWN_MASK) == FOR_EMAC))
615 netif_stop_queue(ndev);
616
617 arc_reg_set(priv, R_STATUS, TXPL_MASK);
618
Alexey Brodkine4f23792013-06-24 09:54:27 +0400619 return NETDEV_TX_OK;
620}
621
Max Schwarz235a2512014-04-18 02:17:32 +0200622static void arc_emac_set_address_internal(struct net_device *ndev)
623{
624 struct arc_emac_priv *priv = netdev_priv(ndev);
625 unsigned int addr_low, addr_hi;
626
627 addr_low = le32_to_cpu(*(__le32 *) &ndev->dev_addr[0]);
628 addr_hi = le16_to_cpu(*(__le16 *) &ndev->dev_addr[4]);
629
630 arc_reg_set(priv, R_ADDRL, addr_low);
631 arc_reg_set(priv, R_ADDRH, addr_hi);
632}
633
Alexey Brodkine4f23792013-06-24 09:54:27 +0400634/**
635 * arc_emac_set_address - Set the MAC address for this device.
636 * @ndev: Pointer to net_device structure.
637 * @p: 6 byte Address to be written as MAC address.
638 *
639 * This function copies the HW address from the sockaddr structure to the
640 * net_device structure and updates the address in HW.
641 *
642 * returns: -EBUSY if the net device is busy or 0 if the address is set
643 * successfully.
644 */
645static int arc_emac_set_address(struct net_device *ndev, void *p)
646{
Alexey Brodkine4f23792013-06-24 09:54:27 +0400647 struct sockaddr *addr = p;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400648
649 if (netif_running(ndev))
650 return -EBUSY;
651
652 if (!is_valid_ether_addr(addr->sa_data))
653 return -EADDRNOTAVAIL;
654
655 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
656
Max Schwarz235a2512014-04-18 02:17:32 +0200657 arc_emac_set_address_internal(ndev);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400658
659 return 0;
660}
661
662static const struct net_device_ops arc_emac_netdev_ops = {
663 .ndo_open = arc_emac_open,
664 .ndo_stop = arc_emac_stop,
665 .ndo_start_xmit = arc_emac_tx,
666 .ndo_set_mac_address = arc_emac_set_address,
667 .ndo_get_stats = arc_emac_stats,
Beniamino Galvani775dd682014-05-11 18:11:47 +0200668 .ndo_set_rx_mode = arc_emac_set_rx_mode,
Beniamino Galvani5a45e572014-05-11 18:11:48 +0200669#ifdef CONFIG_NET_POLL_CONTROLLER
670 .ndo_poll_controller = arc_emac_poll_controller,
671#endif
Alexey Brodkine4f23792013-06-24 09:54:27 +0400672};
673
Romain Perier23d2d9a2014-08-26 13:14:51 +0000674int arc_emac_probe(struct net_device *ndev, int interface)
Alexey Brodkine4f23792013-06-24 09:54:27 +0400675{
Romain Perier23d2d9a2014-08-26 13:14:51 +0000676 struct device *dev = ndev->dev.parent;
Thierry Redingf7578492013-09-18 15:24:44 +0200677 struct resource res_regs;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400678 struct device_node *phy_node;
679 struct arc_emac_priv *priv;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400680 const char *mac_addr;
Thierry Redingf7578492013-09-18 15:24:44 +0200681 unsigned int id, clock_frequency, irq;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400682 int err;
683
Alexey Brodkine4f23792013-06-24 09:54:27 +0400684
685 /* Get PHY from device tree */
Romain Perierf15f44e2014-08-26 13:14:49 +0000686 phy_node = of_parse_phandle(dev->of_node, "phy", 0);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400687 if (!phy_node) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000688 dev_err(dev, "failed to retrieve phy description from device tree\n");
Alexey Brodkine4f23792013-06-24 09:54:27 +0400689 return -ENODEV;
690 }
691
692 /* Get EMAC registers base address from device tree */
Romain Perierf15f44e2014-08-26 13:14:49 +0000693 err = of_address_to_resource(dev->of_node, 0, &res_regs);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400694 if (err) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000695 dev_err(dev, "failed to retrieve registers base from device tree\n");
Alexey Brodkine4f23792013-06-24 09:54:27 +0400696 return -ENODEV;
697 }
698
Alexey Brodkine4f23792013-06-24 09:54:27 +0400699 /* Get IRQ from device tree */
Romain Perierf15f44e2014-08-26 13:14:49 +0000700 irq = irq_of_parse_and_map(dev->of_node, 0);
Thierry Redingf7578492013-09-18 15:24:44 +0200701 if (!irq) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000702 dev_err(dev, "failed to retrieve <irq> value from device tree\n");
Alexey Brodkine4f23792013-06-24 09:54:27 +0400703 return -ENODEV;
704 }
705
Alexey Brodkine4f23792013-06-24 09:54:27 +0400706
707 ndev->netdev_ops = &arc_emac_netdev_ops;
708 ndev->ethtool_ops = &arc_emac_ethtool_ops;
709 ndev->watchdog_timeo = TX_TIMEOUT;
710 /* FIXME :: no multicast support yet */
711 ndev->flags &= ~IFF_MULTICAST;
712
713 priv = netdev_priv(ndev);
Romain Perierf15f44e2014-08-26 13:14:49 +0000714 priv->dev = dev;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400715
Romain Perierf15f44e2014-08-26 13:14:49 +0000716 priv->regs = devm_ioremap_resource(dev, &res_regs);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400717 if (IS_ERR(priv->regs)) {
Romain Perier23d2d9a2014-08-26 13:14:51 +0000718 return PTR_ERR(priv->regs);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400719 }
Romain Perierf15f44e2014-08-26 13:14:49 +0000720 dev_dbg(dev, "Registers base address is 0x%p\n", priv->regs);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400721
Romain Perier23d2d9a2014-08-26 13:14:51 +0000722 if (priv->clk) {
Heiko Stübner88154c92014-04-25 10:06:13 +0200723 err = clk_prepare_enable(priv->clk);
724 if (err) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000725 dev_err(dev, "failed to enable clock\n");
Romain Perier23d2d9a2014-08-26 13:14:51 +0000726 return err;
Heiko Stübner88154c92014-04-25 10:06:13 +0200727 }
728
729 clock_frequency = clk_get_rate(priv->clk);
Romain Perier23d2d9a2014-08-26 13:14:51 +0000730 } else {
731 /* Get CPU clock frequency from device tree */
732 if (of_property_read_u32(dev->of_node, "clock-frequency",
733 &clock_frequency)) {
734 dev_err(dev, "failed to retrieve <clock-frequency> from device tree\n");
735 return -EINVAL;
736 }
Heiko Stübner88154c92014-04-25 10:06:13 +0200737 }
738
Alexey Brodkine4f23792013-06-24 09:54:27 +0400739 id = arc_reg_get(priv, R_ID);
740
741 /* Check for EMAC revision 5 or 7, magic number */
742 if (!(id == 0x0005fd02 || id == 0x0007fd02)) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000743 dev_err(dev, "ARC EMAC not detected, id=0x%x\n", id);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400744 err = -ENODEV;
Heiko Stübner88154c92014-04-25 10:06:13 +0200745 goto out_clken;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400746 }
Romain Perierf15f44e2014-08-26 13:14:49 +0000747 dev_info(dev, "ARC EMAC detected with id: 0x%x\n", id);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400748
749 /* Set poll rate so that it polls every 1 ms */
750 arc_reg_set(priv, R_POLLRATE, clock_frequency / 1000000);
751
Thierry Redingf7578492013-09-18 15:24:44 +0200752 ndev->irq = irq;
Romain Perierf15f44e2014-08-26 13:14:49 +0000753 dev_info(dev, "IRQ is %d\n", ndev->irq);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400754
755 /* Register interrupt handler for device */
Romain Perierf15f44e2014-08-26 13:14:49 +0000756 err = devm_request_irq(dev, ndev->irq, arc_emac_intr, 0,
Alexey Brodkine4f23792013-06-24 09:54:27 +0400757 ndev->name, ndev);
758 if (err) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000759 dev_err(dev, "could not allocate IRQ\n");
Heiko Stübner88154c92014-04-25 10:06:13 +0200760 goto out_clken;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400761 }
762
763 /* Get MAC address from device tree */
Romain Perierf15f44e2014-08-26 13:14:49 +0000764 mac_addr = of_get_mac_address(dev->of_node);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400765
Luka Perkov99470812013-10-30 00:11:00 +0100766 if (mac_addr)
Alexey Brodkine4f23792013-06-24 09:54:27 +0400767 memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
Luka Perkov99470812013-10-30 00:11:00 +0100768 else
769 eth_hw_addr_random(ndev);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400770
Max Schwarz235a2512014-04-18 02:17:32 +0200771 arc_emac_set_address_internal(ndev);
Romain Perierf15f44e2014-08-26 13:14:49 +0000772 dev_info(dev, "MAC address is now %pM\n", ndev->dev_addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400773
774 /* Do 1 allocation instead of 2 separate ones for Rx and Tx BD rings */
Romain Perierf15f44e2014-08-26 13:14:49 +0000775 priv->rxbd = dmam_alloc_coherent(dev, RX_RING_SZ + TX_RING_SZ,
Alexey Brodkine4f23792013-06-24 09:54:27 +0400776 &priv->rxbd_dma, GFP_KERNEL);
777
778 if (!priv->rxbd) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000779 dev_err(dev, "failed to allocate data buffers\n");
Alexey Brodkine4f23792013-06-24 09:54:27 +0400780 err = -ENOMEM;
Heiko Stübner88154c92014-04-25 10:06:13 +0200781 goto out_clken;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400782 }
783
784 priv->txbd = priv->rxbd + RX_BD_NUM;
785
786 priv->txbd_dma = priv->rxbd_dma + RX_RING_SZ;
Romain Perierf15f44e2014-08-26 13:14:49 +0000787 dev_dbg(dev, "EMAC Device addr: Rx Ring [0x%x], Tx Ring[%x]\n",
Alexey Brodkine4f23792013-06-24 09:54:27 +0400788 (unsigned int)priv->rxbd_dma, (unsigned int)priv->txbd_dma);
789
Romain Perier93e91b32014-08-26 13:14:50 +0000790 err = arc_mdio_probe(priv);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400791 if (err) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000792 dev_err(dev, "failed to probe MII bus\n");
Heiko Stübner88154c92014-04-25 10:06:13 +0200793 goto out_clken;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400794 }
795
796 priv->phy_dev = of_phy_connect(ndev, phy_node, arc_emac_adjust_link, 0,
Romain Perier23d2d9a2014-08-26 13:14:51 +0000797 interface);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400798 if (!priv->phy_dev) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000799 dev_err(dev, "of_phy_connect() failed\n");
Alexey Brodkine4f23792013-06-24 09:54:27 +0400800 err = -ENODEV;
Heiko Stübner796bec12014-04-25 10:03:29 +0200801 goto out_mdio;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400802 }
803
Romain Perierf15f44e2014-08-26 13:14:49 +0000804 dev_info(dev, "connected to %s phy with id 0x%x\n",
Alexey Brodkine4f23792013-06-24 09:54:27 +0400805 priv->phy_dev->drv->name, priv->phy_dev->phy_id);
806
807 netif_napi_add(ndev, &priv->napi, arc_emac_poll, ARC_EMAC_NAPI_WEIGHT);
808
809 err = register_netdev(ndev);
810 if (err) {
Romain Perierf15f44e2014-08-26 13:14:49 +0000811 dev_err(dev, "failed to register network device\n");
Heiko Stübner796bec12014-04-25 10:03:29 +0200812 goto out_netif_api;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400813 }
814
815 return 0;
816
Heiko Stübner796bec12014-04-25 10:03:29 +0200817out_netif_api:
818 netif_napi_del(&priv->napi);
819 phy_disconnect(priv->phy_dev);
820 priv->phy_dev = NULL;
821out_mdio:
822 arc_mdio_remove(priv);
Heiko Stübner88154c92014-04-25 10:06:13 +0200823out_clken:
Romain Perier23d2d9a2014-08-26 13:14:51 +0000824 if (priv->clk)
Heiko Stübner88154c92014-04-25 10:06:13 +0200825 clk_disable_unprepare(priv->clk);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400826 return err;
827}
Romain Perier23d2d9a2014-08-26 13:14:51 +0000828EXPORT_SYMBOL_GPL(arc_emac_probe);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400829
Romain Perier23d2d9a2014-08-26 13:14:51 +0000830int arc_emac_remove(struct net_device *ndev)
Alexey Brodkine4f23792013-06-24 09:54:27 +0400831{
Alexey Brodkine4f23792013-06-24 09:54:27 +0400832 struct arc_emac_priv *priv = netdev_priv(ndev);
833
834 phy_disconnect(priv->phy_dev);
835 priv->phy_dev = NULL;
836 arc_mdio_remove(priv);
837 unregister_netdev(ndev);
838 netif_napi_del(&priv->napi);
Heiko Stübner88154c92014-04-25 10:06:13 +0200839
840 if (!IS_ERR(priv->clk)) {
841 clk_disable_unprepare(priv->clk);
Heiko Stübner88154c92014-04-25 10:06:13 +0200842 }
843
Alexey Brodkine4f23792013-06-24 09:54:27 +0400844
845 return 0;
846}
Romain Perier23d2d9a2014-08-26 13:14:51 +0000847EXPORT_SYMBOL_GPL(arc_emac_remove);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400848
849MODULE_AUTHOR("Alexey Brodkin <abrodkin@synopsys.com>");
850MODULE_DESCRIPTION("ARC EMAC driver");
851MODULE_LICENSE("GPL");