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