xref: /openbmc/linux/drivers/net/ethernet/altera/altera_tse_main.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
19952f691SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2bbd2190cSVince Bridgers /* Altera Triple-Speed Ethernet MAC driver
3bbd2190cSVince Bridgers  * Copyright (C) 2008-2014 Altera Corporation. All rights reserved
4bbd2190cSVince Bridgers  *
5bbd2190cSVince Bridgers  * Contributors:
6bbd2190cSVince Bridgers  *   Dalon Westergreen
7bbd2190cSVince Bridgers  *   Thomas Chou
8bbd2190cSVince Bridgers  *   Ian Abbott
9bbd2190cSVince Bridgers  *   Yuriy Kozlov
10bbd2190cSVince Bridgers  *   Tobias Klauser
11bbd2190cSVince Bridgers  *   Andriy Smolskyy
12bbd2190cSVince Bridgers  *   Roman Bulgakov
13bbd2190cSVince Bridgers  *   Dmytro Mytarchuk
14bbd2190cSVince Bridgers  *   Matthew Gerlach
15bbd2190cSVince Bridgers  *
16bbd2190cSVince Bridgers  * Original driver contributed by SLS.
17bbd2190cSVince Bridgers  * Major updates contributed by GlobalLogic
18bbd2190cSVince Bridgers  */
19bbd2190cSVince Bridgers 
20bbd2190cSVince Bridgers #include <linux/atomic.h>
21bbd2190cSVince Bridgers #include <linux/delay.h>
22bbd2190cSVince Bridgers #include <linux/etherdevice.h>
23bbd2190cSVince Bridgers #include <linux/if_vlan.h>
24bbd2190cSVince Bridgers #include <linux/init.h>
25bbd2190cSVince Bridgers #include <linux/interrupt.h>
26bbd2190cSVince Bridgers #include <linux/io.h>
27bbd2190cSVince Bridgers #include <linux/kernel.h>
28bbd2190cSVince Bridgers #include <linux/module.h>
293b804564SNeill Whillans #include <linux/mii.h>
30db48abbaSMaxime Chevallier #include <linux/mdio/mdio-regmap.h>
31bbd2190cSVince Bridgers #include <linux/netdevice.h>
32bbd2190cSVince Bridgers #include <linux/of_device.h>
33bbd2190cSVince Bridgers #include <linux/of_mdio.h>
34bbd2190cSVince Bridgers #include <linux/of_net.h>
35bbd2190cSVince Bridgers #include <linux/of_platform.h>
36db48abbaSMaxime Chevallier #include <linux/pcs-lynx.h>
37bbd2190cSVince Bridgers #include <linux/phy.h>
38bbd2190cSVince Bridgers #include <linux/platform_device.h>
39db48abbaSMaxime Chevallier #include <linux/regmap.h>
40bbd2190cSVince Bridgers #include <linux/skbuff.h>
41bbd2190cSVince Bridgers #include <asm/cacheflush.h>
42bbd2190cSVince Bridgers 
43bbd2190cSVince Bridgers #include "altera_utils.h"
44bbd2190cSVince Bridgers #include "altera_tse.h"
45bbd2190cSVince Bridgers #include "altera_sgdma.h"
46bbd2190cSVince Bridgers #include "altera_msgdma.h"
47bbd2190cSVince Bridgers 
48bbd2190cSVince Bridgers static atomic_t instance_count = ATOMIC_INIT(~0);
49bbd2190cSVince Bridgers /* Module parameters */
50bbd2190cSVince Bridgers static int debug = -1;
51d3757ba4SJoe Perches module_param(debug, int, 0644);
52bbd2190cSVince Bridgers MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
53bbd2190cSVince Bridgers 
54bbd2190cSVince Bridgers static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
55bbd2190cSVince Bridgers 					NETIF_MSG_LINK | NETIF_MSG_IFUP |
56bbd2190cSVince Bridgers 					NETIF_MSG_IFDOWN);
57bbd2190cSVince Bridgers 
58bbd2190cSVince Bridgers #define RX_DESCRIPTORS 64
59bbd2190cSVince Bridgers static int dma_rx_num = RX_DESCRIPTORS;
60d3757ba4SJoe Perches module_param(dma_rx_num, int, 0644);
61bbd2190cSVince Bridgers MODULE_PARM_DESC(dma_rx_num, "Number of descriptors in the RX list");
62bbd2190cSVince Bridgers 
63bbd2190cSVince Bridgers #define TX_DESCRIPTORS 64
64bbd2190cSVince Bridgers static int dma_tx_num = TX_DESCRIPTORS;
65d3757ba4SJoe Perches module_param(dma_tx_num, int, 0644);
66bbd2190cSVince Bridgers MODULE_PARM_DESC(dma_tx_num, "Number of descriptors in the TX list");
67bbd2190cSVince Bridgers 
68bbd2190cSVince Bridgers 
69bbd2190cSVince Bridgers #define POLL_PHY (-1)
70bbd2190cSVince Bridgers 
71bbd2190cSVince Bridgers /* Make sure DMA buffer size is larger than the max frame size
72bbd2190cSVince Bridgers  * plus some alignment offset and a VLAN header. If the max frame size is
73bbd2190cSVince Bridgers  * 1518, a VLAN header would be additional 4 bytes and additional
74bbd2190cSVince Bridgers  * headroom for alignment is 2 bytes, 2048 is just fine.
75bbd2190cSVince Bridgers  */
76bbd2190cSVince Bridgers #define ALTERA_RXDMABUFFER_SIZE	2048
77bbd2190cSVince Bridgers 
78a5e516d0STom Rix /* Allow network stack to resume queuing packets after we've
79bbd2190cSVince Bridgers  * finished transmitting at least 1/4 of the packets in the queue.
80bbd2190cSVince Bridgers  */
81bbd2190cSVince Bridgers #define TSE_TX_THRESH(x)	(x->tx_ring_size / 4)
82bbd2190cSVince Bridgers 
83bbd2190cSVince Bridgers #define TXQUEUESTOP_THRESHHOLD	2
84bbd2190cSVince Bridgers 
8527260530SFabian Frederick static const struct of_device_id altera_tse_ids[];
86bbd2190cSVince Bridgers 
tse_tx_avail(struct altera_tse_private * priv)87bbd2190cSVince Bridgers static inline u32 tse_tx_avail(struct altera_tse_private *priv)
88bbd2190cSVince Bridgers {
89bbd2190cSVince Bridgers 	return priv->tx_cons + priv->tx_ring_size - priv->tx_prod - 1;
90bbd2190cSVince Bridgers }
91bbd2190cSVince Bridgers 
92bbd2190cSVince Bridgers /* MDIO specific functions
93bbd2190cSVince Bridgers  */
altera_tse_mdio_read(struct mii_bus * bus,int mii_id,int regnum)94bbd2190cSVince Bridgers static int altera_tse_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
95bbd2190cSVince Bridgers {
9689830580SVince Bridgers 	struct net_device *ndev = bus->priv;
9789830580SVince Bridgers 	struct altera_tse_private *priv = netdev_priv(ndev);
98bbd2190cSVince Bridgers 
99bbd2190cSVince Bridgers 	/* set MDIO address */
10089830580SVince Bridgers 	csrwr32((mii_id & 0x1f), priv->mac_dev,
101a923fc73SVince Bridgers 		tse_csroffs(mdio_phy1_addr));
102bbd2190cSVince Bridgers 
103bbd2190cSVince Bridgers 	/* get the data */
10489830580SVince Bridgers 	return csrrd32(priv->mac_dev,
105a923fc73SVince Bridgers 		       tse_csroffs(mdio_phy1) + regnum * 4) & 0xffff;
106bbd2190cSVince Bridgers }
107bbd2190cSVince Bridgers 
altera_tse_mdio_write(struct mii_bus * bus,int mii_id,int regnum,u16 value)108bbd2190cSVince Bridgers static int altera_tse_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
109bbd2190cSVince Bridgers 				 u16 value)
110bbd2190cSVince Bridgers {
11189830580SVince Bridgers 	struct net_device *ndev = bus->priv;
11289830580SVince Bridgers 	struct altera_tse_private *priv = netdev_priv(ndev);
113bbd2190cSVince Bridgers 
114bbd2190cSVince Bridgers 	/* set MDIO address */
11589830580SVince Bridgers 	csrwr32((mii_id & 0x1f), priv->mac_dev,
116a923fc73SVince Bridgers 		tse_csroffs(mdio_phy1_addr));
117bbd2190cSVince Bridgers 
118bbd2190cSVince Bridgers 	/* write the data */
119a923fc73SVince Bridgers 	csrwr32(value, priv->mac_dev, tse_csroffs(mdio_phy1) + regnum * 4);
120bbd2190cSVince Bridgers 	return 0;
121bbd2190cSVince Bridgers }
122bbd2190cSVince Bridgers 
altera_tse_mdio_create(struct net_device * dev,unsigned int id)123bbd2190cSVince Bridgers static int altera_tse_mdio_create(struct net_device *dev, unsigned int id)
124bbd2190cSVince Bridgers {
125bbd2190cSVince Bridgers 	struct altera_tse_private *priv = netdev_priv(dev);
126bbd2190cSVince Bridgers 	struct device_node *mdio_node = NULL;
127bbd2190cSVince Bridgers 	struct device_node *child_node = NULL;
1285adb0ed0SMaxime Chevallier 	struct mii_bus *mdio = NULL;
1295adb0ed0SMaxime Chevallier 	int ret;
130bbd2190cSVince Bridgers 
131bbd2190cSVince Bridgers 	for_each_child_of_node(priv->device->of_node, child_node) {
132bbd2190cSVince Bridgers 		if (of_device_is_compatible(child_node, "altr,tse-mdio")) {
133bbd2190cSVince Bridgers 			mdio_node = child_node;
134bbd2190cSVince Bridgers 			break;
135bbd2190cSVince Bridgers 		}
136bbd2190cSVince Bridgers 	}
137bbd2190cSVince Bridgers 
138bbd2190cSVince Bridgers 	if (mdio_node) {
139bbd2190cSVince Bridgers 		netdev_dbg(dev, "FOUND MDIO subnode\n");
140bbd2190cSVince Bridgers 	} else {
141bbd2190cSVince Bridgers 		netdev_dbg(dev, "NO MDIO subnode\n");
142bbd2190cSVince Bridgers 		return 0;
143bbd2190cSVince Bridgers 	}
144bbd2190cSVince Bridgers 
145bbd2190cSVince Bridgers 	mdio = mdiobus_alloc();
146bbd2190cSVince Bridgers 	if (mdio == NULL) {
147bbd2190cSVince Bridgers 		netdev_err(dev, "Error allocating MDIO bus\n");
14811ec18b1SMiaoqian Lin 		ret = -ENOMEM;
14911ec18b1SMiaoqian Lin 		goto put_node;
150bbd2190cSVince Bridgers 	}
151bbd2190cSVince Bridgers 
152bbd2190cSVince Bridgers 	mdio->name = ALTERA_TSE_RESOURCE_NAME;
153bbd2190cSVince Bridgers 	mdio->read = &altera_tse_mdio_read;
154bbd2190cSVince Bridgers 	mdio->write = &altera_tse_mdio_write;
155bbd2190cSVince Bridgers 	snprintf(mdio->id, MII_BUS_ID_SIZE, "%s-%u", mdio->name, id);
156bbd2190cSVince Bridgers 
15789830580SVince Bridgers 	mdio->priv = dev;
158bbd2190cSVince Bridgers 	mdio->parent = priv->device;
159bbd2190cSVince Bridgers 
160bbd2190cSVince Bridgers 	ret = of_mdiobus_register(mdio, mdio_node);
161bbd2190cSVince Bridgers 	if (ret != 0) {
162bbd2190cSVince Bridgers 		netdev_err(dev, "Cannot register MDIO bus %s\n",
163bbd2190cSVince Bridgers 			   mdio->id);
164e7f4dc35SAndrew Lunn 		goto out_free_mdio;
165bbd2190cSVince Bridgers 	}
16611ec18b1SMiaoqian Lin 	of_node_put(mdio_node);
167bbd2190cSVince Bridgers 
168bbd2190cSVince Bridgers 	if (netif_msg_drv(priv))
169bbd2190cSVince Bridgers 		netdev_info(dev, "MDIO bus %s: created\n", mdio->id);
170bbd2190cSVince Bridgers 
171bbd2190cSVince Bridgers 	priv->mdio = mdio;
172bbd2190cSVince Bridgers 	return 0;
173bbd2190cSVince Bridgers out_free_mdio:
174bbd2190cSVince Bridgers 	mdiobus_free(mdio);
175bbd2190cSVince Bridgers 	mdio = NULL;
17611ec18b1SMiaoqian Lin put_node:
17711ec18b1SMiaoqian Lin 	of_node_put(mdio_node);
178bbd2190cSVince Bridgers 	return ret;
179bbd2190cSVince Bridgers }
180bbd2190cSVince Bridgers 
altera_tse_mdio_destroy(struct net_device * dev)181bbd2190cSVince Bridgers static void altera_tse_mdio_destroy(struct net_device *dev)
182bbd2190cSVince Bridgers {
183bbd2190cSVince Bridgers 	struct altera_tse_private *priv = netdev_priv(dev);
184bbd2190cSVince Bridgers 
185bbd2190cSVince Bridgers 	if (priv->mdio == NULL)
186bbd2190cSVince Bridgers 		return;
187bbd2190cSVince Bridgers 
188bbd2190cSVince Bridgers 	if (netif_msg_drv(priv))
189bbd2190cSVince Bridgers 		netdev_info(dev, "MDIO bus %s: removed\n",
190bbd2190cSVince Bridgers 			    priv->mdio->id);
191bbd2190cSVince Bridgers 
192bbd2190cSVince Bridgers 	mdiobus_unregister(priv->mdio);
193bbd2190cSVince Bridgers 	mdiobus_free(priv->mdio);
194bbd2190cSVince Bridgers 	priv->mdio = NULL;
195bbd2190cSVince Bridgers }
196bbd2190cSVince Bridgers 
tse_init_rx_buffer(struct altera_tse_private * priv,struct tse_buffer * rxbuffer,int len)197bbd2190cSVince Bridgers static int tse_init_rx_buffer(struct altera_tse_private *priv,
198bbd2190cSVince Bridgers 			      struct tse_buffer *rxbuffer, int len)
199bbd2190cSVince Bridgers {
200bbd2190cSVince Bridgers 	rxbuffer->skb = netdev_alloc_skb_ip_align(priv->dev, len);
201bbd2190cSVince Bridgers 	if (!rxbuffer->skb)
202bbd2190cSVince Bridgers 		return -ENOMEM;
203bbd2190cSVince Bridgers 
204bbd2190cSVince Bridgers 	rxbuffer->dma_addr = dma_map_single(priv->device, rxbuffer->skb->data,
205bbd2190cSVince Bridgers 						len,
206bbd2190cSVince Bridgers 						DMA_FROM_DEVICE);
207bbd2190cSVince Bridgers 
208bbd2190cSVince Bridgers 	if (dma_mapping_error(priv->device, rxbuffer->dma_addr)) {
209bbd2190cSVince Bridgers 		netdev_err(priv->dev, "%s: DMA mapping error\n", __func__);
210bbd2190cSVince Bridgers 		dev_kfree_skb_any(rxbuffer->skb);
211bbd2190cSVince Bridgers 		return -EINVAL;
212bbd2190cSVince Bridgers 	}
21337c0ffaaSVince Bridgers 	rxbuffer->dma_addr &= (dma_addr_t)~3;
214bbd2190cSVince Bridgers 	rxbuffer->len = len;
215bbd2190cSVince Bridgers 	return 0;
216bbd2190cSVince Bridgers }
217bbd2190cSVince Bridgers 
tse_free_rx_buffer(struct altera_tse_private * priv,struct tse_buffer * rxbuffer)218bbd2190cSVince Bridgers static void tse_free_rx_buffer(struct altera_tse_private *priv,
219bbd2190cSVince Bridgers 			       struct tse_buffer *rxbuffer)
220bbd2190cSVince Bridgers {
221bbd2190cSVince Bridgers 	dma_addr_t dma_addr = rxbuffer->dma_addr;
2225adb0ed0SMaxime Chevallier 	struct sk_buff *skb = rxbuffer->skb;
223bbd2190cSVince Bridgers 
224bbd2190cSVince Bridgers 	if (skb != NULL) {
225bbd2190cSVince Bridgers 		if (dma_addr)
226bbd2190cSVince Bridgers 			dma_unmap_single(priv->device, dma_addr,
227bbd2190cSVince Bridgers 					 rxbuffer->len,
228bbd2190cSVince Bridgers 					 DMA_FROM_DEVICE);
229bbd2190cSVince Bridgers 		dev_kfree_skb_any(skb);
230bbd2190cSVince Bridgers 		rxbuffer->skb = NULL;
231bbd2190cSVince Bridgers 		rxbuffer->dma_addr = 0;
232bbd2190cSVince Bridgers 	}
233bbd2190cSVince Bridgers }
234bbd2190cSVince Bridgers 
235bbd2190cSVince Bridgers /* Unmap and free Tx buffer resources
236bbd2190cSVince Bridgers  */
tse_free_tx_buffer(struct altera_tse_private * priv,struct tse_buffer * buffer)237bbd2190cSVince Bridgers static void tse_free_tx_buffer(struct altera_tse_private *priv,
238bbd2190cSVince Bridgers 			       struct tse_buffer *buffer)
239bbd2190cSVince Bridgers {
240bbd2190cSVince Bridgers 	if (buffer->dma_addr) {
241bbd2190cSVince Bridgers 		if (buffer->mapped_as_page)
242bbd2190cSVince Bridgers 			dma_unmap_page(priv->device, buffer->dma_addr,
243bbd2190cSVince Bridgers 				       buffer->len, DMA_TO_DEVICE);
244bbd2190cSVince Bridgers 		else
245bbd2190cSVince Bridgers 			dma_unmap_single(priv->device, buffer->dma_addr,
246bbd2190cSVince Bridgers 					 buffer->len, DMA_TO_DEVICE);
247bbd2190cSVince Bridgers 		buffer->dma_addr = 0;
248bbd2190cSVince Bridgers 	}
249bbd2190cSVince Bridgers 	if (buffer->skb) {
250bbd2190cSVince Bridgers 		dev_kfree_skb_any(buffer->skb);
251bbd2190cSVince Bridgers 		buffer->skb = NULL;
252bbd2190cSVince Bridgers 	}
253bbd2190cSVince Bridgers }
254bbd2190cSVince Bridgers 
alloc_init_skbufs(struct altera_tse_private * priv)255bbd2190cSVince Bridgers static int alloc_init_skbufs(struct altera_tse_private *priv)
256bbd2190cSVince Bridgers {
257bbd2190cSVince Bridgers 	unsigned int rx_descs = priv->rx_ring_size;
258bbd2190cSVince Bridgers 	unsigned int tx_descs = priv->tx_ring_size;
259bbd2190cSVince Bridgers 	int ret = -ENOMEM;
260bbd2190cSVince Bridgers 	int i;
261bbd2190cSVince Bridgers 
262bbd2190cSVince Bridgers 	/* Create Rx ring buffer */
263bbd2190cSVince Bridgers 	priv->rx_ring = kcalloc(rx_descs, sizeof(struct tse_buffer),
264bbd2190cSVince Bridgers 				GFP_KERNEL);
265bbd2190cSVince Bridgers 	if (!priv->rx_ring)
266bbd2190cSVince Bridgers 		goto err_rx_ring;
267bbd2190cSVince Bridgers 
268bbd2190cSVince Bridgers 	/* Create Tx ring buffer */
269bbd2190cSVince Bridgers 	priv->tx_ring = kcalloc(tx_descs, sizeof(struct tse_buffer),
270bbd2190cSVince Bridgers 				GFP_KERNEL);
271bbd2190cSVince Bridgers 	if (!priv->tx_ring)
272bbd2190cSVince Bridgers 		goto err_tx_ring;
273bbd2190cSVince Bridgers 
274bbd2190cSVince Bridgers 	priv->tx_cons = 0;
275bbd2190cSVince Bridgers 	priv->tx_prod = 0;
276bbd2190cSVince Bridgers 
277bbd2190cSVince Bridgers 	/* Init Rx ring */
278bbd2190cSVince Bridgers 	for (i = 0; i < rx_descs; i++) {
279bbd2190cSVince Bridgers 		ret = tse_init_rx_buffer(priv, &priv->rx_ring[i],
280bbd2190cSVince Bridgers 					 priv->rx_dma_buf_sz);
281bbd2190cSVince Bridgers 		if (ret)
282bbd2190cSVince Bridgers 			goto err_init_rx_buffers;
283bbd2190cSVince Bridgers 	}
284bbd2190cSVince Bridgers 
285bbd2190cSVince Bridgers 	priv->rx_cons = 0;
286bbd2190cSVince Bridgers 	priv->rx_prod = 0;
287bbd2190cSVince Bridgers 
288bbd2190cSVince Bridgers 	return 0;
289bbd2190cSVince Bridgers err_init_rx_buffers:
290bbd2190cSVince Bridgers 	while (--i >= 0)
291bbd2190cSVince Bridgers 		tse_free_rx_buffer(priv, &priv->rx_ring[i]);
292bbd2190cSVince Bridgers 	kfree(priv->tx_ring);
293bbd2190cSVince Bridgers err_tx_ring:
294bbd2190cSVince Bridgers 	kfree(priv->rx_ring);
295bbd2190cSVince Bridgers err_rx_ring:
296bbd2190cSVince Bridgers 	return ret;
297bbd2190cSVince Bridgers }
298bbd2190cSVince Bridgers 
free_skbufs(struct net_device * dev)299bbd2190cSVince Bridgers static void free_skbufs(struct net_device *dev)
300bbd2190cSVince Bridgers {
301bbd2190cSVince Bridgers 	struct altera_tse_private *priv = netdev_priv(dev);
302bbd2190cSVince Bridgers 	unsigned int rx_descs = priv->rx_ring_size;
303bbd2190cSVince Bridgers 	unsigned int tx_descs = priv->tx_ring_size;
304bbd2190cSVince Bridgers 	int i;
305bbd2190cSVince Bridgers 
306bbd2190cSVince Bridgers 	/* Release the DMA TX/RX socket buffers */
307bbd2190cSVince Bridgers 	for (i = 0; i < rx_descs; i++)
308bbd2190cSVince Bridgers 		tse_free_rx_buffer(priv, &priv->rx_ring[i]);
309bbd2190cSVince Bridgers 	for (i = 0; i < tx_descs; i++)
310bbd2190cSVince Bridgers 		tse_free_tx_buffer(priv, &priv->tx_ring[i]);
311bbd2190cSVince Bridgers 
312bbd2190cSVince Bridgers 
313bbd2190cSVince Bridgers 	kfree(priv->tx_ring);
314bbd2190cSVince Bridgers }
315bbd2190cSVince Bridgers 
316bbd2190cSVince Bridgers /* Reallocate the skb for the reception process
317bbd2190cSVince Bridgers  */
tse_rx_refill(struct altera_tse_private * priv)318bbd2190cSVince Bridgers static inline void tse_rx_refill(struct altera_tse_private *priv)
319bbd2190cSVince Bridgers {
320bbd2190cSVince Bridgers 	unsigned int rxsize = priv->rx_ring_size;
321bbd2190cSVince Bridgers 	unsigned int entry;
322bbd2190cSVince Bridgers 	int ret;
323bbd2190cSVince Bridgers 
324bbd2190cSVince Bridgers 	for (; priv->rx_cons - priv->rx_prod > 0;
325bbd2190cSVince Bridgers 			priv->rx_prod++) {
326bbd2190cSVince Bridgers 		entry = priv->rx_prod % rxsize;
327bbd2190cSVince Bridgers 		if (likely(priv->rx_ring[entry].skb == NULL)) {
328bbd2190cSVince Bridgers 			ret = tse_init_rx_buffer(priv, &priv->rx_ring[entry],
329bbd2190cSVince Bridgers 				priv->rx_dma_buf_sz);
330bbd2190cSVince Bridgers 			if (unlikely(ret != 0))
331bbd2190cSVince Bridgers 				break;
332bbd2190cSVince Bridgers 			priv->dmaops->add_rx_desc(priv, &priv->rx_ring[entry]);
333bbd2190cSVince Bridgers 		}
334bbd2190cSVince Bridgers 	}
335bbd2190cSVince Bridgers }
336bbd2190cSVince Bridgers 
337bbd2190cSVince Bridgers /* Pull out the VLAN tag and fix up the packet
338bbd2190cSVince Bridgers  */
tse_rx_vlan(struct net_device * dev,struct sk_buff * skb)339bbd2190cSVince Bridgers static inline void tse_rx_vlan(struct net_device *dev, struct sk_buff *skb)
340bbd2190cSVince Bridgers {
341bbd2190cSVince Bridgers 	struct ethhdr *eth_hdr;
342bbd2190cSVince Bridgers 	u16 vid;
3435adb0ed0SMaxime Chevallier 
344bbd2190cSVince Bridgers 	if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
345bbd2190cSVince Bridgers 	    !__vlan_get_tag(skb, &vid)) {
346bbd2190cSVince Bridgers 		eth_hdr = (struct ethhdr *)skb->data;
347bbd2190cSVince Bridgers 		memmove(skb->data + VLAN_HLEN, eth_hdr, ETH_ALEN * 2);
348bbd2190cSVince Bridgers 		skb_pull(skb, VLAN_HLEN);
349bbd2190cSVince Bridgers 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
350bbd2190cSVince Bridgers 	}
351bbd2190cSVince Bridgers }
352bbd2190cSVince Bridgers 
353bbd2190cSVince Bridgers /* Receive a packet: retrieve and pass over to upper levels
354bbd2190cSVince Bridgers  */
tse_rx(struct altera_tse_private * priv,int limit)355bbd2190cSVince Bridgers static int tse_rx(struct altera_tse_private *priv, int limit)
356bbd2190cSVince Bridgers {
357bbd2190cSVince Bridgers 	unsigned int entry = priv->rx_cons % priv->rx_ring_size;
3585adb0ed0SMaxime Chevallier 	unsigned int next_entry;
3595adb0ed0SMaxime Chevallier 	unsigned int count = 0;
3605adb0ed0SMaxime Chevallier 	struct sk_buff *skb;
361bbd2190cSVince Bridgers 	u32 rxstatus;
362bbd2190cSVince Bridgers 	u16 pktlength;
363bbd2190cSVince Bridgers 	u16 pktstatus;
364bbd2190cSVince Bridgers 
36593ea3378SAndreas Oetken 	/* Check for count < limit first as get_rx_status is changing
36693ea3378SAndreas Oetken 	* the response-fifo so we must process the next packet
36793ea3378SAndreas Oetken 	* after calling get_rx_status if a response is pending.
36893ea3378SAndreas Oetken 	* (reading the last byte of the response pops the value from the fifo.)
36993ea3378SAndreas Oetken 	*/
37093ea3378SAndreas Oetken 	while ((count < limit) &&
37193ea3378SAndreas Oetken 	       ((rxstatus = priv->dmaops->get_rx_status(priv)) != 0)) {
372bbd2190cSVince Bridgers 		pktstatus = rxstatus >> 16;
373bbd2190cSVince Bridgers 		pktlength = rxstatus & 0xffff;
374bbd2190cSVince Bridgers 
375bbd2190cSVince Bridgers 		if ((pktstatus & 0xFF) || (pktlength == 0))
376bbd2190cSVince Bridgers 			netdev_err(priv->dev,
377bbd2190cSVince Bridgers 				   "RCV pktstatus %08X pktlength %08X\n",
378bbd2190cSVince Bridgers 				   pktstatus, pktlength);
379bbd2190cSVince Bridgers 
380a5e516d0STom Rix 		/* DMA transfer from TSE starts with 2 additional bytes for
38148734994SVlastimil Setka 		 * IP payload alignment. Status returned by get_rx_status()
38248734994SVlastimil Setka 		 * contains DMA transfer length. Packet is 2 bytes shorter.
38348734994SVlastimil Setka 		 */
38448734994SVlastimil Setka 		pktlength -= 2;
38548734994SVlastimil Setka 
386bbd2190cSVince Bridgers 		count++;
387bbd2190cSVince Bridgers 		next_entry = (++priv->rx_cons) % priv->rx_ring_size;
388bbd2190cSVince Bridgers 
389bbd2190cSVince Bridgers 		skb = priv->rx_ring[entry].skb;
390bbd2190cSVince Bridgers 		if (unlikely(!skb)) {
391bbd2190cSVince Bridgers 			netdev_err(priv->dev,
392bbd2190cSVince Bridgers 				   "%s: Inconsistent Rx descriptor chain\n",
393bbd2190cSVince Bridgers 				   __func__);
394bbd2190cSVince Bridgers 			priv->dev->stats.rx_dropped++;
395bbd2190cSVince Bridgers 			break;
396bbd2190cSVince Bridgers 		}
397bbd2190cSVince Bridgers 		priv->rx_ring[entry].skb = NULL;
398bbd2190cSVince Bridgers 
399bbd2190cSVince Bridgers 		skb_put(skb, pktlength);
400bbd2190cSVince Bridgers 
401bbd2190cSVince Bridgers 		dma_unmap_single(priv->device, priv->rx_ring[entry].dma_addr,
402bbd2190cSVince Bridgers 				 priv->rx_ring[entry].len, DMA_FROM_DEVICE);
403bbd2190cSVince Bridgers 
404bbd2190cSVince Bridgers 		if (netif_msg_pktdata(priv)) {
405bbd2190cSVince Bridgers 			netdev_info(priv->dev, "frame received %d bytes\n",
406bbd2190cSVince Bridgers 				    pktlength);
407bbd2190cSVince Bridgers 			print_hex_dump(KERN_ERR, "data: ", DUMP_PREFIX_OFFSET,
408bbd2190cSVince Bridgers 				       16, 1, skb->data, pktlength, true);
409bbd2190cSVince Bridgers 		}
410bbd2190cSVince Bridgers 
411bbd2190cSVince Bridgers 		tse_rx_vlan(priv->dev, skb);
412bbd2190cSVince Bridgers 
413bbd2190cSVince Bridgers 		skb->protocol = eth_type_trans(skb, priv->dev);
414bbd2190cSVince Bridgers 		skb_checksum_none_assert(skb);
415bbd2190cSVince Bridgers 
416bbd2190cSVince Bridgers 		napi_gro_receive(&priv->napi, skb);
417bbd2190cSVince Bridgers 
418bbd2190cSVince Bridgers 		priv->dev->stats.rx_packets++;
419bbd2190cSVince Bridgers 		priv->dev->stats.rx_bytes += pktlength;
420bbd2190cSVince Bridgers 
421bbd2190cSVince Bridgers 		entry = next_entry;
422bbd2190cSVince Bridgers 
423bbd2190cSVince Bridgers 		tse_rx_refill(priv);
42437c0ffaaSVince Bridgers 	}
42537c0ffaaSVince Bridgers 
426bbd2190cSVince Bridgers 	return count;
427bbd2190cSVince Bridgers }
428bbd2190cSVince Bridgers 
429bbd2190cSVince Bridgers /* Reclaim resources after transmission completes
430bbd2190cSVince Bridgers  */
tse_tx_complete(struct altera_tse_private * priv)431bbd2190cSVince Bridgers static int tse_tx_complete(struct altera_tse_private *priv)
432bbd2190cSVince Bridgers {
433bbd2190cSVince Bridgers 	unsigned int txsize = priv->tx_ring_size;
434bbd2190cSVince Bridgers 	struct tse_buffer *tx_buff;
4355adb0ed0SMaxime Chevallier 	unsigned int entry;
436bbd2190cSVince Bridgers 	int txcomplete = 0;
4375adb0ed0SMaxime Chevallier 	u32 ready;
438bbd2190cSVince Bridgers 
439bbd2190cSVince Bridgers 	spin_lock(&priv->tx_lock);
440bbd2190cSVince Bridgers 
441bbd2190cSVince Bridgers 	ready = priv->dmaops->tx_completions(priv);
442bbd2190cSVince Bridgers 
443bbd2190cSVince Bridgers 	/* Free sent buffers */
444bbd2190cSVince Bridgers 	while (ready && (priv->tx_cons != priv->tx_prod)) {
445bbd2190cSVince Bridgers 		entry = priv->tx_cons % txsize;
446bbd2190cSVince Bridgers 		tx_buff = &priv->tx_ring[entry];
447bbd2190cSVince Bridgers 
448bbd2190cSVince Bridgers 		if (netif_msg_tx_done(priv))
449bbd2190cSVince Bridgers 			netdev_dbg(priv->dev, "%s: curr %d, dirty %d\n",
450bbd2190cSVince Bridgers 				   __func__, priv->tx_prod, priv->tx_cons);
451bbd2190cSVince Bridgers 
452bbd2190cSVince Bridgers 		if (likely(tx_buff->skb))
453bbd2190cSVince Bridgers 			priv->dev->stats.tx_packets++;
454bbd2190cSVince Bridgers 
455bbd2190cSVince Bridgers 		tse_free_tx_buffer(priv, tx_buff);
456bbd2190cSVince Bridgers 		priv->tx_cons++;
457bbd2190cSVince Bridgers 
458bbd2190cSVince Bridgers 		txcomplete++;
459bbd2190cSVince Bridgers 		ready--;
460bbd2190cSVince Bridgers 	}
461bbd2190cSVince Bridgers 
462bbd2190cSVince Bridgers 	if (unlikely(netif_queue_stopped(priv->dev) &&
463bbd2190cSVince Bridgers 		     tse_tx_avail(priv) > TSE_TX_THRESH(priv))) {
464bbd2190cSVince Bridgers 		if (netif_queue_stopped(priv->dev) &&
465bbd2190cSVince Bridgers 		    tse_tx_avail(priv) > TSE_TX_THRESH(priv)) {
466bbd2190cSVince Bridgers 			if (netif_msg_tx_done(priv))
467bbd2190cSVince Bridgers 				netdev_dbg(priv->dev, "%s: restart transmit\n",
468bbd2190cSVince Bridgers 					   __func__);
469bbd2190cSVince Bridgers 			netif_wake_queue(priv->dev);
470bbd2190cSVince Bridgers 		}
471bbd2190cSVince Bridgers 	}
472bbd2190cSVince Bridgers 
473bbd2190cSVince Bridgers 	spin_unlock(&priv->tx_lock);
474bbd2190cSVince Bridgers 	return txcomplete;
475bbd2190cSVince Bridgers }
476bbd2190cSVince Bridgers 
477bbd2190cSVince Bridgers /* NAPI polling function
478bbd2190cSVince Bridgers  */
tse_poll(struct napi_struct * napi,int budget)479bbd2190cSVince Bridgers static int tse_poll(struct napi_struct *napi, int budget)
480bbd2190cSVince Bridgers {
481bbd2190cSVince Bridgers 	struct altera_tse_private *priv =
482bbd2190cSVince Bridgers 			container_of(napi, struct altera_tse_private, napi);
483bbd2190cSVince Bridgers 	unsigned long int flags;
4845adb0ed0SMaxime Chevallier 	int rxcomplete = 0;
485bbd2190cSVince Bridgers 
4868d4ac39dSVlastimil Setka 	tse_tx_complete(priv);
487bbd2190cSVince Bridgers 
488bbd2190cSVince Bridgers 	rxcomplete = tse_rx(priv, budget);
489bbd2190cSVince Bridgers 
4908d4ac39dSVlastimil Setka 	if (rxcomplete < budget) {
491bbd2190cSVince Bridgers 
4926ad20165SEric Dumazet 		napi_complete_done(napi, rxcomplete);
493bbd2190cSVince Bridgers 
494bbd2190cSVince Bridgers 		netdev_dbg(priv->dev,
495bbd2190cSVince Bridgers 			   "NAPI Complete, did %d packets with budget %d\n",
4968d4ac39dSVlastimil Setka 			   rxcomplete, budget);
497bbd2190cSVince Bridgers 
498bbd2190cSVince Bridgers 		spin_lock_irqsave(&priv->rxdma_irq_lock, flags);
499bbd2190cSVince Bridgers 		priv->dmaops->enable_rxirq(priv);
500bbd2190cSVince Bridgers 		priv->dmaops->enable_txirq(priv);
501bbd2190cSVince Bridgers 		spin_unlock_irqrestore(&priv->rxdma_irq_lock, flags);
5028d4ac39dSVlastimil Setka 	}
5038d4ac39dSVlastimil Setka 	return rxcomplete;
504bbd2190cSVince Bridgers }
505bbd2190cSVince Bridgers 
506bbd2190cSVince Bridgers /* DMA TX & RX FIFO interrupt routing
507bbd2190cSVince Bridgers  */
altera_isr(int irq,void * dev_id)508bbd2190cSVince Bridgers static irqreturn_t altera_isr(int irq, void *dev_id)
509bbd2190cSVince Bridgers {
510bbd2190cSVince Bridgers 	struct net_device *dev = dev_id;
511bbd2190cSVince Bridgers 	struct altera_tse_private *priv;
512bbd2190cSVince Bridgers 
513bbd2190cSVince Bridgers 	if (unlikely(!dev)) {
514bbd2190cSVince Bridgers 		pr_err("%s: invalid dev pointer\n", __func__);
515bbd2190cSVince Bridgers 		return IRQ_NONE;
516bbd2190cSVince Bridgers 	}
517bbd2190cSVince Bridgers 	priv = netdev_priv(dev);
518bbd2190cSVince Bridgers 
5198d4ac39dSVlastimil Setka 	spin_lock(&priv->rxdma_irq_lock);
520bbd2190cSVince Bridgers 	/* reset IRQs */
521bbd2190cSVince Bridgers 	priv->dmaops->clear_rxirq(priv);
522bbd2190cSVince Bridgers 	priv->dmaops->clear_txirq(priv);
5238d4ac39dSVlastimil Setka 	spin_unlock(&priv->rxdma_irq_lock);
524bbd2190cSVince Bridgers 
5258d4ac39dSVlastimil Setka 	if (likely(napi_schedule_prep(&priv->napi))) {
5268d4ac39dSVlastimil Setka 		spin_lock(&priv->rxdma_irq_lock);
5278d4ac39dSVlastimil Setka 		priv->dmaops->disable_rxirq(priv);
5288d4ac39dSVlastimil Setka 		priv->dmaops->disable_txirq(priv);
5298d4ac39dSVlastimil Setka 		spin_unlock(&priv->rxdma_irq_lock);
5308d4ac39dSVlastimil Setka 		__napi_schedule(&priv->napi);
5318d4ac39dSVlastimil Setka 	}
5328d4ac39dSVlastimil Setka 
533bbd2190cSVince Bridgers 
534bbd2190cSVince Bridgers 	return IRQ_HANDLED;
535bbd2190cSVince Bridgers }
536bbd2190cSVince Bridgers 
537bbd2190cSVince Bridgers /* Transmit a packet (called by the kernel). Dispatches
538bbd2190cSVince Bridgers  * either the SGDMA method for transmitting or the
539bbd2190cSVince Bridgers  * MSGDMA method, assumes no scatter/gather support,
540bbd2190cSVince Bridgers  * implying an assumption that there's only one
541bbd2190cSVince Bridgers  * physically contiguous fragment starting at
542bbd2190cSVince Bridgers  * skb->data, for length of skb_headlen(skb).
543bbd2190cSVince Bridgers  */
tse_start_xmit(struct sk_buff * skb,struct net_device * dev)544ab99b7d2SYunjian Wang static netdev_tx_t tse_start_xmit(struct sk_buff *skb, struct net_device *dev)
545bbd2190cSVince Bridgers {
546bbd2190cSVince Bridgers 	struct altera_tse_private *priv = netdev_priv(dev);
547bbd2190cSVince Bridgers 	unsigned int nopaged_len = skb_headlen(skb);
5485adb0ed0SMaxime Chevallier 	unsigned int txsize = priv->tx_ring_size;
5495adb0ed0SMaxime Chevallier 	int nfrags = skb_shinfo(skb)->nr_frags;
5505adb0ed0SMaxime Chevallier 	struct tse_buffer *buffer = NULL;
551ab99b7d2SYunjian Wang 	netdev_tx_t ret = NETDEV_TX_OK;
552bbd2190cSVince Bridgers 	dma_addr_t dma_addr;
5535adb0ed0SMaxime Chevallier 	unsigned int entry;
554bbd2190cSVince Bridgers 
555bbd2190cSVince Bridgers 	spin_lock_bh(&priv->tx_lock);
556bbd2190cSVince Bridgers 
557bbd2190cSVince Bridgers 	if (unlikely(tse_tx_avail(priv) < nfrags + 1)) {
558bbd2190cSVince Bridgers 		if (!netif_queue_stopped(dev)) {
559bbd2190cSVince Bridgers 			netif_stop_queue(dev);
560bbd2190cSVince Bridgers 			/* This is a hard error, log it. */
561bbd2190cSVince Bridgers 			netdev_err(priv->dev,
562bbd2190cSVince Bridgers 				   "%s: Tx list full when queue awake\n",
563bbd2190cSVince Bridgers 				   __func__);
564bbd2190cSVince Bridgers 		}
565bbd2190cSVince Bridgers 		ret = NETDEV_TX_BUSY;
566bbd2190cSVince Bridgers 		goto out;
567bbd2190cSVince Bridgers 	}
568bbd2190cSVince Bridgers 
569bbd2190cSVince Bridgers 	/* Map the first skb fragment */
570bbd2190cSVince Bridgers 	entry = priv->tx_prod % txsize;
571bbd2190cSVince Bridgers 	buffer = &priv->tx_ring[entry];
572bbd2190cSVince Bridgers 
573bbd2190cSVince Bridgers 	dma_addr = dma_map_single(priv->device, skb->data, nopaged_len,
574bbd2190cSVince Bridgers 				  DMA_TO_DEVICE);
575bbd2190cSVince Bridgers 	if (dma_mapping_error(priv->device, dma_addr)) {
576bbd2190cSVince Bridgers 		netdev_err(priv->dev, "%s: DMA mapping error\n", __func__);
577bbd2190cSVince Bridgers 		ret = NETDEV_TX_OK;
578bbd2190cSVince Bridgers 		goto out;
579bbd2190cSVince Bridgers 	}
580bbd2190cSVince Bridgers 
581bbd2190cSVince Bridgers 	buffer->skb = skb;
582bbd2190cSVince Bridgers 	buffer->dma_addr = dma_addr;
583bbd2190cSVince Bridgers 	buffer->len = nopaged_len;
584bbd2190cSVince Bridgers 
58589830580SVince Bridgers 	priv->dmaops->tx_buffer(priv, buffer);
586bbd2190cSVince Bridgers 
587bbd2190cSVince Bridgers 	skb_tx_timestamp(skb);
588bbd2190cSVince Bridgers 
589bbd2190cSVince Bridgers 	priv->tx_prod++;
590bbd2190cSVince Bridgers 	dev->stats.tx_bytes += skb->len;
591bbd2190cSVince Bridgers 
592bbd2190cSVince Bridgers 	if (unlikely(tse_tx_avail(priv) <= TXQUEUESTOP_THRESHHOLD)) {
593bbd2190cSVince Bridgers 		if (netif_msg_hw(priv))
594bbd2190cSVince Bridgers 			netdev_dbg(priv->dev, "%s: stop transmitted packets\n",
595bbd2190cSVince Bridgers 				   __func__);
596bbd2190cSVince Bridgers 		netif_stop_queue(dev);
597bbd2190cSVince Bridgers 	}
598bbd2190cSVince Bridgers 
599bbd2190cSVince Bridgers out:
600bbd2190cSVince Bridgers 	spin_unlock_bh(&priv->tx_lock);
601bbd2190cSVince Bridgers 
602bbd2190cSVince Bridgers 	return ret;
603bbd2190cSVince Bridgers }
604bbd2190cSVince Bridgers 
altera_tse_phy_get_addr_mdio_create(struct net_device * dev)605004fa118SWalter Lozano static int altera_tse_phy_get_addr_mdio_create(struct net_device *dev)
606004fa118SWalter Lozano {
607004fa118SWalter Lozano 	struct altera_tse_private *priv = netdev_priv(dev);
608004fa118SWalter Lozano 	struct device_node *np = priv->device->of_node;
6090c65b2b9SAndrew Lunn 	int ret;
610004fa118SWalter Lozano 
6110c65b2b9SAndrew Lunn 	ret = of_get_phy_mode(np, &priv->phy_iface);
612004fa118SWalter Lozano 
6133354313eSWalter Lozano 	/* Avoid get phy addr and create mdio if no phy is present */
6140c65b2b9SAndrew Lunn 	if (ret)
6153354313eSWalter Lozano 		return 0;
6163354313eSWalter Lozano 
617004fa118SWalter Lozano 	/* try to get PHY address from device tree, use PHY autodetection if
618004fa118SWalter Lozano 	 * no valid address is given
619004fa118SWalter Lozano 	 */
620004fa118SWalter Lozano 
621004fa118SWalter Lozano 	if (of_property_read_u32(priv->device->of_node, "phy-addr",
622004fa118SWalter Lozano 			 &priv->phy_addr)) {
623004fa118SWalter Lozano 		priv->phy_addr = POLL_PHY;
624004fa118SWalter Lozano 	}
625004fa118SWalter Lozano 
626004fa118SWalter Lozano 	if (!((priv->phy_addr == POLL_PHY) ||
627004fa118SWalter Lozano 		  ((priv->phy_addr >= 0) && (priv->phy_addr < PHY_MAX_ADDR)))) {
628004fa118SWalter Lozano 		netdev_err(dev, "invalid phy-addr specified %d\n",
629004fa118SWalter Lozano 			priv->phy_addr);
630004fa118SWalter Lozano 		return -ENODEV;
631004fa118SWalter Lozano 	}
632004fa118SWalter Lozano 
633004fa118SWalter Lozano 	/* Create/attach to MDIO bus */
634004fa118SWalter Lozano 	ret = altera_tse_mdio_create(dev,
635004fa118SWalter Lozano 					 atomic_add_return(1, &instance_count));
636004fa118SWalter Lozano 
637004fa118SWalter Lozano 	if (ret)
638004fa118SWalter Lozano 		return -ENODEV;
639004fa118SWalter Lozano 
640004fa118SWalter Lozano 	return 0;
641004fa118SWalter Lozano }
642004fa118SWalter Lozano 
tse_update_mac_addr(struct altera_tse_private * priv,const u8 * addr)64376660757SJakub Kicinski static void tse_update_mac_addr(struct altera_tse_private *priv, const u8 *addr)
644bbd2190cSVince Bridgers {
645bbd2190cSVince Bridgers 	u32 msb;
646bbd2190cSVince Bridgers 	u32 lsb;
647bbd2190cSVince Bridgers 
648bbd2190cSVince Bridgers 	msb = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
649bbd2190cSVince Bridgers 	lsb = ((addr[5] << 8) | addr[4]) & 0xffff;
650bbd2190cSVince Bridgers 
651bbd2190cSVince Bridgers 	/* Set primary MAC address */
65289830580SVince Bridgers 	csrwr32(msb, priv->mac_dev, tse_csroffs(mac_addr_0));
65389830580SVince Bridgers 	csrwr32(lsb, priv->mac_dev, tse_csroffs(mac_addr_1));
654bbd2190cSVince Bridgers }
655bbd2190cSVince Bridgers 
656bbd2190cSVince Bridgers /* MAC software reset.
657bbd2190cSVince Bridgers  * When reset is triggered, the MAC function completes the current
658bbd2190cSVince Bridgers  * transmission or reception, and subsequently disables the transmit and
659bbd2190cSVince Bridgers  * receive logic, flushes the receive FIFO buffer, and resets the statistics
660bbd2190cSVince Bridgers  * counters.
661bbd2190cSVince Bridgers  */
reset_mac(struct altera_tse_private * priv)662bbd2190cSVince Bridgers static int reset_mac(struct altera_tse_private *priv)
663bbd2190cSVince Bridgers {
664bbd2190cSVince Bridgers 	int counter;
665bbd2190cSVince Bridgers 	u32 dat;
666bbd2190cSVince Bridgers 
66789830580SVince Bridgers 	dat = csrrd32(priv->mac_dev, tse_csroffs(command_config));
668bbd2190cSVince Bridgers 	dat &= ~(MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA);
669bbd2190cSVince Bridgers 	dat |= MAC_CMDCFG_SW_RESET | MAC_CMDCFG_CNT_RESET;
67089830580SVince Bridgers 	csrwr32(dat, priv->mac_dev, tse_csroffs(command_config));
671bbd2190cSVince Bridgers 
672bbd2190cSVince Bridgers 	counter = 0;
673bbd2190cSVince Bridgers 	while (counter++ < ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) {
67489830580SVince Bridgers 		if (tse_bit_is_clear(priv->mac_dev, tse_csroffs(command_config),
67589830580SVince Bridgers 				     MAC_CMDCFG_SW_RESET))
676bbd2190cSVince Bridgers 			break;
677bbd2190cSVince Bridgers 		udelay(1);
678bbd2190cSVince Bridgers 	}
679bbd2190cSVince Bridgers 
680bbd2190cSVince Bridgers 	if (counter >= ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) {
68189830580SVince Bridgers 		dat = csrrd32(priv->mac_dev, tse_csroffs(command_config));
682bbd2190cSVince Bridgers 		dat &= ~MAC_CMDCFG_SW_RESET;
68389830580SVince Bridgers 		csrwr32(dat, priv->mac_dev, tse_csroffs(command_config));
684bbd2190cSVince Bridgers 		return -1;
685bbd2190cSVince Bridgers 	}
686bbd2190cSVince Bridgers 	return 0;
687bbd2190cSVince Bridgers }
688bbd2190cSVince Bridgers 
689bbd2190cSVince Bridgers /* Initialize MAC core registers
690bbd2190cSVince Bridgers */
init_mac(struct altera_tse_private * priv)691bbd2190cSVince Bridgers static int init_mac(struct altera_tse_private *priv)
692bbd2190cSVince Bridgers {
693bbd2190cSVince Bridgers 	unsigned int cmd = 0;
694bbd2190cSVince Bridgers 	u32 frm_length;
695bbd2190cSVince Bridgers 
696bbd2190cSVince Bridgers 	/* Setup Rx FIFO */
69789830580SVince Bridgers 	csrwr32(priv->rx_fifo_depth - ALTERA_TSE_RX_SECTION_EMPTY,
69889830580SVince Bridgers 		priv->mac_dev, tse_csroffs(rx_section_empty));
69989830580SVince Bridgers 
70089830580SVince Bridgers 	csrwr32(ALTERA_TSE_RX_SECTION_FULL, priv->mac_dev,
70189830580SVince Bridgers 		tse_csroffs(rx_section_full));
70289830580SVince Bridgers 
70389830580SVince Bridgers 	csrwr32(ALTERA_TSE_RX_ALMOST_EMPTY, priv->mac_dev,
70489830580SVince Bridgers 		tse_csroffs(rx_almost_empty));
70589830580SVince Bridgers 
70689830580SVince Bridgers 	csrwr32(ALTERA_TSE_RX_ALMOST_FULL, priv->mac_dev,
70789830580SVince Bridgers 		tse_csroffs(rx_almost_full));
708bbd2190cSVince Bridgers 
709bbd2190cSVince Bridgers 	/* Setup Tx FIFO */
71089830580SVince Bridgers 	csrwr32(priv->tx_fifo_depth - ALTERA_TSE_TX_SECTION_EMPTY,
71189830580SVince Bridgers 		priv->mac_dev, tse_csroffs(tx_section_empty));
71289830580SVince Bridgers 
71389830580SVince Bridgers 	csrwr32(ALTERA_TSE_TX_SECTION_FULL, priv->mac_dev,
71489830580SVince Bridgers 		tse_csroffs(tx_section_full));
71589830580SVince Bridgers 
71689830580SVince Bridgers 	csrwr32(ALTERA_TSE_TX_ALMOST_EMPTY, priv->mac_dev,
71789830580SVince Bridgers 		tse_csroffs(tx_almost_empty));
71889830580SVince Bridgers 
71989830580SVince Bridgers 	csrwr32(ALTERA_TSE_TX_ALMOST_FULL, priv->mac_dev,
72089830580SVince Bridgers 		tse_csroffs(tx_almost_full));
721bbd2190cSVince Bridgers 
722bbd2190cSVince Bridgers 	/* MAC Address Configuration */
723bbd2190cSVince Bridgers 	tse_update_mac_addr(priv, priv->dev->dev_addr);
724bbd2190cSVince Bridgers 
725bbd2190cSVince Bridgers 	/* MAC Function Configuration */
726bbd2190cSVince Bridgers 	frm_length = ETH_HLEN + priv->dev->mtu + ETH_FCS_LEN;
72789830580SVince Bridgers 	csrwr32(frm_length, priv->mac_dev, tse_csroffs(frm_length));
72889830580SVince Bridgers 
72989830580SVince Bridgers 	csrwr32(ALTERA_TSE_TX_IPG_LENGTH, priv->mac_dev,
73089830580SVince Bridgers 		tse_csroffs(tx_ipg_length));
731bbd2190cSVince Bridgers 
732bbd2190cSVince Bridgers 	/* Disable RX/TX shift 16 for alignment of all received frames on 16-bit
733bbd2190cSVince Bridgers 	 * start address
734bbd2190cSVince Bridgers 	 */
73589830580SVince Bridgers 	tse_set_bit(priv->mac_dev, tse_csroffs(rx_cmd_stat),
73689830580SVince Bridgers 		    ALTERA_TSE_RX_CMD_STAT_RX_SHIFT16);
73789830580SVince Bridgers 
73889830580SVince Bridgers 	tse_clear_bit(priv->mac_dev, tse_csroffs(tx_cmd_stat),
73989830580SVince Bridgers 		      ALTERA_TSE_TX_CMD_STAT_TX_SHIFT16 |
740bbd2190cSVince Bridgers 		      ALTERA_TSE_TX_CMD_STAT_OMIT_CRC);
741bbd2190cSVince Bridgers 
742bbd2190cSVince Bridgers 	/* Set the MAC options */
74389830580SVince Bridgers 	cmd = csrrd32(priv->mac_dev, tse_csroffs(command_config));
74437c0ffaaSVince Bridgers 	cmd &= ~MAC_CMDCFG_PAD_EN;	/* No padding Removal on Receive */
745bbd2190cSVince Bridgers 	cmd &= ~MAC_CMDCFG_CRC_FWD;	/* CRC Removal */
746bbd2190cSVince Bridgers 	cmd |= MAC_CMDCFG_RX_ERR_DISC;	/* Automatically discard frames
747bbd2190cSVince Bridgers 					 * with CRC errors
748bbd2190cSVince Bridgers 					 */
749bbd2190cSVince Bridgers 	cmd |= MAC_CMDCFG_CNTL_FRM_ENA;
750bbd2190cSVince Bridgers 	cmd &= ~MAC_CMDCFG_TX_ENA;
751bbd2190cSVince Bridgers 	cmd &= ~MAC_CMDCFG_RX_ENA;
75237c0ffaaSVince Bridgers 
75337c0ffaaSVince Bridgers 	/* Default speed and duplex setting, full/100 */
75437c0ffaaSVince Bridgers 	cmd &= ~MAC_CMDCFG_HD_ENA;
75537c0ffaaSVince Bridgers 	cmd &= ~MAC_CMDCFG_ETH_SPEED;
75637c0ffaaSVince Bridgers 	cmd &= ~MAC_CMDCFG_ENA_10;
75737c0ffaaSVince Bridgers 
75889830580SVince Bridgers 	csrwr32(cmd, priv->mac_dev, tse_csroffs(command_config));
759bbd2190cSVince Bridgers 
76089830580SVince Bridgers 	csrwr32(ALTERA_TSE_PAUSE_QUANTA, priv->mac_dev,
76189830580SVince Bridgers 		tse_csroffs(pause_quanta));
7625aec4ee3SVince Bridgers 
763bbd2190cSVince Bridgers 	if (netif_msg_hw(priv))
764bbd2190cSVince Bridgers 		dev_dbg(priv->device,
765bbd2190cSVince Bridgers 			"MAC post-initialization: CMD_CONFIG = 0x%08x\n", cmd);
766bbd2190cSVince Bridgers 
767bbd2190cSVince Bridgers 	return 0;
768bbd2190cSVince Bridgers }
769bbd2190cSVince Bridgers 
770bbd2190cSVince Bridgers /* Start/stop MAC transmission logic
771bbd2190cSVince Bridgers  */
tse_set_mac(struct altera_tse_private * priv,bool enable)772bbd2190cSVince Bridgers static void tse_set_mac(struct altera_tse_private *priv, bool enable)
773bbd2190cSVince Bridgers {
77489830580SVince Bridgers 	u32 value = csrrd32(priv->mac_dev, tse_csroffs(command_config));
775bbd2190cSVince Bridgers 
776bbd2190cSVince Bridgers 	if (enable)
777bbd2190cSVince Bridgers 		value |= MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA;
778bbd2190cSVince Bridgers 	else
779bbd2190cSVince Bridgers 		value &= ~(MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA);
780bbd2190cSVince Bridgers 
78189830580SVince Bridgers 	csrwr32(value, priv->mac_dev, tse_csroffs(command_config));
782bbd2190cSVince Bridgers }
783bbd2190cSVince Bridgers 
784bbd2190cSVince Bridgers /* Change the MTU
785bbd2190cSVince Bridgers  */
tse_change_mtu(struct net_device * dev,int new_mtu)786bbd2190cSVince Bridgers static int tse_change_mtu(struct net_device *dev, int new_mtu)
787bbd2190cSVince Bridgers {
788bbd2190cSVince Bridgers 	if (netif_running(dev)) {
789bbd2190cSVince Bridgers 		netdev_err(dev, "must be stopped to change its MTU\n");
790bbd2190cSVince Bridgers 		return -EBUSY;
791bbd2190cSVince Bridgers 	}
792bbd2190cSVince Bridgers 
793bbd2190cSVince Bridgers 	dev->mtu = new_mtu;
794bbd2190cSVince Bridgers 	netdev_update_features(dev);
795bbd2190cSVince Bridgers 
796bbd2190cSVince Bridgers 	return 0;
797bbd2190cSVince Bridgers }
798bbd2190cSVince Bridgers 
altera_tse_set_mcfilter(struct net_device * dev)799bbd2190cSVince Bridgers static void altera_tse_set_mcfilter(struct net_device *dev)
800bbd2190cSVince Bridgers {
801bbd2190cSVince Bridgers 	struct altera_tse_private *priv = netdev_priv(dev);
802bbd2190cSVince Bridgers 	struct netdev_hw_addr *ha;
8035adb0ed0SMaxime Chevallier 	int i;
804bbd2190cSVince Bridgers 
805bbd2190cSVince Bridgers 	/* clear the hash filter */
806bbd2190cSVince Bridgers 	for (i = 0; i < 64; i++)
80789830580SVince Bridgers 		csrwr32(0, priv->mac_dev, tse_csroffs(hash_table) + i * 4);
808bbd2190cSVince Bridgers 
809bbd2190cSVince Bridgers 	netdev_for_each_mc_addr(ha, dev) {
810bbd2190cSVince Bridgers 		unsigned int hash = 0;
811bbd2190cSVince Bridgers 		int mac_octet;
812bbd2190cSVince Bridgers 
813bbd2190cSVince Bridgers 		for (mac_octet = 5; mac_octet >= 0; mac_octet--) {
814bbd2190cSVince Bridgers 			unsigned char xor_bit = 0;
815bbd2190cSVince Bridgers 			unsigned char octet = ha->addr[mac_octet];
816bbd2190cSVince Bridgers 			unsigned int bitshift;
817bbd2190cSVince Bridgers 
818bbd2190cSVince Bridgers 			for (bitshift = 0; bitshift < 8; bitshift++)
819bbd2190cSVince Bridgers 				xor_bit ^= ((octet >> bitshift) & 0x01);
820bbd2190cSVince Bridgers 
821bbd2190cSVince Bridgers 			hash = (hash << 1) | xor_bit;
822bbd2190cSVince Bridgers 		}
82389830580SVince Bridgers 		csrwr32(1, priv->mac_dev, tse_csroffs(hash_table) + hash * 4);
824bbd2190cSVince Bridgers 	}
825bbd2190cSVince Bridgers }
826bbd2190cSVince Bridgers 
827bbd2190cSVince Bridgers 
altera_tse_set_mcfilterall(struct net_device * dev)828bbd2190cSVince Bridgers static void altera_tse_set_mcfilterall(struct net_device *dev)
829bbd2190cSVince Bridgers {
830bbd2190cSVince Bridgers 	struct altera_tse_private *priv = netdev_priv(dev);
831bbd2190cSVince Bridgers 	int i;
832bbd2190cSVince Bridgers 
833bbd2190cSVince Bridgers 	/* set the hash filter */
834bbd2190cSVince Bridgers 	for (i = 0; i < 64; i++)
83589830580SVince Bridgers 		csrwr32(1, priv->mac_dev, tse_csroffs(hash_table) + i * 4);
836bbd2190cSVince Bridgers }
837bbd2190cSVince Bridgers 
838a5e516d0STom Rix /* Set or clear the multicast filter for this adapter
839bbd2190cSVince Bridgers  */
tse_set_rx_mode_hashfilter(struct net_device * dev)840bbd2190cSVince Bridgers static void tse_set_rx_mode_hashfilter(struct net_device *dev)
841bbd2190cSVince Bridgers {
842bbd2190cSVince Bridgers 	struct altera_tse_private *priv = netdev_priv(dev);
843bbd2190cSVince Bridgers 
844bbd2190cSVince Bridgers 	spin_lock(&priv->mac_cfg_lock);
845bbd2190cSVince Bridgers 
846bbd2190cSVince Bridgers 	if (dev->flags & IFF_PROMISC)
84789830580SVince Bridgers 		tse_set_bit(priv->mac_dev, tse_csroffs(command_config),
84889830580SVince Bridgers 			    MAC_CMDCFG_PROMIS_EN);
849bbd2190cSVince Bridgers 
850bbd2190cSVince Bridgers 	if (dev->flags & IFF_ALLMULTI)
851bbd2190cSVince Bridgers 		altera_tse_set_mcfilterall(dev);
852bbd2190cSVince Bridgers 	else
853bbd2190cSVince Bridgers 		altera_tse_set_mcfilter(dev);
854bbd2190cSVince Bridgers 
855bbd2190cSVince Bridgers 	spin_unlock(&priv->mac_cfg_lock);
856bbd2190cSVince Bridgers }
857bbd2190cSVince Bridgers 
858a5e516d0STom Rix /* Set or clear the multicast filter for this adapter
859bbd2190cSVince Bridgers  */
tse_set_rx_mode(struct net_device * dev)860bbd2190cSVince Bridgers static void tse_set_rx_mode(struct net_device *dev)
861bbd2190cSVince Bridgers {
862bbd2190cSVince Bridgers 	struct altera_tse_private *priv = netdev_priv(dev);
863bbd2190cSVince Bridgers 
864bbd2190cSVince Bridgers 	spin_lock(&priv->mac_cfg_lock);
865bbd2190cSVince Bridgers 
866bbd2190cSVince Bridgers 	if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
867bbd2190cSVince Bridgers 	    !netdev_mc_empty(dev) || !netdev_uc_empty(dev))
86889830580SVince Bridgers 		tse_set_bit(priv->mac_dev, tse_csroffs(command_config),
86989830580SVince Bridgers 			    MAC_CMDCFG_PROMIS_EN);
870bbd2190cSVince Bridgers 	else
87189830580SVince Bridgers 		tse_clear_bit(priv->mac_dev, tse_csroffs(command_config),
87289830580SVince Bridgers 			      MAC_CMDCFG_PROMIS_EN);
873bbd2190cSVince Bridgers 
874bbd2190cSVince Bridgers 	spin_unlock(&priv->mac_cfg_lock);
875bbd2190cSVince Bridgers }
876bbd2190cSVince Bridgers 
877bbd2190cSVince Bridgers /* Open and initialize the interface
878bbd2190cSVince Bridgers  */
tse_open(struct net_device * dev)879bbd2190cSVince Bridgers static int tse_open(struct net_device *dev)
880bbd2190cSVince Bridgers {
881bbd2190cSVince Bridgers 	struct altera_tse_private *priv = netdev_priv(dev);
8825adb0ed0SMaxime Chevallier 	unsigned long flags;
883bbd2190cSVince Bridgers 	int ret = 0;
884bbd2190cSVince Bridgers 	int i;
885bbd2190cSVince Bridgers 
886bbd2190cSVince Bridgers 	/* Reset and configure TSE MAC and probe associated PHY */
887bbd2190cSVince Bridgers 	ret = priv->dmaops->init_dma(priv);
888bbd2190cSVince Bridgers 	if (ret != 0) {
889bbd2190cSVince Bridgers 		netdev_err(dev, "Cannot initialize DMA\n");
890bbd2190cSVince Bridgers 		goto phy_error;
891bbd2190cSVince Bridgers 	}
892bbd2190cSVince Bridgers 
893bbd2190cSVince Bridgers 	if (netif_msg_ifup(priv))
894bbd2190cSVince Bridgers 		netdev_warn(dev, "device MAC address %pM\n",
895bbd2190cSVince Bridgers 			    dev->dev_addr);
896bbd2190cSVince Bridgers 
897bbd2190cSVince Bridgers 	if ((priv->revision < 0xd00) || (priv->revision > 0xe00))
898bbd2190cSVince Bridgers 		netdev_warn(dev, "TSE revision %x\n", priv->revision);
899bbd2190cSVince Bridgers 
900bbd2190cSVince Bridgers 	spin_lock(&priv->mac_cfg_lock);
9013b804564SNeill Whillans 
902bbd2190cSVince Bridgers 	ret = reset_mac(priv);
903ea8860ebSVince Bridgers 	/* Note that reset_mac will fail if the clocks are gated by the PHY
904ea8860ebSVince Bridgers 	 * due to the PHY being put into isolation or power down mode.
905ea8860ebSVince Bridgers 	 * This is not an error if reset fails due to no clock.
906ea8860ebSVince Bridgers 	 */
907bbd2190cSVince Bridgers 	if (ret)
908ea8860ebSVince Bridgers 		netdev_dbg(dev, "Cannot reset MAC core (error: %d)\n", ret);
909bbd2190cSVince Bridgers 
910bbd2190cSVince Bridgers 	ret = init_mac(priv);
911bbd2190cSVince Bridgers 	spin_unlock(&priv->mac_cfg_lock);
912bbd2190cSVince Bridgers 	if (ret) {
913bbd2190cSVince Bridgers 		netdev_err(dev, "Cannot init MAC core (error: %d)\n", ret);
914bbd2190cSVince Bridgers 		goto alloc_skbuf_error;
915bbd2190cSVince Bridgers 	}
916bbd2190cSVince Bridgers 
917bbd2190cSVince Bridgers 	priv->dmaops->reset_dma(priv);
918bbd2190cSVince Bridgers 
919bbd2190cSVince Bridgers 	/* Create and initialize the TX/RX descriptors chains. */
920bbd2190cSVince Bridgers 	priv->rx_ring_size = dma_rx_num;
921bbd2190cSVince Bridgers 	priv->tx_ring_size = dma_tx_num;
922bbd2190cSVince Bridgers 	ret = alloc_init_skbufs(priv);
923bbd2190cSVince Bridgers 	if (ret) {
924bbd2190cSVince Bridgers 		netdev_err(dev, "DMA descriptors initialization failed\n");
925bbd2190cSVince Bridgers 		goto alloc_skbuf_error;
926bbd2190cSVince Bridgers 	}
927bbd2190cSVince Bridgers 
928bbd2190cSVince Bridgers 
929bbd2190cSVince Bridgers 	/* Register RX interrupt */
930bbd2190cSVince Bridgers 	ret = request_irq(priv->rx_irq, altera_isr, IRQF_SHARED,
931bbd2190cSVince Bridgers 			  dev->name, dev);
932bbd2190cSVince Bridgers 	if (ret) {
933bbd2190cSVince Bridgers 		netdev_err(dev, "Unable to register RX interrupt %d\n",
934bbd2190cSVince Bridgers 			   priv->rx_irq);
935bbd2190cSVince Bridgers 		goto init_error;
936bbd2190cSVince Bridgers 	}
937bbd2190cSVince Bridgers 
938bbd2190cSVince Bridgers 	/* Register TX interrupt */
939bbd2190cSVince Bridgers 	ret = request_irq(priv->tx_irq, altera_isr, IRQF_SHARED,
940bbd2190cSVince Bridgers 			  dev->name, dev);
941bbd2190cSVince Bridgers 	if (ret) {
942bbd2190cSVince Bridgers 		netdev_err(dev, "Unable to register TX interrupt %d\n",
943bbd2190cSVince Bridgers 			   priv->tx_irq);
944bbd2190cSVince Bridgers 		goto tx_request_irq_error;
945bbd2190cSVince Bridgers 	}
946bbd2190cSVince Bridgers 
947bbd2190cSVince Bridgers 	/* Enable DMA interrupts */
948bbd2190cSVince Bridgers 	spin_lock_irqsave(&priv->rxdma_irq_lock, flags);
949bbd2190cSVince Bridgers 	priv->dmaops->enable_rxirq(priv);
950bbd2190cSVince Bridgers 	priv->dmaops->enable_txirq(priv);
951bbd2190cSVince Bridgers 
952bbd2190cSVince Bridgers 	/* Setup RX descriptor chain */
953bbd2190cSVince Bridgers 	for (i = 0; i < priv->rx_ring_size; i++)
954bbd2190cSVince Bridgers 		priv->dmaops->add_rx_desc(priv, &priv->rx_ring[i]);
955bbd2190cSVince Bridgers 
956bbd2190cSVince Bridgers 	spin_unlock_irqrestore(&priv->rxdma_irq_lock, flags);
957bbd2190cSVince Bridgers 
958fef29982SMaxime Chevallier 	ret = phylink_of_phy_connect(priv->phylink, priv->device->of_node, 0);
959fef29982SMaxime Chevallier 	if (ret) {
960fef29982SMaxime Chevallier 		netdev_err(dev, "could not connect phylink (%d)\n", ret);
961fef29982SMaxime Chevallier 		goto tx_request_irq_error;
962fef29982SMaxime Chevallier 	}
963fef29982SMaxime Chevallier 	phylink_start(priv->phylink);
964bbd2190cSVince Bridgers 
965bbd2190cSVince Bridgers 	napi_enable(&priv->napi);
966bbd2190cSVince Bridgers 	netif_start_queue(dev);
967bbd2190cSVince Bridgers 
96837c0ffaaSVince Bridgers 	priv->dmaops->start_rxdma(priv);
96937c0ffaaSVince Bridgers 
97037c0ffaaSVince Bridgers 	/* Start MAC Rx/Tx */
97137c0ffaaSVince Bridgers 	spin_lock(&priv->mac_cfg_lock);
97237c0ffaaSVince Bridgers 	tse_set_mac(priv, true);
97337c0ffaaSVince Bridgers 	spin_unlock(&priv->mac_cfg_lock);
97437c0ffaaSVince Bridgers 
975bbd2190cSVince Bridgers 	return 0;
976bbd2190cSVince Bridgers 
977bbd2190cSVince Bridgers tx_request_irq_error:
978bbd2190cSVince Bridgers 	free_irq(priv->rx_irq, dev);
979bbd2190cSVince Bridgers init_error:
980bbd2190cSVince Bridgers 	free_skbufs(dev);
981bbd2190cSVince Bridgers alloc_skbuf_error:
982bbd2190cSVince Bridgers phy_error:
983bbd2190cSVince Bridgers 	return ret;
984bbd2190cSVince Bridgers }
985bbd2190cSVince Bridgers 
986bbd2190cSVince Bridgers /* Stop TSE MAC interface and put the device in an inactive state
987bbd2190cSVince Bridgers  */
tse_shutdown(struct net_device * dev)988bbd2190cSVince Bridgers static int tse_shutdown(struct net_device *dev)
989bbd2190cSVince Bridgers {
990bbd2190cSVince Bridgers 	struct altera_tse_private *priv = netdev_priv(dev);
991bbd2190cSVince Bridgers 	unsigned long int flags;
9925adb0ed0SMaxime Chevallier 	int ret;
993bbd2190cSVince Bridgers 
994fef29982SMaxime Chevallier 	phylink_stop(priv->phylink);
9956aae1bcbSLiu Jian 	phylink_disconnect_phy(priv->phylink);
996bbd2190cSVince Bridgers 	netif_stop_queue(dev);
997bbd2190cSVince Bridgers 	napi_disable(&priv->napi);
998bbd2190cSVince Bridgers 
999bbd2190cSVince Bridgers 	/* Disable DMA interrupts */
1000bbd2190cSVince Bridgers 	spin_lock_irqsave(&priv->rxdma_irq_lock, flags);
1001bbd2190cSVince Bridgers 	priv->dmaops->disable_rxirq(priv);
1002bbd2190cSVince Bridgers 	priv->dmaops->disable_txirq(priv);
1003bbd2190cSVince Bridgers 	spin_unlock_irqrestore(&priv->rxdma_irq_lock, flags);
1004bbd2190cSVince Bridgers 
1005bbd2190cSVince Bridgers 	/* Free the IRQ lines */
1006bbd2190cSVince Bridgers 	free_irq(priv->rx_irq, dev);
1007bbd2190cSVince Bridgers 	free_irq(priv->tx_irq, dev);
1008bbd2190cSVince Bridgers 
1009bbd2190cSVince Bridgers 	/* disable and reset the MAC, empties fifo */
1010bbd2190cSVince Bridgers 	spin_lock(&priv->mac_cfg_lock);
1011bbd2190cSVince Bridgers 	spin_lock(&priv->tx_lock);
1012bbd2190cSVince Bridgers 
1013bbd2190cSVince Bridgers 	ret = reset_mac(priv);
1014ea8860ebSVince Bridgers 	/* Note that reset_mac will fail if the clocks are gated by the PHY
1015ea8860ebSVince Bridgers 	 * due to the PHY being put into isolation or power down mode.
1016ea8860ebSVince Bridgers 	 * This is not an error if reset fails due to no clock.
1017ea8860ebSVince Bridgers 	 */
1018bbd2190cSVince Bridgers 	if (ret)
1019ea8860ebSVince Bridgers 		netdev_dbg(dev, "Cannot reset MAC core (error: %d)\n", ret);
1020bbd2190cSVince Bridgers 	priv->dmaops->reset_dma(priv);
1021bbd2190cSVince Bridgers 	free_skbufs(dev);
1022bbd2190cSVince Bridgers 
1023bbd2190cSVince Bridgers 	spin_unlock(&priv->tx_lock);
1024bbd2190cSVince Bridgers 	spin_unlock(&priv->mac_cfg_lock);
1025bbd2190cSVince Bridgers 
1026bbd2190cSVince Bridgers 	priv->dmaops->uninit_dma(priv);
1027bbd2190cSVince Bridgers 
1028bbd2190cSVince Bridgers 	return 0;
1029bbd2190cSVince Bridgers }
1030bbd2190cSVince Bridgers 
1031bbd2190cSVince Bridgers static struct net_device_ops altera_tse_netdev_ops = {
1032bbd2190cSVince Bridgers 	.ndo_open		= tse_open,
1033bbd2190cSVince Bridgers 	.ndo_stop		= tse_shutdown,
1034bbd2190cSVince Bridgers 	.ndo_start_xmit		= tse_start_xmit,
1035bbd2190cSVince Bridgers 	.ndo_set_mac_address	= eth_mac_addr,
1036bbd2190cSVince Bridgers 	.ndo_set_rx_mode	= tse_set_rx_mode,
1037bbd2190cSVince Bridgers 	.ndo_change_mtu		= tse_change_mtu,
1038bbd2190cSVince Bridgers 	.ndo_validate_addr	= eth_validate_addr,
1039bbd2190cSVince Bridgers };
1040bbd2190cSVince Bridgers 
alt_tse_mac_config(struct phylink_config * config,unsigned int mode,const struct phylink_link_state * state)1041fef29982SMaxime Chevallier static void alt_tse_mac_config(struct phylink_config *config, unsigned int mode,
1042fef29982SMaxime Chevallier 			       const struct phylink_link_state *state)
1043fef29982SMaxime Chevallier {
1044fef29982SMaxime Chevallier 	struct net_device *ndev = to_net_dev(config->dev);
1045fef29982SMaxime Chevallier 	struct altera_tse_private *priv = netdev_priv(ndev);
1046fef29982SMaxime Chevallier 
1047fef29982SMaxime Chevallier 	spin_lock(&priv->mac_cfg_lock);
1048fef29982SMaxime Chevallier 	reset_mac(priv);
1049fef29982SMaxime Chevallier 	tse_set_mac(priv, true);
1050fef29982SMaxime Chevallier 	spin_unlock(&priv->mac_cfg_lock);
1051fef29982SMaxime Chevallier }
1052fef29982SMaxime Chevallier 
alt_tse_mac_link_down(struct phylink_config * config,unsigned int mode,phy_interface_t interface)1053fef29982SMaxime Chevallier static void alt_tse_mac_link_down(struct phylink_config *config,
1054fef29982SMaxime Chevallier 				  unsigned int mode, phy_interface_t interface)
1055fef29982SMaxime Chevallier {
1056fef29982SMaxime Chevallier }
1057fef29982SMaxime Chevallier 
alt_tse_mac_link_up(struct phylink_config * config,struct phy_device * phy,unsigned int mode,phy_interface_t interface,int speed,int duplex,bool tx_pause,bool rx_pause)1058fef29982SMaxime Chevallier static void alt_tse_mac_link_up(struct phylink_config *config,
1059fef29982SMaxime Chevallier 				struct phy_device *phy, unsigned int mode,
1060fef29982SMaxime Chevallier 				phy_interface_t interface, int speed,
1061fef29982SMaxime Chevallier 				int duplex, bool tx_pause, bool rx_pause)
1062fef29982SMaxime Chevallier {
1063fef29982SMaxime Chevallier 	struct net_device *ndev = to_net_dev(config->dev);
1064fef29982SMaxime Chevallier 	struct altera_tse_private *priv = netdev_priv(ndev);
1065fef29982SMaxime Chevallier 	u32 ctrl;
1066fef29982SMaxime Chevallier 
1067fef29982SMaxime Chevallier 	ctrl = csrrd32(priv->mac_dev, tse_csroffs(command_config));
1068fef29982SMaxime Chevallier 	ctrl &= ~(MAC_CMDCFG_ENA_10 | MAC_CMDCFG_ETH_SPEED | MAC_CMDCFG_HD_ENA);
1069fef29982SMaxime Chevallier 
1070fef29982SMaxime Chevallier 	if (duplex == DUPLEX_HALF)
1071fef29982SMaxime Chevallier 		ctrl |= MAC_CMDCFG_HD_ENA;
1072fef29982SMaxime Chevallier 
1073fef29982SMaxime Chevallier 	if (speed == SPEED_1000)
1074fef29982SMaxime Chevallier 		ctrl |= MAC_CMDCFG_ETH_SPEED;
1075fef29982SMaxime Chevallier 	else if (speed == SPEED_10)
1076fef29982SMaxime Chevallier 		ctrl |= MAC_CMDCFG_ENA_10;
1077fef29982SMaxime Chevallier 
1078fef29982SMaxime Chevallier 	spin_lock(&priv->mac_cfg_lock);
1079fef29982SMaxime Chevallier 	csrwr32(ctrl, priv->mac_dev, tse_csroffs(command_config));
1080fef29982SMaxime Chevallier 	spin_unlock(&priv->mac_cfg_lock);
1081fef29982SMaxime Chevallier }
1082fef29982SMaxime Chevallier 
alt_tse_select_pcs(struct phylink_config * config,phy_interface_t interface)1083fef29982SMaxime Chevallier static struct phylink_pcs *alt_tse_select_pcs(struct phylink_config *config,
1084fef29982SMaxime Chevallier 					      phy_interface_t interface)
1085fef29982SMaxime Chevallier {
1086fef29982SMaxime Chevallier 	struct net_device *ndev = to_net_dev(config->dev);
1087fef29982SMaxime Chevallier 	struct altera_tse_private *priv = netdev_priv(ndev);
1088fef29982SMaxime Chevallier 
1089fef29982SMaxime Chevallier 	if (interface == PHY_INTERFACE_MODE_SGMII ||
1090fef29982SMaxime Chevallier 	    interface == PHY_INTERFACE_MODE_1000BASEX)
1091fef29982SMaxime Chevallier 		return priv->pcs;
1092fef29982SMaxime Chevallier 	else
1093fef29982SMaxime Chevallier 		return NULL;
1094fef29982SMaxime Chevallier }
1095fef29982SMaxime Chevallier 
1096fef29982SMaxime Chevallier static const struct phylink_mac_ops alt_tse_phylink_ops = {
1097fef29982SMaxime Chevallier 	.mac_config = alt_tse_mac_config,
1098fef29982SMaxime Chevallier 	.mac_link_down = alt_tse_mac_link_down,
1099fef29982SMaxime Chevallier 	.mac_link_up = alt_tse_mac_link_up,
1100fef29982SMaxime Chevallier 	.mac_select_pcs = alt_tse_select_pcs,
1101fef29982SMaxime Chevallier };
1102fef29982SMaxime Chevallier 
request_and_map(struct platform_device * pdev,const char * name,struct resource ** res,void __iomem ** ptr)1103bbd2190cSVince Bridgers static int request_and_map(struct platform_device *pdev, const char *name,
1104bbd2190cSVince Bridgers 			   struct resource **res, void __iomem **ptr)
1105bbd2190cSVince Bridgers {
1106bbd2190cSVince Bridgers 	struct device *device = &pdev->dev;
11075adb0ed0SMaxime Chevallier 	struct resource *region;
1108bbd2190cSVince Bridgers 
1109bbd2190cSVince Bridgers 	*res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
1110bbd2190cSVince Bridgers 	if (*res == NULL) {
1111bbd2190cSVince Bridgers 		dev_err(device, "resource %s not defined\n", name);
1112bbd2190cSVince Bridgers 		return -ENODEV;
1113bbd2190cSVince Bridgers 	}
1114bbd2190cSVince Bridgers 
1115bbd2190cSVince Bridgers 	region = devm_request_mem_region(device, (*res)->start,
1116bbd2190cSVince Bridgers 					 resource_size(*res), dev_name(device));
1117bbd2190cSVince Bridgers 	if (region == NULL) {
1118bbd2190cSVince Bridgers 		dev_err(device, "unable to request %s\n", name);
1119bbd2190cSVince Bridgers 		return -EBUSY;
1120bbd2190cSVince Bridgers 	}
1121bbd2190cSVince Bridgers 
11224bdc0d67SChristoph Hellwig 	*ptr = devm_ioremap(device, region->start,
1123bbd2190cSVince Bridgers 				    resource_size(region));
1124bbd2190cSVince Bridgers 	if (*ptr == NULL) {
11254bdc0d67SChristoph Hellwig 		dev_err(device, "ioremap of %s failed!", name);
1126bbd2190cSVince Bridgers 		return -ENOMEM;
1127bbd2190cSVince Bridgers 	}
1128bbd2190cSVince Bridgers 
1129bbd2190cSVince Bridgers 	return 0;
1130bbd2190cSVince Bridgers }
1131bbd2190cSVince Bridgers 
1132bbd2190cSVince Bridgers /* Probe Altera TSE MAC device
1133bbd2190cSVince Bridgers  */
altera_tse_probe(struct platform_device * pdev)1134bbd2190cSVince Bridgers static int altera_tse_probe(struct platform_device *pdev)
1135bbd2190cSVince Bridgers {
11365adb0ed0SMaxime Chevallier 	const struct of_device_id *of_id = NULL;
1137db48abbaSMaxime Chevallier 	struct regmap_config pcs_regmap_cfg;
11385adb0ed0SMaxime Chevallier 	struct altera_tse_private *priv;
1139db48abbaSMaxime Chevallier 	struct mdio_regmap_config mrc;
1140bbd2190cSVince Bridgers 	struct resource *control_port;
1141db48abbaSMaxime Chevallier 	struct regmap *pcs_regmap;
1142bbd2190cSVince Bridgers 	struct resource *dma_res;
1143fef29982SMaxime Chevallier 	struct resource *pcs_res;
1144db48abbaSMaxime Chevallier 	struct mii_bus *pcs_bus;
11455adb0ed0SMaxime Chevallier 	struct net_device *ndev;
1146bbd2190cSVince Bridgers 	void __iomem *descmap;
11475adb0ed0SMaxime Chevallier 	int ret = -ENODEV;
1148bbd2190cSVince Bridgers 
1149bbd2190cSVince Bridgers 	ndev = alloc_etherdev(sizeof(struct altera_tse_private));
1150bbd2190cSVince Bridgers 	if (!ndev) {
1151bbd2190cSVince Bridgers 		dev_err(&pdev->dev, "Could not allocate network device\n");
1152bbd2190cSVince Bridgers 		return -ENODEV;
1153bbd2190cSVince Bridgers 	}
1154bbd2190cSVince Bridgers 
1155bbd2190cSVince Bridgers 	SET_NETDEV_DEV(ndev, &pdev->dev);
1156bbd2190cSVince Bridgers 
1157bbd2190cSVince Bridgers 	priv = netdev_priv(ndev);
1158bbd2190cSVince Bridgers 	priv->device = &pdev->dev;
1159bbd2190cSVince Bridgers 	priv->dev = ndev;
1160bbd2190cSVince Bridgers 	priv->msg_enable = netif_msg_init(debug, default_msg_level);
1161bbd2190cSVince Bridgers 
1162bbd2190cSVince Bridgers 	of_id = of_match_device(altera_tse_ids, &pdev->dev);
1163bbd2190cSVince Bridgers 
1164bbd2190cSVince Bridgers 	if (of_id)
1165bbd2190cSVince Bridgers 		priv->dmaops = (struct altera_dmaops *)of_id->data;
1166bbd2190cSVince Bridgers 
1167bbd2190cSVince Bridgers 
1168bbd2190cSVince Bridgers 	if (priv->dmaops &&
1169bbd2190cSVince Bridgers 	    priv->dmaops->altera_dtype == ALTERA_DTYPE_SGDMA) {
1170bbd2190cSVince Bridgers 		/* Get the mapped address to the SGDMA descriptor memory */
1171bbd2190cSVince Bridgers 		ret = request_and_map(pdev, "s1", &dma_res, &descmap);
1172bbd2190cSVince Bridgers 		if (ret)
1173a7642009SVince Bridgers 			goto err_free_netdev;
1174bbd2190cSVince Bridgers 
1175bbd2190cSVince Bridgers 		/* Start of that memory is for transmit descriptors */
1176bbd2190cSVince Bridgers 		priv->tx_dma_desc = descmap;
1177bbd2190cSVince Bridgers 
1178bbd2190cSVince Bridgers 		/* First half is for tx descriptors, other half for tx */
1179bbd2190cSVince Bridgers 		priv->txdescmem = resource_size(dma_res)/2;
1180bbd2190cSVince Bridgers 
1181bbd2190cSVince Bridgers 		priv->txdescmem_busaddr = (dma_addr_t)dma_res->start;
1182bbd2190cSVince Bridgers 
1183bbd2190cSVince Bridgers 		priv->rx_dma_desc = (void __iomem *)((uintptr_t)(descmap +
1184bbd2190cSVince Bridgers 						     priv->txdescmem));
1185bbd2190cSVince Bridgers 		priv->rxdescmem = resource_size(dma_res)/2;
1186bbd2190cSVince Bridgers 		priv->rxdescmem_busaddr = dma_res->start;
1187bbd2190cSVince Bridgers 		priv->rxdescmem_busaddr += priv->txdescmem;
1188bbd2190cSVince Bridgers 
1189bbd2190cSVince Bridgers 		if (upper_32_bits(priv->rxdescmem_busaddr)) {
1190bbd2190cSVince Bridgers 			dev_dbg(priv->device,
1191bbd2190cSVince Bridgers 				"SGDMA bus addresses greater than 32-bits\n");
1192a24a9d7aSWei Yongjun 			ret = -EINVAL;
1193a7642009SVince Bridgers 			goto err_free_netdev;
1194bbd2190cSVince Bridgers 		}
1195bbd2190cSVince Bridgers 		if (upper_32_bits(priv->txdescmem_busaddr)) {
1196bbd2190cSVince Bridgers 			dev_dbg(priv->device,
1197bbd2190cSVince Bridgers 				"SGDMA bus addresses greater than 32-bits\n");
1198a24a9d7aSWei Yongjun 			ret = -EINVAL;
1199a7642009SVince Bridgers 			goto err_free_netdev;
1200bbd2190cSVince Bridgers 		}
1201bbd2190cSVince Bridgers 	} else if (priv->dmaops &&
1202bbd2190cSVince Bridgers 		   priv->dmaops->altera_dtype == ALTERA_DTYPE_MSGDMA) {
1203bbd2190cSVince Bridgers 		ret = request_and_map(pdev, "rx_resp", &dma_res,
1204bbd2190cSVince Bridgers 				      &priv->rx_dma_resp);
1205bbd2190cSVince Bridgers 		if (ret)
1206a7642009SVince Bridgers 			goto err_free_netdev;
1207bbd2190cSVince Bridgers 
1208bbd2190cSVince Bridgers 		ret = request_and_map(pdev, "tx_desc", &dma_res,
1209bbd2190cSVince Bridgers 				      &priv->tx_dma_desc);
1210bbd2190cSVince Bridgers 		if (ret)
1211a7642009SVince Bridgers 			goto err_free_netdev;
1212bbd2190cSVince Bridgers 
1213bbd2190cSVince Bridgers 		priv->txdescmem = resource_size(dma_res);
1214bbd2190cSVince Bridgers 		priv->txdescmem_busaddr = dma_res->start;
1215bbd2190cSVince Bridgers 
1216bbd2190cSVince Bridgers 		ret = request_and_map(pdev, "rx_desc", &dma_res,
1217bbd2190cSVince Bridgers 				      &priv->rx_dma_desc);
1218bbd2190cSVince Bridgers 		if (ret)
1219a7642009SVince Bridgers 			goto err_free_netdev;
1220bbd2190cSVince Bridgers 
1221bbd2190cSVince Bridgers 		priv->rxdescmem = resource_size(dma_res);
1222bbd2190cSVince Bridgers 		priv->rxdescmem_busaddr = dma_res->start;
1223bbd2190cSVince Bridgers 
1224bbd2190cSVince Bridgers 	} else {
1225badd7857SDan Carpenter 		ret = -ENODEV;
1226a7642009SVince Bridgers 		goto err_free_netdev;
1227bbd2190cSVince Bridgers 	}
1228bbd2190cSVince Bridgers 
1229badd7857SDan Carpenter 	if (!dma_set_mask(priv->device, DMA_BIT_MASK(priv->dmaops->dmamask))) {
1230bbd2190cSVince Bridgers 		dma_set_coherent_mask(priv->device,
1231bbd2190cSVince Bridgers 				      DMA_BIT_MASK(priv->dmaops->dmamask));
1232badd7857SDan Carpenter 	} else if (!dma_set_mask(priv->device, DMA_BIT_MASK(32))) {
1233bbd2190cSVince Bridgers 		dma_set_coherent_mask(priv->device, DMA_BIT_MASK(32));
1234badd7857SDan Carpenter 	} else {
1235badd7857SDan Carpenter 		ret = -EIO;
1236a7642009SVince Bridgers 		goto err_free_netdev;
1237badd7857SDan Carpenter 	}
1238bbd2190cSVince Bridgers 
1239bbd2190cSVince Bridgers 	/* MAC address space */
1240bbd2190cSVince Bridgers 	ret = request_and_map(pdev, "control_port", &control_port,
1241bbd2190cSVince Bridgers 			      (void __iomem **)&priv->mac_dev);
1242bbd2190cSVince Bridgers 	if (ret)
1243a7642009SVince Bridgers 		goto err_free_netdev;
1244bbd2190cSVince Bridgers 
1245bbd2190cSVince Bridgers 	/* xSGDMA Rx Dispatcher address space */
1246bbd2190cSVince Bridgers 	ret = request_and_map(pdev, "rx_csr", &dma_res,
1247bbd2190cSVince Bridgers 			      &priv->rx_dma_csr);
1248bbd2190cSVince Bridgers 	if (ret)
1249a7642009SVince Bridgers 		goto err_free_netdev;
1250bbd2190cSVince Bridgers 
1251bbd2190cSVince Bridgers 
1252bbd2190cSVince Bridgers 	/* xSGDMA Tx Dispatcher address space */
1253bbd2190cSVince Bridgers 	ret = request_and_map(pdev, "tx_csr", &dma_res,
1254bbd2190cSVince Bridgers 			      &priv->tx_dma_csr);
1255bbd2190cSVince Bridgers 	if (ret)
1256a7642009SVince Bridgers 		goto err_free_netdev;
1257bbd2190cSVince Bridgers 
12582d830f7aSMaxime Chevallier 	memset(&pcs_regmap_cfg, 0, sizeof(pcs_regmap_cfg));
12592d830f7aSMaxime Chevallier 	memset(&mrc, 0, sizeof(mrc));
1260fef29982SMaxime Chevallier 	/* SGMII PCS address space. The location can vary depending on how the
1261fef29982SMaxime Chevallier 	 * IP is integrated. We can have a resource dedicated to it at a specific
1262fef29982SMaxime Chevallier 	 * address space, but if it's not the case, we fallback to the mdiophy0
1263fef29982SMaxime Chevallier 	 * from the MAC's address space
1264fef29982SMaxime Chevallier 	 */
1265db48abbaSMaxime Chevallier 	ret = request_and_map(pdev, "pcs", &pcs_res, &priv->pcs_base);
1266fef29982SMaxime Chevallier 	if (ret) {
1267db48abbaSMaxime Chevallier 		/* If we can't find a dedicated resource for the PCS, fallback
1268db48abbaSMaxime Chevallier 		 * to the internal PCS, that has a different address stride
1269db48abbaSMaxime Chevallier 		 */
1270fef29982SMaxime Chevallier 		priv->pcs_base = priv->mac_dev + tse_csroffs(mdio_phy0);
1271db48abbaSMaxime Chevallier 		pcs_regmap_cfg.reg_bits = 32;
1272db48abbaSMaxime Chevallier 		/* Values are MDIO-like values, on 16 bits */
1273db48abbaSMaxime Chevallier 		pcs_regmap_cfg.val_bits = 16;
1274db48abbaSMaxime Chevallier 		pcs_regmap_cfg.reg_shift = REGMAP_UPSHIFT(2);
1275db48abbaSMaxime Chevallier 	} else {
1276db48abbaSMaxime Chevallier 		pcs_regmap_cfg.reg_bits = 16;
1277db48abbaSMaxime Chevallier 		pcs_regmap_cfg.val_bits = 16;
1278db48abbaSMaxime Chevallier 		pcs_regmap_cfg.reg_shift = REGMAP_UPSHIFT(1);
1279fef29982SMaxime Chevallier 	}
1280bbd2190cSVince Bridgers 
1281db48abbaSMaxime Chevallier 	/* Create a regmap for the PCS so that it can be used by the PCS driver */
1282db48abbaSMaxime Chevallier 	pcs_regmap = devm_regmap_init_mmio(&pdev->dev, priv->pcs_base,
1283db48abbaSMaxime Chevallier 					   &pcs_regmap_cfg);
1284db48abbaSMaxime Chevallier 	if (IS_ERR(pcs_regmap)) {
1285db48abbaSMaxime Chevallier 		ret = PTR_ERR(pcs_regmap);
1286db48abbaSMaxime Chevallier 		goto err_free_netdev;
1287db48abbaSMaxime Chevallier 	}
1288db48abbaSMaxime Chevallier 	mrc.regmap = pcs_regmap;
1289db48abbaSMaxime Chevallier 	mrc.parent = &pdev->dev;
1290db48abbaSMaxime Chevallier 	mrc.valid_addr = 0x0;
1291*fa19a5d9SMaxime Chevallier 	mrc.autoscan = false;
1292db48abbaSMaxime Chevallier 
1293bbd2190cSVince Bridgers 	/* Rx IRQ */
1294bbd2190cSVince Bridgers 	priv->rx_irq = platform_get_irq_byname(pdev, "rx_irq");
1295bbd2190cSVince Bridgers 	if (priv->rx_irq == -ENXIO) {
1296bbd2190cSVince Bridgers 		dev_err(&pdev->dev, "cannot obtain Rx IRQ\n");
1297bbd2190cSVince Bridgers 		ret = -ENXIO;
1298a7642009SVince Bridgers 		goto err_free_netdev;
1299bbd2190cSVince Bridgers 	}
1300bbd2190cSVince Bridgers 
1301bbd2190cSVince Bridgers 	/* Tx IRQ */
1302bbd2190cSVince Bridgers 	priv->tx_irq = platform_get_irq_byname(pdev, "tx_irq");
1303bbd2190cSVince Bridgers 	if (priv->tx_irq == -ENXIO) {
1304bbd2190cSVince Bridgers 		dev_err(&pdev->dev, "cannot obtain Tx IRQ\n");
1305bbd2190cSVince Bridgers 		ret = -ENXIO;
1306a7642009SVince Bridgers 		goto err_free_netdev;
1307bbd2190cSVince Bridgers 	}
1308bbd2190cSVince Bridgers 
1309bbd2190cSVince Bridgers 	/* get FIFO depths from device tree */
1310bbd2190cSVince Bridgers 	if (of_property_read_u32(pdev->dev.of_node, "rx-fifo-depth",
1311bbd2190cSVince Bridgers 				 &priv->rx_fifo_depth)) {
1312bbd2190cSVince Bridgers 		dev_err(&pdev->dev, "cannot obtain rx-fifo-depth\n");
1313bbd2190cSVince Bridgers 		ret = -ENXIO;
1314a7642009SVince Bridgers 		goto err_free_netdev;
1315bbd2190cSVince Bridgers 	}
1316bbd2190cSVince Bridgers 
1317bbd2190cSVince Bridgers 	if (of_property_read_u32(pdev->dev.of_node, "tx-fifo-depth",
1318fe6e4081SVlastimil Setka 				 &priv->tx_fifo_depth)) {
1319bbd2190cSVince Bridgers 		dev_err(&pdev->dev, "cannot obtain tx-fifo-depth\n");
1320bbd2190cSVince Bridgers 		ret = -ENXIO;
1321a7642009SVince Bridgers 		goto err_free_netdev;
1322bbd2190cSVince Bridgers 	}
1323bbd2190cSVince Bridgers 
1324bbd2190cSVince Bridgers 	/* get hash filter settings for this instance */
1325bbd2190cSVince Bridgers 	priv->hash_filter =
1326bbd2190cSVince Bridgers 		of_property_read_bool(pdev->dev.of_node,
1327bbd2190cSVince Bridgers 				      "altr,has-hash-multicast-filter");
1328bbd2190cSVince Bridgers 
1329d91e5c02SVince Bridgers 	/* Set hash filter to not set for now until the
1330d91e5c02SVince Bridgers 	 * multicast filter receive issue is debugged
1331d91e5c02SVince Bridgers 	 */
1332d91e5c02SVince Bridgers 	priv->hash_filter = 0;
1333d91e5c02SVince Bridgers 
1334bbd2190cSVince Bridgers 	/* get supplemental address settings for this instance */
1335bbd2190cSVince Bridgers 	priv->added_unicast =
1336bbd2190cSVince Bridgers 		of_property_read_bool(pdev->dev.of_node,
1337bbd2190cSVince Bridgers 				      "altr,has-supplementary-unicast");
1338bbd2190cSVince Bridgers 
133944770e11SJarod Wilson 	priv->dev->min_mtu = ETH_ZLEN + ETH_FCS_LEN;
1340bbd2190cSVince Bridgers 	/* Max MTU is 1500, ETH_DATA_LEN */
134144770e11SJarod Wilson 	priv->dev->max_mtu = ETH_DATA_LEN;
1342bbd2190cSVince Bridgers 
1343bbd2190cSVince Bridgers 	/* Get the max mtu from the device tree. Note that the
1344bbd2190cSVince Bridgers 	 * "max-frame-size" parameter is actually max mtu. Definition
1345bbd2190cSVince Bridgers 	 * in the ePAPR v1.1 spec and usage differ, so go with usage.
1346bbd2190cSVince Bridgers 	 */
1347bbd2190cSVince Bridgers 	of_property_read_u32(pdev->dev.of_node, "max-frame-size",
134844770e11SJarod Wilson 			     &priv->dev->max_mtu);
1349bbd2190cSVince Bridgers 
1350bbd2190cSVince Bridgers 	/* The DMA buffer size already accounts for an alignment bias
1351bbd2190cSVince Bridgers 	 * to avoid unaligned access exceptions for the NIOS processor,
1352bbd2190cSVince Bridgers 	 */
1353bbd2190cSVince Bridgers 	priv->rx_dma_buf_sz = ALTERA_RXDMABUFFER_SIZE;
1354bbd2190cSVince Bridgers 
1355bbd2190cSVince Bridgers 	/* get default MAC address from device tree */
13569ca01b25SJakub Kicinski 	ret = of_get_ethdev_address(pdev->dev.of_node, ndev);
135783216e39SMichael Walle 	if (ret)
1358bbd2190cSVince Bridgers 		eth_hw_addr_random(ndev);
1359bbd2190cSVince Bridgers 
1360004fa118SWalter Lozano 	/* get phy addr and create mdio */
1361004fa118SWalter Lozano 	ret = altera_tse_phy_get_addr_mdio_create(ndev);
1362bbd2190cSVince Bridgers 
1363bbd2190cSVince Bridgers 	if (ret)
1364a7642009SVince Bridgers 		goto err_free_netdev;
1365bbd2190cSVince Bridgers 
1366bbd2190cSVince Bridgers 	/* initialize netdev */
1367bbd2190cSVince Bridgers 	ndev->mem_start = control_port->start;
1368bbd2190cSVince Bridgers 	ndev->mem_end = control_port->end;
1369bbd2190cSVince Bridgers 	ndev->netdev_ops = &altera_tse_netdev_ops;
1370bbd2190cSVince Bridgers 	altera_tse_set_ethtool_ops(ndev);
1371bbd2190cSVince Bridgers 
1372bbd2190cSVince Bridgers 	altera_tse_netdev_ops.ndo_set_rx_mode = tse_set_rx_mode;
1373bbd2190cSVince Bridgers 
1374bbd2190cSVince Bridgers 	if (priv->hash_filter)
1375bbd2190cSVince Bridgers 		altera_tse_netdev_ops.ndo_set_rx_mode =
1376bbd2190cSVince Bridgers 			tse_set_rx_mode_hashfilter;
1377bbd2190cSVince Bridgers 
1378bbd2190cSVince Bridgers 	/* Scatter/gather IO is not supported,
1379bbd2190cSVince Bridgers 	 * so it is turned off
1380bbd2190cSVince Bridgers 	 */
1381bbd2190cSVince Bridgers 	ndev->hw_features &= ~NETIF_F_SG;
1382bbd2190cSVince Bridgers 	ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
1383bbd2190cSVince Bridgers 
1384bbd2190cSVince Bridgers 	/* VLAN offloading of tagging, stripping and filtering is not
1385bbd2190cSVince Bridgers 	 * supported by hardware, but driver will accommodate the
1386bbd2190cSVince Bridgers 	 * extra 4-byte VLAN tag for processing by upper layers
1387bbd2190cSVince Bridgers 	 */
1388bbd2190cSVince Bridgers 	ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
1389bbd2190cSVince Bridgers 
1390bbd2190cSVince Bridgers 	/* setup NAPI interface */
1391b48b89f9SJakub Kicinski 	netif_napi_add(ndev, &priv->napi, tse_poll);
1392bbd2190cSVince Bridgers 
1393bbd2190cSVince Bridgers 	spin_lock_init(&priv->mac_cfg_lock);
1394bbd2190cSVince Bridgers 	spin_lock_init(&priv->tx_lock);
1395bbd2190cSVince Bridgers 	spin_lock_init(&priv->rxdma_irq_lock);
1396bbd2190cSVince Bridgers 
1397d43cefcdSAtsushi Nemoto 	netif_carrier_off(ndev);
1398bbd2190cSVince Bridgers 	ret = register_netdev(ndev);
1399bbd2190cSVince Bridgers 	if (ret) {
1400bbd2190cSVince Bridgers 		dev_err(&pdev->dev, "failed to register TSE net device\n");
1401a7642009SVince Bridgers 		goto err_register_netdev;
1402bbd2190cSVince Bridgers 	}
1403bbd2190cSVince Bridgers 
1404bbd2190cSVince Bridgers 	platform_set_drvdata(pdev, ndev);
1405bbd2190cSVince Bridgers 
1406bbd2190cSVince Bridgers 	priv->revision = ioread32(&priv->mac_dev->megacore_revision);
1407bbd2190cSVince Bridgers 
1408bbd2190cSVince Bridgers 	if (netif_msg_probe(priv))
1409bbd2190cSVince Bridgers 		dev_info(&pdev->dev, "Altera TSE MAC version %d.%d at 0x%08lx irq %d/%d\n",
1410bbd2190cSVince Bridgers 			 (priv->revision >> 8) & 0xff,
1411bbd2190cSVince Bridgers 			 priv->revision & 0xff,
1412bbd2190cSVince Bridgers 			 (unsigned long) control_port->start, priv->rx_irq,
1413bbd2190cSVince Bridgers 			 priv->tx_irq);
1414bbd2190cSVince Bridgers 
1415db48abbaSMaxime Chevallier 	snprintf(mrc.name, MII_BUS_ID_SIZE, "%s-pcs-mii", ndev->name);
1416db48abbaSMaxime Chevallier 	pcs_bus = devm_mdio_regmap_register(&pdev->dev, &mrc);
1417db48abbaSMaxime Chevallier 	if (IS_ERR(pcs_bus)) {
1418db48abbaSMaxime Chevallier 		ret = PTR_ERR(pcs_bus);
1419db48abbaSMaxime Chevallier 		goto err_init_pcs;
1420db48abbaSMaxime Chevallier 	}
1421db48abbaSMaxime Chevallier 
1422db48abbaSMaxime Chevallier 	priv->pcs = lynx_pcs_create_mdiodev(pcs_bus, 0);
1423db48abbaSMaxime Chevallier 	if (IS_ERR(priv->pcs)) {
1424db48abbaSMaxime Chevallier 		ret = PTR_ERR(priv->pcs);
1425db48abbaSMaxime Chevallier 		goto err_init_pcs;
1426db48abbaSMaxime Chevallier 	}
1427fef29982SMaxime Chevallier 
1428fef29982SMaxime Chevallier 	priv->phylink_config.dev = &ndev->dev;
1429fef29982SMaxime Chevallier 	priv->phylink_config.type = PHYLINK_NETDEV;
1430fef29982SMaxime Chevallier 	priv->phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_10 |
1431fef29982SMaxime Chevallier 						MAC_100 | MAC_1000FD;
1432fef29982SMaxime Chevallier 
1433fef29982SMaxime Chevallier 	phy_interface_set_rgmii(priv->phylink_config.supported_interfaces);
1434fef29982SMaxime Chevallier 	__set_bit(PHY_INTERFACE_MODE_MII,
1435fef29982SMaxime Chevallier 		  priv->phylink_config.supported_interfaces);
1436fef29982SMaxime Chevallier 	__set_bit(PHY_INTERFACE_MODE_GMII,
1437fef29982SMaxime Chevallier 		  priv->phylink_config.supported_interfaces);
1438fef29982SMaxime Chevallier 	__set_bit(PHY_INTERFACE_MODE_SGMII,
1439fef29982SMaxime Chevallier 		  priv->phylink_config.supported_interfaces);
1440fef29982SMaxime Chevallier 	__set_bit(PHY_INTERFACE_MODE_1000BASEX,
1441fef29982SMaxime Chevallier 		  priv->phylink_config.supported_interfaces);
1442fef29982SMaxime Chevallier 
1443fef29982SMaxime Chevallier 	priv->phylink = phylink_create(&priv->phylink_config,
1444fef29982SMaxime Chevallier 				       of_fwnode_handle(priv->device->of_node),
1445fef29982SMaxime Chevallier 				       priv->phy_iface, &alt_tse_phylink_ops);
1446fef29982SMaxime Chevallier 	if (IS_ERR(priv->phylink)) {
1447fef29982SMaxime Chevallier 		dev_err(&pdev->dev, "failed to create phylink\n");
14489b17dbd9SSun Ke 		ret = PTR_ERR(priv->phylink);
1449db48abbaSMaxime Chevallier 		goto err_init_phylink;
1450bbd2190cSVince Bridgers 	}
1451fef29982SMaxime Chevallier 
1452bbd2190cSVince Bridgers 	return 0;
1453db48abbaSMaxime Chevallier err_init_phylink:
1454db48abbaSMaxime Chevallier 	lynx_pcs_destroy(priv->pcs);
1455db48abbaSMaxime Chevallier err_init_pcs:
1456a7642009SVince Bridgers 	unregister_netdev(ndev);
1457a7642009SVince Bridgers err_register_netdev:
1458a7642009SVince Bridgers 	netif_napi_del(&priv->napi);
1459bbd2190cSVince Bridgers 	altera_tse_mdio_destroy(ndev);
1460a7642009SVince Bridgers err_free_netdev:
1461bbd2190cSVince Bridgers 	free_netdev(ndev);
1462bbd2190cSVince Bridgers 	return ret;
1463bbd2190cSVince Bridgers }
1464bbd2190cSVince Bridgers 
1465bbd2190cSVince Bridgers /* Remove Altera TSE MAC device
1466bbd2190cSVince Bridgers  */
altera_tse_remove(struct platform_device * pdev)1467bbd2190cSVince Bridgers static int altera_tse_remove(struct platform_device *pdev)
1468bbd2190cSVince Bridgers {
1469bbd2190cSVince Bridgers 	struct net_device *ndev = platform_get_drvdata(pdev);
14705a89394aSJohan Hovold 	struct altera_tse_private *priv = netdev_priv(ndev);
1471c484994eSKostya Belezko 
1472bbd2190cSVince Bridgers 	platform_set_drvdata(pdev, NULL);
1473bbd2190cSVince Bridgers 	altera_tse_mdio_destroy(ndev);
1474bbd2190cSVince Bridgers 	unregister_netdev(ndev);
1475fef29982SMaxime Chevallier 	phylink_destroy(priv->phylink);
1476db48abbaSMaxime Chevallier 	lynx_pcs_destroy(priv->pcs);
1477db48abbaSMaxime Chevallier 
1478bbd2190cSVince Bridgers 	free_netdev(ndev);
1479bbd2190cSVince Bridgers 
1480bbd2190cSVince Bridgers 	return 0;
1481bbd2190cSVince Bridgers }
1482bbd2190cSVince Bridgers 
148389830580SVince Bridgers static const struct altera_dmaops altera_dtype_sgdma = {
1484bbd2190cSVince Bridgers 	.altera_dtype = ALTERA_DTYPE_SGDMA,
1485bbd2190cSVince Bridgers 	.dmamask = 32,
1486bbd2190cSVince Bridgers 	.reset_dma = sgdma_reset,
1487bbd2190cSVince Bridgers 	.enable_txirq = sgdma_enable_txirq,
1488bbd2190cSVince Bridgers 	.enable_rxirq = sgdma_enable_rxirq,
1489bbd2190cSVince Bridgers 	.disable_txirq = sgdma_disable_txirq,
1490bbd2190cSVince Bridgers 	.disable_rxirq = sgdma_disable_rxirq,
1491bbd2190cSVince Bridgers 	.clear_txirq = sgdma_clear_txirq,
1492bbd2190cSVince Bridgers 	.clear_rxirq = sgdma_clear_rxirq,
1493bbd2190cSVince Bridgers 	.tx_buffer = sgdma_tx_buffer,
1494bbd2190cSVince Bridgers 	.tx_completions = sgdma_tx_completions,
1495bbd2190cSVince Bridgers 	.add_rx_desc = sgdma_add_rx_desc,
1496bbd2190cSVince Bridgers 	.get_rx_status = sgdma_rx_status,
1497bbd2190cSVince Bridgers 	.init_dma = sgdma_initialize,
1498bbd2190cSVince Bridgers 	.uninit_dma = sgdma_uninitialize,
149937c0ffaaSVince Bridgers 	.start_rxdma = sgdma_start_rxdma,
1500bbd2190cSVince Bridgers };
1501bbd2190cSVince Bridgers 
150289830580SVince Bridgers static const struct altera_dmaops altera_dtype_msgdma = {
1503bbd2190cSVince Bridgers 	.altera_dtype = ALTERA_DTYPE_MSGDMA,
1504bbd2190cSVince Bridgers 	.dmamask = 64,
1505bbd2190cSVince Bridgers 	.reset_dma = msgdma_reset,
1506bbd2190cSVince Bridgers 	.enable_txirq = msgdma_enable_txirq,
1507bbd2190cSVince Bridgers 	.enable_rxirq = msgdma_enable_rxirq,
1508bbd2190cSVince Bridgers 	.disable_txirq = msgdma_disable_txirq,
1509bbd2190cSVince Bridgers 	.disable_rxirq = msgdma_disable_rxirq,
1510bbd2190cSVince Bridgers 	.clear_txirq = msgdma_clear_txirq,
1511bbd2190cSVince Bridgers 	.clear_rxirq = msgdma_clear_rxirq,
1512bbd2190cSVince Bridgers 	.tx_buffer = msgdma_tx_buffer,
1513bbd2190cSVince Bridgers 	.tx_completions = msgdma_tx_completions,
1514bbd2190cSVince Bridgers 	.add_rx_desc = msgdma_add_rx_desc,
1515bbd2190cSVince Bridgers 	.get_rx_status = msgdma_rx_status,
1516bbd2190cSVince Bridgers 	.init_dma = msgdma_initialize,
1517bbd2190cSVince Bridgers 	.uninit_dma = msgdma_uninitialize,
151837c0ffaaSVince Bridgers 	.start_rxdma = msgdma_start_rxdma,
1519bbd2190cSVince Bridgers };
1520bbd2190cSVince Bridgers 
152127260530SFabian Frederick static const struct of_device_id altera_tse_ids[] = {
1522bbd2190cSVince Bridgers 	{ .compatible = "altr,tse-msgdma-1.0", .data = &altera_dtype_msgdma, },
1523bbd2190cSVince Bridgers 	{ .compatible = "altr,tse-1.0", .data = &altera_dtype_sgdma, },
1524bbd2190cSVince Bridgers 	{ .compatible = "ALTR,tse-1.0", .data = &altera_dtype_sgdma, },
1525bbd2190cSVince Bridgers 	{},
1526bbd2190cSVince Bridgers };
1527bbd2190cSVince Bridgers MODULE_DEVICE_TABLE(of, altera_tse_ids);
1528bbd2190cSVince Bridgers 
1529bbd2190cSVince Bridgers static struct platform_driver altera_tse_driver = {
1530bbd2190cSVince Bridgers 	.probe		= altera_tse_probe,
1531bbd2190cSVince Bridgers 	.remove		= altera_tse_remove,
1532bbd2190cSVince Bridgers 	.suspend	= NULL,
1533bbd2190cSVince Bridgers 	.resume		= NULL,
1534bbd2190cSVince Bridgers 	.driver		= {
1535bbd2190cSVince Bridgers 		.name	= ALTERA_TSE_RESOURCE_NAME,
1536bbd2190cSVince Bridgers 		.of_match_table = altera_tse_ids,
1537bbd2190cSVince Bridgers 	},
1538bbd2190cSVince Bridgers };
1539bbd2190cSVince Bridgers 
1540bbd2190cSVince Bridgers module_platform_driver(altera_tse_driver);
1541bbd2190cSVince Bridgers 
1542bbd2190cSVince Bridgers MODULE_AUTHOR("Altera Corporation");
1543bbd2190cSVince Bridgers MODULE_DESCRIPTION("Altera Triple Speed Ethernet MAC driver");
1544bbd2190cSVince Bridgers MODULE_LICENSE("GPL v2");
1545