blob: 6277f6e70f6b7510680df9cd07fc2a02db1aa96f [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 Herrenschmidtd72e01a2017-04-06 11:02:45 +100074 /* Scratch page to use when rx skb alloc fails */
75 void *rx_scratch;
76 dma_addr_t rx_scratch_dma;
77
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100078 /* Component structures */
Po-Yu Chuang69785b72011-06-08 23:32:48 +000079 struct net_device *netdev;
80 struct device *dev;
Gavin Shanbd466c32016-07-19 11:54:23 +100081 struct ncsi_dev *ndev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000082 struct napi_struct napi;
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +100083 struct work_struct reset_task;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000084 struct mii_bus *mii_bus;
Andrew Jeffery7906a4d2016-09-22 08:34:59 +093085
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100086 /* Link management */
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +100087 int cur_speed;
88 int cur_duplex;
Benjamin Herrenschmidt831fb332017-04-05 12:28:43 +100089 bool use_ncsi;
90
91 /* Misc */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +100092 bool need_mac_restart;
Po-Yu Chuang69785b72011-06-08 23:32:48 +000093};
94
Po-Yu Chuang69785b72011-06-08 23:32:48 +000095static void ftgmac100_set_rx_ring_base(struct ftgmac100 *priv, dma_addr_t addr)
96{
97 iowrite32(addr, priv->base + FTGMAC100_OFFSET_RXR_BADR);
98}
99
100static void ftgmac100_set_rx_buffer_size(struct ftgmac100 *priv,
101 unsigned int size)
102{
103 size = FTGMAC100_RBSR_SIZE(size);
104 iowrite32(size, priv->base + FTGMAC100_OFFSET_RBSR);
105}
106
107static void ftgmac100_set_normal_prio_tx_ring_base(struct ftgmac100 *priv,
108 dma_addr_t addr)
109{
110 iowrite32(addr, priv->base + FTGMAC100_OFFSET_NPTXR_BADR);
111}
112
113static void ftgmac100_txdma_normal_prio_start_polling(struct ftgmac100 *priv)
114{
115 iowrite32(1, priv->base + FTGMAC100_OFFSET_NPTXPD);
116}
117
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000118static int ftgmac100_reset_mac(struct ftgmac100 *priv, u32 maccr)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000119{
120 struct net_device *netdev = priv->netdev;
121 int i;
122
123 /* NOTE: reset clears all registers */
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000124 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
125 iowrite32(maccr | FTGMAC100_MACCR_SW_RST,
126 priv->base + FTGMAC100_OFFSET_MACCR);
127 for (i = 0; i < 50; i++) {
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000128 unsigned int maccr;
129
130 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
131 if (!(maccr & FTGMAC100_MACCR_SW_RST))
132 return 0;
133
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000134 udelay(1);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000135 }
136
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000137 netdev_err(netdev, "Hardware reset failed\n");
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000138 return -EIO;
139}
140
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000141static int ftgmac100_reset_and_config_mac(struct ftgmac100 *priv)
142{
143 u32 maccr = 0;
144
145 switch (priv->cur_speed) {
146 case SPEED_10:
147 case 0: /* no link */
148 break;
149
150 case SPEED_100:
151 maccr |= FTGMAC100_MACCR_FAST_MODE;
152 break;
153
154 case SPEED_1000:
155 maccr |= FTGMAC100_MACCR_GIGA_MODE;
156 break;
157 default:
158 netdev_err(priv->netdev, "Unknown speed %d !\n",
159 priv->cur_speed);
160 break;
161 }
162
163 /* (Re)initialize the queue pointers */
164 priv->rx_pointer = 0;
165 priv->tx_clean_pointer = 0;
166 priv->tx_pointer = 0;
167 priv->tx_pending = 0;
168
169 /* The doc says reset twice with 10us interval */
170 if (ftgmac100_reset_mac(priv, maccr))
171 return -EIO;
172 usleep_range(10, 1000);
173 return ftgmac100_reset_mac(priv, maccr);
174}
175
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000176static void ftgmac100_set_mac(struct ftgmac100 *priv, const unsigned char *mac)
177{
178 unsigned int maddr = mac[0] << 8 | mac[1];
179 unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
180
181 iowrite32(maddr, priv->base + FTGMAC100_OFFSET_MAC_MADR);
182 iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR);
183}
184
Gavin Shan113ce102016-07-19 11:54:22 +1000185static void ftgmac100_setup_mac(struct ftgmac100 *priv)
186{
187 u8 mac[ETH_ALEN];
188 unsigned int m;
189 unsigned int l;
190 void *addr;
191
192 addr = device_get_mac_address(priv->dev, mac, ETH_ALEN);
193 if (addr) {
194 ether_addr_copy(priv->netdev->dev_addr, mac);
195 dev_info(priv->dev, "Read MAC address %pM from device tree\n",
196 mac);
197 return;
198 }
199
200 m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR);
201 l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR);
202
203 mac[0] = (m >> 8) & 0xff;
204 mac[1] = m & 0xff;
205 mac[2] = (l >> 24) & 0xff;
206 mac[3] = (l >> 16) & 0xff;
207 mac[4] = (l >> 8) & 0xff;
208 mac[5] = l & 0xff;
209
Gavin Shan113ce102016-07-19 11:54:22 +1000210 if (is_valid_ether_addr(mac)) {
211 ether_addr_copy(priv->netdev->dev_addr, mac);
212 dev_info(priv->dev, "Read MAC address %pM from chip\n", mac);
213 } else {
214 eth_hw_addr_random(priv->netdev);
215 dev_info(priv->dev, "Generated random MAC address %pM\n",
216 priv->netdev->dev_addr);
217 }
218}
219
220static int ftgmac100_set_mac_addr(struct net_device *dev, void *p)
221{
222 int ret;
223
224 ret = eth_prepare_mac_addr_change(dev, p);
225 if (ret < 0)
226 return ret;
227
228 eth_commit_mac_addr_change(dev, p);
229 ftgmac100_set_mac(netdev_priv(dev), dev->dev_addr);
230
231 return 0;
232}
233
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000234static void ftgmac100_init_hw(struct ftgmac100 *priv)
235{
236 /* setup ring buffer base registers */
237 ftgmac100_set_rx_ring_base(priv,
238 priv->descs_dma_addr +
239 offsetof(struct ftgmac100_descs, rxdes));
240 ftgmac100_set_normal_prio_tx_ring_base(priv,
241 priv->descs_dma_addr +
242 offsetof(struct ftgmac100_descs, txdes));
243
244 ftgmac100_set_rx_buffer_size(priv, RX_BUF_SIZE);
245
246 iowrite32(FTGMAC100_APTC_RXPOLL_CNT(1), priv->base + FTGMAC100_OFFSET_APTC);
247
248 ftgmac100_set_mac(priv, priv->netdev->dev_addr);
249}
250
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000251static void ftgmac100_start_hw(struct ftgmac100 *priv)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000252{
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000253 u32 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000254
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000255 /* Keep the original GMAC and FAST bits */
256 maccr &= (FTGMAC100_MACCR_FAST_MODE | FTGMAC100_MACCR_GIGA_MODE);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000257
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000258 /* Add all the main enable bits */
259 maccr |= FTGMAC100_MACCR_TXDMA_EN |
260 FTGMAC100_MACCR_RXDMA_EN |
261 FTGMAC100_MACCR_TXMAC_EN |
262 FTGMAC100_MACCR_RXMAC_EN |
263 FTGMAC100_MACCR_CRC_APD |
264 FTGMAC100_MACCR_PHY_LINK_LEVEL |
265 FTGMAC100_MACCR_RX_RUNT |
266 FTGMAC100_MACCR_RX_BROADPKT;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000267
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000268 /* Add other bits as needed */
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000269 if (priv->cur_duplex == DUPLEX_FULL)
270 maccr |= FTGMAC100_MACCR_FULLDUP;
271
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +1000272 /* Hit the HW */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000273 iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
274}
275
276static void ftgmac100_stop_hw(struct ftgmac100 *priv)
277{
278 iowrite32(0, priv->base + FTGMAC100_OFFSET_MACCR);
279}
280
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000281static bool ftgmac100_rxdes_first_segment(struct ftgmac100_rxdes *rxdes)
282{
283 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_FRS);
284}
285
286static bool ftgmac100_rxdes_last_segment(struct ftgmac100_rxdes *rxdes)
287{
288 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_LRS);
289}
290
291static bool ftgmac100_rxdes_packet_ready(struct ftgmac100_rxdes *rxdes)
292{
293 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RXPKT_RDY);
294}
295
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930296static void ftgmac100_rxdes_set_dma_own(const struct ftgmac100 *priv,
297 struct ftgmac100_rxdes *rxdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000298{
299 /* clear status bits */
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930300 rxdes->rxdes0 &= cpu_to_le32(priv->rxdes0_edorr_mask);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000301}
302
303static bool ftgmac100_rxdes_rx_error(struct ftgmac100_rxdes *rxdes)
304{
305 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RX_ERR);
306}
307
308static bool ftgmac100_rxdes_crc_error(struct ftgmac100_rxdes *rxdes)
309{
310 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_CRC_ERR);
311}
312
313static bool ftgmac100_rxdes_frame_too_long(struct ftgmac100_rxdes *rxdes)
314{
315 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_FTL);
316}
317
318static bool ftgmac100_rxdes_runt(struct ftgmac100_rxdes *rxdes)
319{
320 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RUNT);
321}
322
323static bool ftgmac100_rxdes_odd_nibble(struct ftgmac100_rxdes *rxdes)
324{
325 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RX_ODD_NB);
326}
327
328static unsigned int ftgmac100_rxdes_data_length(struct ftgmac100_rxdes *rxdes)
329{
330 return le32_to_cpu(rxdes->rxdes0) & FTGMAC100_RXDES0_VDBC;
331}
332
333static bool ftgmac100_rxdes_multicast(struct ftgmac100_rxdes *rxdes)
334{
335 return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_MULTICAST);
336}
337
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930338static void ftgmac100_rxdes_set_end_of_ring(const struct ftgmac100 *priv,
339 struct ftgmac100_rxdes *rxdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000340{
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930341 rxdes->rxdes0 |= cpu_to_le32(priv->rxdes0_edorr_mask);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000342}
343
344static void ftgmac100_rxdes_set_dma_addr(struct ftgmac100_rxdes *rxdes,
345 dma_addr_t addr)
346{
347 rxdes->rxdes3 = cpu_to_le32(addr);
348}
349
350static dma_addr_t ftgmac100_rxdes_get_dma_addr(struct ftgmac100_rxdes *rxdes)
351{
352 return le32_to_cpu(rxdes->rxdes3);
353}
354
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000355static inline bool ftgmac100_rxdes_csum_err(struct ftgmac100_rxdes *rxdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000356{
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000357 return !!(rxdes->rxdes1 & cpu_to_le32(FTGMAC100_RXDES1_TCP_CHKSUM_ERR |
358 FTGMAC100_RXDES1_UDP_CHKSUM_ERR |
359 FTGMAC100_RXDES1_IP_CHKSUM_ERR));
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000360}
361
Andrew Jefferyada66b52016-09-22 08:34:58 +0930362static inline struct page **ftgmac100_rxdes_page_slot(struct ftgmac100 *priv,
363 struct ftgmac100_rxdes *rxdes)
364{
365 return &priv->rx_pages[rxdes - priv->descs->rxdes];
366}
367
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000368/*
369 * rxdes2 is not used by hardware. We use it to keep track of page.
370 * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
371 */
Andrew Jefferyada66b52016-09-22 08:34:58 +0930372static void ftgmac100_rxdes_set_page(struct ftgmac100 *priv,
373 struct ftgmac100_rxdes *rxdes,
374 struct page *page)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000375{
Andrew Jefferyada66b52016-09-22 08:34:58 +0930376 *ftgmac100_rxdes_page_slot(priv, rxdes) = page;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000377}
378
Andrew Jefferyada66b52016-09-22 08:34:58 +0930379static struct page *ftgmac100_rxdes_get_page(struct ftgmac100 *priv,
380 struct ftgmac100_rxdes *rxdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000381{
Andrew Jefferyada66b52016-09-22 08:34:58 +0930382 return *ftgmac100_rxdes_page_slot(priv, rxdes);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000383}
384
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000385static int ftgmac100_alloc_rx_page(struct ftgmac100 *priv,
386 struct ftgmac100_rxdes *rxdes, gfp_t gfp)
387{
388 struct net_device *netdev = priv->netdev;
389 struct page *page;
390 dma_addr_t map;
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000391 int err;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000392
393 page = alloc_page(gfp);
394 if (!page) {
395 if (net_ratelimit())
396 netdev_err(netdev, "failed to allocate rx page\n");
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000397 err = -ENOMEM;
398 map = priv->rx_scratch_dma;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000399 }
400
401 map = dma_map_page(priv->dev, page, 0, RX_BUF_SIZE, DMA_FROM_DEVICE);
402 if (unlikely(dma_mapping_error(priv->dev, map))) {
403 if (net_ratelimit())
404 netdev_err(netdev, "failed to map rx page\n");
405 __free_page(page);
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000406 err = -ENOMEM;
407 map = priv->rx_scratch_dma;
408 page = NULL;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000409 }
410
411 ftgmac100_rxdes_set_page(priv, rxdes, page);
412 ftgmac100_rxdes_set_dma_addr(rxdes, map);
413 ftgmac100_rxdes_set_dma_own(priv, rxdes);
414 return 0;
415}
416
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000417static int ftgmac100_next_rx_pointer(int pointer)
418{
419 return (pointer + 1) & (RX_QUEUE_ENTRIES - 1);
420}
421
422static void ftgmac100_rx_pointer_advance(struct ftgmac100 *priv)
423{
424 priv->rx_pointer = ftgmac100_next_rx_pointer(priv->rx_pointer);
425}
426
427static struct ftgmac100_rxdes *ftgmac100_current_rxdes(struct ftgmac100 *priv)
428{
429 return &priv->descs->rxdes[priv->rx_pointer];
430}
431
432static struct ftgmac100_rxdes *
433ftgmac100_rx_locate_first_segment(struct ftgmac100 *priv)
434{
435 struct ftgmac100_rxdes *rxdes = ftgmac100_current_rxdes(priv);
436
437 while (ftgmac100_rxdes_packet_ready(rxdes)) {
438 if (ftgmac100_rxdes_first_segment(rxdes))
439 return rxdes;
440
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930441 ftgmac100_rxdes_set_dma_own(priv, rxdes);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000442 ftgmac100_rx_pointer_advance(priv);
443 rxdes = ftgmac100_current_rxdes(priv);
444 }
445
446 return NULL;
447}
448
449static bool ftgmac100_rx_packet_error(struct ftgmac100 *priv,
450 struct ftgmac100_rxdes *rxdes)
451{
452 struct net_device *netdev = priv->netdev;
453 bool error = false;
454
455 if (unlikely(ftgmac100_rxdes_rx_error(rxdes))) {
456 if (net_ratelimit())
457 netdev_info(netdev, "rx err\n");
458
459 netdev->stats.rx_errors++;
460 error = true;
461 }
462
463 if (unlikely(ftgmac100_rxdes_crc_error(rxdes))) {
464 if (net_ratelimit())
465 netdev_info(netdev, "rx crc err\n");
466
467 netdev->stats.rx_crc_errors++;
468 error = true;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000469 }
470
471 if (unlikely(ftgmac100_rxdes_frame_too_long(rxdes))) {
472 if (net_ratelimit())
473 netdev_info(netdev, "rx frame too long\n");
474
475 netdev->stats.rx_length_errors++;
476 error = true;
477 } else if (unlikely(ftgmac100_rxdes_runt(rxdes))) {
478 if (net_ratelimit())
479 netdev_info(netdev, "rx runt\n");
480
481 netdev->stats.rx_length_errors++;
482 error = true;
483 } else if (unlikely(ftgmac100_rxdes_odd_nibble(rxdes))) {
484 if (net_ratelimit())
485 netdev_info(netdev, "rx odd nibble\n");
486
487 netdev->stats.rx_length_errors++;
488 error = true;
489 }
490
491 return error;
492}
493
494static void ftgmac100_rx_drop_packet(struct ftgmac100 *priv)
495{
496 struct net_device *netdev = priv->netdev;
497 struct ftgmac100_rxdes *rxdes = ftgmac100_current_rxdes(priv);
498 bool done = false;
499
500 if (net_ratelimit())
501 netdev_dbg(netdev, "drop packet %p\n", rxdes);
502
503 do {
504 if (ftgmac100_rxdes_last_segment(rxdes))
505 done = true;
506
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930507 ftgmac100_rxdes_set_dma_own(priv, rxdes);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000508 ftgmac100_rx_pointer_advance(priv);
509 rxdes = ftgmac100_current_rxdes(priv);
510 } while (!done && ftgmac100_rxdes_packet_ready(rxdes));
511
512 netdev->stats.rx_dropped++;
513}
514
515static bool ftgmac100_rx_packet(struct ftgmac100 *priv, int *processed)
516{
517 struct net_device *netdev = priv->netdev;
518 struct ftgmac100_rxdes *rxdes;
519 struct sk_buff *skb;
Benjamin Herrenschmidtb1977bf2017-04-06 11:02:44 +1000520 struct page *page;
521 unsigned int size;
522 dma_addr_t map;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000523
524 rxdes = ftgmac100_rx_locate_first_segment(priv);
525 if (!rxdes)
526 return false;
527
Benjamin Herrenschmidtb1977bf2017-04-06 11:02:44 +1000528 /* We don't support segmented rx frames, so drop these
529 * along with packets with errors.
530 */
531 if (unlikely(!ftgmac100_rxdes_last_segment(rxdes) ||
532 ftgmac100_rx_packet_error(priv, rxdes))) {
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000533 ftgmac100_rx_drop_packet(priv);
534 return true;
535 }
536
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000537 /* If the packet had no buffer (failed to allocate earlier)
538 * then try to allocate one and skip
539 */
540 page = ftgmac100_rxdes_get_page(priv, rxdes);
541 if (!page) {
542 ftgmac100_alloc_rx_page(priv, rxdes, GFP_ATOMIC);
543 ftgmac100_rx_pointer_advance(priv);
544 return true;
545 }
546
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000547 /* start processing */
548 skb = netdev_alloc_skb_ip_align(netdev, 128);
549 if (unlikely(!skb)) {
550 if (net_ratelimit())
551 netdev_err(netdev, "rx skb alloc failed\n");
552
553 ftgmac100_rx_drop_packet(priv);
554 return true;
555 }
556
557 if (unlikely(ftgmac100_rxdes_multicast(rxdes)))
558 netdev->stats.multicast++;
559
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000560 /* If the HW found checksum errors, bounce it to software.
561 *
562 * If we didn't, we need to see if the packet was recognized
563 * by HW as one of the supported checksummed protocols before
564 * we accept the HW test results.
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000565 */
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000566 if (netdev->features & NETIF_F_RXCSUM) {
567 __le32 csum_vlan = rxdes->rxdes1;
568 __le32 err_bits = cpu_to_le32(FTGMAC100_RXDES1_TCP_CHKSUM_ERR |
569 FTGMAC100_RXDES1_UDP_CHKSUM_ERR |
570 FTGMAC100_RXDES1_IP_CHKSUM_ERR);
571 if ((csum_vlan & err_bits) ||
572 !(csum_vlan & cpu_to_le32(FTGMAC100_RXDES1_PROT_MASK)))
573 skb->ip_summed = CHECKSUM_NONE;
574 else
575 skb->ip_summed = CHECKSUM_UNNECESSARY;
576 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000577
Benjamin Herrenschmidtb1977bf2017-04-06 11:02:44 +1000578 map = ftgmac100_rxdes_get_dma_addr(rxdes);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000579
Benjamin Herrenschmidtb1977bf2017-04-06 11:02:44 +1000580 dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000581
Benjamin Herrenschmidtb1977bf2017-04-06 11:02:44 +1000582 size = ftgmac100_rxdes_data_length(rxdes);
583 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page, 0, size);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000584
Benjamin Herrenschmidtb1977bf2017-04-06 11:02:44 +1000585 skb->len += size;
586 skb->data_len += size;
587 skb->truesize += PAGE_SIZE;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000588
Benjamin Herrenschmidtb1977bf2017-04-06 11:02:44 +1000589 ftgmac100_alloc_rx_page(priv, rxdes, GFP_ATOMIC);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000590
Benjamin Herrenschmidtb1977bf2017-04-06 11:02:44 +1000591 ftgmac100_rx_pointer_advance(priv);
592 rxdes = ftgmac100_current_rxdes(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000593
Eric Dumazet6ecd09d2012-07-12 04:19:38 +0000594 /* Small frames are copied into linear part of skb to free one page */
595 if (skb->len <= 128) {
Eric Dumazet5935f812011-10-13 11:30:52 +0000596 skb->truesize -= PAGE_SIZE;
Eric Dumazet6ecd09d2012-07-12 04:19:38 +0000597 __pskb_pull_tail(skb, skb->len);
598 } else {
599 /* We pull the minimum amount into linear part */
600 __pskb_pull_tail(skb, ETH_HLEN);
601 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000602 skb->protocol = eth_type_trans(skb, netdev);
603
604 netdev->stats.rx_packets++;
605 netdev->stats.rx_bytes += skb->len;
606
607 /* push packet to protocol stack */
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000608 if (skb->ip_summed == CHECKSUM_NONE)
609 netif_receive_skb(skb);
610 else
611 napi_gro_receive(&priv->napi, skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000612
613 (*processed)++;
614 return true;
615}
616
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930617static void ftgmac100_txdes_reset(const struct ftgmac100 *priv,
618 struct ftgmac100_txdes *txdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000619{
620 /* clear all except end of ring bit */
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930621 txdes->txdes0 &= cpu_to_le32(priv->txdes0_edotr_mask);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000622 txdes->txdes1 = 0;
623 txdes->txdes2 = 0;
624 txdes->txdes3 = 0;
625}
626
627static bool ftgmac100_txdes_owned_by_dma(struct ftgmac100_txdes *txdes)
628{
629 return txdes->txdes0 & cpu_to_le32(FTGMAC100_TXDES0_TXDMA_OWN);
630}
631
632static void ftgmac100_txdes_set_dma_own(struct ftgmac100_txdes *txdes)
633{
634 /*
635 * Make sure dma own bit will not be set before any other
636 * descriptor fields.
637 */
638 wmb();
639 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_TXDMA_OWN);
640}
641
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930642static void ftgmac100_txdes_set_end_of_ring(const struct ftgmac100 *priv,
643 struct ftgmac100_txdes *txdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000644{
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930645 txdes->txdes0 |= cpu_to_le32(priv->txdes0_edotr_mask);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000646}
647
648static void ftgmac100_txdes_set_first_segment(struct ftgmac100_txdes *txdes)
649{
650 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_FTS);
651}
652
653static void ftgmac100_txdes_set_last_segment(struct ftgmac100_txdes *txdes)
654{
655 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_LTS);
656}
657
658static void ftgmac100_txdes_set_buffer_size(struct ftgmac100_txdes *txdes,
659 unsigned int len)
660{
661 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_TXBUF_SIZE(len));
662}
663
664static void ftgmac100_txdes_set_txint(struct ftgmac100_txdes *txdes)
665{
666 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_TXIC);
667}
668
669static void ftgmac100_txdes_set_tcpcs(struct ftgmac100_txdes *txdes)
670{
671 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_TCP_CHKSUM);
672}
673
674static void ftgmac100_txdes_set_udpcs(struct ftgmac100_txdes *txdes)
675{
676 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_UDP_CHKSUM);
677}
678
679static void ftgmac100_txdes_set_ipcs(struct ftgmac100_txdes *txdes)
680{
681 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_IP_CHKSUM);
682}
683
684static void ftgmac100_txdes_set_dma_addr(struct ftgmac100_txdes *txdes,
685 dma_addr_t addr)
686{
687 txdes->txdes3 = cpu_to_le32(addr);
688}
689
690static dma_addr_t ftgmac100_txdes_get_dma_addr(struct ftgmac100_txdes *txdes)
691{
692 return le32_to_cpu(txdes->txdes3);
693}
694
695/*
696 * txdes2 is not used by hardware. We use it to keep track of socket buffer.
697 * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
698 */
699static void ftgmac100_txdes_set_skb(struct ftgmac100_txdes *txdes,
700 struct sk_buff *skb)
701{
702 txdes->txdes2 = (unsigned int)skb;
703}
704
705static struct sk_buff *ftgmac100_txdes_get_skb(struct ftgmac100_txdes *txdes)
706{
707 return (struct sk_buff *)txdes->txdes2;
708}
709
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000710static int ftgmac100_next_tx_pointer(int pointer)
711{
712 return (pointer + 1) & (TX_QUEUE_ENTRIES - 1);
713}
714
715static void ftgmac100_tx_pointer_advance(struct ftgmac100 *priv)
716{
717 priv->tx_pointer = ftgmac100_next_tx_pointer(priv->tx_pointer);
718}
719
720static void ftgmac100_tx_clean_pointer_advance(struct ftgmac100 *priv)
721{
722 priv->tx_clean_pointer = ftgmac100_next_tx_pointer(priv->tx_clean_pointer);
723}
724
725static struct ftgmac100_txdes *ftgmac100_current_txdes(struct ftgmac100 *priv)
726{
727 return &priv->descs->txdes[priv->tx_pointer];
728}
729
730static struct ftgmac100_txdes *
731ftgmac100_current_clean_txdes(struct ftgmac100 *priv)
732{
733 return &priv->descs->txdes[priv->tx_clean_pointer];
734}
735
736static bool ftgmac100_tx_complete_packet(struct ftgmac100 *priv)
737{
738 struct net_device *netdev = priv->netdev;
739 struct ftgmac100_txdes *txdes;
740 struct sk_buff *skb;
741 dma_addr_t map;
742
743 if (priv->tx_pending == 0)
744 return false;
745
746 txdes = ftgmac100_current_clean_txdes(priv);
747
748 if (ftgmac100_txdes_owned_by_dma(txdes))
749 return false;
750
751 skb = ftgmac100_txdes_get_skb(txdes);
752 map = ftgmac100_txdes_get_dma_addr(txdes);
753
754 netdev->stats.tx_packets++;
755 netdev->stats.tx_bytes += skb->len;
756
757 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
758
759 dev_kfree_skb(skb);
760
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930761 ftgmac100_txdes_reset(priv, txdes);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000762
763 ftgmac100_tx_clean_pointer_advance(priv);
764
765 spin_lock(&priv->tx_lock);
766 priv->tx_pending--;
767 spin_unlock(&priv->tx_lock);
768 netif_wake_queue(netdev);
769
770 return true;
771}
772
773static void ftgmac100_tx_complete(struct ftgmac100 *priv)
774{
775 while (ftgmac100_tx_complete_packet(priv))
776 ;
777}
778
779static int ftgmac100_xmit(struct ftgmac100 *priv, struct sk_buff *skb,
780 dma_addr_t map)
781{
782 struct net_device *netdev = priv->netdev;
783 struct ftgmac100_txdes *txdes;
784 unsigned int len = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
785
786 txdes = ftgmac100_current_txdes(priv);
787 ftgmac100_tx_pointer_advance(priv);
788
789 /* setup TX descriptor */
790 ftgmac100_txdes_set_skb(txdes, skb);
791 ftgmac100_txdes_set_dma_addr(txdes, map);
792 ftgmac100_txdes_set_buffer_size(txdes, len);
793
794 ftgmac100_txdes_set_first_segment(txdes);
795 ftgmac100_txdes_set_last_segment(txdes);
796 ftgmac100_txdes_set_txint(txdes);
797 if (skb->ip_summed == CHECKSUM_PARTIAL) {
798 __be16 protocol = skb->protocol;
799
800 if (protocol == cpu_to_be16(ETH_P_IP)) {
801 u8 ip_proto = ip_hdr(skb)->protocol;
802
803 ftgmac100_txdes_set_ipcs(txdes);
804 if (ip_proto == IPPROTO_TCP)
805 ftgmac100_txdes_set_tcpcs(txdes);
806 else if (ip_proto == IPPROTO_UDP)
807 ftgmac100_txdes_set_udpcs(txdes);
808 }
809 }
810
811 spin_lock(&priv->tx_lock);
812 priv->tx_pending++;
813 if (priv->tx_pending == TX_QUEUE_ENTRIES)
814 netif_stop_queue(netdev);
815
816 /* start transmit */
817 ftgmac100_txdes_set_dma_own(txdes);
818 spin_unlock(&priv->tx_lock);
819
820 ftgmac100_txdma_normal_prio_start_polling(priv);
821
822 return NETDEV_TX_OK;
823}
824
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000825static void ftgmac100_free_buffers(struct ftgmac100 *priv)
826{
827 int i;
828
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000829 /* Free all RX buffers */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000830 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
831 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
Andrew Jefferyada66b52016-09-22 08:34:58 +0930832 struct page *page = ftgmac100_rxdes_get_page(priv, rxdes);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000833 dma_addr_t map = ftgmac100_rxdes_get_dma_addr(rxdes);
834
835 if (!page)
836 continue;
837
838 dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
839 __free_page(page);
840 }
841
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000842 /* Free all TX buffers */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000843 for (i = 0; i < TX_QUEUE_ENTRIES; i++) {
844 struct ftgmac100_txdes *txdes = &priv->descs->txdes[i];
845 struct sk_buff *skb = ftgmac100_txdes_get_skb(txdes);
846 dma_addr_t map = ftgmac100_txdes_get_dma_addr(txdes);
847
848 if (!skb)
849 continue;
850
851 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
Eric Dumazet0113e342014-01-16 23:38:24 -0800852 kfree_skb(skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000853 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000854}
855
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000856static void ftgmac100_free_rings(struct ftgmac100 *priv)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000857{
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000858 /* Free descriptors */
859 if (priv->descs)
860 dma_free_coherent(priv->dev, sizeof(struct ftgmac100_descs),
861 priv->descs, priv->descs_dma_addr);
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000862
863 /* Free scratch packet buffer */
864 if (priv->rx_scratch)
865 dma_free_coherent(priv->dev, RX_BUF_SIZE,
866 priv->rx_scratch, priv->rx_scratch_dma);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000867}
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000868
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000869static int ftgmac100_alloc_rings(struct ftgmac100 *priv)
870{
871 /* Allocate descriptors */
Joe Perchesede23fa82013-08-26 22:45:23 -0700872 priv->descs = dma_zalloc_coherent(priv->dev,
873 sizeof(struct ftgmac100_descs),
874 &priv->descs_dma_addr, GFP_KERNEL);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000875 if (!priv->descs)
876 return -ENOMEM;
877
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000878 /* Allocate scratch packet buffer */
879 priv->rx_scratch = dma_alloc_coherent(priv->dev,
880 RX_BUF_SIZE,
881 &priv->rx_scratch_dma,
882 GFP_KERNEL);
883 if (!priv->rx_scratch)
884 return -ENOMEM;
885
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000886 return 0;
887}
888
889static void ftgmac100_init_rings(struct ftgmac100 *priv)
890{
891 int i;
892
893 /* Initialize RX ring */
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000894 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
895 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
896 ftgmac100_rxdes_set_dma_addr(rxdes, priv->rx_scratch_dma);
897 rxdes->rxdes0 = 0;
898 }
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000899 ftgmac100_rxdes_set_end_of_ring(priv, &priv->descs->rxdes[i - 1]);
900
901 /* Initialize TX ring */
902 for (i = 0; i < TX_QUEUE_ENTRIES; i++)
903 priv->descs->txdes[i].txdes0 = 0;
904 ftgmac100_txdes_set_end_of_ring(priv, &priv->descs->txdes[i -1]);
905}
906
907static int ftgmac100_alloc_rx_buffers(struct ftgmac100 *priv)
908{
909 int i;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000910
911 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
912 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
913
914 if (ftgmac100_alloc_rx_page(priv, rxdes, GFP_KERNEL))
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000915 return -ENOMEM;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000916 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000917 return 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000918}
919
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000920static void ftgmac100_adjust_link(struct net_device *netdev)
921{
922 struct ftgmac100 *priv = netdev_priv(netdev);
Philippe Reynesb3c40ad2016-05-16 01:35:13 +0200923 struct phy_device *phydev = netdev->phydev;
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000924 int new_speed;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000925
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000926 /* We store "no link" as speed 0 */
927 if (!phydev->link)
928 new_speed = 0;
929 else
930 new_speed = phydev->speed;
931
932 if (phydev->speed == priv->cur_speed &&
933 phydev->duplex == priv->cur_duplex)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000934 return;
935
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000936 /* Print status if we have a link or we had one and just lost it,
937 * don't print otherwise.
938 */
939 if (new_speed || priv->cur_speed)
940 phy_print_status(phydev);
941
942 priv->cur_speed = new_speed;
943 priv->cur_duplex = phydev->duplex;
944
945 /* Link is down, do nothing else */
946 if (!new_speed)
947 return;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000948
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +1000949 /* Disable all interrupts */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000950 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
951
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +1000952 /* Reset the adapter asynchronously */
953 schedule_work(&priv->reset_task);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000954}
955
956static int ftgmac100_mii_probe(struct ftgmac100 *priv)
957{
958 struct net_device *netdev = priv->netdev;
Guenter Roecke574f392016-01-10 12:04:32 -0800959 struct phy_device *phydev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000960
Guenter Roecke574f392016-01-10 12:04:32 -0800961 phydev = phy_find_first(priv->mii_bus);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000962 if (!phydev) {
963 netdev_info(netdev, "%s: no PHY found\n", netdev->name);
964 return -ENODEV;
965 }
966
Andrew Lunn84eff6d2016-01-06 20:11:10 +0100967 phydev = phy_connect(netdev, phydev_name(phydev),
Florian Fainellif9a8f832013-01-14 00:52:52 +0000968 &ftgmac100_adjust_link, PHY_INTERFACE_MODE_GMII);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000969
970 if (IS_ERR(phydev)) {
971 netdev_err(netdev, "%s: Could not attach to PHY\n", netdev->name);
972 return PTR_ERR(phydev);
973 }
974
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000975 return 0;
976}
977
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000978static int ftgmac100_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
979{
980 struct net_device *netdev = bus->priv;
981 struct ftgmac100 *priv = netdev_priv(netdev);
982 unsigned int phycr;
983 int i;
984
985 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
986
987 /* preserve MDC cycle threshold */
988 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
989
990 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
991 FTGMAC100_PHYCR_REGAD(regnum) |
992 FTGMAC100_PHYCR_MIIRD;
993
994 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
995
996 for (i = 0; i < 10; i++) {
997 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
998
999 if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) {
1000 int data;
1001
1002 data = ioread32(priv->base + FTGMAC100_OFFSET_PHYDATA);
1003 return FTGMAC100_PHYDATA_MIIRDATA(data);
1004 }
1005
1006 udelay(100);
1007 }
1008
1009 netdev_err(netdev, "mdio read timed out\n");
1010 return -EIO;
1011}
1012
1013static int ftgmac100_mdiobus_write(struct mii_bus *bus, int phy_addr,
1014 int regnum, u16 value)
1015{
1016 struct net_device *netdev = bus->priv;
1017 struct ftgmac100 *priv = netdev_priv(netdev);
1018 unsigned int phycr;
1019 int data;
1020 int i;
1021
1022 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1023
1024 /* preserve MDC cycle threshold */
1025 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
1026
1027 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
1028 FTGMAC100_PHYCR_REGAD(regnum) |
1029 FTGMAC100_PHYCR_MIIWR;
1030
1031 data = FTGMAC100_PHYDATA_MIIWDATA(value);
1032
1033 iowrite32(data, priv->base + FTGMAC100_OFFSET_PHYDATA);
1034 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
1035
1036 for (i = 0; i < 10; i++) {
1037 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1038
1039 if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0)
1040 return 0;
1041
1042 udelay(100);
1043 }
1044
1045 netdev_err(netdev, "mdio write timed out\n");
1046 return -EIO;
1047}
1048
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001049static void ftgmac100_get_drvinfo(struct net_device *netdev,
1050 struct ethtool_drvinfo *info)
1051{
Jiri Pirko7826d432013-01-06 00:44:26 +00001052 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1053 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1054 strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info));
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001055}
1056
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001057static const struct ethtool_ops ftgmac100_ethtool_ops = {
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001058 .get_drvinfo = ftgmac100_get_drvinfo,
1059 .get_link = ethtool_op_get_link,
Philippe Reynesfd24d722016-05-16 01:35:14 +02001060 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1061 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001062};
1063
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001064static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id)
1065{
1066 struct net_device *netdev = dev_id;
1067 struct ftgmac100 *priv = netdev_priv(netdev);
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001068 unsigned int status, new_mask = FTGMAC100_INT_BAD;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001069
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001070 /* Fetch and clear interrupt bits, process abnormal ones */
1071 status = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
1072 iowrite32(status, priv->base + FTGMAC100_OFFSET_ISR);
1073 if (unlikely(status & FTGMAC100_INT_BAD)) {
1074
1075 /* RX buffer unavailable */
1076 if (status & FTGMAC100_INT_NO_RXBUF)
1077 netdev->stats.rx_over_errors++;
1078
1079 /* received packet lost due to RX FIFO full */
1080 if (status & FTGMAC100_INT_RPKT_LOST)
1081 netdev->stats.rx_fifo_errors++;
1082
1083 /* sent packet lost due to excessive TX collision */
1084 if (status & FTGMAC100_INT_XPKT_LOST)
1085 netdev->stats.tx_fifo_errors++;
1086
1087 /* AHB error -> Reset the chip */
1088 if (status & FTGMAC100_INT_AHB_ERR) {
1089 if (net_ratelimit())
1090 netdev_warn(netdev,
1091 "AHB bus error ! Resetting chip.\n");
1092 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1093 schedule_work(&priv->reset_task);
1094 return IRQ_HANDLED;
1095 }
1096
1097 /* We may need to restart the MAC after such errors, delay
1098 * this until after we have freed some Rx buffers though
1099 */
1100 priv->need_mac_restart = true;
1101
1102 /* Disable those errors until we restart */
1103 new_mask &= ~status;
1104 }
1105
1106 /* Only enable "bad" interrupts while NAPI is on */
1107 iowrite32(new_mask, priv->base + FTGMAC100_OFFSET_IER);
1108
1109 /* Schedule NAPI bh */
1110 napi_schedule_irqoff(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001111
1112 return IRQ_HANDLED;
1113}
1114
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001115static int ftgmac100_poll(struct napi_struct *napi, int budget)
1116{
1117 struct ftgmac100 *priv = container_of(napi, struct ftgmac100, napi);
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001118 bool more, completed = true;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001119 int rx = 0;
1120
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001121 ftgmac100_tx_complete(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001122
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001123 do {
1124 more = ftgmac100_rx_packet(priv, &rx);
1125 } while (more && rx < budget);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001126
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001127 if (more && rx == budget)
1128 completed = false;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001129
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001130
1131 /* The interrupt is telling us to kick the MAC back to life
1132 * after an RX overflow
1133 */
1134 if (unlikely(priv->need_mac_restart)) {
1135 ftgmac100_start_hw(priv);
1136
1137 /* Re-enable "bad" interrupts */
1138 iowrite32(FTGMAC100_INT_BAD,
1139 priv->base + FTGMAC100_OFFSET_IER);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001140 }
1141
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001142 /* Keep NAPI going if we have still packets to reclaim */
1143 if (priv->tx_pending)
1144 return budget;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001145
1146 if (completed) {
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001147 /* We are about to re-enable all interrupts. However
1148 * the HW has been latching RX/TX packet interrupts while
1149 * they were masked. So we clear them first, then we need
1150 * to re-check if there's something to process
1151 */
1152 iowrite32(FTGMAC100_INT_RXTX,
1153 priv->base + FTGMAC100_OFFSET_ISR);
1154 if (ftgmac100_rxdes_packet_ready
1155 (ftgmac100_current_rxdes(priv)) || priv->tx_pending)
1156 return budget;
1157
1158 /* deschedule NAPI */
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001159 napi_complete(napi);
1160
1161 /* enable all interrupts */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001162 iowrite32(FTGMAC100_INT_ALL,
Gavin Shanfc6061c2016-07-19 11:54:25 +10001163 priv->base + FTGMAC100_OFFSET_IER);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001164 }
1165
1166 return rx;
1167}
1168
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001169static int ftgmac100_init_all(struct ftgmac100 *priv, bool ignore_alloc_err)
1170{
1171 int err = 0;
1172
1173 /* Re-init descriptors (adjust queue sizes) */
1174 ftgmac100_init_rings(priv);
1175
1176 /* Realloc rx descriptors */
1177 err = ftgmac100_alloc_rx_buffers(priv);
1178 if (err && !ignore_alloc_err)
1179 return err;
1180
1181 /* Reinit and restart HW */
1182 ftgmac100_init_hw(priv);
1183 ftgmac100_start_hw(priv);
1184
1185 /* Re-enable the device */
1186 napi_enable(&priv->napi);
1187 netif_start_queue(priv->netdev);
1188
1189 /* Enable all interrupts */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001190 iowrite32(FTGMAC100_INT_ALL, priv->base + FTGMAC100_OFFSET_IER);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001191
1192 return err;
1193}
1194
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001195static void ftgmac100_reset_task(struct work_struct *work)
1196{
1197 struct ftgmac100 *priv = container_of(work, struct ftgmac100,
1198 reset_task);
1199 struct net_device *netdev = priv->netdev;
1200 int err;
1201
1202 netdev_dbg(netdev, "Resetting NIC...\n");
1203
1204 /* Lock the world */
1205 rtnl_lock();
1206 if (netdev->phydev)
1207 mutex_lock(&netdev->phydev->lock);
1208 if (priv->mii_bus)
1209 mutex_lock(&priv->mii_bus->mdio_lock);
1210
1211
1212 /* Check if the interface is still up */
1213 if (!netif_running(netdev))
1214 goto bail;
1215
1216 /* Stop the network stack */
1217 netif_trans_update(netdev);
1218 napi_disable(&priv->napi);
1219 netif_tx_disable(netdev);
1220
1221 /* Stop and reset the MAC */
1222 ftgmac100_stop_hw(priv);
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +10001223 err = ftgmac100_reset_and_config_mac(priv);
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001224 if (err) {
1225 /* Not much we can do ... it might come back... */
1226 netdev_err(netdev, "attempting to continue...\n");
1227 }
1228
1229 /* Free all rx and tx buffers */
1230 ftgmac100_free_buffers(priv);
1231
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001232 /* Setup everything again and restart chip */
1233 ftgmac100_init_all(priv, true);
1234
1235 netdev_dbg(netdev, "Reset done !\n");
1236 bail:
1237 if (priv->mii_bus)
1238 mutex_unlock(&priv->mii_bus->mdio_lock);
1239 if (netdev->phydev)
1240 mutex_unlock(&netdev->phydev->lock);
1241 rtnl_unlock();
1242}
1243
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001244static int ftgmac100_open(struct net_device *netdev)
1245{
1246 struct ftgmac100 *priv = netdev_priv(netdev);
1247 int err;
1248
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001249 /* Allocate ring buffers */
1250 err = ftgmac100_alloc_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001251 if (err) {
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001252 netdev_err(netdev, "Failed to allocate descriptors\n");
1253 return err;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001254 }
1255
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +10001256 /* When using NC-SI we force the speed to 100Mbit/s full duplex,
1257 *
1258 * Otherwise we leave it set to 0 (no link), the link
1259 * message from the PHY layer will handle setting it up to
1260 * something else if needed.
1261 */
1262 if (priv->use_ncsi) {
1263 priv->cur_duplex = DUPLEX_FULL;
1264 priv->cur_speed = SPEED_100;
1265 } else {
1266 priv->cur_duplex = 0;
1267 priv->cur_speed = 0;
1268 }
1269
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +10001270 /* Reset the hardware */
1271 err = ftgmac100_reset_and_config_mac(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001272 if (err)
1273 goto err_hw;
1274
Benjamin Herrenschmidtb8dbecf2017-04-05 12:28:47 +10001275 /* Initialize NAPI */
1276 netif_napi_add(netdev, &priv->napi, ftgmac100_poll, 64);
1277
Benjamin Herrenschmidt81f1eca2017-04-05 12:28:48 +10001278 /* Grab our interrupt */
1279 err = request_irq(netdev->irq, ftgmac100_interrupt, 0, netdev->name, netdev);
1280 if (err) {
1281 netdev_err(netdev, "failed to request irq %d\n", netdev->irq);
1282 goto err_irq;
1283 }
1284
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001285 /* Start things up */
1286 err = ftgmac100_init_all(priv, false);
1287 if (err) {
1288 netdev_err(netdev, "Failed to allocate packet buffers\n");
1289 goto err_alloc;
1290 }
Gavin Shan08c9c122016-09-22 08:35:01 +09301291
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001292 if (netdev->phydev) {
1293 /* If we have a PHY, start polling */
Gavin Shanbd466c32016-07-19 11:54:23 +10001294 phy_start(netdev->phydev);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001295 } else if (priv->use_ncsi) {
1296 /* If using NC-SI, set our carrier on and start the stack */
Gavin Shanbd466c32016-07-19 11:54:23 +10001297 netif_carrier_on(netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001298
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001299 /* Start the NCSI device */
Gavin Shanbd466c32016-07-19 11:54:23 +10001300 err = ncsi_start_dev(priv->ndev);
1301 if (err)
1302 goto err_ncsi;
1303 }
1304
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001305 return 0;
1306
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001307 err_ncsi:
Gavin Shanbd466c32016-07-19 11:54:23 +10001308 napi_disable(&priv->napi);
1309 netif_stop_queue(netdev);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001310 err_alloc:
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001311 ftgmac100_free_buffers(priv);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001312 free_irq(netdev->irq, netdev);
1313 err_irq:
1314 netif_napi_del(&priv->napi);
1315 err_hw:
1316 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001317 ftgmac100_free_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001318 return err;
1319}
1320
1321static int ftgmac100_stop(struct net_device *netdev)
1322{
1323 struct ftgmac100 *priv = netdev_priv(netdev);
1324
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001325 /* Note about the reset task: We are called with the rtnl lock
1326 * held, so we are synchronized against the core of the reset
1327 * task. We must not try to synchronously cancel it otherwise
1328 * we can deadlock. But since it will test for netif_running()
1329 * which has already been cleared by the net core, we don't
1330 * anything special to do.
1331 */
1332
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001333 /* disable all interrupts */
1334 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1335
1336 netif_stop_queue(netdev);
1337 napi_disable(&priv->napi);
Benjamin Herrenschmidtb8dbecf2017-04-05 12:28:47 +10001338 netif_napi_del(&priv->napi);
Gavin Shanbd466c32016-07-19 11:54:23 +10001339 if (netdev->phydev)
1340 phy_stop(netdev->phydev);
Gavin Shan2c15f252016-10-04 11:25:54 +11001341 else if (priv->use_ncsi)
1342 ncsi_stop_dev(priv->ndev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001343
1344 ftgmac100_stop_hw(priv);
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001345 free_irq(netdev->irq, netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001346 ftgmac100_free_buffers(priv);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001347 ftgmac100_free_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001348
1349 return 0;
1350}
1351
1352static int ftgmac100_hard_start_xmit(struct sk_buff *skb,
1353 struct net_device *netdev)
1354{
1355 struct ftgmac100 *priv = netdev_priv(netdev);
1356 dma_addr_t map;
1357
1358 if (unlikely(skb->len > MAX_PKT_SIZE)) {
1359 if (net_ratelimit())
1360 netdev_dbg(netdev, "tx packet too big\n");
1361
1362 netdev->stats.tx_dropped++;
Eric Dumazet0113e342014-01-16 23:38:24 -08001363 kfree_skb(skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001364 return NETDEV_TX_OK;
1365 }
1366
1367 map = dma_map_single(priv->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
1368 if (unlikely(dma_mapping_error(priv->dev, map))) {
1369 /* drop packet */
1370 if (net_ratelimit())
1371 netdev_err(netdev, "map socket buffer failed\n");
1372
1373 netdev->stats.tx_dropped++;
Eric Dumazet0113e342014-01-16 23:38:24 -08001374 kfree_skb(skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001375 return NETDEV_TX_OK;
1376 }
1377
1378 return ftgmac100_xmit(priv, skb, map);
1379}
1380
1381/* optional */
1382static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1383{
Gavin Shanbd466c32016-07-19 11:54:23 +10001384 if (!netdev->phydev)
1385 return -ENXIO;
1386
Philippe Reynesb3c40ad2016-05-16 01:35:13 +02001387 return phy_mii_ioctl(netdev->phydev, ifr, cmd);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001388}
1389
1390static const struct net_device_ops ftgmac100_netdev_ops = {
1391 .ndo_open = ftgmac100_open,
1392 .ndo_stop = ftgmac100_stop,
1393 .ndo_start_xmit = ftgmac100_hard_start_xmit,
Gavin Shan113ce102016-07-19 11:54:22 +10001394 .ndo_set_mac_address = ftgmac100_set_mac_addr,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001395 .ndo_validate_addr = eth_validate_addr,
1396 .ndo_do_ioctl = ftgmac100_do_ioctl,
1397};
1398
Gavin Shaneb418182016-07-19 11:54:21 +10001399static int ftgmac100_setup_mdio(struct net_device *netdev)
1400{
1401 struct ftgmac100 *priv = netdev_priv(netdev);
1402 struct platform_device *pdev = to_platform_device(priv->dev);
1403 int i, err = 0;
Joel Stanleye07dc632016-09-22 08:35:02 +09301404 u32 reg;
Gavin Shaneb418182016-07-19 11:54:21 +10001405
1406 /* initialize mdio bus */
1407 priv->mii_bus = mdiobus_alloc();
1408 if (!priv->mii_bus)
1409 return -EIO;
1410
Joel Stanleye07dc632016-09-22 08:35:02 +09301411 if (of_machine_is_compatible("aspeed,ast2400") ||
1412 of_machine_is_compatible("aspeed,ast2500")) {
1413 /* This driver supports the old MDIO interface */
1414 reg = ioread32(priv->base + FTGMAC100_OFFSET_REVR);
1415 reg &= ~FTGMAC100_REVR_NEW_MDIO_INTERFACE;
1416 iowrite32(reg, priv->base + FTGMAC100_OFFSET_REVR);
1417 };
1418
Gavin Shaneb418182016-07-19 11:54:21 +10001419 priv->mii_bus->name = "ftgmac100_mdio";
1420 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d",
1421 pdev->name, pdev->id);
1422 priv->mii_bus->priv = priv->netdev;
1423 priv->mii_bus->read = ftgmac100_mdiobus_read;
1424 priv->mii_bus->write = ftgmac100_mdiobus_write;
1425
1426 for (i = 0; i < PHY_MAX_ADDR; i++)
1427 priv->mii_bus->irq[i] = PHY_POLL;
1428
1429 err = mdiobus_register(priv->mii_bus);
1430 if (err) {
1431 dev_err(priv->dev, "Cannot register MDIO bus!\n");
1432 goto err_register_mdiobus;
1433 }
1434
1435 err = ftgmac100_mii_probe(priv);
1436 if (err) {
1437 dev_err(priv->dev, "MII Probe failed!\n");
1438 goto err_mii_probe;
1439 }
1440
1441 return 0;
1442
1443err_mii_probe:
1444 mdiobus_unregister(priv->mii_bus);
1445err_register_mdiobus:
1446 mdiobus_free(priv->mii_bus);
1447 return err;
1448}
1449
1450static void ftgmac100_destroy_mdio(struct net_device *netdev)
1451{
1452 struct ftgmac100 *priv = netdev_priv(netdev);
1453
1454 if (!netdev->phydev)
1455 return;
1456
1457 phy_disconnect(netdev->phydev);
1458 mdiobus_unregister(priv->mii_bus);
1459 mdiobus_free(priv->mii_bus);
1460}
1461
Gavin Shanbd466c32016-07-19 11:54:23 +10001462static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
1463{
1464 if (unlikely(nd->state != ncsi_dev_state_functional))
1465 return;
1466
1467 netdev_info(nd->dev, "NCSI interface %s\n",
1468 nd->link_up ? "up" : "down");
1469}
1470
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001471static int ftgmac100_probe(struct platform_device *pdev)
1472{
1473 struct resource *res;
1474 int irq;
1475 struct net_device *netdev;
1476 struct ftgmac100 *priv;
Gavin Shanbd466c32016-07-19 11:54:23 +10001477 int err = 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001478
1479 if (!pdev)
1480 return -ENODEV;
1481
1482 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1483 if (!res)
1484 return -ENXIO;
1485
1486 irq = platform_get_irq(pdev, 0);
1487 if (irq < 0)
1488 return irq;
1489
1490 /* setup net_device */
1491 netdev = alloc_etherdev(sizeof(*priv));
1492 if (!netdev) {
1493 err = -ENOMEM;
1494 goto err_alloc_etherdev;
1495 }
1496
1497 SET_NETDEV_DEV(netdev, &pdev->dev);
1498
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001499 netdev->ethtool_ops = &ftgmac100_ethtool_ops;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001500 netdev->netdev_ops = &ftgmac100_netdev_ops;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001501
1502 platform_set_drvdata(pdev, netdev);
1503
1504 /* setup private data */
1505 priv = netdev_priv(netdev);
1506 priv->netdev = netdev;
1507 priv->dev = &pdev->dev;
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001508 INIT_WORK(&priv->reset_task, ftgmac100_reset_task);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001509
1510 spin_lock_init(&priv->tx_lock);
1511
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001512 /* map io memory */
1513 priv->res = request_mem_region(res->start, resource_size(res),
1514 dev_name(&pdev->dev));
1515 if (!priv->res) {
1516 dev_err(&pdev->dev, "Could not reserve memory region\n");
1517 err = -ENOMEM;
1518 goto err_req_mem;
1519 }
1520
1521 priv->base = ioremap(res->start, resource_size(res));
1522 if (!priv->base) {
1523 dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
1524 err = -EIO;
1525 goto err_ioremap;
1526 }
1527
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001528 netdev->irq = irq;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001529
Gavin Shan113ce102016-07-19 11:54:22 +10001530 /* MAC address from chip or random one */
1531 ftgmac100_setup_mac(priv);
1532
Joel Stanley2a0ab8eb2016-09-22 08:35:00 +09301533 if (of_machine_is_compatible("aspeed,ast2400") ||
1534 of_machine_is_compatible("aspeed,ast2500")) {
1535 priv->rxdes0_edorr_mask = BIT(30);
1536 priv->txdes0_edotr_mask = BIT(30);
1537 } else {
1538 priv->rxdes0_edorr_mask = BIT(15);
1539 priv->txdes0_edotr_mask = BIT(15);
1540 }
1541
Gavin Shanbd466c32016-07-19 11:54:23 +10001542 if (pdev->dev.of_node &&
1543 of_get_property(pdev->dev.of_node, "use-ncsi", NULL)) {
1544 if (!IS_ENABLED(CONFIG_NET_NCSI)) {
1545 dev_err(&pdev->dev, "NCSI stack not enabled\n");
1546 goto err_ncsi_dev;
1547 }
1548
1549 dev_info(&pdev->dev, "Using NCSI interface\n");
1550 priv->use_ncsi = true;
1551 priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
1552 if (!priv->ndev)
1553 goto err_ncsi_dev;
1554 } else {
1555 priv->use_ncsi = false;
1556 err = ftgmac100_setup_mdio(netdev);
1557 if (err)
1558 goto err_setup_mdio;
1559 }
1560
1561 /* We have to disable on-chip IP checksum functionality
1562 * when NCSI is enabled on the interface. It doesn't work
1563 * in that case.
1564 */
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +10001565 netdev->features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_GRO;
Gavin Shanbd466c32016-07-19 11:54:23 +10001566 if (priv->use_ncsi &&
1567 of_get_property(pdev->dev.of_node, "no-hw-checksum", NULL))
1568 netdev->features &= ~NETIF_F_IP_CSUM;
1569
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001570
1571 /* register network device */
1572 err = register_netdev(netdev);
1573 if (err) {
1574 dev_err(&pdev->dev, "Failed to register netdev\n");
1575 goto err_register_netdev;
1576 }
1577
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001578 netdev_info(netdev, "irq %d, mapped at %p\n", netdev->irq, priv->base);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001579
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001580 return 0;
1581
Gavin Shanbd466c32016-07-19 11:54:23 +10001582err_ncsi_dev:
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001583err_register_netdev:
Gavin Shaneb418182016-07-19 11:54:21 +10001584 ftgmac100_destroy_mdio(netdev);
1585err_setup_mdio:
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001586 iounmap(priv->base);
1587err_ioremap:
1588 release_resource(priv->res);
1589err_req_mem:
1590 netif_napi_del(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001591 free_netdev(netdev);
1592err_alloc_etherdev:
1593 return err;
1594}
1595
Dmitry Torokhovbe125022017-03-01 17:24:47 -08001596static int ftgmac100_remove(struct platform_device *pdev)
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001597{
1598 struct net_device *netdev;
1599 struct ftgmac100 *priv;
1600
1601 netdev = platform_get_drvdata(pdev);
1602 priv = netdev_priv(netdev);
1603
1604 unregister_netdev(netdev);
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001605
1606 /* There's a small chance the reset task will have been re-queued,
1607 * during stop, make sure it's gone before we free the structure.
1608 */
1609 cancel_work_sync(&priv->reset_task);
1610
Gavin Shaneb418182016-07-19 11:54:21 +10001611 ftgmac100_destroy_mdio(netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001612
1613 iounmap(priv->base);
1614 release_resource(priv->res);
1615
1616 netif_napi_del(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001617 free_netdev(netdev);
1618 return 0;
1619}
1620
Gavin Shanbb168e22016-07-19 11:54:24 +10001621static const struct of_device_id ftgmac100_of_match[] = {
1622 { .compatible = "faraday,ftgmac100" },
1623 { }
1624};
1625MODULE_DEVICE_TABLE(of, ftgmac100_of_match);
1626
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001627static struct platform_driver ftgmac100_driver = {
Gavin Shanbb168e22016-07-19 11:54:24 +10001628 .probe = ftgmac100_probe,
Dmitry Torokhovbe125022017-03-01 17:24:47 -08001629 .remove = ftgmac100_remove,
Gavin Shanbb168e22016-07-19 11:54:24 +10001630 .driver = {
1631 .name = DRV_NAME,
1632 .of_match_table = ftgmac100_of_match,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001633 },
1634};
Sachin Kamat14f645d2013-03-18 01:50:48 +00001635module_platform_driver(ftgmac100_driver);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001636
1637MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
1638MODULE_DESCRIPTION("FTGMAC100 driver");
1639MODULE_LICENSE("GPL");