blob: be090e68e1d1ef836f3108709eb3efae5a73e466 [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/**
33 * arc_emac_adjust_link - Adjust the PHY link duplex.
34 * @ndev: Pointer to the net_device structure.
35 *
36 * This function is called to change the duplex setting after auto negotiation
37 * is done by the PHY.
38 */
39static void arc_emac_adjust_link(struct net_device *ndev)
40{
41 struct arc_emac_priv *priv = netdev_priv(ndev);
42 struct phy_device *phy_dev = priv->phy_dev;
43 unsigned int reg, state_changed = 0;
44
45 if (priv->link != phy_dev->link) {
46 priv->link = phy_dev->link;
47 state_changed = 1;
48 }
49
50 if (priv->speed != phy_dev->speed) {
51 priv->speed = phy_dev->speed;
52 state_changed = 1;
53 }
54
55 if (priv->duplex != phy_dev->duplex) {
56 reg = arc_reg_get(priv, R_CTRL);
57
58 if (DUPLEX_FULL == phy_dev->duplex)
59 reg |= ENFL_MASK;
60 else
61 reg &= ~ENFL_MASK;
62
63 arc_reg_set(priv, R_CTRL, reg);
64 priv->duplex = phy_dev->duplex;
65 state_changed = 1;
66 }
67
68 if (state_changed)
69 phy_print_status(phy_dev);
70}
71
72/**
73 * arc_emac_get_settings - Get PHY settings.
74 * @ndev: Pointer to net_device structure.
75 * @cmd: Pointer to ethtool_cmd structure.
76 *
77 * This implements ethtool command for getting PHY settings. If PHY could
78 * not be found, the function returns -ENODEV. This function calls the
79 * relevant PHY ethtool API to get the PHY settings.
80 * Issue "ethtool ethX" under linux prompt to execute this function.
81 */
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{
123 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
124 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
125}
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);
143 struct net_device_stats *stats = &priv->stats;
144 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;
205 struct net_device_stats *stats = &priv->stats;
206 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);
321 struct net_device_stats *stats = &priv->stats;
322 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
366/**
367 * arc_emac_open - Open the network device.
368 * @ndev: Pointer to the network device.
369 *
370 * returns: 0, on success or non-zero error value on failure.
371 *
372 * This function sets the MAC address, requests and enables an IRQ
373 * for the EMAC device and starts the Tx queue.
374 * It also connects to the phy device.
375 */
376static int arc_emac_open(struct net_device *ndev)
377{
378 struct arc_emac_priv *priv = netdev_priv(ndev);
379 struct phy_device *phy_dev = priv->phy_dev;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400380 int i;
381
382 phy_dev->autoneg = AUTONEG_ENABLE;
383 phy_dev->speed = 0;
384 phy_dev->duplex = 0;
Florian Fainellib0ac9562013-12-05 14:52:15 -0800385 phy_dev->advertising &= phy_dev->supported;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400386
Alexey Brodkina4a11392013-06-26 11:49:26 +0400387 priv->last_rx_bd = 0;
388
Alexey Brodkine4f23792013-06-24 09:54:27 +0400389 /* Allocate and set buffers for Rx BD's */
Alexey Brodkine4f23792013-06-24 09:54:27 +0400390 for (i = 0; i < RX_BD_NUM; i++) {
Alexey Brodkina4a11392013-06-26 11:49:26 +0400391 dma_addr_t addr;
392 unsigned int *last_rx_bd = &priv->last_rx_bd;
393 struct arc_emac_bd *rxbd = &priv->rxbd[*last_rx_bd];
394 struct buffer_state *rx_buff = &priv->rx_buff[*last_rx_bd];
395
396 rx_buff->skb = netdev_alloc_skb_ip_align(ndev,
397 EMAC_BUFFER_SIZE);
398 if (unlikely(!rx_buff->skb))
Alexey Brodkine4f23792013-06-24 09:54:27 +0400399 return -ENOMEM;
400
Alexey Brodkina4a11392013-06-26 11:49:26 +0400401 addr = dma_map_single(&ndev->dev, (void *)rx_buff->skb->data,
402 EMAC_BUFFER_SIZE, DMA_FROM_DEVICE);
403 if (dma_mapping_error(&ndev->dev, addr)) {
404 netdev_err(ndev, "cannot dma map\n");
405 dev_kfree_skb(rx_buff->skb);
406 return -ENOMEM;
407 }
408 dma_unmap_addr_set(rx_buff, addr, addr);
409 dma_unmap_len_set(rx_buff, len, EMAC_BUFFER_SIZE);
410
411 rxbd->data = cpu_to_le32(addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400412
413 /* Make sure pointer to data buffer is set */
414 wmb();
415
Alexey Brodkina4a11392013-06-26 11:49:26 +0400416 /* Return ownership to EMAC */
417 rxbd->info = cpu_to_le32(FOR_EMAC | EMAC_BUFFER_SIZE);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400418
Alexey Brodkina4a11392013-06-26 11:49:26 +0400419 *last_rx_bd = (*last_rx_bd + 1) % RX_BD_NUM;
420 }
Alexey Brodkine4f23792013-06-24 09:54:27 +0400421
422 /* Clean Tx BD's */
423 memset(priv->txbd, 0, TX_RING_SZ);
424
425 /* Initialize logical address filter */
426 arc_reg_set(priv, R_LAFL, 0);
427 arc_reg_set(priv, R_LAFH, 0);
428
429 /* Set BD ring pointers for device side */
430 arc_reg_set(priv, R_RX_RING, (unsigned int)priv->rxbd_dma);
431 arc_reg_set(priv, R_TX_RING, (unsigned int)priv->txbd_dma);
432
433 /* Enable interrupts */
434 arc_reg_set(priv, R_ENABLE, RXINT_MASK | ERR_MASK);
435
436 /* Set CONTROL */
437 arc_reg_set(priv, R_CTRL,
438 (RX_BD_NUM << 24) | /* RX BD table length */
439 (TX_BD_NUM << 16) | /* TX BD table length */
440 TXRN_MASK | RXRN_MASK);
441
442 napi_enable(&priv->napi);
443
444 /* Enable EMAC */
445 arc_reg_or(priv, R_CTRL, EN_MASK);
446
447 phy_start_aneg(priv->phy_dev);
448
449 netif_start_queue(ndev);
450
451 return 0;
452}
453
454/**
Beniamino Galvani775dd682014-05-11 18:11:47 +0200455 * arc_emac_set_rx_mode - Change the receive filtering mode.
456 * @ndev: Pointer to the network device.
457 *
458 * This function enables/disables promiscuous or all-multicast mode
459 * and updates the multicast filtering list of the network device.
460 */
461static void arc_emac_set_rx_mode(struct net_device *ndev)
462{
463 struct arc_emac_priv *priv = netdev_priv(ndev);
464
465 if (ndev->flags & IFF_PROMISC) {
466 arc_reg_or(priv, R_CTRL, PROM_MASK);
467 } else {
468 arc_reg_clr(priv, R_CTRL, PROM_MASK);
469
470 if (ndev->flags & IFF_ALLMULTI) {
471 arc_reg_set(priv, R_LAFL, ~0);
472 arc_reg_set(priv, R_LAFH, ~0);
473 } else {
474 struct netdev_hw_addr *ha;
475 unsigned int filter[2] = { 0, 0 };
476 int bit;
477
478 netdev_for_each_mc_addr(ha, ndev) {
479 bit = ether_crc_le(ETH_ALEN, ha->addr) >> 26;
480 filter[bit >> 5] |= 1 << (bit & 31);
481 }
482
483 arc_reg_set(priv, R_LAFL, filter[0]);
484 arc_reg_set(priv, R_LAFH, filter[1]);
485 }
486 }
487}
488
489/**
Alexey Brodkine4f23792013-06-24 09:54:27 +0400490 * arc_emac_stop - Close the network device.
491 * @ndev: Pointer to the network device.
492 *
493 * This function stops the Tx queue, disables interrupts and frees the IRQ for
494 * the EMAC device.
495 * It also disconnects the PHY device associated with the EMAC device.
496 */
497static int arc_emac_stop(struct net_device *ndev)
498{
499 struct arc_emac_priv *priv = netdev_priv(ndev);
500
501 napi_disable(&priv->napi);
502 netif_stop_queue(ndev);
503
504 /* Disable interrupts */
505 arc_reg_clr(priv, R_ENABLE, RXINT_MASK | ERR_MASK);
506
507 /* Disable EMAC */
508 arc_reg_clr(priv, R_CTRL, EN_MASK);
509
510 return 0;
511}
512
513/**
514 * arc_emac_stats - Get system network statistics.
515 * @ndev: Pointer to net_device structure.
516 *
517 * Returns the address of the device statistics structure.
518 * Statistics are updated in interrupt handler.
519 */
520static struct net_device_stats *arc_emac_stats(struct net_device *ndev)
521{
522 struct arc_emac_priv *priv = netdev_priv(ndev);
523 struct net_device_stats *stats = &priv->stats;
524 unsigned long miss, rxerr;
525 u8 rxcrc, rxfram, rxoflow;
526
527 rxerr = arc_reg_get(priv, R_RXERR);
528 miss = arc_reg_get(priv, R_MISS);
529
530 rxcrc = rxerr;
531 rxfram = rxerr >> 8;
532 rxoflow = rxerr >> 16;
533
534 stats->rx_errors += miss;
535 stats->rx_errors += rxcrc + rxfram + rxoflow;
536
537 stats->rx_over_errors += rxoflow;
538 stats->rx_frame_errors += rxfram;
539 stats->rx_crc_errors += rxcrc;
540 stats->rx_missed_errors += miss;
541
542 return stats;
543}
544
545/**
546 * arc_emac_tx - Starts the data transmission.
547 * @skb: sk_buff pointer that contains data to be Transmitted.
548 * @ndev: Pointer to net_device structure.
549 *
550 * returns: NETDEV_TX_OK, on success
551 * NETDEV_TX_BUSY, if any of the descriptors are not free.
552 *
553 * This function is invoked from upper layers to initiate transmission.
554 */
555static int arc_emac_tx(struct sk_buff *skb, struct net_device *ndev)
556{
557 struct arc_emac_priv *priv = netdev_priv(ndev);
558 unsigned int len, *txbd_curr = &priv->txbd_curr;
559 struct net_device_stats *stats = &priv->stats;
560 __le32 *info = &priv->txbd[*txbd_curr].info;
561 dma_addr_t addr;
562
563 if (skb_padto(skb, ETH_ZLEN))
564 return NETDEV_TX_OK;
565
566 len = max_t(unsigned int, ETH_ZLEN, skb->len);
567
568 /* EMAC still holds this buffer in its possession.
569 * CPU must not modify this buffer descriptor
570 */
571 if (unlikely((le32_to_cpu(*info) & OWN_MASK) == FOR_EMAC)) {
572 netif_stop_queue(ndev);
573 return NETDEV_TX_BUSY;
574 }
575
576 addr = dma_map_single(&ndev->dev, (void *)skb->data, len,
577 DMA_TO_DEVICE);
578
579 if (unlikely(dma_mapping_error(&ndev->dev, addr))) {
580 stats->tx_dropped++;
581 stats->tx_errors++;
582 dev_kfree_skb(skb);
583 return NETDEV_TX_OK;
584 }
Alexey Brodkina4a11392013-06-26 11:49:26 +0400585 dma_unmap_addr_set(&priv->tx_buff[*txbd_curr], addr, addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400586 dma_unmap_len_set(&priv->tx_buff[*txbd_curr], len, len);
587
588 priv->tx_buff[*txbd_curr].skb = skb;
Alexey Brodkina4a11392013-06-26 11:49:26 +0400589 priv->txbd[*txbd_curr].data = cpu_to_le32(addr);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400590
591 /* Make sure pointer to data buffer is set */
592 wmb();
593
Eric Dumazet37ec2742013-12-19 18:10:40 -0800594 skb_tx_timestamp(skb);
595
Alexey Brodkine4f23792013-06-24 09:54:27 +0400596 *info = cpu_to_le32(FOR_EMAC | FIRST_OR_LAST_MASK | len);
597
598 /* Increment index to point to the next BD */
599 *txbd_curr = (*txbd_curr + 1) % TX_BD_NUM;
600
601 /* Get "info" of the next BD */
602 info = &priv->txbd[*txbd_curr].info;
603
604 /* Check if if Tx BD ring is full - next BD is still owned by EMAC */
605 if (unlikely((le32_to_cpu(*info) & OWN_MASK) == FOR_EMAC))
606 netif_stop_queue(ndev);
607
608 arc_reg_set(priv, R_STATUS, TXPL_MASK);
609
Alexey Brodkine4f23792013-06-24 09:54:27 +0400610 return NETDEV_TX_OK;
611}
612
Max Schwarz235a2512014-04-18 02:17:32 +0200613static void arc_emac_set_address_internal(struct net_device *ndev)
614{
615 struct arc_emac_priv *priv = netdev_priv(ndev);
616 unsigned int addr_low, addr_hi;
617
618 addr_low = le32_to_cpu(*(__le32 *) &ndev->dev_addr[0]);
619 addr_hi = le16_to_cpu(*(__le16 *) &ndev->dev_addr[4]);
620
621 arc_reg_set(priv, R_ADDRL, addr_low);
622 arc_reg_set(priv, R_ADDRH, addr_hi);
623}
624
Alexey Brodkine4f23792013-06-24 09:54:27 +0400625/**
626 * arc_emac_set_address - Set the MAC address for this device.
627 * @ndev: Pointer to net_device structure.
628 * @p: 6 byte Address to be written as MAC address.
629 *
630 * This function copies the HW address from the sockaddr structure to the
631 * net_device structure and updates the address in HW.
632 *
633 * returns: -EBUSY if the net device is busy or 0 if the address is set
634 * successfully.
635 */
636static int arc_emac_set_address(struct net_device *ndev, void *p)
637{
Alexey Brodkine4f23792013-06-24 09:54:27 +0400638 struct sockaddr *addr = p;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400639
640 if (netif_running(ndev))
641 return -EBUSY;
642
643 if (!is_valid_ether_addr(addr->sa_data))
644 return -EADDRNOTAVAIL;
645
646 memcpy(ndev->dev_addr, addr->sa_data, ndev->addr_len);
647
Max Schwarz235a2512014-04-18 02:17:32 +0200648 arc_emac_set_address_internal(ndev);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400649
650 return 0;
651}
652
653static const struct net_device_ops arc_emac_netdev_ops = {
654 .ndo_open = arc_emac_open,
655 .ndo_stop = arc_emac_stop,
656 .ndo_start_xmit = arc_emac_tx,
657 .ndo_set_mac_address = arc_emac_set_address,
658 .ndo_get_stats = arc_emac_stats,
Beniamino Galvani775dd682014-05-11 18:11:47 +0200659 .ndo_set_rx_mode = arc_emac_set_rx_mode,
Alexey Brodkine4f23792013-06-24 09:54:27 +0400660};
661
662static int arc_emac_probe(struct platform_device *pdev)
663{
Thierry Redingf7578492013-09-18 15:24:44 +0200664 struct resource res_regs;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400665 struct device_node *phy_node;
666 struct arc_emac_priv *priv;
667 struct net_device *ndev;
668 const char *mac_addr;
Thierry Redingf7578492013-09-18 15:24:44 +0200669 unsigned int id, clock_frequency, irq;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400670 int err;
671
672 if (!pdev->dev.of_node)
673 return -ENODEV;
674
675 /* Get PHY from device tree */
676 phy_node = of_parse_phandle(pdev->dev.of_node, "phy", 0);
677 if (!phy_node) {
678 dev_err(&pdev->dev, "failed to retrieve phy description from device tree\n");
679 return -ENODEV;
680 }
681
682 /* Get EMAC registers base address from device tree */
683 err = of_address_to_resource(pdev->dev.of_node, 0, &res_regs);
684 if (err) {
685 dev_err(&pdev->dev, "failed to retrieve registers base from device tree\n");
686 return -ENODEV;
687 }
688
Alexey Brodkine4f23792013-06-24 09:54:27 +0400689 /* Get IRQ from device tree */
Thierry Redingf7578492013-09-18 15:24:44 +0200690 irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
691 if (!irq) {
Alexey Brodkine4f23792013-06-24 09:54:27 +0400692 dev_err(&pdev->dev, "failed to retrieve <irq> value from device tree\n");
693 return -ENODEV;
694 }
695
696 ndev = alloc_etherdev(sizeof(struct arc_emac_priv));
697 if (!ndev)
698 return -ENOMEM;
699
Wei Yongjun45f1b022013-11-11 14:15:12 +0800700 platform_set_drvdata(pdev, ndev);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400701 SET_NETDEV_DEV(ndev, &pdev->dev);
702
703 ndev->netdev_ops = &arc_emac_netdev_ops;
704 ndev->ethtool_ops = &arc_emac_ethtool_ops;
705 ndev->watchdog_timeo = TX_TIMEOUT;
706 /* FIXME :: no multicast support yet */
707 ndev->flags &= ~IFF_MULTICAST;
708
709 priv = netdev_priv(ndev);
710 priv->dev = &pdev->dev;
711 priv->ndev = ndev;
712
713 priv->regs = devm_ioremap_resource(&pdev->dev, &res_regs);
714 if (IS_ERR(priv->regs)) {
715 err = PTR_ERR(priv->regs);
Heiko Stübner796bec12014-04-25 10:03:29 +0200716 goto out_netdev;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400717 }
718 dev_dbg(&pdev->dev, "Registers base address is 0x%p\n", priv->regs);
719
Heiko Stübner88154c92014-04-25 10:06:13 +0200720 priv->clk = of_clk_get(pdev->dev.of_node, 0);
721 if (IS_ERR(priv->clk)) {
722 /* Get CPU clock frequency from device tree */
723 if (of_property_read_u32(pdev->dev.of_node, "clock-frequency",
724 &clock_frequency)) {
725 dev_err(&pdev->dev, "failed to retrieve <clock-frequency> from device tree\n");
726 err = -EINVAL;
727 goto out_netdev;
728 }
729 } else {
730 err = clk_prepare_enable(priv->clk);
731 if (err) {
732 dev_err(&pdev->dev, "failed to enable clock\n");
733 goto out_clkget;
734 }
735
736 clock_frequency = clk_get_rate(priv->clk);
737 }
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)) {
743 dev_err(&pdev->dev, "ARC EMAC not detected, id=0x%x\n", id);
744 err = -ENODEV;
Heiko Stübner88154c92014-04-25 10:06:13 +0200745 goto out_clken;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400746 }
747 dev_info(&pdev->dev, "ARC EMAC detected with id: 0x%x\n", id);
748
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;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400753 dev_info(&pdev->dev, "IRQ is %d\n", ndev->irq);
754
755 /* Register interrupt handler for device */
756 err = devm_request_irq(&pdev->dev, ndev->irq, arc_emac_intr, 0,
757 ndev->name, ndev);
758 if (err) {
759 dev_err(&pdev->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 */
764 mac_addr = of_get_mac_address(pdev->dev.of_node);
765
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);
Alexey Brodkine4f23792013-06-24 09:54:27 +0400772 dev_info(&pdev->dev, "MAC address is now %pM\n", ndev->dev_addr);
773
774 /* Do 1 allocation instead of 2 separate ones for Rx and Tx BD rings */
775 priv->rxbd = dmam_alloc_coherent(&pdev->dev, RX_RING_SZ + TX_RING_SZ,
776 &priv->rxbd_dma, GFP_KERNEL);
777
778 if (!priv->rxbd) {
779 dev_err(&pdev->dev, "failed to allocate data buffers\n");
780 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;
787 dev_dbg(&pdev->dev, "EMAC Device addr: Rx Ring [0x%x], Tx Ring[%x]\n",
788 (unsigned int)priv->rxbd_dma, (unsigned int)priv->txbd_dma);
789
790 err = arc_mdio_probe(pdev, priv);
791 if (err) {
792 dev_err(&pdev->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,
797 PHY_INTERFACE_MODE_MII);
798 if (!priv->phy_dev) {
799 dev_err(&pdev->dev, "of_phy_connect() failed\n");
800 err = -ENODEV;
Heiko Stübner796bec12014-04-25 10:03:29 +0200801 goto out_mdio;
Alexey Brodkine4f23792013-06-24 09:54:27 +0400802 }
803
804 dev_info(&pdev->dev, "connected to %s phy with id 0x%x\n",
805 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) {
Alexey Brodkine4f23792013-06-24 09:54:27 +0400811 dev_err(&pdev->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:
824 if (!IS_ERR(priv->clk))
825 clk_disable_unprepare(priv->clk);
826out_clkget:
827 if (!IS_ERR(priv->clk))
828 clk_put(priv->clk);
Heiko Stübner796bec12014-04-25 10:03:29 +0200829out_netdev:
Alexey Brodkine4f23792013-06-24 09:54:27 +0400830 free_netdev(ndev);
831 return err;
832}
833
834static int arc_emac_remove(struct platform_device *pdev)
835{
836 struct net_device *ndev = platform_get_drvdata(pdev);
837 struct arc_emac_priv *priv = netdev_priv(ndev);
838
839 phy_disconnect(priv->phy_dev);
840 priv->phy_dev = NULL;
841 arc_mdio_remove(priv);
842 unregister_netdev(ndev);
843 netif_napi_del(&priv->napi);
Heiko Stübner88154c92014-04-25 10:06:13 +0200844
845 if (!IS_ERR(priv->clk)) {
846 clk_disable_unprepare(priv->clk);
847 clk_put(priv->clk);
848 }
849
Alexey Brodkine4f23792013-06-24 09:54:27 +0400850 free_netdev(ndev);
851
852 return 0;
853}
854
855static const struct of_device_id arc_emac_dt_ids[] = {
856 { .compatible = "snps,arc-emac" },
857 { /* Sentinel */ }
858};
859MODULE_DEVICE_TABLE(of, arc_emac_dt_ids);
860
861static struct platform_driver arc_emac_driver = {
862 .probe = arc_emac_probe,
863 .remove = arc_emac_remove,
864 .driver = {
865 .name = DRV_NAME,
866 .owner = THIS_MODULE,
867 .of_match_table = arc_emac_dt_ids,
868 },
869};
870
871module_platform_driver(arc_emac_driver);
872
873MODULE_AUTHOR("Alexey Brodkin <abrodkin@synopsys.com>");
874MODULE_DESCRIPTION("ARC EMAC driver");
875MODULE_LICENSE("GPL");