blob: 2d4c7eab8b36b95f7514a2d5992cde381fd56f6c [file] [log] [blame]
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001/*
2 * Faraday FTGMAC100 Gigabit Ethernet
3 *
4 * (C) Copyright 2009-2011 Faraday Technology
5 * Po-Yu Chuang <ratbert@faraday-tech.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24#include <linux/dma-mapping.h>
25#include <linux/etherdevice.h>
26#include <linux/ethtool.h>
Thomas Faber17f1bbc2012-01-18 13:45:44 +000027#include <linux/interrupt.h>
Po-Yu Chuang69785b72011-06-08 23:32:48 +000028#include <linux/io.h>
29#include <linux/module.h>
30#include <linux/netdevice.h>
31#include <linux/phy.h>
32#include <linux/platform_device.h>
33#include <net/ip.h>
Gavin Shanbd466c32016-07-19 11:54:23 +100034#include <net/ncsi.h>
Po-Yu Chuang69785b72011-06-08 23:32:48 +000035
36#include "ftgmac100.h"
37
38#define DRV_NAME "ftgmac100"
39#define DRV_VERSION "0.7"
40
41#define RX_QUEUE_ENTRIES 256 /* must be power of 2 */
42#define TX_QUEUE_ENTRIES 512 /* must be power of 2 */
43
44#define MAX_PKT_SIZE 1518
45#define RX_BUF_SIZE PAGE_SIZE /* must be smaller than 0x3fff */
46
47/******************************************************************************
48 * private data
49 *****************************************************************************/
50struct ftgmac100_descs {
51 struct ftgmac100_rxdes rxdes[RX_QUEUE_ENTRIES];
52 struct ftgmac100_txdes txdes[TX_QUEUE_ENTRIES];
53};
54
55struct ftgmac100 {
56 struct resource *res;
57 void __iomem *base;
58 int irq;
59
60 struct ftgmac100_descs *descs;
61 dma_addr_t descs_dma_addr;
62
63 unsigned int rx_pointer;
64 unsigned int tx_clean_pointer;
65 unsigned int tx_pointer;
66 unsigned int tx_pending;
67
68 spinlock_t tx_lock;
69
70 struct net_device *netdev;
71 struct device *dev;
Gavin Shanbd466c32016-07-19 11:54:23 +100072 struct ncsi_dev *ndev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000073 struct napi_struct napi;
74
75 struct mii_bus *mii_bus;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000076 int old_speed;
Gavin Shanfc6061c2016-07-19 11:54:25 +100077 int int_mask_all;
Gavin Shanbd466c32016-07-19 11:54:23 +100078 bool use_ncsi;
79 bool enabled;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000080};
81
82static int ftgmac100_alloc_rx_page(struct ftgmac100 *priv,
83 struct ftgmac100_rxdes *rxdes, gfp_t gfp);
84
85/******************************************************************************
86 * internal functions (hardware register access)
87 *****************************************************************************/
Po-Yu Chuang69785b72011-06-08 23:32:48 +000088static void ftgmac100_set_rx_ring_base(struct ftgmac100 *priv, dma_addr_t addr)
89{
90 iowrite32(addr, priv->base + FTGMAC100_OFFSET_RXR_BADR);
91}
92
93static void ftgmac100_set_rx_buffer_size(struct ftgmac100 *priv,
94 unsigned int size)
95{
96 size = FTGMAC100_RBSR_SIZE(size);
97 iowrite32(size, priv->base + FTGMAC100_OFFSET_RBSR);
98}
99
100static void ftgmac100_set_normal_prio_tx_ring_base(struct ftgmac100 *priv,
101 dma_addr_t addr)
102{
103 iowrite32(addr, priv->base + FTGMAC100_OFFSET_NPTXR_BADR);
104}
105
106static void ftgmac100_txdma_normal_prio_start_polling(struct ftgmac100 *priv)
107{
108 iowrite32(1, priv->base + FTGMAC100_OFFSET_NPTXPD);
109}
110
111static int ftgmac100_reset_hw(struct ftgmac100 *priv)
112{
113 struct net_device *netdev = priv->netdev;
114 int i;
115
116 /* NOTE: reset clears all registers */
117 iowrite32(FTGMAC100_MACCR_SW_RST, priv->base + FTGMAC100_OFFSET_MACCR);
118 for (i = 0; i < 5; i++) {
119 unsigned int maccr;
120
121 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
122 if (!(maccr & FTGMAC100_MACCR_SW_RST))
123 return 0;
124
125 udelay(1000);
126 }
127
128 netdev_err(netdev, "software reset failed\n");
129 return -EIO;
130}
131
132static void ftgmac100_set_mac(struct ftgmac100 *priv, const unsigned char *mac)
133{
134 unsigned int maddr = mac[0] << 8 | mac[1];
135 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
136
137 iowrite32(maddr, priv->base + FTGMAC100_OFFSET_MAC_MADR);
138 iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR);
139}
140
Gavin Shan113ce102016-07-19 11:54:22 +1000141static void ftgmac100_setup_mac(struct ftgmac100 *priv)
142{
143 u8 mac[ETH_ALEN];
144 unsigned int m;
145 unsigned int l;
146 void *addr;
147
148 addr = device_get_mac_address(priv->dev, mac, ETH_ALEN);
149 if (addr) {
150 ether_addr_copy(priv->netdev->dev_addr, mac);
151 dev_info(priv->dev, "Read MAC address %pM from device tree\n",
152 mac);
153 return;
154 }
155
156 m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR);
157 l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR);
158
159 mac[0] = (m >> 8) & 0xff;
160 mac[1] = m & 0xff;
161 mac[2] = (l >> 24) & 0xff;
162 mac[3] = (l >> 16) & 0xff;
163 mac[4] = (l >> 8) & 0xff;
164 mac[5] = l & 0xff;
165
166 if (!is_valid_ether_addr(mac)) {
167 mac[5] = (m >> 8) & 0xff;
168 mac[4] = m & 0xff;
169 mac[3] = (l >> 24) & 0xff;
170 mac[2] = (l >> 16) & 0xff;
171 mac[1] = (l >> 8) & 0xff;
172 mac[0] = l & 0xff;
173 }
174
175 if (is_valid_ether_addr(mac)) {
176 ether_addr_copy(priv->netdev->dev_addr, mac);
177 dev_info(priv->dev, "Read MAC address %pM from chip\n", mac);
178 } else {
179 eth_hw_addr_random(priv->netdev);
180 dev_info(priv->dev, "Generated random MAC address %pM\n",
181 priv->netdev->dev_addr);
182 }
183}
184
185static int ftgmac100_set_mac_addr(struct net_device *dev, void *p)
186{
187 int ret;
188
189 ret = eth_prepare_mac_addr_change(dev, p);
190 if (ret < 0)
191 return ret;
192
193 eth_commit_mac_addr_change(dev, p);
194 ftgmac100_set_mac(netdev_priv(dev), dev->dev_addr);
195
196 return 0;
197}
198
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000199static void ftgmac100_init_hw(struct ftgmac100 *priv)
200{
201 /* setup ring buffer base registers */
202 ftgmac100_set_rx_ring_base(priv,
203 priv->descs_dma_addr +
204 offsetof(struct ftgmac100_descs, rxdes));
205 ftgmac100_set_normal_prio_tx_ring_base(priv,
206 priv->descs_dma_addr +
207 offsetof(struct ftgmac100_descs, txdes));
208
209 ftgmac100_set_rx_buffer_size(priv, RX_BUF_SIZE);
210
211 iowrite32(FTGMAC100_APTC_RXPOLL_CNT(1), priv->base + FTGMAC100_OFFSET_APTC);
212
213 ftgmac100_set_mac(priv, priv->netdev->dev_addr);
214}
215
216#define MACCR_ENABLE_ALL (FTGMAC100_MACCR_TXDMA_EN | \
217 FTGMAC100_MACCR_RXDMA_EN | \
218 FTGMAC100_MACCR_TXMAC_EN | \
219 FTGMAC100_MACCR_RXMAC_EN | \
220 FTGMAC100_MACCR_FULLDUP | \
221 FTGMAC100_MACCR_CRC_APD | \
222 FTGMAC100_MACCR_RX_RUNT | \
223 FTGMAC100_MACCR_RX_BROADPKT)
224
225static void ftgmac100_start_hw(struct ftgmac100 *priv, int speed)
226{
227 int maccr = MACCR_ENABLE_ALL;
228
229 switch (speed) {
230 default:
231 case 10:
232 break;
233
234 case 100:
235 maccr |= FTGMAC100_MACCR_FAST_MODE;
236 break;
237
238 case 1000:
239 maccr |= FTGMAC100_MACCR_GIGA_MODE;
240 break;
241 }
242
243 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
244}
245
246static void ftgmac100_stop_hw(struct ftgmac100 *priv)
247{
248 iowrite32(0, priv->base + FTGMAC100_OFFSET_MACCR);
249}
250
251/******************************************************************************
252 * internal functions (receive descriptor)
253 *****************************************************************************/
254static bool ftgmac100_rxdes_first_segment(struct ftgmac100_rxdes *rxdes)
255{
256 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_FRS);
257}
258
259static bool ftgmac100_rxdes_last_segment(struct ftgmac100_rxdes *rxdes)
260{
261 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_LRS);
262}
263
264static bool ftgmac100_rxdes_packet_ready(struct ftgmac100_rxdes *rxdes)
265{
266 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RXPKT_RDY);
267}
268
269static void ftgmac100_rxdes_set_dma_own(struct ftgmac100_rxdes *rxdes)
270{
271 /* clear status bits */
272 rxdes->rxdes0 &= cpu_to_le32(FTGMAC100_RXDES0_EDORR);
273}
274
275static bool ftgmac100_rxdes_rx_error(struct ftgmac100_rxdes *rxdes)
276{
277 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RX_ERR);
278}
279
280static bool ftgmac100_rxdes_crc_error(struct ftgmac100_rxdes *rxdes)
281{
282 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_CRC_ERR);
283}
284
285static bool ftgmac100_rxdes_frame_too_long(struct ftgmac100_rxdes *rxdes)
286{
287 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_FTL);
288}
289
290static bool ftgmac100_rxdes_runt(struct ftgmac100_rxdes *rxdes)
291{
292 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RUNT);
293}
294
295static bool ftgmac100_rxdes_odd_nibble(struct ftgmac100_rxdes *rxdes)
296{
297 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RX_ODD_NB);
298}
299
300static unsigned int ftgmac100_rxdes_data_length(struct ftgmac100_rxdes *rxdes)
301{
302 return le32_to_cpu(rxdes->rxdes0) & FTGMAC100_RXDES0_VDBC;
303}
304
305static bool ftgmac100_rxdes_multicast(struct ftgmac100_rxdes *rxdes)
306{
307 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_MULTICAST);
308}
309
310static void ftgmac100_rxdes_set_end_of_ring(struct ftgmac100_rxdes *rxdes)
311{
312 rxdes->rxdes0 |= cpu_to_le32(FTGMAC100_RXDES0_EDORR);
313}
314
315static void ftgmac100_rxdes_set_dma_addr(struct ftgmac100_rxdes *rxdes,
316 dma_addr_t addr)
317{
318 rxdes->rxdes3 = cpu_to_le32(addr);
319}
320
321static dma_addr_t ftgmac100_rxdes_get_dma_addr(struct ftgmac100_rxdes *rxdes)
322{
323 return le32_to_cpu(rxdes->rxdes3);
324}
325
326static bool ftgmac100_rxdes_is_tcp(struct ftgmac100_rxdes *rxdes)
327{
328 return (rxdes->rxdes1 & cpu_to_le32(FTGMAC100_RXDES1_PROT_MASK)) ==
329 cpu_to_le32(FTGMAC100_RXDES1_PROT_TCPIP);
330}
331
332static bool ftgmac100_rxdes_is_udp(struct ftgmac100_rxdes *rxdes)
333{
334 return (rxdes->rxdes1 & cpu_to_le32(FTGMAC100_RXDES1_PROT_MASK)) ==
335 cpu_to_le32(FTGMAC100_RXDES1_PROT_UDPIP);
336}
337
338static bool ftgmac100_rxdes_tcpcs_err(struct ftgmac100_rxdes *rxdes)
339{
340 return rxdes->rxdes1 & cpu_to_le32(FTGMAC100_RXDES1_TCP_CHKSUM_ERR);
341}
342
343static bool ftgmac100_rxdes_udpcs_err(struct ftgmac100_rxdes *rxdes)
344{
345 return rxdes->rxdes1 & cpu_to_le32(FTGMAC100_RXDES1_UDP_CHKSUM_ERR);
346}
347
348static bool ftgmac100_rxdes_ipcs_err(struct ftgmac100_rxdes *rxdes)
349{
350 return rxdes->rxdes1 & cpu_to_le32(FTGMAC100_RXDES1_IP_CHKSUM_ERR);
351}
352
353/*
354 * rxdes2 is not used by hardware. We use it to keep track of page.
355 * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
356 */
357static void ftgmac100_rxdes_set_page(struct ftgmac100_rxdes *rxdes, struct page *page)
358{
359 rxdes->rxdes2 = (unsigned int)page;
360}
361
362static struct page *ftgmac100_rxdes_get_page(struct ftgmac100_rxdes *rxdes)
363{
364 return (struct page *)rxdes->rxdes2;
365}
366
367/******************************************************************************
368 * internal functions (receive)
369 *****************************************************************************/
370static int ftgmac100_next_rx_pointer(int pointer)
371{
372 return (pointer + 1) & (RX_QUEUE_ENTRIES - 1);
373}
374
375static void ftgmac100_rx_pointer_advance(struct ftgmac100 *priv)
376{
377 priv->rx_pointer = ftgmac100_next_rx_pointer(priv->rx_pointer);
378}
379
380static struct ftgmac100_rxdes *ftgmac100_current_rxdes(struct ftgmac100 *priv)
381{
382 return &priv->descs->rxdes[priv->rx_pointer];
383}
384
385static struct ftgmac100_rxdes *
386ftgmac100_rx_locate_first_segment(struct ftgmac100 *priv)
387{
388 struct ftgmac100_rxdes *rxdes = ftgmac100_current_rxdes(priv);
389
390 while (ftgmac100_rxdes_packet_ready(rxdes)) {
391 if (ftgmac100_rxdes_first_segment(rxdes))
392 return rxdes;
393
394 ftgmac100_rxdes_set_dma_own(rxdes);
395 ftgmac100_rx_pointer_advance(priv);
396 rxdes = ftgmac100_current_rxdes(priv);
397 }
398
399 return NULL;
400}
401
402static bool ftgmac100_rx_packet_error(struct ftgmac100 *priv,
403 struct ftgmac100_rxdes *rxdes)
404{
405 struct net_device *netdev = priv->netdev;
406 bool error = false;
407
408 if (unlikely(ftgmac100_rxdes_rx_error(rxdes))) {
409 if (net_ratelimit())
410 netdev_info(netdev, "rx err\n");
411
412 netdev->stats.rx_errors++;
413 error = true;
414 }
415
416 if (unlikely(ftgmac100_rxdes_crc_error(rxdes))) {
417 if (net_ratelimit())
418 netdev_info(netdev, "rx crc err\n");
419
420 netdev->stats.rx_crc_errors++;
421 error = true;
422 } else if (unlikely(ftgmac100_rxdes_ipcs_err(rxdes))) {
423 if (net_ratelimit())
424 netdev_info(netdev, "rx IP checksum err\n");
425
426 error = true;
427 }
428
429 if (unlikely(ftgmac100_rxdes_frame_too_long(rxdes))) {
430 if (net_ratelimit())
431 netdev_info(netdev, "rx frame too long\n");
432
433 netdev->stats.rx_length_errors++;
434 error = true;
435 } else if (unlikely(ftgmac100_rxdes_runt(rxdes))) {
436 if (net_ratelimit())
437 netdev_info(netdev, "rx runt\n");
438
439 netdev->stats.rx_length_errors++;
440 error = true;
441 } else if (unlikely(ftgmac100_rxdes_odd_nibble(rxdes))) {
442 if (net_ratelimit())
443 netdev_info(netdev, "rx odd nibble\n");
444
445 netdev->stats.rx_length_errors++;
446 error = true;
447 }
448
449 return error;
450}
451
452static void ftgmac100_rx_drop_packet(struct ftgmac100 *priv)
453{
454 struct net_device *netdev = priv->netdev;
455 struct ftgmac100_rxdes *rxdes = ftgmac100_current_rxdes(priv);
456 bool done = false;
457
458 if (net_ratelimit())
459 netdev_dbg(netdev, "drop packet %p\n", rxdes);
460
461 do {
462 if (ftgmac100_rxdes_last_segment(rxdes))
463 done = true;
464
465 ftgmac100_rxdes_set_dma_own(rxdes);
466 ftgmac100_rx_pointer_advance(priv);
467 rxdes = ftgmac100_current_rxdes(priv);
468 } while (!done && ftgmac100_rxdes_packet_ready(rxdes));
469
470 netdev->stats.rx_dropped++;
471}
472
473static bool ftgmac100_rx_packet(struct ftgmac100 *priv, int *processed)
474{
475 struct net_device *netdev = priv->netdev;
476 struct ftgmac100_rxdes *rxdes;
477 struct sk_buff *skb;
478 bool done = false;
479
480 rxdes = ftgmac100_rx_locate_first_segment(priv);
481 if (!rxdes)
482 return false;
483
484 if (unlikely(ftgmac100_rx_packet_error(priv, rxdes))) {
485 ftgmac100_rx_drop_packet(priv);
486 return true;
487 }
488
489 /* start processing */
490 skb = netdev_alloc_skb_ip_align(netdev, 128);
491 if (unlikely(!skb)) {
492 if (net_ratelimit())
493 netdev_err(netdev, "rx skb alloc failed\n");
494
495 ftgmac100_rx_drop_packet(priv);
496 return true;
497 }
498
499 if (unlikely(ftgmac100_rxdes_multicast(rxdes)))
500 netdev->stats.multicast++;
501
502 /*
503 * It seems that HW does checksum incorrectly with fragmented packets,
504 * so we are conservative here - if HW checksum error, let software do
505 * the checksum again.
506 */
507 if ((ftgmac100_rxdes_is_tcp(rxdes) && !ftgmac100_rxdes_tcpcs_err(rxdes)) ||
508 (ftgmac100_rxdes_is_udp(rxdes) && !ftgmac100_rxdes_udpcs_err(rxdes)))
509 skb->ip_summed = CHECKSUM_UNNECESSARY;
510
511 do {
512 dma_addr_t map = ftgmac100_rxdes_get_dma_addr(rxdes);
513 struct page *page = ftgmac100_rxdes_get_page(rxdes);
514 unsigned int size;
515
516 dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
517
518 size = ftgmac100_rxdes_data_length(rxdes);
519 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page, 0, size);
520
521 skb->len += size;
522 skb->data_len += size;
Eric Dumazet5935f812011-10-13 11:30:52 +0000523 skb->truesize += PAGE_SIZE;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000524
525 if (ftgmac100_rxdes_last_segment(rxdes))
526 done = true;
527
528 ftgmac100_alloc_rx_page(priv, rxdes, GFP_ATOMIC);
529
530 ftgmac100_rx_pointer_advance(priv);
531 rxdes = ftgmac100_current_rxdes(priv);
532 } while (!done);
533
Eric Dumazet6ecd09d2012-07-12 04:19:38 +0000534 /* Small frames are copied into linear part of skb to free one page */
535 if (skb->len <= 128) {
Eric Dumazet5935f812011-10-13 11:30:52 +0000536 skb->truesize -= PAGE_SIZE;
Eric Dumazet6ecd09d2012-07-12 04:19:38 +0000537 __pskb_pull_tail(skb, skb->len);
538 } else {
539 /* We pull the minimum amount into linear part */
540 __pskb_pull_tail(skb, ETH_HLEN);
541 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000542 skb->protocol = eth_type_trans(skb, netdev);
543
544 netdev->stats.rx_packets++;
545 netdev->stats.rx_bytes += skb->len;
546
547 /* push packet to protocol stack */
548 napi_gro_receive(&priv->napi, skb);
549
550 (*processed)++;
551 return true;
552}
553
554/******************************************************************************
555 * internal functions (transmit descriptor)
556 *****************************************************************************/
557static void ftgmac100_txdes_reset(struct ftgmac100_txdes *txdes)
558{
559 /* clear all except end of ring bit */
560 txdes->txdes0 &= cpu_to_le32(FTGMAC100_TXDES0_EDOTR);
561 txdes->txdes1 = 0;
562 txdes->txdes2 = 0;
563 txdes->txdes3 = 0;
564}
565
566static bool ftgmac100_txdes_owned_by_dma(struct ftgmac100_txdes *txdes)
567{
568 return txdes->txdes0 & cpu_to_le32(FTGMAC100_TXDES0_TXDMA_OWN);
569}
570
571static void ftgmac100_txdes_set_dma_own(struct ftgmac100_txdes *txdes)
572{
573 /*
574 * Make sure dma own bit will not be set before any other
575 * descriptor fields.
576 */
577 wmb();
578 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_TXDMA_OWN);
579}
580
581static void ftgmac100_txdes_set_end_of_ring(struct ftgmac100_txdes *txdes)
582{
583 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_EDOTR);
584}
585
586static void ftgmac100_txdes_set_first_segment(struct ftgmac100_txdes *txdes)
587{
588 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_FTS);
589}
590
591static void ftgmac100_txdes_set_last_segment(struct ftgmac100_txdes *txdes)
592{
593 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_LTS);
594}
595
596static void ftgmac100_txdes_set_buffer_size(struct ftgmac100_txdes *txdes,
597 unsigned int len)
598{
599 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_TXBUF_SIZE(len));
600}
601
602static void ftgmac100_txdes_set_txint(struct ftgmac100_txdes *txdes)
603{
604 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_TXIC);
605}
606
607static void ftgmac100_txdes_set_tcpcs(struct ftgmac100_txdes *txdes)
608{
609 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_TCP_CHKSUM);
610}
611
612static void ftgmac100_txdes_set_udpcs(struct ftgmac100_txdes *txdes)
613{
614 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_UDP_CHKSUM);
615}
616
617static void ftgmac100_txdes_set_ipcs(struct ftgmac100_txdes *txdes)
618{
619 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_IP_CHKSUM);
620}
621
622static void ftgmac100_txdes_set_dma_addr(struct ftgmac100_txdes *txdes,
623 dma_addr_t addr)
624{
625 txdes->txdes3 = cpu_to_le32(addr);
626}
627
628static dma_addr_t ftgmac100_txdes_get_dma_addr(struct ftgmac100_txdes *txdes)
629{
630 return le32_to_cpu(txdes->txdes3);
631}
632
633/*
634 * txdes2 is not used by hardware. We use it to keep track of socket buffer.
635 * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
636 */
637static void ftgmac100_txdes_set_skb(struct ftgmac100_txdes *txdes,
638 struct sk_buff *skb)
639{
640 txdes->txdes2 = (unsigned int)skb;
641}
642
643static struct sk_buff *ftgmac100_txdes_get_skb(struct ftgmac100_txdes *txdes)
644{
645 return (struct sk_buff *)txdes->txdes2;
646}
647
648/******************************************************************************
649 * internal functions (transmit)
650 *****************************************************************************/
651static int ftgmac100_next_tx_pointer(int pointer)
652{
653 return (pointer + 1) & (TX_QUEUE_ENTRIES - 1);
654}
655
656static void ftgmac100_tx_pointer_advance(struct ftgmac100 *priv)
657{
658 priv->tx_pointer = ftgmac100_next_tx_pointer(priv->tx_pointer);
659}
660
661static void ftgmac100_tx_clean_pointer_advance(struct ftgmac100 *priv)
662{
663 priv->tx_clean_pointer = ftgmac100_next_tx_pointer(priv->tx_clean_pointer);
664}
665
666static struct ftgmac100_txdes *ftgmac100_current_txdes(struct ftgmac100 *priv)
667{
668 return &priv->descs->txdes[priv->tx_pointer];
669}
670
671static struct ftgmac100_txdes *
672ftgmac100_current_clean_txdes(struct ftgmac100 *priv)
673{
674 return &priv->descs->txdes[priv->tx_clean_pointer];
675}
676
677static bool ftgmac100_tx_complete_packet(struct ftgmac100 *priv)
678{
679 struct net_device *netdev = priv->netdev;
680 struct ftgmac100_txdes *txdes;
681 struct sk_buff *skb;
682 dma_addr_t map;
683
684 if (priv->tx_pending == 0)
685 return false;
686
687 txdes = ftgmac100_current_clean_txdes(priv);
688
689 if (ftgmac100_txdes_owned_by_dma(txdes))
690 return false;
691
692 skb = ftgmac100_txdes_get_skb(txdes);
693 map = ftgmac100_txdes_get_dma_addr(txdes);
694
695 netdev->stats.tx_packets++;
696 netdev->stats.tx_bytes += skb->len;
697
698 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
699
700 dev_kfree_skb(skb);
701
702 ftgmac100_txdes_reset(txdes);
703
704 ftgmac100_tx_clean_pointer_advance(priv);
705
706 spin_lock(&priv->tx_lock);
707 priv->tx_pending--;
708 spin_unlock(&priv->tx_lock);
709 netif_wake_queue(netdev);
710
711 return true;
712}
713
714static void ftgmac100_tx_complete(struct ftgmac100 *priv)
715{
716 while (ftgmac100_tx_complete_packet(priv))
717 ;
718}
719
720static int ftgmac100_xmit(struct ftgmac100 *priv, struct sk_buff *skb,
721 dma_addr_t map)
722{
723 struct net_device *netdev = priv->netdev;
724 struct ftgmac100_txdes *txdes;
725 unsigned int len = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
726
727 txdes = ftgmac100_current_txdes(priv);
728 ftgmac100_tx_pointer_advance(priv);
729
730 /* setup TX descriptor */
731 ftgmac100_txdes_set_skb(txdes, skb);
732 ftgmac100_txdes_set_dma_addr(txdes, map);
733 ftgmac100_txdes_set_buffer_size(txdes, len);
734
735 ftgmac100_txdes_set_first_segment(txdes);
736 ftgmac100_txdes_set_last_segment(txdes);
737 ftgmac100_txdes_set_txint(txdes);
738 if (skb->ip_summed == CHECKSUM_PARTIAL) {
739 __be16 protocol = skb->protocol;
740
741 if (protocol == cpu_to_be16(ETH_P_IP)) {
742 u8 ip_proto = ip_hdr(skb)->protocol;
743
744 ftgmac100_txdes_set_ipcs(txdes);
745 if (ip_proto == IPPROTO_TCP)
746 ftgmac100_txdes_set_tcpcs(txdes);
747 else if (ip_proto == IPPROTO_UDP)
748 ftgmac100_txdes_set_udpcs(txdes);
749 }
750 }
751
752 spin_lock(&priv->tx_lock);
753 priv->tx_pending++;
754 if (priv->tx_pending == TX_QUEUE_ENTRIES)
755 netif_stop_queue(netdev);
756
757 /* start transmit */
758 ftgmac100_txdes_set_dma_own(txdes);
759 spin_unlock(&priv->tx_lock);
760
761 ftgmac100_txdma_normal_prio_start_polling(priv);
762
763 return NETDEV_TX_OK;
764}
765
766/******************************************************************************
767 * internal functions (buffer)
768 *****************************************************************************/
769static int ftgmac100_alloc_rx_page(struct ftgmac100 *priv,
770 struct ftgmac100_rxdes *rxdes, gfp_t gfp)
771{
772 struct net_device *netdev = priv->netdev;
773 struct page *page;
774 dma_addr_t map;
775
776 page = alloc_page(gfp);
777 if (!page) {
778 if (net_ratelimit())
779 netdev_err(netdev, "failed to allocate rx page\n");
780 return -ENOMEM;
781 }
782
783 map = dma_map_page(priv->dev, page, 0, RX_BUF_SIZE, DMA_FROM_DEVICE);
784 if (unlikely(dma_mapping_error(priv->dev, map))) {
785 if (net_ratelimit())
786 netdev_err(netdev, "failed to map rx page\n");
787 __free_page(page);
788 return -ENOMEM;
789 }
790
791 ftgmac100_rxdes_set_page(rxdes, page);
792 ftgmac100_rxdes_set_dma_addr(rxdes, map);
793 ftgmac100_rxdes_set_dma_own(rxdes);
794 return 0;
795}
796
797static void ftgmac100_free_buffers(struct ftgmac100 *priv)
798{
799 int i;
800
801 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
802 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
803 struct page *page = ftgmac100_rxdes_get_page(rxdes);
804 dma_addr_t map = ftgmac100_rxdes_get_dma_addr(rxdes);
805
806 if (!page)
807 continue;
808
809 dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
810 __free_page(page);
811 }
812
813 for (i = 0; i < TX_QUEUE_ENTRIES; i++) {
814 struct ftgmac100_txdes *txdes = &priv->descs->txdes[i];
815 struct sk_buff *skb = ftgmac100_txdes_get_skb(txdes);
816 dma_addr_t map = ftgmac100_txdes_get_dma_addr(txdes);
817
818 if (!skb)
819 continue;
820
821 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
Eric Dumazet0113e342014-01-16 23:38:24 -0800822 kfree_skb(skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000823 }
824
825 dma_free_coherent(priv->dev, sizeof(struct ftgmac100_descs),
826 priv->descs, priv->descs_dma_addr);
827}
828
829static int ftgmac100_alloc_buffers(struct ftgmac100 *priv)
830{
831 int i;
832
Joe Perchesede23fa82013-08-26 22:45:23 -0700833 priv->descs = dma_zalloc_coherent(priv->dev,
834 sizeof(struct ftgmac100_descs),
835 &priv->descs_dma_addr, GFP_KERNEL);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000836 if (!priv->descs)
837 return -ENOMEM;
838
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000839 /* initialize RX ring */
840 ftgmac100_rxdes_set_end_of_ring(&priv->descs->rxdes[RX_QUEUE_ENTRIES - 1]);
841
842 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
843 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
844
845 if (ftgmac100_alloc_rx_page(priv, rxdes, GFP_KERNEL))
846 goto err;
847 }
848
849 /* initialize TX ring */
850 ftgmac100_txdes_set_end_of_ring(&priv->descs->txdes[TX_QUEUE_ENTRIES - 1]);
851 return 0;
852
853err:
854 ftgmac100_free_buffers(priv);
855 return -ENOMEM;
856}
857
858/******************************************************************************
859 * internal functions (mdio)
860 *****************************************************************************/
861static void ftgmac100_adjust_link(struct net_device *netdev)
862{
863 struct ftgmac100 *priv = netdev_priv(netdev);
Philippe Reynesb3c40ad2016-05-16 01:35:13 +0200864 struct phy_device *phydev = netdev->phydev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000865 int ier;
866
867 if (phydev->speed == priv->old_speed)
868 return;
869
870 priv->old_speed = phydev->speed;
871
872 ier = ioread32(priv->base + FTGMAC100_OFFSET_IER);
873
874 /* disable all interrupts */
875 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
876
877 netif_stop_queue(netdev);
878 ftgmac100_stop_hw(priv);
879
880 netif_start_queue(netdev);
881 ftgmac100_init_hw(priv);
882 ftgmac100_start_hw(priv, phydev->speed);
883
884 /* re-enable interrupts */
885 iowrite32(ier, priv->base + FTGMAC100_OFFSET_IER);
886}
887
888static int ftgmac100_mii_probe(struct ftgmac100 *priv)
889{
890 struct net_device *netdev = priv->netdev;
Guenter Roecke574f392016-01-10 12:04:32 -0800891 struct phy_device *phydev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000892
Guenter Roecke574f392016-01-10 12:04:32 -0800893 phydev = phy_find_first(priv->mii_bus);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000894 if (!phydev) {
895 netdev_info(netdev, "%s: no PHY found\n", netdev->name);
896 return -ENODEV;
897 }
898
Andrew Lunn84eff6d2016-01-06 20:11:10 +0100899 phydev = phy_connect(netdev, phydev_name(phydev),
Florian Fainellif9a8f832013-01-14 00:52:52 +0000900 &ftgmac100_adjust_link, PHY_INTERFACE_MODE_GMII);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000901
902 if (IS_ERR(phydev)) {
903 netdev_err(netdev, "%s: Could not attach to PHY\n", netdev->name);
904 return PTR_ERR(phydev);
905 }
906
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000907 return 0;
908}
909
910/******************************************************************************
911 * struct mii_bus functions
912 *****************************************************************************/
913static int ftgmac100_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
914{
915 struct net_device *netdev = bus->priv;
916 struct ftgmac100 *priv = netdev_priv(netdev);
917 unsigned int phycr;
918 int i;
919
920 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
921
922 /* preserve MDC cycle threshold */
923 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
924
925 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
926 FTGMAC100_PHYCR_REGAD(regnum) |
927 FTGMAC100_PHYCR_MIIRD;
928
929 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
930
931 for (i = 0; i < 10; i++) {
932 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
933
934 if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) {
935 int data;
936
937 data = ioread32(priv->base + FTGMAC100_OFFSET_PHYDATA);
938 return FTGMAC100_PHYDATA_MIIRDATA(data);
939 }
940
941 udelay(100);
942 }
943
944 netdev_err(netdev, "mdio read timed out\n");
945 return -EIO;
946}
947
948static int ftgmac100_mdiobus_write(struct mii_bus *bus, int phy_addr,
949 int regnum, u16 value)
950{
951 struct net_device *netdev = bus->priv;
952 struct ftgmac100 *priv = netdev_priv(netdev);
953 unsigned int phycr;
954 int data;
955 int i;
956
957 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
958
959 /* preserve MDC cycle threshold */
960 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
961
962 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
963 FTGMAC100_PHYCR_REGAD(regnum) |
964 FTGMAC100_PHYCR_MIIWR;
965
966 data = FTGMAC100_PHYDATA_MIIWDATA(value);
967
968 iowrite32(data, priv->base + FTGMAC100_OFFSET_PHYDATA);
969 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
970
971 for (i = 0; i < 10; i++) {
972 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
973
974 if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0)
975 return 0;
976
977 udelay(100);
978 }
979
980 netdev_err(netdev, "mdio write timed out\n");
981 return -EIO;
982}
983
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000984/******************************************************************************
985 * struct ethtool_ops functions
986 *****************************************************************************/
987static void ftgmac100_get_drvinfo(struct net_device *netdev,
988 struct ethtool_drvinfo *info)
989{
Jiri Pirko7826d432013-01-06 00:44:26 +0000990 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
991 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
992 strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info));
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000993}
994
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000995static const struct ethtool_ops ftgmac100_ethtool_ops = {
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000996 .get_drvinfo = ftgmac100_get_drvinfo,
997 .get_link = ethtool_op_get_link,
Philippe Reynesfd24d722016-05-16 01:35:14 +0200998 .get_link_ksettings = phy_ethtool_get_link_ksettings,
999 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001000};
1001
1002/******************************************************************************
1003 * interrupt handler
1004 *****************************************************************************/
1005static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id)
1006{
1007 struct net_device *netdev = dev_id;
1008 struct ftgmac100 *priv = netdev_priv(netdev);
1009
Gavin Shanbd466c32016-07-19 11:54:23 +10001010 /* When running in NCSI mode, the interface should be ready for
1011 * receiving or transmitting NCSI packets before it's opened.
1012 */
1013 if (likely(priv->use_ncsi || netif_running(netdev))) {
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001014 /* Disable interrupts for polling */
1015 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1016 napi_schedule(&priv->napi);
1017 }
1018
1019 return IRQ_HANDLED;
1020}
1021
1022/******************************************************************************
1023 * struct napi_struct functions
1024 *****************************************************************************/
1025static int ftgmac100_poll(struct napi_struct *napi, int budget)
1026{
1027 struct ftgmac100 *priv = container_of(napi, struct ftgmac100, napi);
1028 struct net_device *netdev = priv->netdev;
1029 unsigned int status;
1030 bool completed = true;
1031 int rx = 0;
1032
1033 status = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
1034 iowrite32(status, priv->base + FTGMAC100_OFFSET_ISR);
1035
1036 if (status & (FTGMAC100_INT_RPKT_BUF | FTGMAC100_INT_NO_RXBUF)) {
1037 /*
1038 * FTGMAC100_INT_RPKT_BUF:
1039 * RX DMA has received packets into RX buffer successfully
1040 *
1041 * FTGMAC100_INT_NO_RXBUF:
1042 * RX buffer unavailable
1043 */
1044 bool retry;
1045
1046 do {
1047 retry = ftgmac100_rx_packet(priv, &rx);
1048 } while (retry && rx < budget);
1049
1050 if (retry && rx == budget)
1051 completed = false;
1052 }
1053
1054 if (status & (FTGMAC100_INT_XPKT_ETH | FTGMAC100_INT_XPKT_LOST)) {
1055 /*
1056 * FTGMAC100_INT_XPKT_ETH:
1057 * packet transmitted to ethernet successfully
1058 *
1059 * FTGMAC100_INT_XPKT_LOST:
1060 * packet transmitted to ethernet lost due to late
1061 * collision or excessive collision
1062 */
1063 ftgmac100_tx_complete(priv);
1064 }
1065
Gavin Shanfc6061c2016-07-19 11:54:25 +10001066 if (status & priv->int_mask_all & (FTGMAC100_INT_NO_RXBUF |
1067 FTGMAC100_INT_RPKT_LOST | FTGMAC100_INT_AHB_ERR |
1068 FTGMAC100_INT_PHYSTS_CHG)) {
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001069 if (net_ratelimit())
1070 netdev_info(netdev, "[ISR] = 0x%x: %s%s%s%s\n", status,
1071 status & FTGMAC100_INT_NO_RXBUF ? "NO_RXBUF " : "",
1072 status & FTGMAC100_INT_RPKT_LOST ? "RPKT_LOST " : "",
1073 status & FTGMAC100_INT_AHB_ERR ? "AHB_ERR " : "",
1074 status & FTGMAC100_INT_PHYSTS_CHG ? "PHYSTS_CHG" : "");
1075
1076 if (status & FTGMAC100_INT_NO_RXBUF) {
1077 /* RX buffer unavailable */
1078 netdev->stats.rx_over_errors++;
1079 }
1080
1081 if (status & FTGMAC100_INT_RPKT_LOST) {
1082 /* received packet lost due to RX FIFO full */
1083 netdev->stats.rx_fifo_errors++;
1084 }
1085 }
1086
1087 if (completed) {
1088 napi_complete(napi);
1089
1090 /* enable all interrupts */
Gavin Shanfc6061c2016-07-19 11:54:25 +10001091 iowrite32(priv->int_mask_all,
1092 priv->base + FTGMAC100_OFFSET_IER);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001093 }
1094
1095 return rx;
1096}
1097
1098/******************************************************************************
1099 * struct net_device_ops functions
1100 *****************************************************************************/
1101static int ftgmac100_open(struct net_device *netdev)
1102{
1103 struct ftgmac100 *priv = netdev_priv(netdev);
1104 int err;
1105
1106 err = ftgmac100_alloc_buffers(priv);
1107 if (err) {
1108 netdev_err(netdev, "failed to allocate buffers\n");
1109 goto err_alloc;
1110 }
1111
1112 err = request_irq(priv->irq, ftgmac100_interrupt, 0, netdev->name, netdev);
1113 if (err) {
1114 netdev_err(netdev, "failed to request irq %d\n", priv->irq);
1115 goto err_irq;
1116 }
1117
1118 priv->rx_pointer = 0;
1119 priv->tx_clean_pointer = 0;
1120 priv->tx_pointer = 0;
1121 priv->tx_pending = 0;
1122
1123 err = ftgmac100_reset_hw(priv);
1124 if (err)
1125 goto err_hw;
1126
1127 ftgmac100_init_hw(priv);
Gavin Shanbd466c32016-07-19 11:54:23 +10001128 ftgmac100_start_hw(priv, priv->use_ncsi ? 100 : 10);
1129 if (netdev->phydev)
1130 phy_start(netdev->phydev);
1131 else if (priv->use_ncsi)
1132 netif_carrier_on(netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001133
1134 napi_enable(&priv->napi);
1135 netif_start_queue(netdev);
1136
1137 /* enable all interrupts */
Gavin Shanfc6061c2016-07-19 11:54:25 +10001138 iowrite32(priv->int_mask_all, priv->base + FTGMAC100_OFFSET_IER);
Gavin Shanbd466c32016-07-19 11:54:23 +10001139
1140 /* Start the NCSI device */
1141 if (priv->use_ncsi) {
1142 err = ncsi_start_dev(priv->ndev);
1143 if (err)
1144 goto err_ncsi;
1145 }
1146
1147 priv->enabled = true;
1148
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001149 return 0;
1150
Gavin Shanbd466c32016-07-19 11:54:23 +10001151err_ncsi:
1152 napi_disable(&priv->napi);
1153 netif_stop_queue(netdev);
1154 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001155err_hw:
1156 free_irq(priv->irq, netdev);
1157err_irq:
1158 ftgmac100_free_buffers(priv);
1159err_alloc:
1160 return err;
1161}
1162
1163static int ftgmac100_stop(struct net_device *netdev)
1164{
1165 struct ftgmac100 *priv = netdev_priv(netdev);
1166
Gavin Shanbd466c32016-07-19 11:54:23 +10001167 if (!priv->enabled)
1168 return 0;
1169
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001170 /* disable all interrupts */
Gavin Shanbd466c32016-07-19 11:54:23 +10001171 priv->enabled = false;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001172 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1173
1174 netif_stop_queue(netdev);
1175 napi_disable(&priv->napi);
Gavin Shanbd466c32016-07-19 11:54:23 +10001176 if (netdev->phydev)
1177 phy_stop(netdev->phydev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001178
1179 ftgmac100_stop_hw(priv);
1180 free_irq(priv->irq, netdev);
1181 ftgmac100_free_buffers(priv);
1182
1183 return 0;
1184}
1185
1186static int ftgmac100_hard_start_xmit(struct sk_buff *skb,
1187 struct net_device *netdev)
1188{
1189 struct ftgmac100 *priv = netdev_priv(netdev);
1190 dma_addr_t map;
1191
1192 if (unlikely(skb->len > MAX_PKT_SIZE)) {
1193 if (net_ratelimit())
1194 netdev_dbg(netdev, "tx packet too big\n");
1195
1196 netdev->stats.tx_dropped++;
Eric Dumazet0113e342014-01-16 23:38:24 -08001197 kfree_skb(skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001198 return NETDEV_TX_OK;
1199 }
1200
1201 map = dma_map_single(priv->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
1202 if (unlikely(dma_mapping_error(priv->dev, map))) {
1203 /* drop packet */
1204 if (net_ratelimit())
1205 netdev_err(netdev, "map socket buffer failed\n");
1206
1207 netdev->stats.tx_dropped++;
Eric Dumazet0113e342014-01-16 23:38:24 -08001208 kfree_skb(skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001209 return NETDEV_TX_OK;
1210 }
1211
1212 return ftgmac100_xmit(priv, skb, map);
1213}
1214
1215/* optional */
1216static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1217{
Gavin Shanbd466c32016-07-19 11:54:23 +10001218 if (!netdev->phydev)
1219 return -ENXIO;
1220
Philippe Reynesb3c40ad2016-05-16 01:35:13 +02001221 return phy_mii_ioctl(netdev->phydev, ifr, cmd);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001222}
1223
1224static const struct net_device_ops ftgmac100_netdev_ops = {
1225 .ndo_open = ftgmac100_open,
1226 .ndo_stop = ftgmac100_stop,
1227 .ndo_start_xmit = ftgmac100_hard_start_xmit,
Gavin Shan113ce102016-07-19 11:54:22 +10001228 .ndo_set_mac_address = ftgmac100_set_mac_addr,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001229 .ndo_validate_addr = eth_validate_addr,
1230 .ndo_do_ioctl = ftgmac100_do_ioctl,
1231};
1232
Gavin Shaneb418182016-07-19 11:54:21 +10001233static int ftgmac100_setup_mdio(struct net_device *netdev)
1234{
1235 struct ftgmac100 *priv = netdev_priv(netdev);
1236 struct platform_device *pdev = to_platform_device(priv->dev);
1237 int i, err = 0;
1238
1239 /* initialize mdio bus */
1240 priv->mii_bus = mdiobus_alloc();
1241 if (!priv->mii_bus)
1242 return -EIO;
1243
1244 priv->mii_bus->name = "ftgmac100_mdio";
1245 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d",
1246 pdev->name, pdev->id);
1247 priv->mii_bus->priv = priv->netdev;
1248 priv->mii_bus->read = ftgmac100_mdiobus_read;
1249 priv->mii_bus->write = ftgmac100_mdiobus_write;
1250
1251 for (i = 0; i < PHY_MAX_ADDR; i++)
1252 priv->mii_bus->irq[i] = PHY_POLL;
1253
1254 err = mdiobus_register(priv->mii_bus);
1255 if (err) {
1256 dev_err(priv->dev, "Cannot register MDIO bus!\n");
1257 goto err_register_mdiobus;
1258 }
1259
1260 err = ftgmac100_mii_probe(priv);
1261 if (err) {
1262 dev_err(priv->dev, "MII Probe failed!\n");
1263 goto err_mii_probe;
1264 }
1265
1266 return 0;
1267
1268err_mii_probe:
1269 mdiobus_unregister(priv->mii_bus);
1270err_register_mdiobus:
1271 mdiobus_free(priv->mii_bus);
1272 return err;
1273}
1274
1275static void ftgmac100_destroy_mdio(struct net_device *netdev)
1276{
1277 struct ftgmac100 *priv = netdev_priv(netdev);
1278
1279 if (!netdev->phydev)
1280 return;
1281
1282 phy_disconnect(netdev->phydev);
1283 mdiobus_unregister(priv->mii_bus);
1284 mdiobus_free(priv->mii_bus);
1285}
1286
Gavin Shanbd466c32016-07-19 11:54:23 +10001287static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
1288{
1289 if (unlikely(nd->state != ncsi_dev_state_functional))
1290 return;
1291
1292 netdev_info(nd->dev, "NCSI interface %s\n",
1293 nd->link_up ? "up" : "down");
1294}
1295
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001296/******************************************************************************
1297 * struct platform_driver functions
1298 *****************************************************************************/
1299static int ftgmac100_probe(struct platform_device *pdev)
1300{
1301 struct resource *res;
1302 int irq;
1303 struct net_device *netdev;
1304 struct ftgmac100 *priv;
Gavin Shanbd466c32016-07-19 11:54:23 +10001305 int err = 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001306
1307 if (!pdev)
1308 return -ENODEV;
1309
1310 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1311 if (!res)
1312 return -ENXIO;
1313
1314 irq = platform_get_irq(pdev, 0);
1315 if (irq < 0)
1316 return irq;
1317
1318 /* setup net_device */
1319 netdev = alloc_etherdev(sizeof(*priv));
1320 if (!netdev) {
1321 err = -ENOMEM;
1322 goto err_alloc_etherdev;
1323 }
1324
1325 SET_NETDEV_DEV(netdev, &pdev->dev);
1326
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001327 netdev->ethtool_ops = &ftgmac100_ethtool_ops;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001328 netdev->netdev_ops = &ftgmac100_netdev_ops;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001329
1330 platform_set_drvdata(pdev, netdev);
1331
1332 /* setup private data */
1333 priv = netdev_priv(netdev);
1334 priv->netdev = netdev;
1335 priv->dev = &pdev->dev;
1336
1337 spin_lock_init(&priv->tx_lock);
1338
1339 /* initialize NAPI */
1340 netif_napi_add(netdev, &priv->napi, ftgmac100_poll, 64);
1341
1342 /* map io memory */
1343 priv->res = request_mem_region(res->start, resource_size(res),
1344 dev_name(&pdev->dev));
1345 if (!priv->res) {
1346 dev_err(&pdev->dev, "Could not reserve memory region\n");
1347 err = -ENOMEM;
1348 goto err_req_mem;
1349 }
1350
1351 priv->base = ioremap(res->start, resource_size(res));
1352 if (!priv->base) {
1353 dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
1354 err = -EIO;
1355 goto err_ioremap;
1356 }
1357
1358 priv->irq = irq;
1359
Gavin Shan113ce102016-07-19 11:54:22 +10001360 /* MAC address from chip or random one */
1361 ftgmac100_setup_mac(priv);
1362
Gavin Shanfc6061c2016-07-19 11:54:25 +10001363 priv->int_mask_all = (FTGMAC100_INT_RPKT_LOST |
1364 FTGMAC100_INT_XPKT_ETH |
1365 FTGMAC100_INT_XPKT_LOST |
1366 FTGMAC100_INT_AHB_ERR |
1367 FTGMAC100_INT_PHYSTS_CHG |
1368 FTGMAC100_INT_RPKT_BUF |
1369 FTGMAC100_INT_NO_RXBUF);
Gavin Shanbd466c32016-07-19 11:54:23 +10001370 if (pdev->dev.of_node &&
1371 of_get_property(pdev->dev.of_node, "use-ncsi", NULL)) {
1372 if (!IS_ENABLED(CONFIG_NET_NCSI)) {
1373 dev_err(&pdev->dev, "NCSI stack not enabled\n");
1374 goto err_ncsi_dev;
1375 }
1376
1377 dev_info(&pdev->dev, "Using NCSI interface\n");
1378 priv->use_ncsi = true;
Gavin Shanfc6061c2016-07-19 11:54:25 +10001379 priv->int_mask_all &= ~FTGMAC100_INT_PHYSTS_CHG;
Gavin Shanbd466c32016-07-19 11:54:23 +10001380 priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
1381 if (!priv->ndev)
1382 goto err_ncsi_dev;
1383 } else {
1384 priv->use_ncsi = false;
1385 err = ftgmac100_setup_mdio(netdev);
1386 if (err)
1387 goto err_setup_mdio;
1388 }
1389
1390 /* We have to disable on-chip IP checksum functionality
1391 * when NCSI is enabled on the interface. It doesn't work
1392 * in that case.
1393 */
1394 netdev->features = NETIF_F_IP_CSUM | NETIF_F_GRO;
1395 if (priv->use_ncsi &&
1396 of_get_property(pdev->dev.of_node, "no-hw-checksum", NULL))
1397 netdev->features &= ~NETIF_F_IP_CSUM;
1398
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001399
1400 /* register network device */
1401 err = register_netdev(netdev);
1402 if (err) {
1403 dev_err(&pdev->dev, "Failed to register netdev\n");
1404 goto err_register_netdev;
1405 }
1406
1407 netdev_info(netdev, "irq %d, mapped at %p\n", priv->irq, priv->base);
1408
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001409 return 0;
1410
Gavin Shanbd466c32016-07-19 11:54:23 +10001411err_ncsi_dev:
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001412err_register_netdev:
Gavin Shaneb418182016-07-19 11:54:21 +10001413 ftgmac100_destroy_mdio(netdev);
1414err_setup_mdio:
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001415 iounmap(priv->base);
1416err_ioremap:
1417 release_resource(priv->res);
1418err_req_mem:
1419 netif_napi_del(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001420 free_netdev(netdev);
1421err_alloc_etherdev:
1422 return err;
1423}
1424
1425static int __exit ftgmac100_remove(struct platform_device *pdev)
1426{
1427 struct net_device *netdev;
1428 struct ftgmac100 *priv;
1429
1430 netdev = platform_get_drvdata(pdev);
1431 priv = netdev_priv(netdev);
1432
1433 unregister_netdev(netdev);
Gavin Shaneb418182016-07-19 11:54:21 +10001434 ftgmac100_destroy_mdio(netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001435
1436 iounmap(priv->base);
1437 release_resource(priv->res);
1438
1439 netif_napi_del(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001440 free_netdev(netdev);
1441 return 0;
1442}
1443
Gavin Shanbb168e22016-07-19 11:54:24 +10001444static const struct of_device_id ftgmac100_of_match[] = {
1445 { .compatible = "faraday,ftgmac100" },
1446 { }
1447};
1448MODULE_DEVICE_TABLE(of, ftgmac100_of_match);
1449
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001450static struct platform_driver ftgmac100_driver = {
Gavin Shanbb168e22016-07-19 11:54:24 +10001451 .probe = ftgmac100_probe,
1452 .remove = __exit_p(ftgmac100_remove),
1453 .driver = {
1454 .name = DRV_NAME,
1455 .of_match_table = ftgmac100_of_match,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001456 },
1457};
Sachin Kamat14f645d2013-03-18 01:50:48 +00001458module_platform_driver(ftgmac100_driver);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001459
1460MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
1461MODULE_DESCRIPTION("FTGMAC100 driver");
1462MODULE_LICENSE("GPL");