improve ethernet driver debugging
[openwrt/openwrt.git] / target / linux / mcs814x / files-3.3 / drivers / net / ethernet / mcs8140 / nuport_mac.c
1 /*
2 * Moschip MCS8140 Ethernet MAC driver
3 *
4 * Copyright (C) 2003, Moschip Semiconductors
5 * Copyright (C) 2012, Florian Fainelli <florian@openwrt.org>
6 *
7 * Licensed under GPLv2
8 */
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/delay.h>
16 #include <linux/ethtool.h>
17 #include <linux/mii.h>
18 #include <linux/io.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/of.h>
22 #include <linux/irq.h>
23 #include <linux/err.h>
24 #include <linux/phy.h>
25 #include <linux/clk.h>
26 #include <linux/dma-mapping.h>
27
28 #include <asm/unaligned.h>
29 #include <asm/sizes.h>
30 #include <mach/hardware.h>
31
32 /* Hardware registers */
33 #define MAC_BASE_ADDR ((priv->mac_base))
34
35 #define CTRL_REG (MAC_BASE_ADDR)
36 #define MII_BUSY 0x00000001
37 #define MII_WRITE 0x00000002
38 #define MAC_ADDR_HIGH_REG (MAC_BASE_ADDR + 0x04)
39 #define MAC_ADDR_LOW_REG (MAC_BASE_ADDR + 0x08)
40 #define MII_ADDR_REG (MAC_BASE_ADDR + 0x14)
41 #define MII_DATA_REG (MAC_BASE_ADDR + 0x18)
42 /* Link interrupt registers */
43 #define LINK_INT_CSR (MAC_BASE_ADDR + 0xD0)
44 #define LINK_INT_POLL_TIME (MAC_BASE_ADDR + 0xD4)
45
46 #define DMA_CHAN_WIDTH 32
47 #define DMA_RX_CHAN 0
48 #define DMA_TX_CHAN 2
49
50 /* Receive DMA registers */
51 #define RX_DMA_BASE ((priv->dma_base) + \
52 (DMA_CHAN_WIDTH * DMA_RX_CHAN))
53 #define RX_BUFFER_ADDR (RX_DMA_BASE + 0x00)
54 #define RX_MAX_BYTES (RX_DMA_BASE + 0x04)
55 #define RX_ACT_BYTES (RX_DMA_BASE + 0x08)
56 #define RX_START_DMA (RX_DMA_BASE + 0x0C)
57 #define RX_DMA_ENH (RX_DMA_BASE + 0x14)
58
59 /* Transmit DMA registers */
60 #define TX_DMA_BASE ((priv->dma_base) + \
61 (DMA_CHAN_WIDTH * DMA_TX_CHAN))
62 #define TX_BUFFER_ADDR (TX_DMA_BASE + 0x00)
63 #define TX_PKT_BYTES (TX_DMA_BASE + 0x04)
64 #define TX_BYTES_SENT (TX_DMA_BASE + 0x08)
65 #define TX_START_DMA (TX_DMA_BASE + 0x0C)
66 #define TX_DMA_STATUS (TX_DMA_BASE + 0x10)
67 #define TX_DMA_ENH (TX_DMA_BASE + 0x14)
68
69 #define RX_ALLOC_SIZE SZ_2K
70 #define MAX_ETH_FRAME_SIZE 1536
71 #define RX_SKB_TAILROOM 128
72 #define RX_SKB_HEADROOM (RX_ALLOC_SIZE - \
73 (MAX_ETH_FRAME_SIZE + RX_SKB_TAILROOM) + 0)
74
75 /* WDT Late COL Lenght COL Type */
76 #define ERROR_FILTER_MASK ((1<<14) | (1<<15) | (1<<16) | (1<<17) | (0<<18) | \
77 /* MII Dribbling CRC Len/type Control */\
78 (1<<19) | (1<<20) | (1<<21) | (0<<24) | (1<<25) | \
79 /* Unsup Missed */\
80 (1<<26) | (0<<31))
81 #define TX_RING_SIZE 30
82 #define RX_RING_SIZE 30
83
84 static inline u32 nuport_mac_readl(void __iomem *reg)
85 {
86 return __raw_readl(reg);
87 }
88
89 static inline u8 nuport_mac_readb(void __iomem *reg)
90 {
91 return __raw_readb(reg);
92 }
93
94 static inline void nuport_mac_writel(u32 value, void __iomem *reg)
95 {
96 __raw_writel(value, reg);
97 }
98
99 static inline void nuport_mac_writeb(u8 value, void __iomem *reg)
100 {
101 __raw_writel(value, reg);
102 }
103
104 /* MAC private data */
105 struct nuport_mac_priv {
106 spinlock_t lock;
107
108 void __iomem *mac_base;
109 void __iomem *dma_base;
110
111 int rx_irq;
112 int tx_irq;
113 int link_irq;
114 struct clk *emac_clk;
115 struct clk *ephy_clk;
116
117 /* Transmit buffers */
118 struct sk_buff *tx_skb[TX_RING_SIZE];
119 unsigned int valid_txskb[TX_RING_SIZE];
120 unsigned int cur_tx;
121 unsigned int dma_tx;
122 unsigned int tx_full;
123
124 /* Receive buffers */
125 struct sk_buff *rx_skb[RX_RING_SIZE];
126 unsigned int irq_rxskb[RX_RING_SIZE];
127 int pkt_len[RX_RING_SIZE];
128 unsigned int cur_rx;
129 unsigned int dma_rx;
130 unsigned int rx_full;
131
132 unsigned int first_pkt;
133
134 /* Private data */
135 struct napi_struct napi;
136 struct net_device *dev;
137 struct platform_device *pdev;
138 struct mii_bus *mii_bus;
139 struct phy_device *phydev;
140 int old_link;
141 int old_duplex;
142 u32 msg_level;
143 };
144
145 void dcache_invalidate_only(unsigned long start, unsigned long end)
146 {
147 asm("\n"
148 " bic r0, r0, #31\n"
149 "1: mcr p15, 0, r0, c7, c6, 1\n"
150 " add r0, r0, #32\n"
151 " cmp r0, r1\n" " blo 1b\n");
152 }
153
154 void dcache_clean_range(unsigned long start, unsigned long end)
155 {
156 asm("\n"
157 " bic r0, r0, #31\n"
158 "1: mcr p15, 0, r0, c7, c10, 1 @ clean D entry\n"
159 " add r0, r0, #32\n"
160 " cmp r0, r1\n"
161 " blo 1b\n" \
162 " mcr p15, 0, r0, c7, c10, 4 @ drain WB\n");
163 }
164
165 static inline int nuport_mac_mii_busy_wait(struct nuport_mac_priv *priv)
166 {
167 unsigned long curr;
168 unsigned long finish = jiffies + 3 * HZ;
169
170 do {
171 curr = jiffies;
172 if (!(nuport_mac_readl(MII_ADDR_REG) & MII_BUSY))
173 return 0;
174 cpu_relax();
175 } while (!time_after_eq(curr, finish));
176
177 return -EBUSY;
178 }
179
180 /* Read from PHY registers */
181 static int nuport_mac_mii_read(struct mii_bus *bus,
182 int mii_id, int regnum)
183 {
184 struct net_device *dev = bus->priv;
185 struct nuport_mac_priv *priv = netdev_priv(dev);
186 int ret;
187 u32 val = 0;
188
189 ret = nuport_mac_mii_busy_wait(priv);
190 if (ret)
191 return ret;
192
193 val |= (mii_id << 11) | (regnum << 6) | MII_BUSY;
194 nuport_mac_writel(val, MII_ADDR_REG);
195 ret = nuport_mac_mii_busy_wait(priv);
196 if (ret)
197 return ret;
198
199 return nuport_mac_readl(MII_DATA_REG);
200 }
201
202 static int nuport_mac_mii_write(struct mii_bus *bus, int mii_id,
203 int regnum, u16 value)
204 {
205 struct net_device *dev = bus->priv;
206 struct nuport_mac_priv *priv = netdev_priv(dev);
207 int ret;
208 u32 val = 0;
209
210 ret = nuport_mac_mii_busy_wait(priv);
211 if (ret)
212 return ret;
213
214 val |= (mii_id << 11) | (regnum << 6) | MII_BUSY | MII_WRITE;
215 nuport_mac_writel(value, MII_DATA_REG);
216 nuport_mac_writel(val, MII_ADDR_REG);
217
218 return nuport_mac_mii_busy_wait(priv);
219 }
220
221 static int nuport_mac_mii_reset(struct mii_bus *bus)
222 {
223 return 0;
224 }
225
226 static int nuport_mac_start_tx_dma(struct nuport_mac_priv *priv,
227 struct sk_buff *skb)
228 {
229 dma_addr_t p;
230 u32 reg;
231 unsigned int timeout = 2048;
232
233 while (timeout--) {
234 reg = nuport_mac_readl(TX_START_DMA);
235 if (!(reg & 0x01)) {
236 netdev_dbg(priv->dev, "dma ready\n");
237 break;
238 }
239 cpu_relax();
240 }
241
242 if (!timeout)
243 return -EBUSY;
244
245 p = dma_map_single(&priv->pdev->dev, skb->data,
246 skb->len, DMA_TO_DEVICE);
247
248 /* enable enhanced mode */
249 nuport_mac_writel(0x01, TX_DMA_ENH);
250 nuport_mac_writel(p, TX_BUFFER_ADDR);
251 nuport_mac_writel((skb->len) - 1, TX_PKT_BYTES);
252 wmb();
253 nuport_mac_writel(0x0D, TX_START_DMA);
254
255 return 0;
256 }
257
258 static void nuport_mac_reset_tx_dma(struct nuport_mac_priv *priv)
259 {
260 u32 reg;
261
262 reg = nuport_mac_readl(TX_START_DMA);
263 reg |= (1 << 24);
264 nuport_mac_writel(reg, TX_START_DMA);
265 }
266
267 static int nuport_mac_start_rx_dma(struct nuport_mac_priv *priv,
268 struct sk_buff *skb)
269 {
270 dma_addr_t p;
271 u32 reg;
272 unsigned int timeout = 2048;
273
274 while (timeout--) {
275 reg = nuport_mac_readl(RX_START_DMA);
276 if (!(reg & 0x01)) {
277 netdev_dbg(priv->dev, "dma ready\n");
278 break;
279 }
280 cpu_relax();
281 }
282
283 if (!timeout)
284 return -EBUSY;
285
286 p = dma_map_single(&priv->pdev->dev, skb->data,
287 RX_ALLOC_SIZE, DMA_FROM_DEVICE);
288
289 nuport_mac_writel(p, RX_BUFFER_ADDR);
290 wmb();
291 nuport_mac_writel(0x01, RX_START_DMA);
292
293 return 0;
294 }
295
296 static void nuport_mac_reset_rx_dma(struct nuport_mac_priv *priv)
297 {
298 u32 reg;
299
300 reg = nuport_mac_readl(RX_START_DMA);
301 reg |= (1 << 1);
302 nuport_mac_writel(reg, RX_START_DMA);
303 }
304
305 /* I suppose this might do something, but I am not sure actually */
306 static void nuport_mac_disable_rx_dma(struct nuport_mac_priv *priv)
307 {
308 u32 reg;
309
310 reg = nuport_mac_readl(RX_DMA_ENH);
311 reg &= ~(1 << 1);
312 nuport_mac_writel(reg, RX_DMA_ENH);
313 }
314
315 static void nuport_mac_enable_rx_dma(struct nuport_mac_priv *priv)
316 {
317 u32 reg;
318
319 reg = nuport_mac_readl(RX_DMA_ENH);
320 reg |= (1 << 1);
321 nuport_mac_writel(reg, RX_DMA_ENH);
322 }
323
324 /* Add packets to the transmit queue */
325 static int nuport_mac_start_xmit(struct sk_buff *skb, struct net_device *dev)
326 {
327 unsigned long flags;
328 struct nuport_mac_priv *priv = netdev_priv(dev);
329 int ret;
330
331 if (netif_queue_stopped(dev)) {
332 netdev_warn(dev, "netif queue was stopped, restarting\n");
333 netif_start_queue(dev);
334 }
335
336 spin_lock_irqsave(&priv->lock, flags);
337 if (priv->first_pkt) {
338 ret = nuport_mac_start_tx_dma(priv, skb);
339 if (ret) {
340 netif_stop_queue(dev);
341 spin_unlock_irqrestore(&priv->lock, flags);
342 netdev_err(dev, "transmit path busy\n");
343 return NETDEV_TX_BUSY;
344 }
345 priv->first_pkt = 0;
346 }
347
348 priv->tx_skb[priv->cur_tx] = skb;
349 dev->stats.tx_bytes += skb->len;
350 dev->stats.tx_packets++;
351 priv->valid_txskb[priv->cur_tx] = 1;
352 priv->cur_tx++;
353 dev->trans_start = jiffies;
354
355 if (priv->cur_tx >= TX_RING_SIZE)
356 priv->cur_tx = 0;
357
358 spin_unlock_irqrestore(&priv->lock, flags);
359
360 if (priv->valid_txskb[priv->cur_tx]) {
361 priv->tx_full = 1;
362 netdev_err(dev, "stopping queue\n");
363 netif_stop_queue(dev);
364 }
365
366 return NETDEV_TX_OK;
367 }
368
369 static void nuport_mac_adjust_link(struct net_device *dev)
370 {
371 struct nuport_mac_priv *priv = netdev_priv(dev);
372 struct phy_device *phydev = priv->phydev;
373 unsigned int status_changed = 0;
374 u32 reg;
375
376 BUG_ON(!phydev);
377
378 if (priv->old_link != phydev->link) {
379 status_changed = 1;
380 priv->old_link = phydev->link;
381 }
382
383 if (phydev->link & (priv->old_duplex != phydev->duplex)) {
384 reg = nuport_mac_readl(CTRL_REG);
385 if (phydev->duplex == DUPLEX_FULL)
386 reg |= (1 << 20);
387 else
388 reg &= ~(1 << 20);
389 nuport_mac_writel(reg, CTRL_REG);
390
391 status_changed = 1;
392 priv->old_duplex = phydev->duplex;
393 }
394
395 if (!status_changed)
396 return;
397
398 pr_info("%s: link %s", dev->name, phydev->link ?
399 "UP" : "DOWN");
400 if (phydev->link) {
401 pr_cont(" - %d/%s", phydev->speed,
402 phydev->duplex == DUPLEX_FULL ? "full" : "half");
403 }
404 pr_cont("\n");
405 }
406
407 static irqreturn_t nuport_mac_link_interrupt(int irq, void *dev_id)
408 {
409 struct net_device *dev = dev_id;
410 struct nuport_mac_priv *priv = netdev_priv(dev);
411 u32 reg;
412 u8 phy_addr;
413
414 reg = nuport_mac_readl(LINK_INT_CSR);
415 phy_addr = (reg >> 1) & 0x0f;
416
417 if (phy_addr != priv->phydev->addr) {
418 netdev_err(dev, "spurious PHY irq (phy: %d)\n", phy_addr);
419 return IRQ_NONE;
420 }
421
422 priv->phydev->link = (reg & (1 << 16));
423 nuport_mac_adjust_link(dev);
424
425 return IRQ_HANDLED;
426 }
427
428 static irqreturn_t nuport_mac_tx_interrupt(int irq, void *dev_id)
429 {
430 struct net_device *dev = (struct net_device *)dev_id;
431 struct nuport_mac_priv *priv = netdev_priv(dev);
432 struct sk_buff *skb;
433 unsigned long flags;
434 int ret;
435 u32 reg;
436
437 spin_lock_irqsave(&priv->lock, flags);
438 /* clear status word available if ready */
439 reg = nuport_mac_readl(TX_START_DMA);
440 if (reg & (1 << 18)) {
441 nuport_mac_writel(reg, TX_START_DMA);
442 reg = nuport_mac_readl(TX_DMA_STATUS);
443
444 if (reg & 1)
445 dev->stats.tx_errors++;
446 } else
447 netdev_dbg(dev, "no status word: %08x\n", reg);
448
449 skb = priv->tx_skb[priv->dma_tx];
450 priv->tx_skb[priv->dma_tx] = NULL;
451 priv->valid_txskb[priv->dma_tx] = 0;
452 dev_kfree_skb_irq(skb);
453
454 priv->dma_tx++;
455 if (priv->dma_tx >= TX_RING_SIZE)
456 priv->dma_tx = 0;
457
458 if (!priv->valid_txskb[priv->dma_tx])
459 priv->first_pkt = 1;
460 else {
461 ret = nuport_mac_start_tx_dma(priv, priv->tx_skb[priv->dma_tx]);
462 if (ret)
463 netdev_err(dev, "failed to restart TX dma\n");
464 }
465
466 if (priv->tx_full) {
467 netdev_dbg(dev, "restarting transmit queue\n");
468 netif_wake_queue(dev);
469 priv->tx_full = 0;
470 }
471
472 spin_unlock_irqrestore(&priv->lock, flags);
473
474 return IRQ_HANDLED;
475 }
476
477 static unsigned int nuport_mac_has_work(struct nuport_mac_priv *priv)
478 {
479 unsigned int i;
480
481 for (i = 0; i < RX_RING_SIZE; i++)
482 if (priv->rx_skb[i])
483 return 1;
484
485 return 0;
486 }
487
488 static irqreturn_t nuport_mac_rx_interrupt(int irq, void *dev_id)
489 {
490 struct net_device *dev = (struct net_device *)dev_id;
491 struct nuport_mac_priv *priv = netdev_priv(dev);
492 unsigned long flags;
493 int ret;
494
495 spin_lock_irqsave(&priv->lock, flags);
496 if (!priv->rx_full) {
497 priv->pkt_len[priv->dma_rx] = nuport_mac_readl(RX_ACT_BYTES) - 4;
498 priv->irq_rxskb[priv->dma_rx] = 0;
499 priv->dma_rx++;
500
501 if (priv->dma_rx >= RX_RING_SIZE)
502 priv->dma_rx = 0;
503 } else
504 priv->rx_full = 0;
505
506 if (priv->irq_rxskb[priv->dma_rx] == 1) {
507 ret = nuport_mac_start_rx_dma(priv, priv->rx_skb[priv->dma_rx]);
508 if (ret)
509 netdev_err(dev, "failed to start rx dma\n");
510 } else {
511 priv->rx_full = 1;
512 netdev_dbg(dev, "RX ring full\n");
513 }
514
515 if (likely(nuport_mac_has_work(priv))) {
516 /* find a way to disable DMA rx irq */
517 nuport_mac_disable_rx_dma(priv);
518 napi_schedule(&priv->napi);
519 }
520 spin_unlock_irqrestore(&priv->lock, flags);
521
522 return IRQ_HANDLED;
523 }
524
525 /* Process received packets in tasklet */
526 static int nuport_mac_rx(struct net_device *dev, int limit)
527 {
528 struct nuport_mac_priv *priv = netdev_priv(dev);
529 struct sk_buff *skb;
530 int len, status;
531 int count = 0;
532
533 while (count < limit && !priv->irq_rxskb[priv->cur_rx]) {
534 skb = priv->rx_skb[priv->cur_rx];
535 len = priv->pkt_len[priv->cur_rx];
536 dcache_invalidate_only(((u32) skb->data),
537 ((u32) (skb->data + len + 4)));
538
539 /* Remove 2 bytes added by RX buffer shifting */
540 len = len - 2;
541 skb->data = skb->data + 2;
542
543 /* Get packet status */
544 status = get_unaligned((u32 *) (skb->data + len));
545 skb->dev = dev;
546
547 /* packet filter failed */
548 if (!(status & (1 << 30))) {
549 dev_kfree_skb_irq(skb);
550 goto exit;
551 }
552
553 /* missed frame */
554 if (status & (1 << 31)) {
555 dev->stats.rx_missed_errors++;
556 dev_kfree_skb_irq(skb);
557 goto exit;
558 }
559
560 /* Not ethernet type */
561 if ((!(status & (1 << 18))) || (status & ERROR_FILTER_MASK))
562 dev->stats.rx_errors++;
563
564 if (len > MAX_ETH_FRAME_SIZE) {
565 dev_kfree_skb_irq(skb);
566 goto exit;
567 } else
568 skb_put(skb, len);
569
570 skb->protocol = eth_type_trans(skb, dev);
571 dev->stats.rx_packets++;
572
573 if (status & (1 << 29))
574 skb->pkt_type = PACKET_OTHERHOST;
575 if (status & (1 << 27))
576 skb->pkt_type = PACKET_MULTICAST;
577 if (status & (1 << 28))
578 skb->pkt_type = PACKET_BROADCAST;
579
580 skb->ip_summed = CHECKSUM_UNNECESSARY;
581
582 /* Pass the received packet to network layer */
583 netif_receive_skb(skb);
584
585 if (status != NET_RX_DROP)
586 dev->stats.rx_bytes += len - 4; /* Without CRC */
587 else
588 dev->stats.rx_dropped++;
589
590 dev->last_rx = jiffies;
591
592 exit:
593 skb = netdev_alloc_skb(dev, RX_ALLOC_SIZE);
594 skb_reserve(skb, RX_SKB_HEADROOM);
595 priv->rx_skb[priv->cur_rx] = skb;
596 priv->irq_rxskb[priv->cur_rx] = 1;
597 priv->cur_rx++;
598
599 if (priv->cur_rx >= RX_RING_SIZE)
600 priv->cur_rx = 0;
601 count++;
602 }
603
604 return count;
605 }
606
607 static int nuport_mac_poll(struct napi_struct *napi, int budget)
608 {
609 struct nuport_mac_priv *priv =
610 container_of(napi, struct nuport_mac_priv, napi);
611 struct net_device *dev = priv->dev;
612 int work_done;
613
614 work_done = nuport_mac_rx(dev, budget);
615
616 if (work_done < budget) {
617 napi_complete(napi);
618 nuport_mac_enable_rx_dma(priv);
619 }
620
621 return work_done;
622 }
623
624 static void nuport_mac_init_tx_ring(struct nuport_mac_priv *priv)
625 {
626 int i;
627
628 priv->cur_tx = priv->dma_tx = priv->tx_full = 0;
629 for (i = 0; i < TX_RING_SIZE; i++) {
630 priv->tx_skb[i] = NULL;
631 priv->valid_txskb[i] = 0;
632 }
633 priv->first_pkt = 1;
634 }
635
636 static int nuport_mac_init_rx_ring(struct net_device *dev)
637 {
638 struct nuport_mac_priv *priv = netdev_priv(dev);
639 struct sk_buff *skb;
640 int i;
641
642 priv->cur_rx = priv->dma_rx = priv->rx_full = 0;
643
644 for (i = 0; i < RX_RING_SIZE; i++) {
645 skb = netdev_alloc_skb(dev, RX_ALLOC_SIZE);
646 if (!skb)
647 return -ENOMEM;
648 skb_reserve(skb, RX_SKB_HEADROOM);
649 priv->rx_skb[i] = skb;
650 priv->irq_rxskb[i] = 1;
651 }
652
653 return 0;
654 }
655
656 static void nuport_mac_free_rx_ring(struct nuport_mac_priv *priv)
657 {
658 int i;
659
660 for (i = 0; i < RX_RING_SIZE; i++) {
661 if (!priv->rx_skb[i])
662 continue;
663
664 dev_kfree_skb(priv->rx_skb[i]);
665 priv->rx_skb[i] = NULL;
666 }
667 }
668
669 static void nuport_mac_read_mac_address(struct net_device *dev)
670 {
671 struct nuport_mac_priv *priv = netdev_priv(dev);
672 int i;
673
674 for (i = 0; i < 4; i++)
675 dev->dev_addr[i] = nuport_mac_readb(MAC_ADDR_LOW_REG + i);
676 dev->dev_addr[4] = nuport_mac_readb(MAC_ADDR_HIGH_REG);
677 dev->dev_addr[5] = nuport_mac_readb(MAC_ADDR_HIGH_REG + 1);
678
679 if (!is_valid_ether_addr(dev->dev_addr)) {
680 dev_info(&priv->pdev->dev, "using random address\n");
681 random_ether_addr(dev->dev_addr);
682 }
683 }
684
685 static int nuport_mac_change_mac_address(struct net_device *dev, void *mac_addr)
686 {
687 struct sockaddr *addr = mac_addr;
688 struct nuport_mac_priv *priv = netdev_priv(dev);
689 unsigned long *temp = (unsigned long *)dev->dev_addr;
690 u32 high, low;
691
692 if (netif_running(dev))
693 return -EBUSY;
694
695 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
696
697 spin_lock_irq(&priv->lock);
698
699 nuport_mac_writel(*temp, MAC_ADDR_LOW_REG);
700 temp = (unsigned long *)(dev->dev_addr + 4);
701 nuport_mac_writel(*temp, MAC_ADDR_HIGH_REG);
702
703 low = nuport_mac_readl(MAC_ADDR_LOW_REG);
704 high = nuport_mac_readl(MAC_ADDR_HIGH_REG);
705
706 spin_unlock_irq(&priv->lock);
707
708 return 0;
709 }
710
711 static int nuport_mac_open(struct net_device *dev)
712 {
713 int ret;
714 struct nuport_mac_priv *priv = netdev_priv(dev);
715 unsigned long flags;
716 u32 reg;
717 u8 tmp;
718
719 /* Enable hardware filters */
720 reg = nuport_mac_readl((void __iomem *)_CONFADDR_DBGLED);
721 reg |= 0x80;
722 nuport_mac_writel(reg, (void __iomem *)_CONFADDR_DBGLED);
723
724 /* Set LEDs to Link act and RX/TX mode */
725 reg = nuport_mac_readl((void __iomem *)(_CONFADDR_SYSDBG + 0x04));
726 reg |= 0x01;
727 nuport_mac_writel(reg, (void __iomem *)(_CONFADDR_SYSDBG + 0x04));
728
729 ret = clk_enable(priv->emac_clk);
730 if (ret) {
731 netdev_err(dev, "failed to enable EMAC clock\n");
732 return ret;
733 }
734
735 /* Set MAC into full duplex mode by default */
736 nuport_mac_writel(0x1010052C, CTRL_REG);
737
738 /* set mac address in hardware in case it was not already */
739 nuport_mac_change_mac_address(dev, dev->dev_addr);
740
741 ret = request_irq(priv->link_irq, &nuport_mac_link_interrupt,
742 0, dev->name, dev);
743 if (ret) {
744 netdev_err(dev, "unable to request link interrupt\n");
745 goto out_emac_clk;
746 }
747
748 phy_start(priv->phydev);
749
750 /* Enable link interrupt monitoring */
751 spin_lock_irqsave(&priv->lock, flags);
752 nuport_mac_writel(0x1041 | (priv->phydev->addr << 1), LINK_INT_CSR);
753 nuport_mac_writel(0xFFFFF, LINK_INT_POLL_TIME);
754 spin_unlock_irqrestore(&priv->lock, flags);
755
756 ret = request_irq(priv->tx_irq, &nuport_mac_tx_interrupt,
757 0, dev->name, dev);
758 if (ret) {
759 netdev_err(dev, "unable to request rx interrupt\n");
760 goto out_link_irq;
761 }
762
763 napi_enable(&priv->napi);
764
765 ret = request_irq(priv->rx_irq, &nuport_mac_rx_interrupt,
766 0, dev->name, dev);
767 if (ret) {
768 netdev_err(dev, "unable to request tx interrupt\n");
769 goto out_tx_irq;
770 }
771
772 /* Enable buffer shifting in RX */
773 tmp = nuport_mac_readb((void __iomem *)(_CONFADDR_SYSDBG + 0x1D));
774 tmp |= 0x01;
775 nuport_mac_writeb(tmp, (void __iomem *)(_CONFADDR_SYSDBG + 0x1D));
776
777 netif_start_queue(dev);
778
779 nuport_mac_init_tx_ring(priv);
780
781 ret = nuport_mac_init_rx_ring(dev);
782 if (ret) {
783 netdev_err(dev, "rx ring init failed\n");
784 goto out_rx_skb;
785 }
786
787 nuport_mac_reset_tx_dma(priv);
788 nuport_mac_reset_rx_dma(priv);
789
790 /* Start RX DMA */
791 return nuport_mac_start_rx_dma(priv, priv->rx_skb[0]);
792
793 out_rx_skb:
794 nuport_mac_free_rx_ring(priv);
795 free_irq(priv->rx_irq, dev);
796 out_tx_irq:
797 free_irq(priv->tx_irq, dev);
798 out_link_irq:
799 free_irq(priv->link_irq, dev);
800 out_emac_clk:
801 clk_disable(priv->emac_clk);
802 return ret;
803 }
804
805 static int nuport_mac_close(struct net_device *dev)
806 {
807 struct nuport_mac_priv *priv = netdev_priv(dev);
808
809 spin_lock_irq(&priv->lock);
810 napi_disable(&priv->napi);
811 netif_stop_queue(dev);
812
813 free_irq(priv->link_irq, dev);
814 nuport_mac_writel(0x00, LINK_INT_CSR);
815 nuport_mac_writel(0x00, LINK_INT_POLL_TIME);
816 phy_stop(priv->phydev);
817
818 free_irq(priv->tx_irq, dev);
819 free_irq(priv->rx_irq, dev);
820 spin_unlock_irq(&priv->lock);
821
822 nuport_mac_free_rx_ring(priv);
823
824 clk_disable(priv->emac_clk);
825
826 return 0;
827 }
828
829 static void nuport_mac_tx_timeout(struct net_device *dev)
830 {
831 struct nuport_mac_priv *priv = netdev_priv(dev);
832 unsigned int i;
833
834 netdev_warn(dev, "transmit timeout, attempting recovery\n");
835
836 netdev_info(dev, "TX DMA regs\n");
837 for (i = 0; i < DMA_CHAN_WIDTH; i += 4)
838 netdev_info(dev, "[%02x]: 0x%08x\n", i, nuport_mac_readl(TX_DMA_BASE + i));
839 netdev_info(dev, "RX DMA regs\n");
840 for (i = 0; i < DMA_CHAN_WIDTH; i += 4)
841 netdev_info(dev, "[%02x]: 0x%08x\n", i, nuport_mac_readl(RX_DMA_BASE + i));
842
843 nuport_mac_init_tx_ring(priv);
844 nuport_mac_reset_tx_dma(priv);
845
846 netif_wake_queue(dev);
847 }
848
849 static int nuport_mac_mii_probe(struct net_device *dev)
850 {
851 struct nuport_mac_priv *priv = netdev_priv(dev);
852 struct phy_device *phydev = NULL;
853 int ret;
854
855 ret = clk_enable(priv->ephy_clk);
856 if (ret) {
857 netdev_err(dev, "unable to enable ePHY clk\n");
858 return ret;
859 }
860
861 phydev = phy_find_first(priv->mii_bus);
862 if (!phydev) {
863 netdev_err(dev, "no PHYs found\n");
864 ret = -ENODEV;
865 goto out;
866 }
867
868 phydev = phy_connect(dev, dev_name(&phydev->dev),
869 nuport_mac_adjust_link, 0,
870 PHY_INTERFACE_MODE_MII);
871 if (IS_ERR(phydev)) {
872 netdev_err(dev, "could not attach PHY\n");
873 ret = PTR_ERR(phydev);
874 goto out;
875 }
876
877 phydev->supported &= PHY_BASIC_FEATURES;
878 phydev->advertising = phydev->supported;
879 priv->phydev = phydev;
880 priv->old_link = 0;
881 priv->old_duplex = -1;
882
883 dev_info(&priv->pdev->dev, "attached PHY driver [%s] "
884 "(mii_bus:phy_addr=%d)\n",
885 phydev->drv->name, phydev->addr);
886
887 return 0;
888
889 out:
890 /* disable the Ethernet PHY clock for the moment */
891 clk_disable(priv->ephy_clk);
892
893 return ret;
894 }
895
896 static void nuport_mac_ethtool_drvinfo(struct net_device *dev,
897 struct ethtool_drvinfo *info)
898 {
899 strncpy(info->driver, "nuport-mac", sizeof(info->driver));
900 strncpy(info->version, "0.1", sizeof(info->version));
901 strncpy(info->fw_version, "N/A", sizeof(info->fw_version));
902 strncpy(info->bus_info, "internal", sizeof(info->bus_info));
903 info->n_stats = 0;
904 info->testinfo_len = 0;
905 info->regdump_len = 0;
906 info->eedump_len = 0;
907 }
908
909 static int nuport_mac_ethtool_get_settings(struct net_device *dev,
910 struct ethtool_cmd *cmd)
911 {
912 struct nuport_mac_priv *priv = netdev_priv(dev);
913
914 if (priv->phydev)
915 return phy_ethtool_gset(priv->phydev, cmd);
916
917 return -EINVAL;
918 }
919
920 static int nuport_mac_ethtool_set_settings(struct net_device *dev,
921 struct ethtool_cmd *cmd)
922 {
923 struct nuport_mac_priv *priv = netdev_priv(dev);
924
925 if (priv->phydev)
926 return phy_ethtool_sset(priv->phydev, cmd);
927
928 return -EINVAL;
929 }
930
931 static void nuport_mac_set_msglevel(struct net_device *dev, u32 msg_level)
932 {
933 struct nuport_mac_priv *priv = netdev_priv(dev);
934
935 priv->msg_level = msg_level;
936 }
937
938 static u32 nuport_mac_get_msglevel(struct net_device *dev)
939 {
940 struct nuport_mac_priv *priv = netdev_priv(dev);
941
942 return priv->msg_level;
943 }
944
945 static const struct ethtool_ops nuport_mac_ethtool_ops = {
946 .get_drvinfo = nuport_mac_ethtool_drvinfo,
947 .get_link = ethtool_op_get_link,
948 .get_settings = nuport_mac_ethtool_get_settings,
949 .set_settings = nuport_mac_ethtool_set_settings,
950 .set_msglevel = nuport_mac_set_msglevel,
951 .get_msglevel = nuport_mac_get_msglevel,
952 };
953
954 static const struct net_device_ops nuport_mac_ops = {
955 .ndo_open = nuport_mac_open,
956 .ndo_stop = nuport_mac_close,
957 .ndo_start_xmit = nuport_mac_start_xmit,
958 .ndo_change_mtu = eth_change_mtu,
959 .ndo_validate_addr = eth_validate_addr,
960 .ndo_set_mac_address = nuport_mac_change_mac_address,
961 .ndo_tx_timeout = nuport_mac_tx_timeout,
962 };
963
964 static int __init nuport_mac_probe(struct platform_device *pdev)
965 {
966 struct net_device *dev;
967 struct nuport_mac_priv *priv = NULL;
968 struct resource *regs, *dma;
969 int ret = 0;
970 int rx_irq, tx_irq, link_irq;
971 int i;
972
973 dev = alloc_etherdev(sizeof(struct nuport_mac_priv));
974 if (!dev) {
975 dev_err(&pdev->dev, "no memory for net_device\n");
976 return -ENOMEM;
977 }
978
979 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
980 dma = platform_get_resource(pdev, IORESOURCE_MEM, 1);
981 if (!regs || !dma) {
982 dev_err(&pdev->dev, "failed to get regs resources\n");
983 ret = -ENODEV;
984 goto out;
985 }
986
987 rx_irq = platform_get_irq(pdev, 0);
988 tx_irq = platform_get_irq(pdev, 1);
989 link_irq = platform_get_irq(pdev, 2);
990 if (rx_irq < 0 || tx_irq < 0 || link_irq < 0) {
991 ret = -ENODEV;
992 goto out;
993 }
994
995 platform_set_drvdata(pdev, dev);
996 SET_NETDEV_DEV(dev, &pdev->dev);
997 priv = netdev_priv(dev);
998 priv->pdev = pdev;
999 priv->dev = dev;
1000 spin_lock_init(&priv->lock);
1001
1002 priv->mac_base = devm_ioremap(&pdev->dev,
1003 regs->start, resource_size(regs));
1004 if (!priv->mac_base) {
1005 dev_err(&pdev->dev, "failed to remap regs\n");
1006 ret = -ENOMEM;
1007 goto out_platform;
1008 }
1009
1010 priv->dma_base = devm_ioremap(&pdev->dev,
1011 dma->start, resource_size(dma));
1012 if (!priv->dma_base) {
1013 dev_err(&pdev->dev, "failed to remap dma-regs\n");
1014 ret = -ENOMEM;
1015 goto out_platform;
1016 }
1017
1018 priv->emac_clk = clk_get(&pdev->dev, "emac");
1019 if (IS_ERR_OR_NULL(priv->emac_clk)) {
1020 dev_err(&pdev->dev, "failed to get emac clk\n");
1021 ret = PTR_ERR(priv->emac_clk);
1022 goto out_platform;
1023 }
1024
1025 priv->ephy_clk = clk_get(&pdev->dev, "ephy");
1026 if (IS_ERR_OR_NULL(priv->ephy_clk)) {
1027 dev_err(&pdev->dev, "failed to get ephy clk\n");
1028 ret = PTR_ERR(priv->ephy_clk);
1029 goto out_platform;
1030 }
1031
1032 priv->link_irq = link_irq;
1033 priv->rx_irq = rx_irq;
1034 priv->tx_irq = tx_irq;
1035 priv->msg_level = NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK;
1036 dev->netdev_ops = &nuport_mac_ops;
1037 dev->ethtool_ops = &nuport_mac_ethtool_ops;
1038 dev->watchdog_timeo = HZ;
1039 dev->flags = IFF_BROADCAST; /* Supports Broadcast */
1040 dev->tx_queue_len = TX_RING_SIZE / 2;
1041
1042 netif_napi_add(dev, &priv->napi, nuport_mac_poll, 64);
1043
1044 priv->mii_bus = mdiobus_alloc();
1045 if (!priv->mii_bus) {
1046 dev_err(&pdev->dev, "mii bus allocation failed\n");
1047 goto out;
1048 }
1049
1050 priv->mii_bus->priv = dev;
1051 priv->mii_bus->read = nuport_mac_mii_read;
1052 priv->mii_bus->write = nuport_mac_mii_write;
1053 priv->mii_bus->reset = nuport_mac_mii_reset;
1054 priv->mii_bus->name = "nuport-mac-mii";
1055 priv->mii_bus->phy_mask = (1 << 0);
1056 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s", pdev->name);
1057 priv->mii_bus->irq = kzalloc(PHY_MAX_ADDR * sizeof(int), GFP_KERNEL);
1058 if (!priv->mii_bus->irq) {
1059 dev_err(&pdev->dev, "failed to allocate mii_bus irqs\n");
1060 ret = -ENOMEM;
1061 goto out_mdio;
1062 }
1063
1064 /* We support PHY interrupts routed back to the MAC */
1065 for (i = 0; i < PHY_MAX_ADDR; i++)
1066 priv->mii_bus->irq[i] = PHY_IGNORE_INTERRUPT;
1067
1068 ret = mdiobus_register(priv->mii_bus);
1069 if (ret) {
1070 dev_err(&pdev->dev, "failed to register mii_bus\n");
1071 goto out_mdio_irq;
1072 }
1073
1074 ret = nuport_mac_mii_probe(dev);
1075 if (ret) {
1076 dev_err(&pdev->dev, "failed to probe MII bus\n");
1077 goto out_mdio_unregister;
1078 }
1079
1080 ret = register_netdev(dev);
1081 if (ret) {
1082 dev_err(&pdev->dev, "failed to register net_device\n");
1083 goto out_mdio_probe;
1084 }
1085
1086 /* read existing mac address */
1087 nuport_mac_read_mac_address(dev);
1088
1089 dev_info(&pdev->dev, "registered (MAC: %pM)\n", dev->dev_addr);
1090
1091 return ret;
1092
1093 out_mdio_probe:
1094 phy_disconnect(priv->phydev);
1095 out_mdio_unregister:
1096 mdiobus_unregister(priv->mii_bus);
1097 out_mdio_irq:
1098 kfree(priv->mii_bus->irq);
1099 out_mdio:
1100 mdiobus_free(priv->mii_bus);
1101 out_platform:
1102 platform_set_drvdata(pdev, NULL);
1103 out:
1104 clk_put(priv->ephy_clk);
1105 clk_put(priv->emac_clk);
1106 free_netdev(dev);
1107 platform_set_drvdata(pdev, NULL);
1108 return ret;
1109 }
1110
1111 static int nuport_mac_remove(struct platform_device *pdev)
1112 {
1113 struct net_device *dev = platform_get_drvdata(pdev);
1114 struct nuport_mac_priv *priv = netdev_priv(dev);
1115
1116 unregister_netdev(dev);
1117 phy_disconnect(priv->phydev);
1118 mdiobus_unregister(priv->mii_bus);
1119 kfree(priv->mii_bus->irq);
1120 mdiobus_free(priv->mii_bus);
1121 clk_put(priv->ephy_clk);
1122 clk_put(priv->emac_clk);
1123 free_netdev(dev);
1124
1125 platform_set_drvdata(pdev, NULL);
1126
1127 return 0;
1128 }
1129
1130 static struct of_device_id nuport_eth_ids[] __initdata = {
1131 {.compatible = "moschip,nuport-mac",},
1132 { /* sentinel */ },
1133 };
1134
1135 static struct platform_driver nuport_eth_driver = {
1136 .driver = {
1137 .name = "nuport-mac",
1138 .owner = THIS_MODULE,
1139 .of_match_table = nuport_eth_ids,
1140 },
1141 .probe = nuport_mac_probe,
1142 .remove = __devexit_p(nuport_mac_remove),
1143 };
1144
1145 module_platform_driver(nuport_eth_driver);
1146
1147 MODULE_AUTHOR("Moschip Semiconductors Ltd.");
1148 MODULE_DESCRIPTION("Moschip MCS8140 Ethernet MAC driver");
1149 MODULE_LICENSE("GPL");