blob: 298f6f0da0d2f4d099c3d8f6946a023d6dee4840 [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>
Mark Brown3af887c2017-03-30 17:00:12 +010031#include <linux/of.h>
Po-Yu Chuang69785b72011-06-08 23:32:48 +000032#include <linux/phy.h>
33#include <linux/platform_device.h>
Mark Brown3af887c2017-03-30 17:00:12 +010034#include <linux/property.h>
Po-Yu Chuang69785b72011-06-08 23:32:48 +000035#include <net/ip.h>
Gavin Shanbd466c32016-07-19 11:54:23 +100036#include <net/ncsi.h>
Po-Yu Chuang69785b72011-06-08 23:32:48 +000037
38#include "ftgmac100.h"
39
40#define DRV_NAME "ftgmac100"
41#define DRV_VERSION "0.7"
42
43#define RX_QUEUE_ENTRIES 256 /* must be power of 2 */
44#define TX_QUEUE_ENTRIES 512 /* must be power of 2 */
45
46#define MAX_PKT_SIZE 1518
47#define RX_BUF_SIZE PAGE_SIZE /* must be smaller than 0x3fff */
48
Po-Yu Chuang69785b72011-06-08 23:32:48 +000049struct ftgmac100_descs {
50 struct ftgmac100_rxdes rxdes[RX_QUEUE_ENTRIES];
51 struct ftgmac100_txdes txdes[TX_QUEUE_ENTRIES];
52};
53
54struct ftgmac100 {
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100055 /* Registers */
Po-Yu Chuang69785b72011-06-08 23:32:48 +000056 struct resource *res;
57 void __iomem *base;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000058
59 struct ftgmac100_descs *descs;
60 dma_addr_t descs_dma_addr;
61
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100062 /* Rx ring */
Andrew Jefferyada66b52016-09-22 08:34:58 +093063 struct page *rx_pages[RX_QUEUE_ENTRIES];
Po-Yu Chuang69785b72011-06-08 23:32:48 +000064 unsigned int rx_pointer;
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100065 u32 rxdes0_edorr_mask;
66
67 /* Tx ring */
Po-Yu Chuang69785b72011-06-08 23:32:48 +000068 unsigned int tx_clean_pointer;
69 unsigned int tx_pointer;
70 unsigned int tx_pending;
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100071 u32 txdes0_edotr_mask;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000072 spinlock_t tx_lock;
73
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100074 /* Component structures */
Po-Yu Chuang69785b72011-06-08 23:32:48 +000075 struct net_device *netdev;
76 struct device *dev;
Gavin Shanbd466c32016-07-19 11:54:23 +100077 struct ncsi_dev *ndev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000078 struct napi_struct napi;
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +100079 struct work_struct reset_task;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000080 struct mii_bus *mii_bus;
Andrew Jeffery7906a4da2016-09-22 08:34:59 +093081
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100082 /* Link management */
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +100083 int cur_speed;
84 int cur_duplex;
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100085 bool use_ncsi;
86
87 /* Misc */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +100088 bool need_mac_restart;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000089};
90
Po-Yu Chuang69785b72011-06-08 23:32:48 +000091static void ftgmac100_set_rx_ring_base(struct ftgmac100 *priv, dma_addr_t addr)
92{
93 iowrite32(addr, priv->base + FTGMAC100_OFFSET_RXR_BADR);
94}
95
96static void ftgmac100_set_rx_buffer_size(struct ftgmac100 *priv,
97 unsigned int size)
98{
99 size = FTGMAC100_RBSR_SIZE(size);
100 iowrite32(size, priv->base + FTGMAC100_OFFSET_RBSR);
101}
102
103static void ftgmac100_set_normal_prio_tx_ring_base(struct ftgmac100 *priv,
104 dma_addr_t addr)
105{
106 iowrite32(addr, priv->base + FTGMAC100_OFFSET_NPTXR_BADR);
107}
108
109static void ftgmac100_txdma_normal_prio_start_polling(struct ftgmac100 *priv)
110{
111 iowrite32(1, priv->base + FTGMAC100_OFFSET_NPTXPD);
112}
113
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000114static int ftgmac100_reset_mac(struct ftgmac100 *priv, u32 maccr)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000115{
116 struct net_device *netdev = priv->netdev;
117 int i;
118
119 /* NOTE: reset clears all registers */
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000120 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
121 iowrite32(maccr | FTGMAC100_MACCR_SW_RST,
122 priv->base + FTGMAC100_OFFSET_MACCR);
123 for (i = 0; i < 50; i++) {
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000124 unsigned int maccr;
125
126 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
127 if (!(maccr & FTGMAC100_MACCR_SW_RST))
128 return 0;
129
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000130 udelay(1);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000131 }
132
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000133 netdev_err(netdev, "Hardware reset failed\n");
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000134 return -EIO;
135}
136
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000137static int ftgmac100_reset_and_config_mac(struct ftgmac100 *priv)
138{
139 u32 maccr = 0;
140
141 switch (priv->cur_speed) {
142 case SPEED_10:
143 case 0: /* no link */
144 break;
145
146 case SPEED_100:
147 maccr |= FTGMAC100_MACCR_FAST_MODE;
148 break;
149
150 case SPEED_1000:
151 maccr |= FTGMAC100_MACCR_GIGA_MODE;
152 break;
153 default:
154 netdev_err(priv->netdev, "Unknown speed %d !\n",
155 priv->cur_speed);
156 break;
157 }
158
159 /* (Re)initialize the queue pointers */
160 priv->rx_pointer = 0;
161 priv->tx_clean_pointer = 0;
162 priv->tx_pointer = 0;
163 priv->tx_pending = 0;
164
165 /* The doc says reset twice with 10us interval */
166 if (ftgmac100_reset_mac(priv, maccr))
167 return -EIO;
168 usleep_range(10, 1000);
169 return ftgmac100_reset_mac(priv, maccr);
170}
171
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000172static void ftgmac100_set_mac(struct ftgmac100 *priv, const unsigned char *mac)
173{
174 unsigned int maddr = mac[0] << 8 | mac[1];
175 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
176
177 iowrite32(maddr, priv->base + FTGMAC100_OFFSET_MAC_MADR);
178 iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR);
179}
180
Gavin Shan113ce102016-07-19 11:54:22 +1000181static void ftgmac100_setup_mac(struct ftgmac100 *priv)
182{
183 u8 mac[ETH_ALEN];
184 unsigned int m;
185 unsigned int l;
186 void *addr;
187
188 addr = device_get_mac_address(priv->dev, mac, ETH_ALEN);
189 if (addr) {
190 ether_addr_copy(priv->netdev->dev_addr, mac);
191 dev_info(priv->dev, "Read MAC address %pM from device tree\n",
192 mac);
193 return;
194 }
195
196 m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR);
197 l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR);
198
199 mac[0] = (m >> 8) & 0xff;
200 mac[1] = m & 0xff;
201 mac[2] = (l >> 24) & 0xff;
202 mac[3] = (l >> 16) & 0xff;
203 mac[4] = (l >> 8) & 0xff;
204 mac[5] = l & 0xff;
205
Gavin Shan113ce102016-07-19 11:54:22 +1000206 if (is_valid_ether_addr(mac)) {
207 ether_addr_copy(priv->netdev->dev_addr, mac);
208 dev_info(priv->dev, "Read MAC address %pM from chip\n", mac);
209 } else {
210 eth_hw_addr_random(priv->netdev);
211 dev_info(priv->dev, "Generated random MAC address %pM\n",
212 priv->netdev->dev_addr);
213 }
214}
215
216static int ftgmac100_set_mac_addr(struct net_device *dev, void *p)
217{
218 int ret;
219
220 ret = eth_prepare_mac_addr_change(dev, p);
221 if (ret < 0)
222 return ret;
223
224 eth_commit_mac_addr_change(dev, p);
225 ftgmac100_set_mac(netdev_priv(dev), dev->dev_addr);
226
227 return 0;
228}
229
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000230static void ftgmac100_init_hw(struct ftgmac100 *priv)
231{
232 /* setup ring buffer base registers */
233 ftgmac100_set_rx_ring_base(priv,
234 priv->descs_dma_addr +
235 offsetof(struct ftgmac100_descs, rxdes));
236 ftgmac100_set_normal_prio_tx_ring_base(priv,
237 priv->descs_dma_addr +
238 offsetof(struct ftgmac100_descs, txdes));
239
240 ftgmac100_set_rx_buffer_size(priv, RX_BUF_SIZE);
241
242 iowrite32(FTGMAC100_APTC_RXPOLL_CNT(1), priv->base + FTGMAC100_OFFSET_APTC);
243
244 ftgmac100_set_mac(priv, priv->netdev->dev_addr);
245}
246
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000247static void ftgmac100_start_hw(struct ftgmac100 *priv)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000248{
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000249 u32 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000250
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000251 /* Keep the original GMAC and FAST bits */
252 maccr &= (FTGMAC100_MACCR_FAST_MODE | FTGMAC100_MACCR_GIGA_MODE);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000253
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000254 /* Add all the main enable bits */
255 maccr |= FTGMAC100_MACCR_TXDMA_EN |
256 FTGMAC100_MACCR_RXDMA_EN |
257 FTGMAC100_MACCR_TXMAC_EN |
258 FTGMAC100_MACCR_RXMAC_EN |
259 FTGMAC100_MACCR_CRC_APD |
260 FTGMAC100_MACCR_PHY_LINK_LEVEL |
261 FTGMAC100_MACCR_RX_RUNT |
262 FTGMAC100_MACCR_RX_BROADPKT;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000263
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000264 /* Add other bits as needed */
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000265 if (priv->cur_duplex == DUPLEX_FULL)
266 maccr |= FTGMAC100_MACCR_FULLDUP;
267
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000268 /* Hit the HW */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000269 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
270}
271
272static void ftgmac100_stop_hw(struct ftgmac100 *priv)
273{
274 iowrite32(0, priv->base + FTGMAC100_OFFSET_MACCR);
275}
276
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000277static bool ftgmac100_rxdes_first_segment(struct ftgmac100_rxdes *rxdes)
278{
279 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_FRS);
280}
281
282static bool ftgmac100_rxdes_last_segment(struct ftgmac100_rxdes *rxdes)
283{
284 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_LRS);
285}
286
287static bool ftgmac100_rxdes_packet_ready(struct ftgmac100_rxdes *rxdes)
288{
289 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RXPKT_RDY);
290}
291
Andrew Jeffery7906a4da2016-09-22 08:34:59 +0930292static void ftgmac100_rxdes_set_dma_own(const struct ftgmac100 *priv,
293 struct ftgmac100_rxdes *rxdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000294{
295 /* clear status bits */
Andrew Jeffery7906a4da2016-09-22 08:34:59 +0930296 rxdes->rxdes0 &= cpu_to_le32(priv->rxdes0_edorr_mask);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000297}
298
299static bool ftgmac100_rxdes_rx_error(struct ftgmac100_rxdes *rxdes)
300{
301 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RX_ERR);
302}
303
304static bool ftgmac100_rxdes_crc_error(struct ftgmac100_rxdes *rxdes)
305{
306 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_CRC_ERR);
307}
308
309static bool ftgmac100_rxdes_frame_too_long(struct ftgmac100_rxdes *rxdes)
310{
311 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_FTL);
312}
313
314static bool ftgmac100_rxdes_runt(struct ftgmac100_rxdes *rxdes)
315{
316 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RUNT);
317}
318
319static bool ftgmac100_rxdes_odd_nibble(struct ftgmac100_rxdes *rxdes)
320{
321 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RX_ODD_NB);
322}
323
324static unsigned int ftgmac100_rxdes_data_length(struct ftgmac100_rxdes *rxdes)
325{
326 return le32_to_cpu(rxdes->rxdes0) & FTGMAC100_RXDES0_VDBC;
327}
328
329static bool ftgmac100_rxdes_multicast(struct ftgmac100_rxdes *rxdes)
330{
331 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_MULTICAST);
332}
333
Andrew Jeffery7906a4da2016-09-22 08:34:59 +0930334static void ftgmac100_rxdes_set_end_of_ring(const struct ftgmac100 *priv,
335 struct ftgmac100_rxdes *rxdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000336{
Andrew Jeffery7906a4da2016-09-22 08:34:59 +0930337 rxdes->rxdes0 |= cpu_to_le32(priv->rxdes0_edorr_mask);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000338}
339
340static void ftgmac100_rxdes_set_dma_addr(struct ftgmac100_rxdes *rxdes,
341 dma_addr_t addr)
342{
343 rxdes->rxdes3 = cpu_to_le32(addr);
344}
345
346static dma_addr_t ftgmac100_rxdes_get_dma_addr(struct ftgmac100_rxdes *rxdes)
347{
348 return le32_to_cpu(rxdes->rxdes3);
349}
350
351static bool ftgmac100_rxdes_is_tcp(struct ftgmac100_rxdes *rxdes)
352{
353 return (rxdes->rxdes1 & cpu_to_le32(FTGMAC100_RXDES1_PROT_MASK)) ==
354 cpu_to_le32(FTGMAC100_RXDES1_PROT_TCPIP);
355}
356
357static bool ftgmac100_rxdes_is_udp(struct ftgmac100_rxdes *rxdes)
358{
359 return (rxdes->rxdes1 & cpu_to_le32(FTGMAC100_RXDES1_PROT_MASK)) ==
360 cpu_to_le32(FTGMAC100_RXDES1_PROT_UDPIP);
361}
362
363static bool ftgmac100_rxdes_tcpcs_err(struct ftgmac100_rxdes *rxdes)
364{
365 return rxdes->rxdes1 & cpu_to_le32(FTGMAC100_RXDES1_TCP_CHKSUM_ERR);
366}
367
368static bool ftgmac100_rxdes_udpcs_err(struct ftgmac100_rxdes *rxdes)
369{
370 return rxdes->rxdes1 & cpu_to_le32(FTGMAC100_RXDES1_UDP_CHKSUM_ERR);
371}
372
373static bool ftgmac100_rxdes_ipcs_err(struct ftgmac100_rxdes *rxdes)
374{
375 return rxdes->rxdes1 & cpu_to_le32(FTGMAC100_RXDES1_IP_CHKSUM_ERR);
376}
377
Andrew Jefferyada66b52016-09-22 08:34:58 +0930378static inline struct page **ftgmac100_rxdes_page_slot(struct ftgmac100 *priv,
379 struct ftgmac100_rxdes *rxdes)
380{
381 return &priv->rx_pages[rxdes - priv->descs->rxdes];
382}
383
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000384/*
385 * rxdes2 is not used by hardware. We use it to keep track of page.
386 * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
387 */
Andrew Jefferyada66b52016-09-22 08:34:58 +0930388static void ftgmac100_rxdes_set_page(struct ftgmac100 *priv,
389 struct ftgmac100_rxdes *rxdes,
390 struct page *page)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000391{
Andrew Jefferyada66b52016-09-22 08:34:58 +0930392 *ftgmac100_rxdes_page_slot(priv, rxdes) = page;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000393}
394
Andrew Jefferyada66b52016-09-22 08:34:58 +0930395static struct page *ftgmac100_rxdes_get_page(struct ftgmac100 *priv,
396 struct ftgmac100_rxdes *rxdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000397{
Andrew Jefferyada66b52016-09-22 08:34:58 +0930398 return *ftgmac100_rxdes_page_slot(priv, rxdes);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000399}
400
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000401static int ftgmac100_alloc_rx_page(struct ftgmac100 *priv,
402 struct ftgmac100_rxdes *rxdes, gfp_t gfp)
403{
404 struct net_device *netdev = priv->netdev;
405 struct page *page;
406 dma_addr_t map;
407
408 page = alloc_page(gfp);
409 if (!page) {
410 if (net_ratelimit())
411 netdev_err(netdev, "failed to allocate rx page\n");
412 return -ENOMEM;
413 }
414
415 map = dma_map_page(priv->dev, page, 0, RX_BUF_SIZE, DMA_FROM_DEVICE);
416 if (unlikely(dma_mapping_error(priv->dev, map))) {
417 if (net_ratelimit())
418 netdev_err(netdev, "failed to map rx page\n");
419 __free_page(page);
420 return -ENOMEM;
421 }
422
423 ftgmac100_rxdes_set_page(priv, rxdes, page);
424 ftgmac100_rxdes_set_dma_addr(rxdes, map);
425 ftgmac100_rxdes_set_dma_own(priv, rxdes);
426 return 0;
427}
428
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000429static int ftgmac100_next_rx_pointer(int pointer)
430{
431 return (pointer + 1) & (RX_QUEUE_ENTRIES - 1);
432}
433
434static void ftgmac100_rx_pointer_advance(struct ftgmac100 *priv)
435{
436 priv->rx_pointer = ftgmac100_next_rx_pointer(priv->rx_pointer);
437}
438
439static struct ftgmac100_rxdes *ftgmac100_current_rxdes(struct ftgmac100 *priv)
440{
441 return &priv->descs->rxdes[priv->rx_pointer];
442}
443
444static struct ftgmac100_rxdes *
445ftgmac100_rx_locate_first_segment(struct ftgmac100 *priv)
446{
447 struct ftgmac100_rxdes *rxdes = ftgmac100_current_rxdes(priv);
448
449 while (ftgmac100_rxdes_packet_ready(rxdes)) {
450 if (ftgmac100_rxdes_first_segment(rxdes))
451 return rxdes;
452
Andrew Jeffery7906a4da2016-09-22 08:34:59 +0930453 ftgmac100_rxdes_set_dma_own(priv, rxdes);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000454 ftgmac100_rx_pointer_advance(priv);
455 rxdes = ftgmac100_current_rxdes(priv);
456 }
457
458 return NULL;
459}
460
461static bool ftgmac100_rx_packet_error(struct ftgmac100 *priv,
462 struct ftgmac100_rxdes *rxdes)
463{
464 struct net_device *netdev = priv->netdev;
465 bool error = false;
466
467 if (unlikely(ftgmac100_rxdes_rx_error(rxdes))) {
468 if (net_ratelimit())
469 netdev_info(netdev, "rx err\n");
470
471 netdev->stats.rx_errors++;
472 error = true;
473 }
474
475 if (unlikely(ftgmac100_rxdes_crc_error(rxdes))) {
476 if (net_ratelimit())
477 netdev_info(netdev, "rx crc err\n");
478
479 netdev->stats.rx_crc_errors++;
480 error = true;
481 } else if (unlikely(ftgmac100_rxdes_ipcs_err(rxdes))) {
482 if (net_ratelimit())
483 netdev_info(netdev, "rx IP checksum err\n");
484
485 error = true;
486 }
487
488 if (unlikely(ftgmac100_rxdes_frame_too_long(rxdes))) {
489 if (net_ratelimit())
490 netdev_info(netdev, "rx frame too long\n");
491
492 netdev->stats.rx_length_errors++;
493 error = true;
494 } else if (unlikely(ftgmac100_rxdes_runt(rxdes))) {
495 if (net_ratelimit())
496 netdev_info(netdev, "rx runt\n");
497
498 netdev->stats.rx_length_errors++;
499 error = true;
500 } else if (unlikely(ftgmac100_rxdes_odd_nibble(rxdes))) {
501 if (net_ratelimit())
502 netdev_info(netdev, "rx odd nibble\n");
503
504 netdev->stats.rx_length_errors++;
505 error = true;
506 }
507
508 return error;
509}
510
511static void ftgmac100_rx_drop_packet(struct ftgmac100 *priv)
512{
513 struct net_device *netdev = priv->netdev;
514 struct ftgmac100_rxdes *rxdes = ftgmac100_current_rxdes(priv);
515 bool done = false;
516
517 if (net_ratelimit())
518 netdev_dbg(netdev, "drop packet %p\n", rxdes);
519
520 do {
521 if (ftgmac100_rxdes_last_segment(rxdes))
522 done = true;
523
Andrew Jeffery7906a4da2016-09-22 08:34:59 +0930524 ftgmac100_rxdes_set_dma_own(priv, rxdes);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000525 ftgmac100_rx_pointer_advance(priv);
526 rxdes = ftgmac100_current_rxdes(priv);
527 } while (!done && ftgmac100_rxdes_packet_ready(rxdes));
528
529 netdev->stats.rx_dropped++;
530}
531
532static bool ftgmac100_rx_packet(struct ftgmac100 *priv, int *processed)
533{
534 struct net_device *netdev = priv->netdev;
535 struct ftgmac100_rxdes *rxdes;
536 struct sk_buff *skb;
537 bool done = false;
538
539 rxdes = ftgmac100_rx_locate_first_segment(priv);
540 if (!rxdes)
541 return false;
542
543 if (unlikely(ftgmac100_rx_packet_error(priv, rxdes))) {
544 ftgmac100_rx_drop_packet(priv);
545 return true;
546 }
547
548 /* start processing */
549 skb = netdev_alloc_skb_ip_align(netdev, 128);
550 if (unlikely(!skb)) {
551 if (net_ratelimit())
552 netdev_err(netdev, "rx skb alloc failed\n");
553
554 ftgmac100_rx_drop_packet(priv);
555 return true;
556 }
557
558 if (unlikely(ftgmac100_rxdes_multicast(rxdes)))
559 netdev->stats.multicast++;
560
561 /*
562 * It seems that HW does checksum incorrectly with fragmented packets,
563 * so we are conservative here - if HW checksum error, let software do
564 * the checksum again.
565 */
566 if ((ftgmac100_rxdes_is_tcp(rxdes) && !ftgmac100_rxdes_tcpcs_err(rxdes)) ||
567 (ftgmac100_rxdes_is_udp(rxdes) && !ftgmac100_rxdes_udpcs_err(rxdes)))
568 skb->ip_summed = CHECKSUM_UNNECESSARY;
569
570 do {
571 dma_addr_t map = ftgmac100_rxdes_get_dma_addr(rxdes);
Andrew Jefferyada66b52016-09-22 08:34:58 +0930572 struct page *page = ftgmac100_rxdes_get_page(priv, rxdes);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000573 unsigned int size;
574
575 dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
576
577 size = ftgmac100_rxdes_data_length(rxdes);
578 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page, 0, size);
579
580 skb->len += size;
581 skb->data_len += size;
Eric Dumazet5935f812011-10-13 11:30:52 +0000582 skb->truesize += PAGE_SIZE;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000583
584 if (ftgmac100_rxdes_last_segment(rxdes))
585 done = true;
586
587 ftgmac100_alloc_rx_page(priv, rxdes, GFP_ATOMIC);
588
589 ftgmac100_rx_pointer_advance(priv);
590 rxdes = ftgmac100_current_rxdes(priv);
591 } while (!done);
592
Eric Dumazet6ecd09d2012-07-12 04:19:38 +0000593 /* Small frames are copied into linear part of skb to free one page */
594 if (skb->len <= 128) {
Eric Dumazet5935f812011-10-13 11:30:52 +0000595 skb->truesize -= PAGE_SIZE;
Eric Dumazet6ecd09d2012-07-12 04:19:38 +0000596 __pskb_pull_tail(skb, skb->len);
597 } else {
598 /* We pull the minimum amount into linear part */
599 __pskb_pull_tail(skb, ETH_HLEN);
600 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000601 skb->protocol = eth_type_trans(skb, netdev);
602
603 netdev->stats.rx_packets++;
604 netdev->stats.rx_bytes += skb->len;
605
606 /* push packet to protocol stack */
607 napi_gro_receive(&priv->napi, skb);
608
609 (*processed)++;
610 return true;
611}
612
Andrew Jeffery7906a4da2016-09-22 08:34:59 +0930613static void ftgmac100_txdes_reset(const struct ftgmac100 *priv,
614 struct ftgmac100_txdes *txdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000615{
616 /* clear all except end of ring bit */
Andrew Jeffery7906a4da2016-09-22 08:34:59 +0930617 txdes->txdes0 &= cpu_to_le32(priv->txdes0_edotr_mask);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000618 txdes->txdes1 = 0;
619 txdes->txdes2 = 0;
620 txdes->txdes3 = 0;
621}
622
623static bool ftgmac100_txdes_owned_by_dma(struct ftgmac100_txdes *txdes)
624{
625 return txdes->txdes0 & cpu_to_le32(FTGMAC100_TXDES0_TXDMA_OWN);
626}
627
628static void ftgmac100_txdes_set_dma_own(struct ftgmac100_txdes *txdes)
629{
630 /*
631 * Make sure dma own bit will not be set before any other
632 * descriptor fields.
633 */
634 wmb();
635 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_TXDMA_OWN);
636}
637
Andrew Jeffery7906a4da2016-09-22 08:34:59 +0930638static void ftgmac100_txdes_set_end_of_ring(const struct ftgmac100 *priv,
639 struct ftgmac100_txdes *txdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000640{
Andrew Jeffery7906a4da2016-09-22 08:34:59 +0930641 txdes->txdes0 |= cpu_to_le32(priv->txdes0_edotr_mask);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000642}
643
644static void ftgmac100_txdes_set_first_segment(struct ftgmac100_txdes *txdes)
645{
646 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_FTS);
647}
648
649static void ftgmac100_txdes_set_last_segment(struct ftgmac100_txdes *txdes)
650{
651 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_LTS);
652}
653
654static void ftgmac100_txdes_set_buffer_size(struct ftgmac100_txdes *txdes,
655 unsigned int len)
656{
657 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_TXBUF_SIZE(len));
658}
659
660static void ftgmac100_txdes_set_txint(struct ftgmac100_txdes *txdes)
661{
662 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_TXIC);
663}
664
665static void ftgmac100_txdes_set_tcpcs(struct ftgmac100_txdes *txdes)
666{
667 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_TCP_CHKSUM);
668}
669
670static void ftgmac100_txdes_set_udpcs(struct ftgmac100_txdes *txdes)
671{
672 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_UDP_CHKSUM);
673}
674
675static void ftgmac100_txdes_set_ipcs(struct ftgmac100_txdes *txdes)
676{
677 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_IP_CHKSUM);
678}
679
680static void ftgmac100_txdes_set_dma_addr(struct ftgmac100_txdes *txdes,
681 dma_addr_t addr)
682{
683 txdes->txdes3 = cpu_to_le32(addr);
684}
685
686static dma_addr_t ftgmac100_txdes_get_dma_addr(struct ftgmac100_txdes *txdes)
687{
688 return le32_to_cpu(txdes->txdes3);
689}
690
691/*
692 * txdes2 is not used by hardware. We use it to keep track of socket buffer.
693 * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
694 */
695static void ftgmac100_txdes_set_skb(struct ftgmac100_txdes *txdes,
696 struct sk_buff *skb)
697{
698 txdes->txdes2 = (unsigned int)skb;
699}
700
701static struct sk_buff *ftgmac100_txdes_get_skb(struct ftgmac100_txdes *txdes)
702{
703 return (struct sk_buff *)txdes->txdes2;
704}
705
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000706static int ftgmac100_next_tx_pointer(int pointer)
707{
708 return (pointer + 1) & (TX_QUEUE_ENTRIES - 1);
709}
710
711static void ftgmac100_tx_pointer_advance(struct ftgmac100 *priv)
712{
713 priv->tx_pointer = ftgmac100_next_tx_pointer(priv->tx_pointer);
714}
715
716static void ftgmac100_tx_clean_pointer_advance(struct ftgmac100 *priv)
717{
718 priv->tx_clean_pointer = ftgmac100_next_tx_pointer(priv->tx_clean_pointer);
719}
720
721static struct ftgmac100_txdes *ftgmac100_current_txdes(struct ftgmac100 *priv)
722{
723 return &priv->descs->txdes[priv->tx_pointer];
724}
725
726static struct ftgmac100_txdes *
727ftgmac100_current_clean_txdes(struct ftgmac100 *priv)
728{
729 return &priv->descs->txdes[priv->tx_clean_pointer];
730}
731
732static bool ftgmac100_tx_complete_packet(struct ftgmac100 *priv)
733{
734 struct net_device *netdev = priv->netdev;
735 struct ftgmac100_txdes *txdes;
736 struct sk_buff *skb;
737 dma_addr_t map;
738
739 if (priv->tx_pending == 0)
740 return false;
741
742 txdes = ftgmac100_current_clean_txdes(priv);
743
744 if (ftgmac100_txdes_owned_by_dma(txdes))
745 return false;
746
747 skb = ftgmac100_txdes_get_skb(txdes);
748 map = ftgmac100_txdes_get_dma_addr(txdes);
749
750 netdev->stats.tx_packets++;
751 netdev->stats.tx_bytes += skb->len;
752
753 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
754
755 dev_kfree_skb(skb);
756
Andrew Jeffery7906a4da2016-09-22 08:34:59 +0930757 ftgmac100_txdes_reset(priv, txdes);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000758
759 ftgmac100_tx_clean_pointer_advance(priv);
760
761 spin_lock(&priv->tx_lock);
762 priv->tx_pending--;
763 spin_unlock(&priv->tx_lock);
764 netif_wake_queue(netdev);
765
766 return true;
767}
768
769static void ftgmac100_tx_complete(struct ftgmac100 *priv)
770{
771 while (ftgmac100_tx_complete_packet(priv))
772 ;
773}
774
775static int ftgmac100_xmit(struct ftgmac100 *priv, struct sk_buff *skb,
776 dma_addr_t map)
777{
778 struct net_device *netdev = priv->netdev;
779 struct ftgmac100_txdes *txdes;
780 unsigned int len = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
781
782 txdes = ftgmac100_current_txdes(priv);
783 ftgmac100_tx_pointer_advance(priv);
784
785 /* setup TX descriptor */
786 ftgmac100_txdes_set_skb(txdes, skb);
787 ftgmac100_txdes_set_dma_addr(txdes, map);
788 ftgmac100_txdes_set_buffer_size(txdes, len);
789
790 ftgmac100_txdes_set_first_segment(txdes);
791 ftgmac100_txdes_set_last_segment(txdes);
792 ftgmac100_txdes_set_txint(txdes);
793 if (skb->ip_summed == CHECKSUM_PARTIAL) {
794 __be16 protocol = skb->protocol;
795
796 if (protocol == cpu_to_be16(ETH_P_IP)) {
797 u8 ip_proto = ip_hdr(skb)->protocol;
798
799 ftgmac100_txdes_set_ipcs(txdes);
800 if (ip_proto == IPPROTO_TCP)
801 ftgmac100_txdes_set_tcpcs(txdes);
802 else if (ip_proto == IPPROTO_UDP)
803 ftgmac100_txdes_set_udpcs(txdes);
804 }
805 }
806
807 spin_lock(&priv->tx_lock);
808 priv->tx_pending++;
809 if (priv->tx_pending == TX_QUEUE_ENTRIES)
810 netif_stop_queue(netdev);
811
812 /* start transmit */
813 ftgmac100_txdes_set_dma_own(txdes);
814 spin_unlock(&priv->tx_lock);
815
816 ftgmac100_txdma_normal_prio_start_polling(priv);
817
818 return NETDEV_TX_OK;
819}
820
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000821static void ftgmac100_free_buffers(struct ftgmac100 *priv)
822{
823 int i;
824
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000825 /* Free all RX buffers */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000826 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
827 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
Andrew Jefferyada66b52016-09-22 08:34:58 +0930828 struct page *page = ftgmac100_rxdes_get_page(priv, rxdes);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000829 dma_addr_t map = ftgmac100_rxdes_get_dma_addr(rxdes);
830
831 if (!page)
832 continue;
833
834 dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
835 __free_page(page);
836 }
837
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000838 /* Free all TX buffers */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000839 for (i = 0; i < TX_QUEUE_ENTRIES; i++) {
840 struct ftgmac100_txdes *txdes = &priv->descs->txdes[i];
841 struct sk_buff *skb = ftgmac100_txdes_get_skb(txdes);
842 dma_addr_t map = ftgmac100_txdes_get_dma_addr(txdes);
843
844 if (!skb)
845 continue;
846
847 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
Eric Dumazet0113e342014-01-16 23:38:24 -0800848 kfree_skb(skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000849 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000850}
851
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000852static void ftgmac100_free_rings(struct ftgmac100 *priv)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000853{
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000854 /* Free descriptors */
855 if (priv->descs)
856 dma_free_coherent(priv->dev, sizeof(struct ftgmac100_descs),
857 priv->descs, priv->descs_dma_addr);
858}
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000859
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000860static int ftgmac100_alloc_rings(struct ftgmac100 *priv)
861{
862 /* Allocate descriptors */
Joe Perchesede23fa82013-08-26 22:45:23 -0700863 priv->descs = dma_zalloc_coherent(priv->dev,
864 sizeof(struct ftgmac100_descs),
865 &priv->descs_dma_addr, GFP_KERNEL);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000866 if (!priv->descs)
867 return -ENOMEM;
868
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000869 return 0;
870}
871
872static void ftgmac100_init_rings(struct ftgmac100 *priv)
873{
874 int i;
875
876 /* Initialize RX ring */
877 for (i = 0; i < RX_QUEUE_ENTRIES; i++)
878 priv->descs->rxdes[i].rxdes0 = 0;
879 ftgmac100_rxdes_set_end_of_ring(priv, &priv->descs->rxdes[i - 1]);
880
881 /* Initialize TX ring */
882 for (i = 0; i < TX_QUEUE_ENTRIES; i++)
883 priv->descs->txdes[i].txdes0 = 0;
884 ftgmac100_txdes_set_end_of_ring(priv, &priv->descs->txdes[i -1]);
885}
886
887static int ftgmac100_alloc_rx_buffers(struct ftgmac100 *priv)
888{
889 int i;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000890
891 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
892 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
893
894 if (ftgmac100_alloc_rx_page(priv, rxdes, GFP_KERNEL))
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000895 return -ENOMEM;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000896 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000897 return 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000898}
899
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000900static void ftgmac100_adjust_link(struct net_device *netdev)
901{
902 struct ftgmac100 *priv = netdev_priv(netdev);
Philippe Reynesb3c40ad2016-05-16 01:35:13 +0200903 struct phy_device *phydev = netdev->phydev;
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000904 int new_speed;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000905
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000906 /* We store "no link" as speed 0 */
907 if (!phydev->link)
908 new_speed = 0;
909 else
910 new_speed = phydev->speed;
911
912 if (phydev->speed == priv->cur_speed &&
913 phydev->duplex == priv->cur_duplex)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000914 return;
915
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000916 /* Print status if we have a link or we had one and just lost it,
917 * don't print otherwise.
918 */
919 if (new_speed || priv->cur_speed)
920 phy_print_status(phydev);
921
922 priv->cur_speed = new_speed;
923 priv->cur_duplex = phydev->duplex;
924
925 /* Link is down, do nothing else */
926 if (!new_speed)
927 return;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000928
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +1000929 /* Disable all interrupts */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000930 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
931
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +1000932 /* Reset the adapter asynchronously */
933 schedule_work(&priv->reset_task);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000934}
935
936static int ftgmac100_mii_probe(struct ftgmac100 *priv)
937{
938 struct net_device *netdev = priv->netdev;
Guenter Roecke574f392016-01-10 12:04:32 -0800939 struct phy_device *phydev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000940
Guenter Roecke574f392016-01-10 12:04:32 -0800941 phydev = phy_find_first(priv->mii_bus);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000942 if (!phydev) {
943 netdev_info(netdev, "%s: no PHY found\n", netdev->name);
944 return -ENODEV;
945 }
946
Andrew Lunn84eff6d2016-01-06 20:11:10 +0100947 phydev = phy_connect(netdev, phydev_name(phydev),
Florian Fainellif9a8f832013-01-14 00:52:52 +0000948 &ftgmac100_adjust_link, PHY_INTERFACE_MODE_GMII);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000949
950 if (IS_ERR(phydev)) {
951 netdev_err(netdev, "%s: Could not attach to PHY\n", netdev->name);
952 return PTR_ERR(phydev);
953 }
954
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000955 return 0;
956}
957
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000958static int ftgmac100_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
959{
960 struct net_device *netdev = bus->priv;
961 struct ftgmac100 *priv = netdev_priv(netdev);
962 unsigned int phycr;
963 int i;
964
965 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
966
967 /* preserve MDC cycle threshold */
968 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
969
970 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
971 FTGMAC100_PHYCR_REGAD(regnum) |
972 FTGMAC100_PHYCR_MIIRD;
973
974 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
975
976 for (i = 0; i < 10; i++) {
977 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
978
979 if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) {
980 int data;
981
982 data = ioread32(priv->base + FTGMAC100_OFFSET_PHYDATA);
983 return FTGMAC100_PHYDATA_MIIRDATA(data);
984 }
985
986 udelay(100);
987 }
988
989 netdev_err(netdev, "mdio read timed out\n");
990 return -EIO;
991}
992
993static int ftgmac100_mdiobus_write(struct mii_bus *bus, int phy_addr,
994 int regnum, u16 value)
995{
996 struct net_device *netdev = bus->priv;
997 struct ftgmac100 *priv = netdev_priv(netdev);
998 unsigned int phycr;
999 int data;
1000 int i;
1001
1002 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1003
1004 /* preserve MDC cycle threshold */
1005 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
1006
1007 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
1008 FTGMAC100_PHYCR_REGAD(regnum) |
1009 FTGMAC100_PHYCR_MIIWR;
1010
1011 data = FTGMAC100_PHYDATA_MIIWDATA(value);
1012
1013 iowrite32(data, priv->base + FTGMAC100_OFFSET_PHYDATA);
1014 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
1015
1016 for (i = 0; i < 10; i++) {
1017 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1018
1019 if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0)
1020 return 0;
1021
1022 udelay(100);
1023 }
1024
1025 netdev_err(netdev, "mdio write timed out\n");
1026 return -EIO;
1027}
1028
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001029static void ftgmac100_get_drvinfo(struct net_device *netdev,
1030 struct ethtool_drvinfo *info)
1031{
Jiri Pirko7826d432013-01-06 00:44:26 +00001032 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1033 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1034 strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info));
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001035}
1036
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001037static const struct ethtool_ops ftgmac100_ethtool_ops = {
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001038 .get_drvinfo = ftgmac100_get_drvinfo,
1039 .get_link = ethtool_op_get_link,
Philippe Reynesfd24d722016-05-16 01:35:14 +02001040 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1041 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001042};
1043
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001044static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id)
1045{
1046 struct net_device *netdev = dev_id;
1047 struct ftgmac100 *priv = netdev_priv(netdev);
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001048 unsigned int status, new_mask = FTGMAC100_INT_BAD;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001049
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001050 /* Fetch and clear interrupt bits, process abnormal ones */
1051 status = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
1052 iowrite32(status, priv->base + FTGMAC100_OFFSET_ISR);
1053 if (unlikely(status & FTGMAC100_INT_BAD)) {
1054
1055 /* RX buffer unavailable */
1056 if (status & FTGMAC100_INT_NO_RXBUF)
1057 netdev->stats.rx_over_errors++;
1058
1059 /* received packet lost due to RX FIFO full */
1060 if (status & FTGMAC100_INT_RPKT_LOST)
1061 netdev->stats.rx_fifo_errors++;
1062
1063 /* sent packet lost due to excessive TX collision */
1064 if (status & FTGMAC100_INT_XPKT_LOST)
1065 netdev->stats.tx_fifo_errors++;
1066
1067 /* AHB error -> Reset the chip */
1068 if (status & FTGMAC100_INT_AHB_ERR) {
1069 if (net_ratelimit())
1070 netdev_warn(netdev,
1071 "AHB bus error ! Resetting chip.\n");
1072 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1073 schedule_work(&priv->reset_task);
1074 return IRQ_HANDLED;
1075 }
1076
1077 /* We may need to restart the MAC after such errors, delay
1078 * this until after we have freed some Rx buffers though
1079 */
1080 priv->need_mac_restart = true;
1081
1082 /* Disable those errors until we restart */
1083 new_mask &= ~status;
1084 }
1085
1086 /* Only enable "bad" interrupts while NAPI is on */
1087 iowrite32(new_mask, priv->base + FTGMAC100_OFFSET_IER);
1088
1089 /* Schedule NAPI bh */
1090 napi_schedule_irqoff(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001091
1092 return IRQ_HANDLED;
1093}
1094
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001095static int ftgmac100_poll(struct napi_struct *napi, int budget)
1096{
1097 struct ftgmac100 *priv = container_of(napi, struct ftgmac100, napi);
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001098 bool more, completed = true;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001099 int rx = 0;
1100
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001101 ftgmac100_tx_complete(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001102
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001103 do {
1104 more = ftgmac100_rx_packet(priv, &rx);
1105 } while (more && rx < budget);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001106
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001107 if (more && rx == budget)
1108 completed = false;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001109
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001110
1111 /* The interrupt is telling us to kick the MAC back to life
1112 * after an RX overflow
1113 */
1114 if (unlikely(priv->need_mac_restart)) {
1115 ftgmac100_start_hw(priv);
1116
1117 /* Re-enable "bad" interrupts */
1118 iowrite32(FTGMAC100_INT_BAD,
1119 priv->base + FTGMAC100_OFFSET_IER);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001120 }
1121
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001122 /* Keep NAPI going if we have still packets to reclaim */
1123 if (priv->tx_pending)
1124 return budget;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001125
1126 if (completed) {
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001127 /* We are about to re-enable all interrupts. However
1128 * the HW has been latching RX/TX packet interrupts while
1129 * they were masked. So we clear them first, then we need
1130 * to re-check if there's something to process
1131 */
1132 iowrite32(FTGMAC100_INT_RXTX,
1133 priv->base + FTGMAC100_OFFSET_ISR);
1134 if (ftgmac100_rxdes_packet_ready
1135 (ftgmac100_current_rxdes(priv)) || priv->tx_pending)
1136 return budget;
1137
1138 /* deschedule NAPI */
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001139 napi_complete(napi);
1140
1141 /* enable all interrupts */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001142 iowrite32(FTGMAC100_INT_ALL,
Gavin Shanfc6061c2016-07-19 11:54:25 +10001143 priv->base + FTGMAC100_OFFSET_IER);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001144 }
1145
1146 return rx;
1147}
1148
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001149static int ftgmac100_init_all(struct ftgmac100 *priv, bool ignore_alloc_err)
1150{
1151 int err = 0;
1152
1153 /* Re-init descriptors (adjust queue sizes) */
1154 ftgmac100_init_rings(priv);
1155
1156 /* Realloc rx descriptors */
1157 err = ftgmac100_alloc_rx_buffers(priv);
1158 if (err && !ignore_alloc_err)
1159 return err;
1160
1161 /* Reinit and restart HW */
1162 ftgmac100_init_hw(priv);
1163 ftgmac100_start_hw(priv);
1164
1165 /* Re-enable the device */
1166 napi_enable(&priv->napi);
1167 netif_start_queue(priv->netdev);
1168
1169 /* Enable all interrupts */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001170 iowrite32(FTGMAC100_INT_ALL, priv->base + FTGMAC100_OFFSET_IER);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001171
1172 return err;
1173}
1174
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001175static void ftgmac100_reset_task(struct work_struct *work)
1176{
1177 struct ftgmac100 *priv = container_of(work, struct ftgmac100,
1178 reset_task);
1179 struct net_device *netdev = priv->netdev;
1180 int err;
1181
1182 netdev_dbg(netdev, "Resetting NIC...\n");
1183
1184 /* Lock the world */
1185 rtnl_lock();
1186 if (netdev->phydev)
1187 mutex_lock(&netdev->phydev->lock);
1188 if (priv->mii_bus)
1189 mutex_lock(&priv->mii_bus->mdio_lock);
1190
1191
1192 /* Check if the interface is still up */
1193 if (!netif_running(netdev))
1194 goto bail;
1195
1196 /* Stop the network stack */
1197 netif_trans_update(netdev);
1198 napi_disable(&priv->napi);
1199 netif_tx_disable(netdev);
1200
1201 /* Stop and reset the MAC */
1202 ftgmac100_stop_hw(priv);
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +10001203 err = ftgmac100_reset_and_config_mac(priv);
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001204 if (err) {
1205 /* Not much we can do ... it might come back... */
1206 netdev_err(netdev, "attempting to continue...\n");
1207 }
1208
1209 /* Free all rx and tx buffers */
1210 ftgmac100_free_buffers(priv);
1211
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001212 /* Setup everything again and restart chip */
1213 ftgmac100_init_all(priv, true);
1214
1215 netdev_dbg(netdev, "Reset done !\n");
1216 bail:
1217 if (priv->mii_bus)
1218 mutex_unlock(&priv->mii_bus->mdio_lock);
1219 if (netdev->phydev)
1220 mutex_unlock(&netdev->phydev->lock);
1221 rtnl_unlock();
1222}
1223
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001224static int ftgmac100_open(struct net_device *netdev)
1225{
1226 struct ftgmac100 *priv = netdev_priv(netdev);
1227 int err;
1228
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001229 /* Allocate ring buffers */
1230 err = ftgmac100_alloc_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001231 if (err) {
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001232 netdev_err(netdev, "Failed to allocate descriptors\n");
1233 return err;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001234 }
1235
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +10001236 /* When using NC-SI we force the speed to 100Mbit/s full duplex,
1237 *
1238 * Otherwise we leave it set to 0 (no link), the link
1239 * message from the PHY layer will handle setting it up to
1240 * something else if needed.
1241 */
1242 if (priv->use_ncsi) {
1243 priv->cur_duplex = DUPLEX_FULL;
1244 priv->cur_speed = SPEED_100;
1245 } else {
1246 priv->cur_duplex = 0;
1247 priv->cur_speed = 0;
1248 }
1249
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +10001250 /* Reset the hardware */
1251 err = ftgmac100_reset_and_config_mac(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001252 if (err)
1253 goto err_hw;
1254
Benjamin Herrenschmidtb8dbecf2017-04-05 12:28:47 +10001255 /* Initialize NAPI */
1256 netif_napi_add(netdev, &priv->napi, ftgmac100_poll, 64);
1257
Benjamin Herrenschmidt81f1eca2017-04-05 12:28:48 +10001258 /* Grab our interrupt */
1259 err = request_irq(netdev->irq, ftgmac100_interrupt, 0, netdev->name, netdev);
1260 if (err) {
1261 netdev_err(netdev, "failed to request irq %d\n", netdev->irq);
1262 goto err_irq;
1263 }
1264
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001265 /* Start things up */
1266 err = ftgmac100_init_all(priv, false);
1267 if (err) {
1268 netdev_err(netdev, "Failed to allocate packet buffers\n");
1269 goto err_alloc;
1270 }
Gavin Shan08c9c122016-09-22 08:35:01 +09301271
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001272 if (netdev->phydev) {
1273 /* If we have a PHY, start polling */
Gavin Shanbd466c32016-07-19 11:54:23 +10001274 phy_start(netdev->phydev);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001275 } else if (priv->use_ncsi) {
1276 /* If using NC-SI, set our carrier on and start the stack */
Gavin Shanbd466c32016-07-19 11:54:23 +10001277 netif_carrier_on(netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001278
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001279 /* Start the NCSI device */
Gavin Shanbd466c32016-07-19 11:54:23 +10001280 err = ncsi_start_dev(priv->ndev);
1281 if (err)
1282 goto err_ncsi;
1283 }
1284
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001285 return 0;
1286
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001287 err_ncsi:
Gavin Shanbd466c32016-07-19 11:54:23 +10001288 napi_disable(&priv->napi);
1289 netif_stop_queue(netdev);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001290 err_alloc:
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001291 ftgmac100_free_buffers(priv);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001292 free_irq(netdev->irq, netdev);
1293 err_irq:
1294 netif_napi_del(&priv->napi);
1295 err_hw:
1296 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001297 ftgmac100_free_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001298 return err;
1299}
1300
1301static int ftgmac100_stop(struct net_device *netdev)
1302{
1303 struct ftgmac100 *priv = netdev_priv(netdev);
1304
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001305 /* Note about the reset task: We are called with the rtnl lock
1306 * held, so we are synchronized against the core of the reset
1307 * task. We must not try to synchronously cancel it otherwise
1308 * we can deadlock. But since it will test for netif_running()
1309 * which has already been cleared by the net core, we don't
1310 * anything special to do.
1311 */
1312
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001313 /* disable all interrupts */
1314 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1315
1316 netif_stop_queue(netdev);
1317 napi_disable(&priv->napi);
Benjamin Herrenschmidtb8dbecf2017-04-05 12:28:47 +10001318 netif_napi_del(&priv->napi);
Gavin Shanbd466c32016-07-19 11:54:23 +10001319 if (netdev->phydev)
1320 phy_stop(netdev->phydev);
Gavin Shan2c15f252016-10-04 11:25:54 +11001321 else if (priv->use_ncsi)
1322 ncsi_stop_dev(priv->ndev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001323
1324 ftgmac100_stop_hw(priv);
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001325 free_irq(netdev->irq, netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001326 ftgmac100_free_buffers(priv);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001327 ftgmac100_free_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001328
1329 return 0;
1330}
1331
1332static int ftgmac100_hard_start_xmit(struct sk_buff *skb,
1333 struct net_device *netdev)
1334{
1335 struct ftgmac100 *priv = netdev_priv(netdev);
1336 dma_addr_t map;
1337
1338 if (unlikely(skb->len > MAX_PKT_SIZE)) {
1339 if (net_ratelimit())
1340 netdev_dbg(netdev, "tx packet too big\n");
1341
1342 netdev->stats.tx_dropped++;
Eric Dumazet0113e342014-01-16 23:38:24 -08001343 kfree_skb(skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001344 return NETDEV_TX_OK;
1345 }
1346
1347 map = dma_map_single(priv->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
1348 if (unlikely(dma_mapping_error(priv->dev, map))) {
1349 /* drop packet */
1350 if (net_ratelimit())
1351 netdev_err(netdev, "map socket buffer failed\n");
1352
1353 netdev->stats.tx_dropped++;
Eric Dumazet0113e342014-01-16 23:38:24 -08001354 kfree_skb(skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001355 return NETDEV_TX_OK;
1356 }
1357
1358 return ftgmac100_xmit(priv, skb, map);
1359}
1360
1361/* optional */
1362static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1363{
Gavin Shanbd466c32016-07-19 11:54:23 +10001364 if (!netdev->phydev)
1365 return -ENXIO;
1366
Philippe Reynesb3c40ad2016-05-16 01:35:13 +02001367 return phy_mii_ioctl(netdev->phydev, ifr, cmd);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001368}
1369
1370static const struct net_device_ops ftgmac100_netdev_ops = {
1371 .ndo_open = ftgmac100_open,
1372 .ndo_stop = ftgmac100_stop,
1373 .ndo_start_xmit = ftgmac100_hard_start_xmit,
Gavin Shan113ce102016-07-19 11:54:22 +10001374 .ndo_set_mac_address = ftgmac100_set_mac_addr,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001375 .ndo_validate_addr = eth_validate_addr,
1376 .ndo_do_ioctl = ftgmac100_do_ioctl,
1377};
1378
Gavin Shaneb418182016-07-19 11:54:21 +10001379static int ftgmac100_setup_mdio(struct net_device *netdev)
1380{
1381 struct ftgmac100 *priv = netdev_priv(netdev);
1382 struct platform_device *pdev = to_platform_device(priv->dev);
1383 int i, err = 0;
Joel Stanleye07dc632016-09-22 08:35:02 +09301384 u32 reg;
Gavin Shaneb418182016-07-19 11:54:21 +10001385
1386 /* initialize mdio bus */
1387 priv->mii_bus = mdiobus_alloc();
1388 if (!priv->mii_bus)
1389 return -EIO;
1390
Joel Stanleye07dc632016-09-22 08:35:02 +09301391 if (of_machine_is_compatible("aspeed,ast2400") ||
1392 of_machine_is_compatible("aspeed,ast2500")) {
1393 /* This driver supports the old MDIO interface */
1394 reg = ioread32(priv->base + FTGMAC100_OFFSET_REVR);
1395 reg &= ~FTGMAC100_REVR_NEW_MDIO_INTERFACE;
1396 iowrite32(reg, priv->base + FTGMAC100_OFFSET_REVR);
1397 };
1398
Gavin Shaneb418182016-07-19 11:54:21 +10001399 priv->mii_bus->name = "ftgmac100_mdio";
1400 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d",
1401 pdev->name, pdev->id);
1402 priv->mii_bus->priv = priv->netdev;
1403 priv->mii_bus->read = ftgmac100_mdiobus_read;
1404 priv->mii_bus->write = ftgmac100_mdiobus_write;
1405
1406 for (i = 0; i < PHY_MAX_ADDR; i++)
1407 priv->mii_bus->irq[i] = PHY_POLL;
1408
1409 err = mdiobus_register(priv->mii_bus);
1410 if (err) {
1411 dev_err(priv->dev, "Cannot register MDIO bus!\n");
1412 goto err_register_mdiobus;
1413 }
1414
1415 err = ftgmac100_mii_probe(priv);
1416 if (err) {
1417 dev_err(priv->dev, "MII Probe failed!\n");
1418 goto err_mii_probe;
1419 }
1420
1421 return 0;
1422
1423err_mii_probe:
1424 mdiobus_unregister(priv->mii_bus);
1425err_register_mdiobus:
1426 mdiobus_free(priv->mii_bus);
1427 return err;
1428}
1429
1430static void ftgmac100_destroy_mdio(struct net_device *netdev)
1431{
1432 struct ftgmac100 *priv = netdev_priv(netdev);
1433
1434 if (!netdev->phydev)
1435 return;
1436
1437 phy_disconnect(netdev->phydev);
1438 mdiobus_unregister(priv->mii_bus);
1439 mdiobus_free(priv->mii_bus);
1440}
1441
Gavin Shanbd466c32016-07-19 11:54:23 +10001442static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
1443{
1444 if (unlikely(nd->state != ncsi_dev_state_functional))
1445 return;
1446
1447 netdev_info(nd->dev, "NCSI interface %s\n",
1448 nd->link_up ? "up" : "down");
1449}
1450
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001451static int ftgmac100_probe(struct platform_device *pdev)
1452{
1453 struct resource *res;
1454 int irq;
1455 struct net_device *netdev;
1456 struct ftgmac100 *priv;
Gavin Shanbd466c32016-07-19 11:54:23 +10001457 int err = 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001458
1459 if (!pdev)
1460 return -ENODEV;
1461
1462 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1463 if (!res)
1464 return -ENXIO;
1465
1466 irq = platform_get_irq(pdev, 0);
1467 if (irq < 0)
1468 return irq;
1469
1470 /* setup net_device */
1471 netdev = alloc_etherdev(sizeof(*priv));
1472 if (!netdev) {
1473 err = -ENOMEM;
1474 goto err_alloc_etherdev;
1475 }
1476
1477 SET_NETDEV_DEV(netdev, &pdev->dev);
1478
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001479 netdev->ethtool_ops = &ftgmac100_ethtool_ops;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001480 netdev->netdev_ops = &ftgmac100_netdev_ops;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001481
1482 platform_set_drvdata(pdev, netdev);
1483
1484 /* setup private data */
1485 priv = netdev_priv(netdev);
1486 priv->netdev = netdev;
1487 priv->dev = &pdev->dev;
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001488 INIT_WORK(&priv->reset_task, ftgmac100_reset_task);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001489
1490 spin_lock_init(&priv->tx_lock);
1491
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001492 /* map io memory */
1493 priv->res = request_mem_region(res->start, resource_size(res),
1494 dev_name(&pdev->dev));
1495 if (!priv->res) {
1496 dev_err(&pdev->dev, "Could not reserve memory region\n");
1497 err = -ENOMEM;
1498 goto err_req_mem;
1499 }
1500
1501 priv->base = ioremap(res->start, resource_size(res));
1502 if (!priv->base) {
1503 dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
1504 err = -EIO;
1505 goto err_ioremap;
1506 }
1507
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001508 netdev->irq = irq;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001509
Gavin Shan113ce102016-07-19 11:54:22 +10001510 /* MAC address from chip or random one */
1511 ftgmac100_setup_mac(priv);
1512
Joel Stanley2a0ab8eb2016-09-22 08:35:00 +09301513 if (of_machine_is_compatible("aspeed,ast2400") ||
1514 of_machine_is_compatible("aspeed,ast2500")) {
1515 priv->rxdes0_edorr_mask = BIT(30);
1516 priv->txdes0_edotr_mask = BIT(30);
1517 } else {
1518 priv->rxdes0_edorr_mask = BIT(15);
1519 priv->txdes0_edotr_mask = BIT(15);
1520 }
1521
Gavin Shanbd466c32016-07-19 11:54:23 +10001522 if (pdev->dev.of_node &&
1523 of_get_property(pdev->dev.of_node, "use-ncsi", NULL)) {
1524 if (!IS_ENABLED(CONFIG_NET_NCSI)) {
1525 dev_err(&pdev->dev, "NCSI stack not enabled\n");
1526 goto err_ncsi_dev;
1527 }
1528
1529 dev_info(&pdev->dev, "Using NCSI interface\n");
1530 priv->use_ncsi = true;
1531 priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
1532 if (!priv->ndev)
1533 goto err_ncsi_dev;
1534 } else {
1535 priv->use_ncsi = false;
1536 err = ftgmac100_setup_mdio(netdev);
1537 if (err)
1538 goto err_setup_mdio;
1539 }
1540
1541 /* We have to disable on-chip IP checksum functionality
1542 * when NCSI is enabled on the interface. It doesn't work
1543 * in that case.
1544 */
1545 netdev->features = NETIF_F_IP_CSUM | NETIF_F_GRO;
1546 if (priv->use_ncsi &&
1547 of_get_property(pdev->dev.of_node, "no-hw-checksum", NULL))
1548 netdev->features &= ~NETIF_F_IP_CSUM;
1549
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001550
1551 /* register network device */
1552 err = register_netdev(netdev);
1553 if (err) {
1554 dev_err(&pdev->dev, "Failed to register netdev\n");
1555 goto err_register_netdev;
1556 }
1557
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001558 netdev_info(netdev, "irq %d, mapped at %p\n", netdev->irq, priv->base);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001559
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001560 return 0;
1561
Gavin Shanbd466c32016-07-19 11:54:23 +10001562err_ncsi_dev:
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001563err_register_netdev:
Gavin Shaneb418182016-07-19 11:54:21 +10001564 ftgmac100_destroy_mdio(netdev);
1565err_setup_mdio:
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001566 iounmap(priv->base);
1567err_ioremap:
1568 release_resource(priv->res);
1569err_req_mem:
1570 netif_napi_del(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001571 free_netdev(netdev);
1572err_alloc_etherdev:
1573 return err;
1574}
1575
Dmitry Torokhovbe125022017-03-01 17:24:47 -08001576static int ftgmac100_remove(struct platform_device *pdev)
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001577{
1578 struct net_device *netdev;
1579 struct ftgmac100 *priv;
1580
1581 netdev = platform_get_drvdata(pdev);
1582 priv = netdev_priv(netdev);
1583
1584 unregister_netdev(netdev);
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001585
1586 /* There's a small chance the reset task will have been re-queued,
1587 * during stop, make sure it's gone before we free the structure.
1588 */
1589 cancel_work_sync(&priv->reset_task);
1590
Gavin Shaneb418182016-07-19 11:54:21 +10001591 ftgmac100_destroy_mdio(netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001592
1593 iounmap(priv->base);
1594 release_resource(priv->res);
1595
1596 netif_napi_del(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001597 free_netdev(netdev);
1598 return 0;
1599}
1600
Gavin Shanbb168e22016-07-19 11:54:24 +10001601static const struct of_device_id ftgmac100_of_match[] = {
1602 { .compatible = "faraday,ftgmac100" },
1603 { }
1604};
1605MODULE_DEVICE_TABLE(of, ftgmac100_of_match);
1606
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001607static struct platform_driver ftgmac100_driver = {
Gavin Shanbb168e22016-07-19 11:54:24 +10001608 .probe = ftgmac100_probe,
Dmitry Torokhovbe125022017-03-01 17:24:47 -08001609 .remove = ftgmac100_remove,
Gavin Shanbb168e22016-07-19 11:54:24 +10001610 .driver = {
1611 .name = DRV_NAME,
1612 .of_match_table = ftgmac100_of_match,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001613 },
1614};
Sachin Kamat14f645d2013-03-18 01:50:48 +00001615module_platform_driver(ftgmac100_driver);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001616
1617MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
1618MODULE_DESCRIPTION("FTGMAC100 driver");
1619MODULE_LICENSE("GPL");