blob: d8bca949d810d790d1cdc58e70de481fb7ed960a [file] [log] [blame]
Vince Bridgersbbd21902014-03-17 17:52:38 -05001/* Altera Triple-Speed Ethernet MAC driver
2 * Copyright (C) 2008-2014 Altera Corporation. All rights reserved
3 *
4 * Contributors:
5 * Dalon Westergreen
6 * Thomas Chou
7 * Ian Abbott
8 * Yuriy Kozlov
9 * Tobias Klauser
10 * Andriy Smolskyy
11 * Roman Bulgakov
12 * Dmytro Mytarchuk
13 * Matthew Gerlach
14 *
15 * Original driver contributed by SLS.
16 * Major updates contributed by GlobalLogic
17 *
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms and conditions of the GNU General Public License,
20 * version 2, as published by the Free Software Foundation.
21 *
22 * This program is distributed in the hope it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
25 * more details.
26 *
27 * You should have received a copy of the GNU General Public License along with
28 * this program. If not, see <http://www.gnu.org/licenses/>.
29 */
30
31#include <linux/atomic.h>
32#include <linux/delay.h>
33#include <linux/etherdevice.h>
34#include <linux/if_vlan.h>
35#include <linux/init.h>
36#include <linux/interrupt.h>
37#include <linux/io.h>
38#include <linux/kernel.h>
39#include <linux/module.h>
Neill Whillans3b804562016-11-30 13:41:05 +000040#include <linux/mii.h>
Vince Bridgersbbd21902014-03-17 17:52:38 -050041#include <linux/netdevice.h>
42#include <linux/of_device.h>
43#include <linux/of_mdio.h>
44#include <linux/of_net.h>
45#include <linux/of_platform.h>
46#include <linux/phy.h>
47#include <linux/platform_device.h>
48#include <linux/skbuff.h>
49#include <asm/cacheflush.h>
50
51#include "altera_utils.h"
52#include "altera_tse.h"
53#include "altera_sgdma.h"
54#include "altera_msgdma.h"
55
56static atomic_t instance_count = ATOMIC_INIT(~0);
57/* Module parameters */
58static int debug = -1;
59module_param(debug, int, S_IRUGO | S_IWUSR);
60MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
61
62static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
63 NETIF_MSG_LINK | NETIF_MSG_IFUP |
64 NETIF_MSG_IFDOWN);
65
66#define RX_DESCRIPTORS 64
67static int dma_rx_num = RX_DESCRIPTORS;
68module_param(dma_rx_num, int, S_IRUGO | S_IWUSR);
69MODULE_PARM_DESC(dma_rx_num, "Number of descriptors in the RX list");
70
71#define TX_DESCRIPTORS 64
72static int dma_tx_num = TX_DESCRIPTORS;
73module_param(dma_tx_num, int, S_IRUGO | S_IWUSR);
74MODULE_PARM_DESC(dma_tx_num, "Number of descriptors in the TX list");
75
76
77#define POLL_PHY (-1)
78
79/* Make sure DMA buffer size is larger than the max frame size
80 * plus some alignment offset and a VLAN header. If the max frame size is
81 * 1518, a VLAN header would be additional 4 bytes and additional
82 * headroom for alignment is 2 bytes, 2048 is just fine.
83 */
84#define ALTERA_RXDMABUFFER_SIZE 2048
85
86/* Allow network stack to resume queueing packets after we've
87 * finished transmitting at least 1/4 of the packets in the queue.
88 */
89#define TSE_TX_THRESH(x) (x->tx_ring_size / 4)
90
91#define TXQUEUESTOP_THRESHHOLD 2
92
Fabian Frederick27260532015-03-17 19:37:33 +010093static const struct of_device_id altera_tse_ids[];
Vince Bridgersbbd21902014-03-17 17:52:38 -050094
95static inline u32 tse_tx_avail(struct altera_tse_private *priv)
96{
97 return priv->tx_cons + priv->tx_ring_size - priv->tx_prod - 1;
98}
99
Neill Whillans3b804562016-11-30 13:41:05 +0000100/* PCS Register read/write functions
101 */
102static u16 sgmii_pcs_read(struct altera_tse_private *priv, int regnum)
103{
104 return csrrd32(priv->mac_dev,
105 tse_csroffs(mdio_phy0) + regnum * 4) & 0xffff;
106}
107
108static void sgmii_pcs_write(struct altera_tse_private *priv, int regnum,
109 u16 value)
110{
111 csrwr32(value, priv->mac_dev, tse_csroffs(mdio_phy0) + regnum * 4);
112}
113
114/* Check PCS scratch memory */
115static int sgmii_pcs_scratch_test(struct altera_tse_private *priv, u16 value)
116{
117 sgmii_pcs_write(priv, SGMII_PCS_SCRATCH, value);
118 return (sgmii_pcs_read(priv, SGMII_PCS_SCRATCH) == value);
119}
120
Vince Bridgersbbd21902014-03-17 17:52:38 -0500121/* MDIO specific functions
122 */
123static int altera_tse_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
124{
Vince Bridgers89830582014-05-14 14:38:36 -0500125 struct net_device *ndev = bus->priv;
126 struct altera_tse_private *priv = netdev_priv(ndev);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500127
128 /* set MDIO address */
Vince Bridgers89830582014-05-14 14:38:36 -0500129 csrwr32((mii_id & 0x1f), priv->mac_dev,
Vince Bridgersa923fc72015-02-12 10:47:33 -0600130 tse_csroffs(mdio_phy1_addr));
Vince Bridgersbbd21902014-03-17 17:52:38 -0500131
132 /* get the data */
Vince Bridgers89830582014-05-14 14:38:36 -0500133 return csrrd32(priv->mac_dev,
Vince Bridgersa923fc72015-02-12 10:47:33 -0600134 tse_csroffs(mdio_phy1) + regnum * 4) & 0xffff;
Vince Bridgersbbd21902014-03-17 17:52:38 -0500135}
136
137static int altera_tse_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
138 u16 value)
139{
Vince Bridgers89830582014-05-14 14:38:36 -0500140 struct net_device *ndev = bus->priv;
141 struct altera_tse_private *priv = netdev_priv(ndev);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500142
143 /* set MDIO address */
Vince Bridgers89830582014-05-14 14:38:36 -0500144 csrwr32((mii_id & 0x1f), priv->mac_dev,
Vince Bridgersa923fc72015-02-12 10:47:33 -0600145 tse_csroffs(mdio_phy1_addr));
Vince Bridgersbbd21902014-03-17 17:52:38 -0500146
147 /* write the data */
Vince Bridgersa923fc72015-02-12 10:47:33 -0600148 csrwr32(value, priv->mac_dev, tse_csroffs(mdio_phy1) + regnum * 4);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500149 return 0;
150}
151
152static int altera_tse_mdio_create(struct net_device *dev, unsigned int id)
153{
154 struct altera_tse_private *priv = netdev_priv(dev);
155 int ret;
Vince Bridgersbbd21902014-03-17 17:52:38 -0500156 struct device_node *mdio_node = NULL;
157 struct mii_bus *mdio = NULL;
158 struct device_node *child_node = NULL;
159
160 for_each_child_of_node(priv->device->of_node, child_node) {
161 if (of_device_is_compatible(child_node, "altr,tse-mdio")) {
162 mdio_node = child_node;
163 break;
164 }
165 }
166
167 if (mdio_node) {
168 netdev_dbg(dev, "FOUND MDIO subnode\n");
169 } else {
170 netdev_dbg(dev, "NO MDIO subnode\n");
171 return 0;
172 }
173
174 mdio = mdiobus_alloc();
175 if (mdio == NULL) {
176 netdev_err(dev, "Error allocating MDIO bus\n");
177 return -ENOMEM;
178 }
179
180 mdio->name = ALTERA_TSE_RESOURCE_NAME;
181 mdio->read = &altera_tse_mdio_read;
182 mdio->write = &altera_tse_mdio_write;
183 snprintf(mdio->id, MII_BUS_ID_SIZE, "%s-%u", mdio->name, id);
184
Vince Bridgers89830582014-05-14 14:38:36 -0500185 mdio->priv = dev;
Vince Bridgersbbd21902014-03-17 17:52:38 -0500186 mdio->parent = priv->device;
187
188 ret = of_mdiobus_register(mdio, mdio_node);
189 if (ret != 0) {
190 netdev_err(dev, "Cannot register MDIO bus %s\n",
191 mdio->id);
Andrew Lunne7f4dc32016-01-06 20:11:15 +0100192 goto out_free_mdio;
Vince Bridgersbbd21902014-03-17 17:52:38 -0500193 }
194
195 if (netif_msg_drv(priv))
196 netdev_info(dev, "MDIO bus %s: created\n", mdio->id);
197
198 priv->mdio = mdio;
199 return 0;
Vince Bridgersbbd21902014-03-17 17:52:38 -0500200out_free_mdio:
201 mdiobus_free(mdio);
202 mdio = NULL;
203 return ret;
204}
205
206static void altera_tse_mdio_destroy(struct net_device *dev)
207{
208 struct altera_tse_private *priv = netdev_priv(dev);
209
210 if (priv->mdio == NULL)
211 return;
212
213 if (netif_msg_drv(priv))
214 netdev_info(dev, "MDIO bus %s: removed\n",
215 priv->mdio->id);
216
217 mdiobus_unregister(priv->mdio);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500218 mdiobus_free(priv->mdio);
219 priv->mdio = NULL;
220}
221
222static int tse_init_rx_buffer(struct altera_tse_private *priv,
223 struct tse_buffer *rxbuffer, int len)
224{
225 rxbuffer->skb = netdev_alloc_skb_ip_align(priv->dev, len);
226 if (!rxbuffer->skb)
227 return -ENOMEM;
228
229 rxbuffer->dma_addr = dma_map_single(priv->device, rxbuffer->skb->data,
230 len,
231 DMA_FROM_DEVICE);
232
233 if (dma_mapping_error(priv->device, rxbuffer->dma_addr)) {
234 netdev_err(priv->dev, "%s: DMA mapping error\n", __func__);
235 dev_kfree_skb_any(rxbuffer->skb);
236 return -EINVAL;
237 }
Vince Bridgers37c0ffa2014-04-24 16:58:08 -0500238 rxbuffer->dma_addr &= (dma_addr_t)~3;
Vince Bridgersbbd21902014-03-17 17:52:38 -0500239 rxbuffer->len = len;
240 return 0;
241}
242
243static void tse_free_rx_buffer(struct altera_tse_private *priv,
244 struct tse_buffer *rxbuffer)
245{
246 struct sk_buff *skb = rxbuffer->skb;
247 dma_addr_t dma_addr = rxbuffer->dma_addr;
248
249 if (skb != NULL) {
250 if (dma_addr)
251 dma_unmap_single(priv->device, dma_addr,
252 rxbuffer->len,
253 DMA_FROM_DEVICE);
254 dev_kfree_skb_any(skb);
255 rxbuffer->skb = NULL;
256 rxbuffer->dma_addr = 0;
257 }
258}
259
260/* Unmap and free Tx buffer resources
261 */
262static void tse_free_tx_buffer(struct altera_tse_private *priv,
263 struct tse_buffer *buffer)
264{
265 if (buffer->dma_addr) {
266 if (buffer->mapped_as_page)
267 dma_unmap_page(priv->device, buffer->dma_addr,
268 buffer->len, DMA_TO_DEVICE);
269 else
270 dma_unmap_single(priv->device, buffer->dma_addr,
271 buffer->len, DMA_TO_DEVICE);
272 buffer->dma_addr = 0;
273 }
274 if (buffer->skb) {
275 dev_kfree_skb_any(buffer->skb);
276 buffer->skb = NULL;
277 }
278}
279
280static int alloc_init_skbufs(struct altera_tse_private *priv)
281{
282 unsigned int rx_descs = priv->rx_ring_size;
283 unsigned int tx_descs = priv->tx_ring_size;
284 int ret = -ENOMEM;
285 int i;
286
287 /* Create Rx ring buffer */
288 priv->rx_ring = kcalloc(rx_descs, sizeof(struct tse_buffer),
289 GFP_KERNEL);
290 if (!priv->rx_ring)
291 goto err_rx_ring;
292
293 /* Create Tx ring buffer */
294 priv->tx_ring = kcalloc(tx_descs, sizeof(struct tse_buffer),
295 GFP_KERNEL);
296 if (!priv->tx_ring)
297 goto err_tx_ring;
298
299 priv->tx_cons = 0;
300 priv->tx_prod = 0;
301
302 /* Init Rx ring */
303 for (i = 0; i < rx_descs; i++) {
304 ret = tse_init_rx_buffer(priv, &priv->rx_ring[i],
305 priv->rx_dma_buf_sz);
306 if (ret)
307 goto err_init_rx_buffers;
308 }
309
310 priv->rx_cons = 0;
311 priv->rx_prod = 0;
312
313 return 0;
314err_init_rx_buffers:
315 while (--i >= 0)
316 tse_free_rx_buffer(priv, &priv->rx_ring[i]);
317 kfree(priv->tx_ring);
318err_tx_ring:
319 kfree(priv->rx_ring);
320err_rx_ring:
321 return ret;
322}
323
324static void free_skbufs(struct net_device *dev)
325{
326 struct altera_tse_private *priv = netdev_priv(dev);
327 unsigned int rx_descs = priv->rx_ring_size;
328 unsigned int tx_descs = priv->tx_ring_size;
329 int i;
330
331 /* Release the DMA TX/RX socket buffers */
332 for (i = 0; i < rx_descs; i++)
333 tse_free_rx_buffer(priv, &priv->rx_ring[i]);
334 for (i = 0; i < tx_descs; i++)
335 tse_free_tx_buffer(priv, &priv->tx_ring[i]);
336
337
338 kfree(priv->tx_ring);
339}
340
341/* Reallocate the skb for the reception process
342 */
343static inline void tse_rx_refill(struct altera_tse_private *priv)
344{
345 unsigned int rxsize = priv->rx_ring_size;
346 unsigned int entry;
347 int ret;
348
349 for (; priv->rx_cons - priv->rx_prod > 0;
350 priv->rx_prod++) {
351 entry = priv->rx_prod % rxsize;
352 if (likely(priv->rx_ring[entry].skb == NULL)) {
353 ret = tse_init_rx_buffer(priv, &priv->rx_ring[entry],
354 priv->rx_dma_buf_sz);
355 if (unlikely(ret != 0))
356 break;
357 priv->dmaops->add_rx_desc(priv, &priv->rx_ring[entry]);
358 }
359 }
360}
361
362/* Pull out the VLAN tag and fix up the packet
363 */
364static inline void tse_rx_vlan(struct net_device *dev, struct sk_buff *skb)
365{
366 struct ethhdr *eth_hdr;
367 u16 vid;
368 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
369 !__vlan_get_tag(skb, &vid)) {
370 eth_hdr = (struct ethhdr *)skb->data;
371 memmove(skb->data + VLAN_HLEN, eth_hdr, ETH_ALEN * 2);
372 skb_pull(skb, VLAN_HLEN);
373 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
374 }
375}
376
377/* Receive a packet: retrieve and pass over to upper levels
378 */
379static int tse_rx(struct altera_tse_private *priv, int limit)
380{
381 unsigned int count = 0;
382 unsigned int next_entry;
383 struct sk_buff *skb;
384 unsigned int entry = priv->rx_cons % priv->rx_ring_size;
385 u32 rxstatus;
386 u16 pktlength;
387 u16 pktstatus;
388
Andreas Oetken93ea3372015-04-16 23:48:08 +0200389 /* Check for count < limit first as get_rx_status is changing
390 * the response-fifo so we must process the next packet
391 * after calling get_rx_status if a response is pending.
392 * (reading the last byte of the response pops the value from the fifo.)
393 */
394 while ((count < limit) &&
395 ((rxstatus = priv->dmaops->get_rx_status(priv)) != 0)) {
Vince Bridgersbbd21902014-03-17 17:52:38 -0500396 pktstatus = rxstatus >> 16;
397 pktlength = rxstatus & 0xffff;
398
399 if ((pktstatus & 0xFF) || (pktlength == 0))
400 netdev_err(priv->dev,
401 "RCV pktstatus %08X pktlength %08X\n",
402 pktstatus, pktlength);
403
Vlastimil Setka48734992015-04-29 00:17:11 +0200404 /* DMA trasfer from TSE starts with 2 aditional bytes for
405 * IP payload alignment. Status returned by get_rx_status()
406 * contains DMA transfer length. Packet is 2 bytes shorter.
407 */
408 pktlength -= 2;
409
Vince Bridgersbbd21902014-03-17 17:52:38 -0500410 count++;
411 next_entry = (++priv->rx_cons) % priv->rx_ring_size;
412
413 skb = priv->rx_ring[entry].skb;
414 if (unlikely(!skb)) {
415 netdev_err(priv->dev,
416 "%s: Inconsistent Rx descriptor chain\n",
417 __func__);
418 priv->dev->stats.rx_dropped++;
419 break;
420 }
421 priv->rx_ring[entry].skb = NULL;
422
423 skb_put(skb, pktlength);
424
425 /* make cache consistent with receive packet buffer */
426 dma_sync_single_for_cpu(priv->device,
427 priv->rx_ring[entry].dma_addr,
428 priv->rx_ring[entry].len,
429 DMA_FROM_DEVICE);
430
431 dma_unmap_single(priv->device, priv->rx_ring[entry].dma_addr,
432 priv->rx_ring[entry].len, DMA_FROM_DEVICE);
433
434 if (netif_msg_pktdata(priv)) {
435 netdev_info(priv->dev, "frame received %d bytes\n",
436 pktlength);
437 print_hex_dump(KERN_ERR, "data: ", DUMP_PREFIX_OFFSET,
438 16, 1, skb->data, pktlength, true);
439 }
440
441 tse_rx_vlan(priv->dev, skb);
442
443 skb->protocol = eth_type_trans(skb, priv->dev);
444 skb_checksum_none_assert(skb);
445
446 napi_gro_receive(&priv->napi, skb);
447
448 priv->dev->stats.rx_packets++;
449 priv->dev->stats.rx_bytes += pktlength;
450
451 entry = next_entry;
Vince Bridgers37c0ffa2014-04-24 16:58:08 -0500452
453 tse_rx_refill(priv);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500454 }
455
Vince Bridgersbbd21902014-03-17 17:52:38 -0500456 return count;
457}
458
459/* Reclaim resources after transmission completes
460 */
461static int tse_tx_complete(struct altera_tse_private *priv)
462{
463 unsigned int txsize = priv->tx_ring_size;
464 u32 ready;
465 unsigned int entry;
466 struct tse_buffer *tx_buff;
467 int txcomplete = 0;
468
469 spin_lock(&priv->tx_lock);
470
471 ready = priv->dmaops->tx_completions(priv);
472
473 /* Free sent buffers */
474 while (ready && (priv->tx_cons != priv->tx_prod)) {
475 entry = priv->tx_cons % txsize;
476 tx_buff = &priv->tx_ring[entry];
477
478 if (netif_msg_tx_done(priv))
479 netdev_dbg(priv->dev, "%s: curr %d, dirty %d\n",
480 __func__, priv->tx_prod, priv->tx_cons);
481
482 if (likely(tx_buff->skb))
483 priv->dev->stats.tx_packets++;
484
485 tse_free_tx_buffer(priv, tx_buff);
486 priv->tx_cons++;
487
488 txcomplete++;
489 ready--;
490 }
491
492 if (unlikely(netif_queue_stopped(priv->dev) &&
493 tse_tx_avail(priv) > TSE_TX_THRESH(priv))) {
494 netif_tx_lock(priv->dev);
495 if (netif_queue_stopped(priv->dev) &&
496 tse_tx_avail(priv) > TSE_TX_THRESH(priv)) {
497 if (netif_msg_tx_done(priv))
498 netdev_dbg(priv->dev, "%s: restart transmit\n",
499 __func__);
500 netif_wake_queue(priv->dev);
501 }
502 netif_tx_unlock(priv->dev);
503 }
504
505 spin_unlock(&priv->tx_lock);
506 return txcomplete;
507}
508
509/* NAPI polling function
510 */
511static int tse_poll(struct napi_struct *napi, int budget)
512{
513 struct altera_tse_private *priv =
514 container_of(napi, struct altera_tse_private, napi);
515 int rxcomplete = 0;
Vince Bridgersbbd21902014-03-17 17:52:38 -0500516 unsigned long int flags;
517
Vlastimil Setka8d4ac392015-02-23 11:30:29 -0600518 tse_tx_complete(priv);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500519
520 rxcomplete = tse_rx(priv, budget);
521
Vlastimil Setka8d4ac392015-02-23 11:30:29 -0600522 if (rxcomplete < budget) {
Vince Bridgersbbd21902014-03-17 17:52:38 -0500523
Atsushi Nemoto4548a692015-09-02 17:49:29 +0900524 napi_complete(napi);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500525
Vlastimil Setka8d4ac392015-02-23 11:30:29 -0600526 netdev_dbg(priv->dev,
527 "NAPI Complete, did %d packets with budget %d\n",
528 rxcomplete, budget);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500529
Vlastimil Setka8d4ac392015-02-23 11:30:29 -0600530 spin_lock_irqsave(&priv->rxdma_irq_lock, flags);
531 priv->dmaops->enable_rxirq(priv);
532 priv->dmaops->enable_txirq(priv);
533 spin_unlock_irqrestore(&priv->rxdma_irq_lock, flags);
534 }
535 return rxcomplete;
Vince Bridgersbbd21902014-03-17 17:52:38 -0500536}
537
538/* DMA TX & RX FIFO interrupt routing
539 */
540static irqreturn_t altera_isr(int irq, void *dev_id)
541{
542 struct net_device *dev = dev_id;
543 struct altera_tse_private *priv;
Vince Bridgersbbd21902014-03-17 17:52:38 -0500544
Vince Bridgersbbd21902014-03-17 17:52:38 -0500545 if (unlikely(!dev)) {
546 pr_err("%s: invalid dev pointer\n", __func__);
547 return IRQ_NONE;
548 }
549 priv = netdev_priv(dev);
550
Vlastimil Setka8d4ac392015-02-23 11:30:29 -0600551 spin_lock(&priv->rxdma_irq_lock);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500552 /* reset IRQs */
553 priv->dmaops->clear_rxirq(priv);
554 priv->dmaops->clear_txirq(priv);
Vlastimil Setka8d4ac392015-02-23 11:30:29 -0600555 spin_unlock(&priv->rxdma_irq_lock);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500556
Vlastimil Setka8d4ac392015-02-23 11:30:29 -0600557 if (likely(napi_schedule_prep(&priv->napi))) {
558 spin_lock(&priv->rxdma_irq_lock);
559 priv->dmaops->disable_rxirq(priv);
560 priv->dmaops->disable_txirq(priv);
561 spin_unlock(&priv->rxdma_irq_lock);
562 __napi_schedule(&priv->napi);
563 }
564
Vince Bridgersbbd21902014-03-17 17:52:38 -0500565
566 return IRQ_HANDLED;
567}
568
569/* Transmit a packet (called by the kernel). Dispatches
570 * either the SGDMA method for transmitting or the
571 * MSGDMA method, assumes no scatter/gather support,
572 * implying an assumption that there's only one
573 * physically contiguous fragment starting at
574 * skb->data, for length of skb_headlen(skb).
575 */
576static int tse_start_xmit(struct sk_buff *skb, struct net_device *dev)
577{
578 struct altera_tse_private *priv = netdev_priv(dev);
579 unsigned int txsize = priv->tx_ring_size;
580 unsigned int entry;
581 struct tse_buffer *buffer = NULL;
582 int nfrags = skb_shinfo(skb)->nr_frags;
583 unsigned int nopaged_len = skb_headlen(skb);
584 enum netdev_tx ret = NETDEV_TX_OK;
585 dma_addr_t dma_addr;
Vince Bridgersbbd21902014-03-17 17:52:38 -0500586
587 spin_lock_bh(&priv->tx_lock);
588
589 if (unlikely(tse_tx_avail(priv) < nfrags + 1)) {
590 if (!netif_queue_stopped(dev)) {
591 netif_stop_queue(dev);
592 /* This is a hard error, log it. */
593 netdev_err(priv->dev,
594 "%s: Tx list full when queue awake\n",
595 __func__);
596 }
597 ret = NETDEV_TX_BUSY;
598 goto out;
599 }
600
601 /* Map the first skb fragment */
602 entry = priv->tx_prod % txsize;
603 buffer = &priv->tx_ring[entry];
604
605 dma_addr = dma_map_single(priv->device, skb->data, nopaged_len,
606 DMA_TO_DEVICE);
607 if (dma_mapping_error(priv->device, dma_addr)) {
608 netdev_err(priv->dev, "%s: DMA mapping error\n", __func__);
609 ret = NETDEV_TX_OK;
610 goto out;
611 }
612
613 buffer->skb = skb;
614 buffer->dma_addr = dma_addr;
615 buffer->len = nopaged_len;
616
617 /* Push data out of the cache hierarchy into main memory */
618 dma_sync_single_for_device(priv->device, buffer->dma_addr,
619 buffer->len, DMA_TO_DEVICE);
620
Vince Bridgers89830582014-05-14 14:38:36 -0500621 priv->dmaops->tx_buffer(priv, buffer);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500622
623 skb_tx_timestamp(skb);
624
625 priv->tx_prod++;
626 dev->stats.tx_bytes += skb->len;
627
628 if (unlikely(tse_tx_avail(priv) <= TXQUEUESTOP_THRESHHOLD)) {
629 if (netif_msg_hw(priv))
630 netdev_dbg(priv->dev, "%s: stop transmitted packets\n",
631 __func__);
632 netif_stop_queue(dev);
633 }
634
635out:
636 spin_unlock_bh(&priv->tx_lock);
637
638 return ret;
639}
640
641/* Called every time the controller might need to be made
642 * aware of new link state. The PHY code conveys this
643 * information through variables in the phydev structure, and this
644 * function converts those variables into the appropriate
645 * register values, and can bring down the device if needed.
646 */
647static void altera_tse_adjust_link(struct net_device *dev)
648{
649 struct altera_tse_private *priv = netdev_priv(dev);
Philippe Reynes941ea692016-06-18 16:37:20 +0200650 struct phy_device *phydev = dev->phydev;
Vince Bridgersbbd21902014-03-17 17:52:38 -0500651 int new_state = 0;
652
653 /* only change config if there is a link */
654 spin_lock(&priv->mac_cfg_lock);
655 if (phydev->link) {
656 /* Read old config */
657 u32 cfg_reg = ioread32(&priv->mac_dev->command_config);
658
659 /* Check duplex */
660 if (phydev->duplex != priv->oldduplex) {
661 new_state = 1;
662 if (!(phydev->duplex))
663 cfg_reg |= MAC_CMDCFG_HD_ENA;
664 else
665 cfg_reg &= ~MAC_CMDCFG_HD_ENA;
666
667 netdev_dbg(priv->dev, "%s: Link duplex = 0x%x\n",
668 dev->name, phydev->duplex);
669
670 priv->oldduplex = phydev->duplex;
671 }
672
673 /* Check speed */
674 if (phydev->speed != priv->oldspeed) {
675 new_state = 1;
676 switch (phydev->speed) {
677 case 1000:
678 cfg_reg |= MAC_CMDCFG_ETH_SPEED;
679 cfg_reg &= ~MAC_CMDCFG_ENA_10;
680 break;
681 case 100:
682 cfg_reg &= ~MAC_CMDCFG_ETH_SPEED;
683 cfg_reg &= ~MAC_CMDCFG_ENA_10;
684 break;
685 case 10:
686 cfg_reg &= ~MAC_CMDCFG_ETH_SPEED;
687 cfg_reg |= MAC_CMDCFG_ENA_10;
688 break;
689 default:
690 if (netif_msg_link(priv))
691 netdev_warn(dev, "Speed (%d) is not 10/100/1000!\n",
692 phydev->speed);
693 break;
694 }
695 priv->oldspeed = phydev->speed;
696 }
697 iowrite32(cfg_reg, &priv->mac_dev->command_config);
698
699 if (!priv->oldlink) {
700 new_state = 1;
701 priv->oldlink = 1;
702 }
703 } else if (priv->oldlink) {
704 new_state = 1;
705 priv->oldlink = 0;
706 priv->oldspeed = 0;
707 priv->oldduplex = -1;
708 }
709
710 if (new_state && netif_msg_link(priv))
711 phy_print_status(phydev);
712
713 spin_unlock(&priv->mac_cfg_lock);
714}
715static struct phy_device *connect_local_phy(struct net_device *dev)
716{
717 struct altera_tse_private *priv = netdev_priv(dev);
718 struct phy_device *phydev = NULL;
719 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
Vince Bridgersbbd21902014-03-17 17:52:38 -0500720
721 if (priv->phy_addr != POLL_PHY) {
722 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT,
723 priv->mdio->id, priv->phy_addr);
724
725 netdev_dbg(dev, "trying to attach to %s\n", phy_id_fmt);
726
727 phydev = phy_connect(dev, phy_id_fmt, &altera_tse_adjust_link,
728 priv->phy_iface);
729 if (IS_ERR(phydev))
730 netdev_err(dev, "Could not attach to PHY\n");
731
732 } else {
Vince Bridgers89830582014-05-14 14:38:36 -0500733 int ret;
Vince Bridgersbbd21902014-03-17 17:52:38 -0500734 phydev = phy_find_first(priv->mdio);
735 if (phydev == NULL) {
736 netdev_err(dev, "No PHY found\n");
737 return phydev;
738 }
739
740 ret = phy_connect_direct(dev, phydev, &altera_tse_adjust_link,
741 priv->phy_iface);
742 if (ret != 0) {
743 netdev_err(dev, "Could not attach to PHY\n");
744 phydev = NULL;
745 }
746 }
747 return phydev;
748}
749
Walter Lozano004fa112014-10-03 15:09:00 -0300750static int altera_tse_phy_get_addr_mdio_create(struct net_device *dev)
751{
752 struct altera_tse_private *priv = netdev_priv(dev);
753 struct device_node *np = priv->device->of_node;
754 int ret = 0;
755
756 priv->phy_iface = of_get_phy_mode(np);
757
Walter Lozano33543132014-10-03 15:09:01 -0300758 /* Avoid get phy addr and create mdio if no phy is present */
759 if (!priv->phy_iface)
760 return 0;
761
Walter Lozano004fa112014-10-03 15:09:00 -0300762 /* try to get PHY address from device tree, use PHY autodetection if
763 * no valid address is given
764 */
765
766 if (of_property_read_u32(priv->device->of_node, "phy-addr",
767 &priv->phy_addr)) {
768 priv->phy_addr = POLL_PHY;
769 }
770
771 if (!((priv->phy_addr == POLL_PHY) ||
772 ((priv->phy_addr >= 0) && (priv->phy_addr < PHY_MAX_ADDR)))) {
773 netdev_err(dev, "invalid phy-addr specified %d\n",
774 priv->phy_addr);
775 return -ENODEV;
776 }
777
778 /* Create/attach to MDIO bus */
779 ret = altera_tse_mdio_create(dev,
780 atomic_add_return(1, &instance_count));
781
782 if (ret)
783 return -ENODEV;
784
785 return 0;
786}
787
Vince Bridgersbbd21902014-03-17 17:52:38 -0500788/* Initialize driver's PHY state, and attach to the PHY
789 */
790static int init_phy(struct net_device *dev)
791{
792 struct altera_tse_private *priv = netdev_priv(dev);
793 struct phy_device *phydev;
794 struct device_node *phynode;
Andreas Oetken7cdbc6f2015-04-25 18:07:52 +0200795 bool fixed_link = false;
796 int rc = 0;
Vince Bridgersbbd21902014-03-17 17:52:38 -0500797
Walter Lozano33543132014-10-03 15:09:01 -0300798 /* Avoid init phy in case of no phy present */
799 if (!priv->phy_iface)
800 return 0;
801
Vince Bridgersbbd21902014-03-17 17:52:38 -0500802 priv->oldlink = 0;
803 priv->oldspeed = 0;
804 priv->oldduplex = -1;
805
806 phynode = of_parse_phandle(priv->device->of_node, "phy-handle", 0);
807
808 if (!phynode) {
Andreas Oetken7cdbc6f2015-04-25 18:07:52 +0200809 /* check if a fixed-link is defined in device-tree */
810 if (of_phy_is_fixed_link(priv->device->of_node)) {
811 rc = of_phy_register_fixed_link(priv->device->of_node);
812 if (rc < 0) {
813 netdev_err(dev, "cannot register fixed PHY\n");
814 return rc;
815 }
816
817 /* In the case of a fixed PHY, the DT node associated
818 * to the PHY is the Ethernet MAC DT node.
819 */
820 phynode = of_node_get(priv->device->of_node);
821 fixed_link = true;
822
823 netdev_dbg(dev, "fixed-link detected\n");
824 phydev = of_phy_connect(dev, phynode,
825 &altera_tse_adjust_link,
826 0, priv->phy_iface);
827 } else {
828 netdev_dbg(dev, "no phy-handle found\n");
829 if (!priv->mdio) {
830 netdev_err(dev, "No phy-handle nor local mdio specified\n");
831 return -ENODEV;
832 }
833 phydev = connect_local_phy(dev);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500834 }
Vince Bridgersbbd21902014-03-17 17:52:38 -0500835 } else {
836 netdev_dbg(dev, "phy-handle found\n");
837 phydev = of_phy_connect(dev, phynode,
838 &altera_tse_adjust_link, 0, priv->phy_iface);
839 }
Peter Chen5d972222016-08-01 15:02:29 +0800840 of_node_put(phynode);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500841
842 if (!phydev) {
843 netdev_err(dev, "Could not find the PHY\n");
844 return -ENODEV;
845 }
846
847 /* Stop Advertising 1000BASE Capability if interface is not GMII
848 * Note: Checkpatch throws CHECKs for the camel case defines below,
849 * it's ok to ignore.
850 */
851 if ((priv->phy_iface == PHY_INTERFACE_MODE_MII) ||
852 (priv->phy_iface == PHY_INTERFACE_MODE_RMII))
853 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
854 SUPPORTED_1000baseT_Full);
855
856 /* Broken HW is sometimes missing the pull-up resistor on the
857 * MDIO line, which results in reads to non-existent devices returning
858 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
Andreas Oetken7cdbc6f2015-04-25 18:07:52 +0200859 * device as well. If a fixed-link is used the phy_id is always 0.
Vince Bridgersbbd21902014-03-17 17:52:38 -0500860 * Note: phydev->phy_id is the result of reading the UID PHY registers.
861 */
Andreas Oetken7cdbc6f2015-04-25 18:07:52 +0200862 if ((phydev->phy_id == 0) && !fixed_link) {
Vince Bridgersbbd21902014-03-17 17:52:38 -0500863 netdev_err(dev, "Bad PHY UID 0x%08x\n", phydev->phy_id);
864 phy_disconnect(phydev);
865 return -ENODEV;
866 }
867
868 netdev_dbg(dev, "attached to PHY %d UID 0x%08x Link = %d\n",
Andrew Lunne5a03bf2016-01-06 20:11:16 +0100869 phydev->mdio.addr, phydev->phy_id, phydev->link);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500870
Vince Bridgersbbd21902014-03-17 17:52:38 -0500871 return 0;
872}
873
874static void tse_update_mac_addr(struct altera_tse_private *priv, u8 *addr)
875{
Vince Bridgersbbd21902014-03-17 17:52:38 -0500876 u32 msb;
877 u32 lsb;
878
879 msb = (addr[3] << 24) | (addr[2] << 16) | (addr[1] << 8) | addr[0];
880 lsb = ((addr[5] << 8) | addr[4]) & 0xffff;
881
882 /* Set primary MAC address */
Vince Bridgers89830582014-05-14 14:38:36 -0500883 csrwr32(msb, priv->mac_dev, tse_csroffs(mac_addr_0));
884 csrwr32(lsb, priv->mac_dev, tse_csroffs(mac_addr_1));
Vince Bridgersbbd21902014-03-17 17:52:38 -0500885}
886
887/* MAC software reset.
888 * When reset is triggered, the MAC function completes the current
889 * transmission or reception, and subsequently disables the transmit and
890 * receive logic, flushes the receive FIFO buffer, and resets the statistics
891 * counters.
892 */
893static int reset_mac(struct altera_tse_private *priv)
894{
Vince Bridgersbbd21902014-03-17 17:52:38 -0500895 int counter;
896 u32 dat;
897
Vince Bridgers89830582014-05-14 14:38:36 -0500898 dat = csrrd32(priv->mac_dev, tse_csroffs(command_config));
Vince Bridgersbbd21902014-03-17 17:52:38 -0500899 dat &= ~(MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA);
900 dat |= MAC_CMDCFG_SW_RESET | MAC_CMDCFG_CNT_RESET;
Vince Bridgers89830582014-05-14 14:38:36 -0500901 csrwr32(dat, priv->mac_dev, tse_csroffs(command_config));
Vince Bridgersbbd21902014-03-17 17:52:38 -0500902
903 counter = 0;
904 while (counter++ < ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) {
Vince Bridgers89830582014-05-14 14:38:36 -0500905 if (tse_bit_is_clear(priv->mac_dev, tse_csroffs(command_config),
906 MAC_CMDCFG_SW_RESET))
Vince Bridgersbbd21902014-03-17 17:52:38 -0500907 break;
908 udelay(1);
909 }
910
911 if (counter >= ALTERA_TSE_SW_RESET_WATCHDOG_CNTR) {
Vince Bridgers89830582014-05-14 14:38:36 -0500912 dat = csrrd32(priv->mac_dev, tse_csroffs(command_config));
Vince Bridgersbbd21902014-03-17 17:52:38 -0500913 dat &= ~MAC_CMDCFG_SW_RESET;
Vince Bridgers89830582014-05-14 14:38:36 -0500914 csrwr32(dat, priv->mac_dev, tse_csroffs(command_config));
Vince Bridgersbbd21902014-03-17 17:52:38 -0500915 return -1;
916 }
917 return 0;
918}
919
920/* Initialize MAC core registers
921*/
922static int init_mac(struct altera_tse_private *priv)
923{
Vince Bridgersbbd21902014-03-17 17:52:38 -0500924 unsigned int cmd = 0;
925 u32 frm_length;
926
927 /* Setup Rx FIFO */
Vince Bridgers89830582014-05-14 14:38:36 -0500928 csrwr32(priv->rx_fifo_depth - ALTERA_TSE_RX_SECTION_EMPTY,
929 priv->mac_dev, tse_csroffs(rx_section_empty));
930
931 csrwr32(ALTERA_TSE_RX_SECTION_FULL, priv->mac_dev,
932 tse_csroffs(rx_section_full));
933
934 csrwr32(ALTERA_TSE_RX_ALMOST_EMPTY, priv->mac_dev,
935 tse_csroffs(rx_almost_empty));
936
937 csrwr32(ALTERA_TSE_RX_ALMOST_FULL, priv->mac_dev,
938 tse_csroffs(rx_almost_full));
Vince Bridgersbbd21902014-03-17 17:52:38 -0500939
940 /* Setup Tx FIFO */
Vince Bridgers89830582014-05-14 14:38:36 -0500941 csrwr32(priv->tx_fifo_depth - ALTERA_TSE_TX_SECTION_EMPTY,
942 priv->mac_dev, tse_csroffs(tx_section_empty));
943
944 csrwr32(ALTERA_TSE_TX_SECTION_FULL, priv->mac_dev,
945 tse_csroffs(tx_section_full));
946
947 csrwr32(ALTERA_TSE_TX_ALMOST_EMPTY, priv->mac_dev,
948 tse_csroffs(tx_almost_empty));
949
950 csrwr32(ALTERA_TSE_TX_ALMOST_FULL, priv->mac_dev,
951 tse_csroffs(tx_almost_full));
Vince Bridgersbbd21902014-03-17 17:52:38 -0500952
953 /* MAC Address Configuration */
954 tse_update_mac_addr(priv, priv->dev->dev_addr);
955
956 /* MAC Function Configuration */
957 frm_length = ETH_HLEN + priv->dev->mtu + ETH_FCS_LEN;
Vince Bridgers89830582014-05-14 14:38:36 -0500958 csrwr32(frm_length, priv->mac_dev, tse_csroffs(frm_length));
959
960 csrwr32(ALTERA_TSE_TX_IPG_LENGTH, priv->mac_dev,
961 tse_csroffs(tx_ipg_length));
Vince Bridgersbbd21902014-03-17 17:52:38 -0500962
963 /* Disable RX/TX shift 16 for alignment of all received frames on 16-bit
964 * start address
965 */
Vince Bridgers89830582014-05-14 14:38:36 -0500966 tse_set_bit(priv->mac_dev, tse_csroffs(rx_cmd_stat),
967 ALTERA_TSE_RX_CMD_STAT_RX_SHIFT16);
968
969 tse_clear_bit(priv->mac_dev, tse_csroffs(tx_cmd_stat),
970 ALTERA_TSE_TX_CMD_STAT_TX_SHIFT16 |
971 ALTERA_TSE_TX_CMD_STAT_OMIT_CRC);
Vince Bridgersbbd21902014-03-17 17:52:38 -0500972
973 /* Set the MAC options */
Vince Bridgers89830582014-05-14 14:38:36 -0500974 cmd = csrrd32(priv->mac_dev, tse_csroffs(command_config));
Vince Bridgers37c0ffa2014-04-24 16:58:08 -0500975 cmd &= ~MAC_CMDCFG_PAD_EN; /* No padding Removal on Receive */
Vince Bridgersbbd21902014-03-17 17:52:38 -0500976 cmd &= ~MAC_CMDCFG_CRC_FWD; /* CRC Removal */
977 cmd |= MAC_CMDCFG_RX_ERR_DISC; /* Automatically discard frames
978 * with CRC errors
979 */
980 cmd |= MAC_CMDCFG_CNTL_FRM_ENA;
981 cmd &= ~MAC_CMDCFG_TX_ENA;
982 cmd &= ~MAC_CMDCFG_RX_ENA;
Vince Bridgers37c0ffa2014-04-24 16:58:08 -0500983
984 /* Default speed and duplex setting, full/100 */
985 cmd &= ~MAC_CMDCFG_HD_ENA;
986 cmd &= ~MAC_CMDCFG_ETH_SPEED;
987 cmd &= ~MAC_CMDCFG_ENA_10;
988
Vince Bridgers89830582014-05-14 14:38:36 -0500989 csrwr32(cmd, priv->mac_dev, tse_csroffs(command_config));
Vince Bridgersbbd21902014-03-17 17:52:38 -0500990
Vince Bridgers89830582014-05-14 14:38:36 -0500991 csrwr32(ALTERA_TSE_PAUSE_QUANTA, priv->mac_dev,
992 tse_csroffs(pause_quanta));
Vince Bridgers5aec4ee2014-04-24 16:58:09 -0500993
Vince Bridgersbbd21902014-03-17 17:52:38 -0500994 if (netif_msg_hw(priv))
995 dev_dbg(priv->device,
996 "MAC post-initialization: CMD_CONFIG = 0x%08x\n", cmd);
997
998 return 0;
999}
1000
1001/* Start/stop MAC transmission logic
1002 */
1003static void tse_set_mac(struct altera_tse_private *priv, bool enable)
1004{
Vince Bridgers89830582014-05-14 14:38:36 -05001005 u32 value = csrrd32(priv->mac_dev, tse_csroffs(command_config));
Vince Bridgersbbd21902014-03-17 17:52:38 -05001006
1007 if (enable)
1008 value |= MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA;
1009 else
1010 value &= ~(MAC_CMDCFG_TX_ENA | MAC_CMDCFG_RX_ENA);
1011
Vince Bridgers89830582014-05-14 14:38:36 -05001012 csrwr32(value, priv->mac_dev, tse_csroffs(command_config));
Vince Bridgersbbd21902014-03-17 17:52:38 -05001013}
1014
1015/* Change the MTU
1016 */
1017static int tse_change_mtu(struct net_device *dev, int new_mtu)
1018{
Vince Bridgersbbd21902014-03-17 17:52:38 -05001019 if (netif_running(dev)) {
1020 netdev_err(dev, "must be stopped to change its MTU\n");
1021 return -EBUSY;
1022 }
1023
Vince Bridgersbbd21902014-03-17 17:52:38 -05001024 dev->mtu = new_mtu;
1025 netdev_update_features(dev);
1026
1027 return 0;
1028}
1029
1030static void altera_tse_set_mcfilter(struct net_device *dev)
1031{
1032 struct altera_tse_private *priv = netdev_priv(dev);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001033 int i;
1034 struct netdev_hw_addr *ha;
1035
1036 /* clear the hash filter */
1037 for (i = 0; i < 64; i++)
Vince Bridgers89830582014-05-14 14:38:36 -05001038 csrwr32(0, priv->mac_dev, tse_csroffs(hash_table) + i * 4);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001039
1040 netdev_for_each_mc_addr(ha, dev) {
1041 unsigned int hash = 0;
1042 int mac_octet;
1043
1044 for (mac_octet = 5; mac_octet >= 0; mac_octet--) {
1045 unsigned char xor_bit = 0;
1046 unsigned char octet = ha->addr[mac_octet];
1047 unsigned int bitshift;
1048
1049 for (bitshift = 0; bitshift < 8; bitshift++)
1050 xor_bit ^= ((octet >> bitshift) & 0x01);
1051
1052 hash = (hash << 1) | xor_bit;
1053 }
Vince Bridgers89830582014-05-14 14:38:36 -05001054 csrwr32(1, priv->mac_dev, tse_csroffs(hash_table) + hash * 4);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001055 }
1056}
1057
1058
1059static void altera_tse_set_mcfilterall(struct net_device *dev)
1060{
1061 struct altera_tse_private *priv = netdev_priv(dev);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001062 int i;
1063
1064 /* set the hash filter */
1065 for (i = 0; i < 64; i++)
Vince Bridgers89830582014-05-14 14:38:36 -05001066 csrwr32(1, priv->mac_dev, tse_csroffs(hash_table) + i * 4);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001067}
1068
1069/* Set or clear the multicast filter for this adaptor
1070 */
1071static void tse_set_rx_mode_hashfilter(struct net_device *dev)
1072{
1073 struct altera_tse_private *priv = netdev_priv(dev);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001074
1075 spin_lock(&priv->mac_cfg_lock);
1076
1077 if (dev->flags & IFF_PROMISC)
Vince Bridgers89830582014-05-14 14:38:36 -05001078 tse_set_bit(priv->mac_dev, tse_csroffs(command_config),
1079 MAC_CMDCFG_PROMIS_EN);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001080
1081 if (dev->flags & IFF_ALLMULTI)
1082 altera_tse_set_mcfilterall(dev);
1083 else
1084 altera_tse_set_mcfilter(dev);
1085
1086 spin_unlock(&priv->mac_cfg_lock);
1087}
1088
1089/* Set or clear the multicast filter for this adaptor
1090 */
1091static void tse_set_rx_mode(struct net_device *dev)
1092{
1093 struct altera_tse_private *priv = netdev_priv(dev);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001094
1095 spin_lock(&priv->mac_cfg_lock);
1096
1097 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
1098 !netdev_mc_empty(dev) || !netdev_uc_empty(dev))
Vince Bridgers89830582014-05-14 14:38:36 -05001099 tse_set_bit(priv->mac_dev, tse_csroffs(command_config),
1100 MAC_CMDCFG_PROMIS_EN);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001101 else
Vince Bridgers89830582014-05-14 14:38:36 -05001102 tse_clear_bit(priv->mac_dev, tse_csroffs(command_config),
1103 MAC_CMDCFG_PROMIS_EN);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001104
1105 spin_unlock(&priv->mac_cfg_lock);
1106}
1107
Neill Whillans3b804562016-11-30 13:41:05 +00001108/* Initialise (if necessary) the SGMII PCS component
1109 */
1110static int init_sgmii_pcs(struct net_device *dev)
1111{
1112 struct altera_tse_private *priv = netdev_priv(dev);
1113 int n;
1114 unsigned int tmp_reg = 0;
1115
1116 if (priv->phy_iface != PHY_INTERFACE_MODE_SGMII)
1117 return 0; /* Nothing to do, not in SGMII mode */
1118
1119 /* The TSE SGMII PCS block looks a little like a PHY, it is
1120 * mapped into the zeroth MDIO space of the MAC and it has
1121 * ID registers like a PHY would. Sadly this is often
1122 * configured to zeroes, so don't be surprised if it does
1123 * show 0x00000000.
1124 */
1125
1126 if (sgmii_pcs_scratch_test(priv, 0x0000) &&
1127 sgmii_pcs_scratch_test(priv, 0xffff) &&
1128 sgmii_pcs_scratch_test(priv, 0xa5a5) &&
1129 sgmii_pcs_scratch_test(priv, 0x5a5a)) {
1130 netdev_info(dev, "PCS PHY ID: 0x%04x%04x\n",
1131 sgmii_pcs_read(priv, MII_PHYSID1),
1132 sgmii_pcs_read(priv, MII_PHYSID2));
1133 } else {
1134 netdev_err(dev, "SGMII PCS Scratch memory test failed.\n");
1135 return -ENOMEM;
1136 }
1137
1138 /* Starting on page 5-29 of the MegaCore Function User Guide
1139 * Set SGMII Link timer to 1.6ms
1140 */
1141 sgmii_pcs_write(priv, SGMII_PCS_LINK_TIMER_0, 0x0D40);
1142 sgmii_pcs_write(priv, SGMII_PCS_LINK_TIMER_1, 0x03);
1143
1144 /* Enable SGMII Interface and Enable SGMII Auto Negotiation */
1145 sgmii_pcs_write(priv, SGMII_PCS_IF_MODE, 0x3);
1146
1147 /* Enable Autonegotiation */
1148 tmp_reg = sgmii_pcs_read(priv, MII_BMCR);
1149 tmp_reg |= (BMCR_SPEED1000 | BMCR_FULLDPLX | BMCR_ANENABLE);
1150 sgmii_pcs_write(priv, MII_BMCR, tmp_reg);
1151
1152 /* Reset PCS block */
1153 tmp_reg |= BMCR_RESET;
1154 sgmii_pcs_write(priv, MII_BMCR, tmp_reg);
1155 for (n = 0; n < SGMII_PCS_SW_RESET_TIMEOUT; n++) {
1156 if (!(sgmii_pcs_read(priv, MII_BMCR) & BMCR_RESET)) {
1157 netdev_info(dev, "SGMII PCS block initialised OK\n");
1158 return 0;
1159 }
1160 udelay(1);
1161 }
1162
1163 /* We failed to reset the block, return a timeout */
1164 netdev_err(dev, "SGMII PCS block reset failed.\n");
1165 return -ETIMEDOUT;
1166}
1167
Vince Bridgersbbd21902014-03-17 17:52:38 -05001168/* Open and initialize the interface
1169 */
1170static int tse_open(struct net_device *dev)
1171{
1172 struct altera_tse_private *priv = netdev_priv(dev);
1173 int ret = 0;
1174 int i;
1175 unsigned long int flags;
1176
1177 /* Reset and configure TSE MAC and probe associated PHY */
1178 ret = priv->dmaops->init_dma(priv);
1179 if (ret != 0) {
1180 netdev_err(dev, "Cannot initialize DMA\n");
1181 goto phy_error;
1182 }
1183
1184 if (netif_msg_ifup(priv))
1185 netdev_warn(dev, "device MAC address %pM\n",
1186 dev->dev_addr);
1187
1188 if ((priv->revision < 0xd00) || (priv->revision > 0xe00))
1189 netdev_warn(dev, "TSE revision %x\n", priv->revision);
1190
1191 spin_lock(&priv->mac_cfg_lock);
Neill Whillans3b804562016-11-30 13:41:05 +00001192 /* no-op if MAC not operating in SGMII mode*/
1193 ret = init_sgmii_pcs(dev);
1194 if (ret) {
1195 netdev_err(dev,
1196 "Cannot init the SGMII PCS (error: %d)\n", ret);
1197 spin_unlock(&priv->mac_cfg_lock);
1198 goto phy_error;
1199 }
1200
Vince Bridgersbbd21902014-03-17 17:52:38 -05001201 ret = reset_mac(priv);
Vince Bridgersea8860eb2015-02-12 10:47:45 -06001202 /* Note that reset_mac will fail if the clocks are gated by the PHY
1203 * due to the PHY being put into isolation or power down mode.
1204 * This is not an error if reset fails due to no clock.
1205 */
Vince Bridgersbbd21902014-03-17 17:52:38 -05001206 if (ret)
Vince Bridgersea8860eb2015-02-12 10:47:45 -06001207 netdev_dbg(dev, "Cannot reset MAC core (error: %d)\n", ret);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001208
1209 ret = init_mac(priv);
1210 spin_unlock(&priv->mac_cfg_lock);
1211 if (ret) {
1212 netdev_err(dev, "Cannot init MAC core (error: %d)\n", ret);
1213 goto alloc_skbuf_error;
1214 }
1215
1216 priv->dmaops->reset_dma(priv);
1217
1218 /* Create and initialize the TX/RX descriptors chains. */
1219 priv->rx_ring_size = dma_rx_num;
1220 priv->tx_ring_size = dma_tx_num;
1221 ret = alloc_init_skbufs(priv);
1222 if (ret) {
1223 netdev_err(dev, "DMA descriptors initialization failed\n");
1224 goto alloc_skbuf_error;
1225 }
1226
1227
1228 /* Register RX interrupt */
1229 ret = request_irq(priv->rx_irq, altera_isr, IRQF_SHARED,
1230 dev->name, dev);
1231 if (ret) {
1232 netdev_err(dev, "Unable to register RX interrupt %d\n",
1233 priv->rx_irq);
1234 goto init_error;
1235 }
1236
1237 /* Register TX interrupt */
1238 ret = request_irq(priv->tx_irq, altera_isr, IRQF_SHARED,
1239 dev->name, dev);
1240 if (ret) {
1241 netdev_err(dev, "Unable to register TX interrupt %d\n",
1242 priv->tx_irq);
1243 goto tx_request_irq_error;
1244 }
1245
1246 /* Enable DMA interrupts */
1247 spin_lock_irqsave(&priv->rxdma_irq_lock, flags);
1248 priv->dmaops->enable_rxirq(priv);
1249 priv->dmaops->enable_txirq(priv);
1250
1251 /* Setup RX descriptor chain */
1252 for (i = 0; i < priv->rx_ring_size; i++)
1253 priv->dmaops->add_rx_desc(priv, &priv->rx_ring[i]);
1254
1255 spin_unlock_irqrestore(&priv->rxdma_irq_lock, flags);
1256
Philippe Reynes941ea692016-06-18 16:37:20 +02001257 if (dev->phydev)
1258 phy_start(dev->phydev);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001259
1260 napi_enable(&priv->napi);
1261 netif_start_queue(dev);
1262
Vince Bridgers37c0ffa2014-04-24 16:58:08 -05001263 priv->dmaops->start_rxdma(priv);
1264
1265 /* Start MAC Rx/Tx */
1266 spin_lock(&priv->mac_cfg_lock);
1267 tse_set_mac(priv, true);
1268 spin_unlock(&priv->mac_cfg_lock);
1269
Vince Bridgersbbd21902014-03-17 17:52:38 -05001270 return 0;
1271
1272tx_request_irq_error:
1273 free_irq(priv->rx_irq, dev);
1274init_error:
1275 free_skbufs(dev);
1276alloc_skbuf_error:
Vince Bridgersbbd21902014-03-17 17:52:38 -05001277phy_error:
1278 return ret;
1279}
1280
1281/* Stop TSE MAC interface and put the device in an inactive state
1282 */
1283static int tse_shutdown(struct net_device *dev)
1284{
1285 struct altera_tse_private *priv = netdev_priv(dev);
1286 int ret;
1287 unsigned long int flags;
1288
Kostya Belezkoc4849942014-12-30 12:27:09 -05001289 /* Stop the PHY */
Philippe Reynes941ea692016-06-18 16:37:20 +02001290 if (dev->phydev)
1291 phy_stop(dev->phydev);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001292
1293 netif_stop_queue(dev);
1294 napi_disable(&priv->napi);
1295
1296 /* Disable DMA interrupts */
1297 spin_lock_irqsave(&priv->rxdma_irq_lock, flags);
1298 priv->dmaops->disable_rxirq(priv);
1299 priv->dmaops->disable_txirq(priv);
1300 spin_unlock_irqrestore(&priv->rxdma_irq_lock, flags);
1301
1302 /* Free the IRQ lines */
1303 free_irq(priv->rx_irq, dev);
1304 free_irq(priv->tx_irq, dev);
1305
1306 /* disable and reset the MAC, empties fifo */
1307 spin_lock(&priv->mac_cfg_lock);
1308 spin_lock(&priv->tx_lock);
1309
1310 ret = reset_mac(priv);
Vince Bridgersea8860eb2015-02-12 10:47:45 -06001311 /* Note that reset_mac will fail if the clocks are gated by the PHY
1312 * due to the PHY being put into isolation or power down mode.
1313 * This is not an error if reset fails due to no clock.
1314 */
Vince Bridgersbbd21902014-03-17 17:52:38 -05001315 if (ret)
Vince Bridgersea8860eb2015-02-12 10:47:45 -06001316 netdev_dbg(dev, "Cannot reset MAC core (error: %d)\n", ret);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001317 priv->dmaops->reset_dma(priv);
1318 free_skbufs(dev);
1319
1320 spin_unlock(&priv->tx_lock);
1321 spin_unlock(&priv->mac_cfg_lock);
1322
1323 priv->dmaops->uninit_dma(priv);
1324
1325 return 0;
1326}
1327
1328static struct net_device_ops altera_tse_netdev_ops = {
1329 .ndo_open = tse_open,
1330 .ndo_stop = tse_shutdown,
1331 .ndo_start_xmit = tse_start_xmit,
1332 .ndo_set_mac_address = eth_mac_addr,
1333 .ndo_set_rx_mode = tse_set_rx_mode,
1334 .ndo_change_mtu = tse_change_mtu,
1335 .ndo_validate_addr = eth_validate_addr,
1336};
1337
Vince Bridgersbbd21902014-03-17 17:52:38 -05001338static int request_and_map(struct platform_device *pdev, const char *name,
1339 struct resource **res, void __iomem **ptr)
1340{
1341 struct resource *region;
1342 struct device *device = &pdev->dev;
1343
1344 *res = platform_get_resource_byname(pdev, IORESOURCE_MEM, name);
1345 if (*res == NULL) {
1346 dev_err(device, "resource %s not defined\n", name);
1347 return -ENODEV;
1348 }
1349
1350 region = devm_request_mem_region(device, (*res)->start,
1351 resource_size(*res), dev_name(device));
1352 if (region == NULL) {
1353 dev_err(device, "unable to request %s\n", name);
1354 return -EBUSY;
1355 }
1356
1357 *ptr = devm_ioremap_nocache(device, region->start,
1358 resource_size(region));
1359 if (*ptr == NULL) {
1360 dev_err(device, "ioremap_nocache of %s failed!", name);
1361 return -ENOMEM;
1362 }
1363
1364 return 0;
1365}
1366
1367/* Probe Altera TSE MAC device
1368 */
1369static int altera_tse_probe(struct platform_device *pdev)
1370{
1371 struct net_device *ndev;
1372 int ret = -ENODEV;
1373 struct resource *control_port;
1374 struct resource *dma_res;
1375 struct altera_tse_private *priv;
1376 const unsigned char *macaddr;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001377 void __iomem *descmap;
1378 const struct of_device_id *of_id = NULL;
1379
1380 ndev = alloc_etherdev(sizeof(struct altera_tse_private));
1381 if (!ndev) {
1382 dev_err(&pdev->dev, "Could not allocate network device\n");
1383 return -ENODEV;
1384 }
1385
1386 SET_NETDEV_DEV(ndev, &pdev->dev);
1387
1388 priv = netdev_priv(ndev);
1389 priv->device = &pdev->dev;
1390 priv->dev = ndev;
1391 priv->msg_enable = netif_msg_init(debug, default_msg_level);
1392
1393 of_id = of_match_device(altera_tse_ids, &pdev->dev);
1394
1395 if (of_id)
1396 priv->dmaops = (struct altera_dmaops *)of_id->data;
1397
1398
1399 if (priv->dmaops &&
1400 priv->dmaops->altera_dtype == ALTERA_DTYPE_SGDMA) {
1401 /* Get the mapped address to the SGDMA descriptor memory */
1402 ret = request_and_map(pdev, "s1", &dma_res, &descmap);
1403 if (ret)
Vince Bridgersa7642002014-04-24 16:58:10 -05001404 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001405
1406 /* Start of that memory is for transmit descriptors */
1407 priv->tx_dma_desc = descmap;
1408
1409 /* First half is for tx descriptors, other half for tx */
1410 priv->txdescmem = resource_size(dma_res)/2;
1411
1412 priv->txdescmem_busaddr = (dma_addr_t)dma_res->start;
1413
1414 priv->rx_dma_desc = (void __iomem *)((uintptr_t)(descmap +
1415 priv->txdescmem));
1416 priv->rxdescmem = resource_size(dma_res)/2;
1417 priv->rxdescmem_busaddr = dma_res->start;
1418 priv->rxdescmem_busaddr += priv->txdescmem;
1419
1420 if (upper_32_bits(priv->rxdescmem_busaddr)) {
1421 dev_dbg(priv->device,
1422 "SGDMA bus addresses greater than 32-bits\n");
Wei Yongjuna24a9d72016-10-22 14:28:38 +00001423 ret = -EINVAL;
Vince Bridgersa7642002014-04-24 16:58:10 -05001424 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001425 }
1426 if (upper_32_bits(priv->txdescmem_busaddr)) {
1427 dev_dbg(priv->device,
1428 "SGDMA bus addresses greater than 32-bits\n");
Wei Yongjuna24a9d72016-10-22 14:28:38 +00001429 ret = -EINVAL;
Vince Bridgersa7642002014-04-24 16:58:10 -05001430 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001431 }
1432 } else if (priv->dmaops &&
1433 priv->dmaops->altera_dtype == ALTERA_DTYPE_MSGDMA) {
1434 ret = request_and_map(pdev, "rx_resp", &dma_res,
1435 &priv->rx_dma_resp);
1436 if (ret)
Vince Bridgersa7642002014-04-24 16:58:10 -05001437 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001438
1439 ret = request_and_map(pdev, "tx_desc", &dma_res,
1440 &priv->tx_dma_desc);
1441 if (ret)
Vince Bridgersa7642002014-04-24 16:58:10 -05001442 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001443
1444 priv->txdescmem = resource_size(dma_res);
1445 priv->txdescmem_busaddr = dma_res->start;
1446
1447 ret = request_and_map(pdev, "rx_desc", &dma_res,
1448 &priv->rx_dma_desc);
1449 if (ret)
Vince Bridgersa7642002014-04-24 16:58:10 -05001450 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001451
1452 priv->rxdescmem = resource_size(dma_res);
1453 priv->rxdescmem_busaddr = dma_res->start;
1454
1455 } else {
Vince Bridgersa7642002014-04-24 16:58:10 -05001456 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001457 }
1458
1459 if (!dma_set_mask(priv->device, DMA_BIT_MASK(priv->dmaops->dmamask)))
1460 dma_set_coherent_mask(priv->device,
1461 DMA_BIT_MASK(priv->dmaops->dmamask));
1462 else if (!dma_set_mask(priv->device, DMA_BIT_MASK(32)))
1463 dma_set_coherent_mask(priv->device, DMA_BIT_MASK(32));
1464 else
Vince Bridgersa7642002014-04-24 16:58:10 -05001465 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001466
1467 /* MAC address space */
1468 ret = request_and_map(pdev, "control_port", &control_port,
1469 (void __iomem **)&priv->mac_dev);
1470 if (ret)
Vince Bridgersa7642002014-04-24 16:58:10 -05001471 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001472
1473 /* xSGDMA Rx Dispatcher address space */
1474 ret = request_and_map(pdev, "rx_csr", &dma_res,
1475 &priv->rx_dma_csr);
1476 if (ret)
Vince Bridgersa7642002014-04-24 16:58:10 -05001477 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001478
1479
1480 /* xSGDMA Tx Dispatcher address space */
1481 ret = request_and_map(pdev, "tx_csr", &dma_res,
1482 &priv->tx_dma_csr);
1483 if (ret)
Vince Bridgersa7642002014-04-24 16:58:10 -05001484 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001485
1486
1487 /* Rx IRQ */
1488 priv->rx_irq = platform_get_irq_byname(pdev, "rx_irq");
1489 if (priv->rx_irq == -ENXIO) {
1490 dev_err(&pdev->dev, "cannot obtain Rx IRQ\n");
1491 ret = -ENXIO;
Vince Bridgersa7642002014-04-24 16:58:10 -05001492 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001493 }
1494
1495 /* Tx IRQ */
1496 priv->tx_irq = platform_get_irq_byname(pdev, "tx_irq");
1497 if (priv->tx_irq == -ENXIO) {
1498 dev_err(&pdev->dev, "cannot obtain Tx IRQ\n");
1499 ret = -ENXIO;
Vince Bridgersa7642002014-04-24 16:58:10 -05001500 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001501 }
1502
1503 /* get FIFO depths from device tree */
1504 if (of_property_read_u32(pdev->dev.of_node, "rx-fifo-depth",
1505 &priv->rx_fifo_depth)) {
1506 dev_err(&pdev->dev, "cannot obtain rx-fifo-depth\n");
1507 ret = -ENXIO;
Vince Bridgersa7642002014-04-24 16:58:10 -05001508 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001509 }
1510
1511 if (of_property_read_u32(pdev->dev.of_node, "tx-fifo-depth",
Vlastimil Setkafe6e4082015-02-23 11:27:37 -06001512 &priv->tx_fifo_depth)) {
Vince Bridgersbbd21902014-03-17 17:52:38 -05001513 dev_err(&pdev->dev, "cannot obtain tx-fifo-depth\n");
1514 ret = -ENXIO;
Vince Bridgersa7642002014-04-24 16:58:10 -05001515 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001516 }
1517
1518 /* get hash filter settings for this instance */
1519 priv->hash_filter =
1520 of_property_read_bool(pdev->dev.of_node,
1521 "altr,has-hash-multicast-filter");
1522
Vince Bridgersd91e5c02014-05-14 14:38:37 -05001523 /* Set hash filter to not set for now until the
1524 * multicast filter receive issue is debugged
1525 */
1526 priv->hash_filter = 0;
1527
Vince Bridgersbbd21902014-03-17 17:52:38 -05001528 /* get supplemental address settings for this instance */
1529 priv->added_unicast =
1530 of_property_read_bool(pdev->dev.of_node,
1531 "altr,has-supplementary-unicast");
1532
Jarod Wilson44770e12016-10-17 15:54:17 -04001533 priv->dev->min_mtu = ETH_ZLEN + ETH_FCS_LEN;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001534 /* Max MTU is 1500, ETH_DATA_LEN */
Jarod Wilson44770e12016-10-17 15:54:17 -04001535 priv->dev->max_mtu = ETH_DATA_LEN;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001536
1537 /* Get the max mtu from the device tree. Note that the
1538 * "max-frame-size" parameter is actually max mtu. Definition
1539 * in the ePAPR v1.1 spec and usage differ, so go with usage.
1540 */
1541 of_property_read_u32(pdev->dev.of_node, "max-frame-size",
Jarod Wilson44770e12016-10-17 15:54:17 -04001542 &priv->dev->max_mtu);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001543
1544 /* The DMA buffer size already accounts for an alignment bias
1545 * to avoid unaligned access exceptions for the NIOS processor,
1546 */
1547 priv->rx_dma_buf_sz = ALTERA_RXDMABUFFER_SIZE;
1548
1549 /* get default MAC address from device tree */
1550 macaddr = of_get_mac_address(pdev->dev.of_node);
1551 if (macaddr)
1552 ether_addr_copy(ndev->dev_addr, macaddr);
1553 else
1554 eth_hw_addr_random(ndev);
1555
Walter Lozano004fa112014-10-03 15:09:00 -03001556 /* get phy addr and create mdio */
1557 ret = altera_tse_phy_get_addr_mdio_create(ndev);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001558
1559 if (ret)
Vince Bridgersa7642002014-04-24 16:58:10 -05001560 goto err_free_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001561
1562 /* initialize netdev */
Vince Bridgersbbd21902014-03-17 17:52:38 -05001563 ndev->mem_start = control_port->start;
1564 ndev->mem_end = control_port->end;
1565 ndev->netdev_ops = &altera_tse_netdev_ops;
1566 altera_tse_set_ethtool_ops(ndev);
1567
1568 altera_tse_netdev_ops.ndo_set_rx_mode = tse_set_rx_mode;
1569
1570 if (priv->hash_filter)
1571 altera_tse_netdev_ops.ndo_set_rx_mode =
1572 tse_set_rx_mode_hashfilter;
1573
1574 /* Scatter/gather IO is not supported,
1575 * so it is turned off
1576 */
1577 ndev->hw_features &= ~NETIF_F_SG;
1578 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
1579
1580 /* VLAN offloading of tagging, stripping and filtering is not
1581 * supported by hardware, but driver will accommodate the
1582 * extra 4-byte VLAN tag for processing by upper layers
1583 */
1584 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
1585
1586 /* setup NAPI interface */
1587 netif_napi_add(ndev, &priv->napi, tse_poll, NAPI_POLL_WEIGHT);
1588
1589 spin_lock_init(&priv->mac_cfg_lock);
1590 spin_lock_init(&priv->tx_lock);
1591 spin_lock_init(&priv->rxdma_irq_lock);
1592
Atsushi Nemotod43cefc2015-09-08 18:15:41 +09001593 netif_carrier_off(ndev);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001594 ret = register_netdev(ndev);
1595 if (ret) {
1596 dev_err(&pdev->dev, "failed to register TSE net device\n");
Vince Bridgersa7642002014-04-24 16:58:10 -05001597 goto err_register_netdev;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001598 }
1599
1600 platform_set_drvdata(pdev, ndev);
1601
1602 priv->revision = ioread32(&priv->mac_dev->megacore_revision);
1603
1604 if (netif_msg_probe(priv))
1605 dev_info(&pdev->dev, "Altera TSE MAC version %d.%d at 0x%08lx irq %d/%d\n",
1606 (priv->revision >> 8) & 0xff,
1607 priv->revision & 0xff,
1608 (unsigned long) control_port->start, priv->rx_irq,
1609 priv->tx_irq);
1610
1611 ret = init_phy(ndev);
1612 if (ret != 0) {
1613 netdev_err(ndev, "Cannot attach to PHY (error: %d)\n", ret);
Vince Bridgersa7642002014-04-24 16:58:10 -05001614 goto err_init_phy;
Vince Bridgersbbd21902014-03-17 17:52:38 -05001615 }
1616 return 0;
1617
Vince Bridgersa7642002014-04-24 16:58:10 -05001618err_init_phy:
1619 unregister_netdev(ndev);
1620err_register_netdev:
1621 netif_napi_del(&priv->napi);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001622 altera_tse_mdio_destroy(ndev);
Vince Bridgersa7642002014-04-24 16:58:10 -05001623err_free_netdev:
Vince Bridgersbbd21902014-03-17 17:52:38 -05001624 free_netdev(ndev);
1625 return ret;
1626}
1627
1628/* Remove Altera TSE MAC device
1629 */
1630static int altera_tse_remove(struct platform_device *pdev)
1631{
1632 struct net_device *ndev = platform_get_drvdata(pdev);
Kostya Belezkoc4849942014-12-30 12:27:09 -05001633
Philippe Reynes941ea692016-06-18 16:37:20 +02001634 if (ndev->phydev)
1635 phy_disconnect(ndev->phydev);
Vince Bridgersbbd21902014-03-17 17:52:38 -05001636
1637 platform_set_drvdata(pdev, NULL);
1638 altera_tse_mdio_destroy(ndev);
1639 unregister_netdev(ndev);
1640 free_netdev(ndev);
1641
1642 return 0;
1643}
1644
Vince Bridgers89830582014-05-14 14:38:36 -05001645static const struct altera_dmaops altera_dtype_sgdma = {
Vince Bridgersbbd21902014-03-17 17:52:38 -05001646 .altera_dtype = ALTERA_DTYPE_SGDMA,
1647 .dmamask = 32,
1648 .reset_dma = sgdma_reset,
1649 .enable_txirq = sgdma_enable_txirq,
1650 .enable_rxirq = sgdma_enable_rxirq,
1651 .disable_txirq = sgdma_disable_txirq,
1652 .disable_rxirq = sgdma_disable_rxirq,
1653 .clear_txirq = sgdma_clear_txirq,
1654 .clear_rxirq = sgdma_clear_rxirq,
1655 .tx_buffer = sgdma_tx_buffer,
1656 .tx_completions = sgdma_tx_completions,
1657 .add_rx_desc = sgdma_add_rx_desc,
1658 .get_rx_status = sgdma_rx_status,
1659 .init_dma = sgdma_initialize,
1660 .uninit_dma = sgdma_uninitialize,
Vince Bridgers37c0ffa2014-04-24 16:58:08 -05001661 .start_rxdma = sgdma_start_rxdma,
Vince Bridgersbbd21902014-03-17 17:52:38 -05001662};
1663
Vince Bridgers89830582014-05-14 14:38:36 -05001664static const struct altera_dmaops altera_dtype_msgdma = {
Vince Bridgersbbd21902014-03-17 17:52:38 -05001665 .altera_dtype = ALTERA_DTYPE_MSGDMA,
1666 .dmamask = 64,
1667 .reset_dma = msgdma_reset,
1668 .enable_txirq = msgdma_enable_txirq,
1669 .enable_rxirq = msgdma_enable_rxirq,
1670 .disable_txirq = msgdma_disable_txirq,
1671 .disable_rxirq = msgdma_disable_rxirq,
1672 .clear_txirq = msgdma_clear_txirq,
1673 .clear_rxirq = msgdma_clear_rxirq,
1674 .tx_buffer = msgdma_tx_buffer,
1675 .tx_completions = msgdma_tx_completions,
1676 .add_rx_desc = msgdma_add_rx_desc,
1677 .get_rx_status = msgdma_rx_status,
1678 .init_dma = msgdma_initialize,
1679 .uninit_dma = msgdma_uninitialize,
Vince Bridgers37c0ffa2014-04-24 16:58:08 -05001680 .start_rxdma = msgdma_start_rxdma,
Vince Bridgersbbd21902014-03-17 17:52:38 -05001681};
1682
Fabian Frederick27260532015-03-17 19:37:33 +01001683static const struct of_device_id altera_tse_ids[] = {
Vince Bridgersbbd21902014-03-17 17:52:38 -05001684 { .compatible = "altr,tse-msgdma-1.0", .data = &altera_dtype_msgdma, },
1685 { .compatible = "altr,tse-1.0", .data = &altera_dtype_sgdma, },
1686 { .compatible = "ALTR,tse-1.0", .data = &altera_dtype_sgdma, },
1687 {},
1688};
1689MODULE_DEVICE_TABLE(of, altera_tse_ids);
1690
1691static struct platform_driver altera_tse_driver = {
1692 .probe = altera_tse_probe,
1693 .remove = altera_tse_remove,
1694 .suspend = NULL,
1695 .resume = NULL,
1696 .driver = {
1697 .name = ALTERA_TSE_RESOURCE_NAME,
Vince Bridgersbbd21902014-03-17 17:52:38 -05001698 .of_match_table = altera_tse_ids,
1699 },
1700};
1701
1702module_platform_driver(altera_tse_driver);
1703
1704MODULE_AUTHOR("Altera Corporation");
1705MODULE_DESCRIPTION("Altera Triple Speed Ethernet MAC driver");
1706MODULE_LICENSE("GPL v2");