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