blob: 18b02170928631a1b9978b0fd2e2ec5d9d3e4df4 [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
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +100046#define MAX_PKT_SIZE 1536
47#define RX_BUF_SIZE MAX_PKT_SIZE /* must be smaller than 0x3fff */
Po-Yu Chuang69785b72011-06-08 23:32:48 +000048
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 */
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +100063 struct sk_buff *rx_skbs[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
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000281static int ftgmac100_alloc_rx_buf(struct ftgmac100 *priv, unsigned int entry,
282 struct ftgmac100_rxdes *rxdes, gfp_t gfp)
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000283{
284 struct net_device *netdev = priv->netdev;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000285 struct sk_buff *skb;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000286 dma_addr_t map;
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000287 int err;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000288
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000289 skb = netdev_alloc_skb_ip_align(netdev, RX_BUF_SIZE);
290 if (unlikely(!skb)) {
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000291 if (net_ratelimit())
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000292 netdev_warn(netdev, "failed to allocate rx skb\n");
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000293 err = -ENOMEM;
294 map = priv->rx_scratch_dma;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000295 } else {
296 map = dma_map_single(priv->dev, skb->data, RX_BUF_SIZE,
297 DMA_FROM_DEVICE);
298 if (unlikely(dma_mapping_error(priv->dev, map))) {
299 if (net_ratelimit())
300 netdev_err(netdev, "failed to map rx page\n");
301 dev_kfree_skb_any(skb);
302 map = priv->rx_scratch_dma;
303 skb = NULL;
304 err = -ENOMEM;
305 }
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000306 }
307
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000308 /* Store skb */
309 priv->rx_skbs[entry] = skb;
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000310
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000311 /* Store DMA address into RX desc */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000312 rxdes->rxdes3 = cpu_to_le32(map);
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000313
314 /* Ensure the above is ordered vs clearing the OWN bit */
315 dma_wmb();
316
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000317 /* Clean status (which resets own bit) */
318 if (entry == (RX_QUEUE_ENTRIES - 1))
319 rxdes->rxdes0 = cpu_to_le32(priv->rxdes0_edorr_mask);
320 else
321 rxdes->rxdes0 = 0;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000322
Benjamin Herrenschmidtc06f73f2017-04-06 11:02:43 +1000323 return 0;
324}
325
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000326static int ftgmac100_next_rx_pointer(int pointer)
327{
328 return (pointer + 1) & (RX_QUEUE_ENTRIES - 1);
329}
330
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000331static void ftgmac100_rx_packet_error(struct ftgmac100 *priv, u32 status)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000332{
333 struct net_device *netdev = priv->netdev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000334
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000335 if (status & FTGMAC100_RXDES0_RX_ERR)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000336 netdev->stats.rx_errors++;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000337
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000338 if (status & FTGMAC100_RXDES0_CRC_ERR)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000339 netdev->stats.rx_crc_errors++;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000340
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000341 if (status & (FTGMAC100_RXDES0_FTL |
342 FTGMAC100_RXDES0_RUNT |
343 FTGMAC100_RXDES0_RX_ODD_NB))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000344 netdev->stats.rx_length_errors++;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000345}
346
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000347static bool ftgmac100_rx_packet(struct ftgmac100 *priv, int *processed)
348{
349 struct net_device *netdev = priv->netdev;
350 struct ftgmac100_rxdes *rxdes;
351 struct sk_buff *skb;
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000352 unsigned int pointer, size;
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000353 u32 status, csum_vlan;
Benjamin Herrenschmidtb1977bf2017-04-06 11:02:44 +1000354 dma_addr_t map;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000355
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000356 /* Grab next RX descriptor */
357 pointer = priv->rx_pointer;
358 rxdes = &priv->descs->rxdes[pointer];
359
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000360 /* Grab descriptor status */
361 status = le32_to_cpu(rxdes->rxdes0);
362
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000363 /* Do we have a packet ? */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000364 if (!(status & FTGMAC100_RXDES0_RXPKT_RDY))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000365 return false;
366
Benjamin Herrenschmidt027f4262017-04-06 11:02:50 +1000367 /* Order subsequent reads with the test for the ready bit */
368 dma_rmb();
369
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000370 /* We don't cope with fragmented RX packets */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000371 if (unlikely(!(status & FTGMAC100_RXDES0_FRS) ||
372 !(status & FTGMAC100_RXDES0_LRS)))
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000373 goto drop;
374
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000375 /* Grab received size and csum vlan field in the descriptor */
376 size = status & FTGMAC100_RXDES0_VDBC;
377 csum_vlan = le32_to_cpu(rxdes->rxdes1);
378
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000379 /* Any error (other than csum offload) flagged ? */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000380 if (unlikely(status & RXDES0_ANY_ERROR)) {
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000381 /* Correct for incorrect flagging of runt packets
382 * with vlan tags... Just accept a runt packet that
383 * has been flagged as vlan and whose size is at
384 * least 60 bytes.
385 */
386 if ((status & FTGMAC100_RXDES0_RUNT) &&
387 (csum_vlan & FTGMAC100_RXDES1_VLANTAG_AVAIL) &&
388 (size >= 60))
389 status &= ~FTGMAC100_RXDES0_RUNT;
390
391 /* Any error still in there ? */
392 if (status & RXDES0_ANY_ERROR) {
393 ftgmac100_rx_packet_error(priv, status);
394 goto drop;
395 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000396 }
397
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000398 /* If the packet had no skb (failed to allocate earlier)
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000399 * then try to allocate one and skip
400 */
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000401 skb = priv->rx_skbs[pointer];
402 if (!unlikely(skb)) {
403 ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC);
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000404 goto drop;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000405 }
406
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000407 if (unlikely(status & FTGMAC100_RXDES0_MULTICAST))
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000408 netdev->stats.multicast++;
409
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000410 /* If the HW found checksum errors, bounce it to software.
411 *
412 * If we didn't, we need to see if the packet was recognized
413 * by HW as one of the supported checksummed protocols before
414 * we accept the HW test results.
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000415 */
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000416 if (netdev->features & NETIF_F_RXCSUM) {
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000417 u32 err_bits = FTGMAC100_RXDES1_TCP_CHKSUM_ERR |
418 FTGMAC100_RXDES1_UDP_CHKSUM_ERR |
419 FTGMAC100_RXDES1_IP_CHKSUM_ERR;
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000420 if ((csum_vlan & err_bits) ||
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000421 !(csum_vlan & FTGMAC100_RXDES1_PROT_MASK))
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000422 skb->ip_summed = CHECKSUM_NONE;
423 else
424 skb->ip_summed = CHECKSUM_UNNECESSARY;
425 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000426
Benjamin Herrenschmidtd9306552017-04-06 11:02:52 +1000427 /* Transfer received size to skb */
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000428 skb_put(skb, size);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000429
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000430 /* Tear down DMA mapping, do necessary cache management */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000431 map = le32_to_cpu(rxdes->rxdes3);
432
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000433#if defined(CONFIG_ARM) && !defined(CONFIG_ARM_DMA_USE_IOMMU)
434 /* When we don't have an iommu, we can save cycles by not
435 * invalidating the cache for the part of the packet that
436 * wasn't received.
437 */
438 dma_unmap_single(priv->dev, map, size, DMA_FROM_DEVICE);
439#else
440 dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
441#endif
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000442
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000443
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000444 /* Resplenish rx ring */
445 ftgmac100_alloc_rx_buf(priv, pointer, rxdes, GFP_ATOMIC);
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000446 priv->rx_pointer = ftgmac100_next_rx_pointer(pointer);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000447
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000448 skb->protocol = eth_type_trans(skb, netdev);
449
450 netdev->stats.rx_packets++;
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000451 netdev->stats.rx_bytes += size;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000452
453 /* push packet to protocol stack */
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +1000454 if (skb->ip_summed == CHECKSUM_NONE)
455 netif_receive_skb(skb);
456 else
457 napi_gro_receive(&priv->napi, skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000458
459 (*processed)++;
460 return true;
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000461
462 drop:
463 /* Clean rxdes0 (which resets own bit) */
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000464 rxdes->rxdes0 = cpu_to_le32(status & priv->rxdes0_edorr_mask);
Benjamin Herrenschmidt01dd70b2017-04-06 11:02:48 +1000465 priv->rx_pointer = ftgmac100_next_rx_pointer(pointer);
466 netdev->stats.rx_dropped++;
467 return true;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000468}
469
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930470static void ftgmac100_txdes_reset(const struct ftgmac100 *priv,
471 struct ftgmac100_txdes *txdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000472{
473 /* clear all except end of ring bit */
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930474 txdes->txdes0 &= cpu_to_le32(priv->txdes0_edotr_mask);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000475 txdes->txdes1 = 0;
476 txdes->txdes2 = 0;
477 txdes->txdes3 = 0;
478}
479
480static bool ftgmac100_txdes_owned_by_dma(struct ftgmac100_txdes *txdes)
481{
482 return txdes->txdes0 & cpu_to_le32(FTGMAC100_TXDES0_TXDMA_OWN);
483}
484
485static void ftgmac100_txdes_set_dma_own(struct ftgmac100_txdes *txdes)
486{
487 /*
488 * Make sure dma own bit will not be set before any other
489 * descriptor fields.
490 */
491 wmb();
492 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_TXDMA_OWN);
493}
494
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930495static void ftgmac100_txdes_set_end_of_ring(const struct ftgmac100 *priv,
496 struct ftgmac100_txdes *txdes)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000497{
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930498 txdes->txdes0 |= cpu_to_le32(priv->txdes0_edotr_mask);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000499}
500
501static void ftgmac100_txdes_set_first_segment(struct ftgmac100_txdes *txdes)
502{
503 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_FTS);
504}
505
506static void ftgmac100_txdes_set_last_segment(struct ftgmac100_txdes *txdes)
507{
508 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_LTS);
509}
510
511static void ftgmac100_txdes_set_buffer_size(struct ftgmac100_txdes *txdes,
512 unsigned int len)
513{
514 txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_TXBUF_SIZE(len));
515}
516
517static void ftgmac100_txdes_set_txint(struct ftgmac100_txdes *txdes)
518{
519 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_TXIC);
520}
521
522static void ftgmac100_txdes_set_tcpcs(struct ftgmac100_txdes *txdes)
523{
524 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_TCP_CHKSUM);
525}
526
527static void ftgmac100_txdes_set_udpcs(struct ftgmac100_txdes *txdes)
528{
529 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_UDP_CHKSUM);
530}
531
532static void ftgmac100_txdes_set_ipcs(struct ftgmac100_txdes *txdes)
533{
534 txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_IP_CHKSUM);
535}
536
537static void ftgmac100_txdes_set_dma_addr(struct ftgmac100_txdes *txdes,
538 dma_addr_t addr)
539{
540 txdes->txdes3 = cpu_to_le32(addr);
541}
542
543static dma_addr_t ftgmac100_txdes_get_dma_addr(struct ftgmac100_txdes *txdes)
544{
545 return le32_to_cpu(txdes->txdes3);
546}
547
548/*
549 * txdes2 is not used by hardware. We use it to keep track of socket buffer.
550 * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
551 */
552static void ftgmac100_txdes_set_skb(struct ftgmac100_txdes *txdes,
553 struct sk_buff *skb)
554{
555 txdes->txdes2 = (unsigned int)skb;
556}
557
558static struct sk_buff *ftgmac100_txdes_get_skb(struct ftgmac100_txdes *txdes)
559{
560 return (struct sk_buff *)txdes->txdes2;
561}
562
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000563static int ftgmac100_next_tx_pointer(int pointer)
564{
565 return (pointer + 1) & (TX_QUEUE_ENTRIES - 1);
566}
567
568static void ftgmac100_tx_pointer_advance(struct ftgmac100 *priv)
569{
570 priv->tx_pointer = ftgmac100_next_tx_pointer(priv->tx_pointer);
571}
572
573static void ftgmac100_tx_clean_pointer_advance(struct ftgmac100 *priv)
574{
575 priv->tx_clean_pointer = ftgmac100_next_tx_pointer(priv->tx_clean_pointer);
576}
577
578static struct ftgmac100_txdes *ftgmac100_current_txdes(struct ftgmac100 *priv)
579{
580 return &priv->descs->txdes[priv->tx_pointer];
581}
582
583static struct ftgmac100_txdes *
584ftgmac100_current_clean_txdes(struct ftgmac100 *priv)
585{
586 return &priv->descs->txdes[priv->tx_clean_pointer];
587}
588
589static bool ftgmac100_tx_complete_packet(struct ftgmac100 *priv)
590{
591 struct net_device *netdev = priv->netdev;
592 struct ftgmac100_txdes *txdes;
593 struct sk_buff *skb;
594 dma_addr_t map;
595
596 if (priv->tx_pending == 0)
597 return false;
598
599 txdes = ftgmac100_current_clean_txdes(priv);
600
601 if (ftgmac100_txdes_owned_by_dma(txdes))
602 return false;
603
604 skb = ftgmac100_txdes_get_skb(txdes);
605 map = ftgmac100_txdes_get_dma_addr(txdes);
606
607 netdev->stats.tx_packets++;
608 netdev->stats.tx_bytes += skb->len;
609
610 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
611
612 dev_kfree_skb(skb);
613
Andrew Jeffery7906a4d2016-09-22 08:34:59 +0930614 ftgmac100_txdes_reset(priv, txdes);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000615
616 ftgmac100_tx_clean_pointer_advance(priv);
617
618 spin_lock(&priv->tx_lock);
619 priv->tx_pending--;
620 spin_unlock(&priv->tx_lock);
621 netif_wake_queue(netdev);
622
623 return true;
624}
625
626static void ftgmac100_tx_complete(struct ftgmac100 *priv)
627{
628 while (ftgmac100_tx_complete_packet(priv))
629 ;
630}
631
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000632static int ftgmac100_hard_start_xmit(struct sk_buff *skb,
633 struct net_device *netdev)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000634{
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000635 unsigned int len = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
Benjamin Herrenschmidt43b25ee2017-04-10 11:15:17 +1000636 struct ftgmac100 *priv = netdev_priv(netdev);
637 struct ftgmac100_txdes *txdes;
638 dma_addr_t map;
639
640 if (unlikely(skb->len > MAX_PKT_SIZE)) {
641 if (net_ratelimit())
642 netdev_dbg(netdev, "tx packet too big\n");
643
644 netdev->stats.tx_dropped++;
645 kfree_skb(skb);
646 return NETDEV_TX_OK;
647 }
648
649 map = dma_map_single(priv->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
650 if (unlikely(dma_mapping_error(priv->dev, map))) {
651 /* drop packet */
652 if (net_ratelimit())
653 netdev_err(netdev, "map socket buffer failed\n");
654
655 netdev->stats.tx_dropped++;
656 kfree_skb(skb);
657 return NETDEV_TX_OK;
658 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000659
660 txdes = ftgmac100_current_txdes(priv);
661 ftgmac100_tx_pointer_advance(priv);
662
663 /* setup TX descriptor */
664 ftgmac100_txdes_set_skb(txdes, skb);
665 ftgmac100_txdes_set_dma_addr(txdes, map);
666 ftgmac100_txdes_set_buffer_size(txdes, len);
667
668 ftgmac100_txdes_set_first_segment(txdes);
669 ftgmac100_txdes_set_last_segment(txdes);
670 ftgmac100_txdes_set_txint(txdes);
671 if (skb->ip_summed == CHECKSUM_PARTIAL) {
672 __be16 protocol = skb->protocol;
673
674 if (protocol == cpu_to_be16(ETH_P_IP)) {
675 u8 ip_proto = ip_hdr(skb)->protocol;
676
677 ftgmac100_txdes_set_ipcs(txdes);
678 if (ip_proto == IPPROTO_TCP)
679 ftgmac100_txdes_set_tcpcs(txdes);
680 else if (ip_proto == IPPROTO_UDP)
681 ftgmac100_txdes_set_udpcs(txdes);
682 }
683 }
684
685 spin_lock(&priv->tx_lock);
686 priv->tx_pending++;
687 if (priv->tx_pending == TX_QUEUE_ENTRIES)
688 netif_stop_queue(netdev);
689
690 /* start transmit */
691 ftgmac100_txdes_set_dma_own(txdes);
692 spin_unlock(&priv->tx_lock);
693
694 ftgmac100_txdma_normal_prio_start_polling(priv);
695
696 return NETDEV_TX_OK;
697}
698
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000699static void ftgmac100_free_buffers(struct ftgmac100 *priv)
700{
701 int i;
702
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000703 /* Free all RX buffers */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000704 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
705 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000706 struct sk_buff *skb = priv->rx_skbs[i];
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000707 dma_addr_t map = le32_to_cpu(rxdes->rxdes3);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000708
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000709 if (!skb)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000710 continue;
711
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000712 priv->rx_skbs[i] = NULL;
713 dma_unmap_single(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
714 dev_kfree_skb_any(skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000715 }
716
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000717 /* Free all TX buffers */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000718 for (i = 0; i < TX_QUEUE_ENTRIES; i++) {
719 struct ftgmac100_txdes *txdes = &priv->descs->txdes[i];
720 struct sk_buff *skb = ftgmac100_txdes_get_skb(txdes);
721 dma_addr_t map = ftgmac100_txdes_get_dma_addr(txdes);
722
723 if (!skb)
724 continue;
725
726 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
Eric Dumazet0113e342014-01-16 23:38:24 -0800727 kfree_skb(skb);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000728 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000729}
730
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000731static void ftgmac100_free_rings(struct ftgmac100 *priv)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000732{
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000733 /* Free descriptors */
734 if (priv->descs)
735 dma_free_coherent(priv->dev, sizeof(struct ftgmac100_descs),
736 priv->descs, priv->descs_dma_addr);
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000737
738 /* Free scratch packet buffer */
739 if (priv->rx_scratch)
740 dma_free_coherent(priv->dev, RX_BUF_SIZE,
741 priv->rx_scratch, priv->rx_scratch_dma);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000742}
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000743
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000744static int ftgmac100_alloc_rings(struct ftgmac100 *priv)
745{
746 /* Allocate descriptors */
Joe Perchesede23fa82013-08-26 22:45:23 -0700747 priv->descs = dma_zalloc_coherent(priv->dev,
748 sizeof(struct ftgmac100_descs),
749 &priv->descs_dma_addr, GFP_KERNEL);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000750 if (!priv->descs)
751 return -ENOMEM;
752
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000753 /* Allocate scratch packet buffer */
754 priv->rx_scratch = dma_alloc_coherent(priv->dev,
755 RX_BUF_SIZE,
756 &priv->rx_scratch_dma,
757 GFP_KERNEL);
758 if (!priv->rx_scratch)
759 return -ENOMEM;
760
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000761 return 0;
762}
763
764static void ftgmac100_init_rings(struct ftgmac100 *priv)
765{
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000766 struct ftgmac100_rxdes *rxdes;
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000767 int i;
768
769 /* Initialize RX ring */
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000770 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000771 rxdes = &priv->descs->rxdes[i];
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000772 rxdes->rxdes0 = 0;
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000773 rxdes->rxdes3 = cpu_to_le32(priv->rx_scratch_dma);
Benjamin Herrenschmidtd72e01a2017-04-06 11:02:45 +1000774 }
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000775 /* Mark the end of the ring */
776 rxdes->rxdes0 |= cpu_to_le32(priv->rxdes0_edorr_mask);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000777
778 /* Initialize TX ring */
779 for (i = 0; i < TX_QUEUE_ENTRIES; i++)
780 priv->descs->txdes[i].txdes0 = 0;
781 ftgmac100_txdes_set_end_of_ring(priv, &priv->descs->txdes[i -1]);
782}
783
784static int ftgmac100_alloc_rx_buffers(struct ftgmac100 *priv)
785{
786 int i;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000787
788 for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
789 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
790
Benjamin Herrenschmidt7b49cd12017-04-06 11:02:49 +1000791 if (ftgmac100_alloc_rx_buf(priv, i, rxdes, GFP_KERNEL))
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +1000792 return -ENOMEM;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000793 }
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000794 return 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000795}
796
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000797static void ftgmac100_adjust_link(struct net_device *netdev)
798{
799 struct ftgmac100 *priv = netdev_priv(netdev);
Philippe Reynesb3c40ad2016-05-16 01:35:13 +0200800 struct phy_device *phydev = netdev->phydev;
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000801 int new_speed;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000802
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000803 /* We store "no link" as speed 0 */
804 if (!phydev->link)
805 new_speed = 0;
806 else
807 new_speed = phydev->speed;
808
809 if (phydev->speed == priv->cur_speed &&
810 phydev->duplex == priv->cur_duplex)
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000811 return;
812
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +1000813 /* Print status if we have a link or we had one and just lost it,
814 * don't print otherwise.
815 */
816 if (new_speed || priv->cur_speed)
817 phy_print_status(phydev);
818
819 priv->cur_speed = new_speed;
820 priv->cur_duplex = phydev->duplex;
821
822 /* Link is down, do nothing else */
823 if (!new_speed)
824 return;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000825
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +1000826 /* Disable all interrupts */
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000827 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
828
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +1000829 /* Reset the adapter asynchronously */
830 schedule_work(&priv->reset_task);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000831}
832
833static int ftgmac100_mii_probe(struct ftgmac100 *priv)
834{
835 struct net_device *netdev = priv->netdev;
Guenter Roecke574f392016-01-10 12:04:32 -0800836 struct phy_device *phydev;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000837
Guenter Roecke574f392016-01-10 12:04:32 -0800838 phydev = phy_find_first(priv->mii_bus);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000839 if (!phydev) {
840 netdev_info(netdev, "%s: no PHY found\n", netdev->name);
841 return -ENODEV;
842 }
843
Andrew Lunn84eff6d2016-01-06 20:11:10 +0100844 phydev = phy_connect(netdev, phydev_name(phydev),
Florian Fainellif9a8f832013-01-14 00:52:52 +0000845 &ftgmac100_adjust_link, PHY_INTERFACE_MODE_GMII);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000846
847 if (IS_ERR(phydev)) {
848 netdev_err(netdev, "%s: Could not attach to PHY\n", netdev->name);
849 return PTR_ERR(phydev);
850 }
851
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000852 return 0;
853}
854
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000855static int ftgmac100_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
856{
857 struct net_device *netdev = bus->priv;
858 struct ftgmac100 *priv = netdev_priv(netdev);
859 unsigned int phycr;
860 int i;
861
862 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
863
864 /* preserve MDC cycle threshold */
865 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
866
867 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
868 FTGMAC100_PHYCR_REGAD(regnum) |
869 FTGMAC100_PHYCR_MIIRD;
870
871 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
872
873 for (i = 0; i < 10; i++) {
874 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
875
876 if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) {
877 int data;
878
879 data = ioread32(priv->base + FTGMAC100_OFFSET_PHYDATA);
880 return FTGMAC100_PHYDATA_MIIRDATA(data);
881 }
882
883 udelay(100);
884 }
885
886 netdev_err(netdev, "mdio read timed out\n");
887 return -EIO;
888}
889
890static int ftgmac100_mdiobus_write(struct mii_bus *bus, int phy_addr,
891 int regnum, u16 value)
892{
893 struct net_device *netdev = bus->priv;
894 struct ftgmac100 *priv = netdev_priv(netdev);
895 unsigned int phycr;
896 int data;
897 int i;
898
899 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
900
901 /* preserve MDC cycle threshold */
902 phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
903
904 phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
905 FTGMAC100_PHYCR_REGAD(regnum) |
906 FTGMAC100_PHYCR_MIIWR;
907
908 data = FTGMAC100_PHYDATA_MIIWDATA(value);
909
910 iowrite32(data, priv->base + FTGMAC100_OFFSET_PHYDATA);
911 iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
912
913 for (i = 0; i < 10; i++) {
914 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
915
916 if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0)
917 return 0;
918
919 udelay(100);
920 }
921
922 netdev_err(netdev, "mdio write timed out\n");
923 return -EIO;
924}
925
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000926static void ftgmac100_get_drvinfo(struct net_device *netdev,
927 struct ethtool_drvinfo *info)
928{
Jiri Pirko7826d432013-01-06 00:44:26 +0000929 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
930 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
931 strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info));
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000932}
933
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000934static const struct ethtool_ops ftgmac100_ethtool_ops = {
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000935 .get_drvinfo = ftgmac100_get_drvinfo,
936 .get_link = ethtool_op_get_link,
Philippe Reynesfd24d722016-05-16 01:35:14 +0200937 .get_link_ksettings = phy_ethtool_get_link_ksettings,
938 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000939};
940
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000941static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id)
942{
943 struct net_device *netdev = dev_id;
944 struct ftgmac100 *priv = netdev_priv(netdev);
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +1000945 unsigned int status, new_mask = FTGMAC100_INT_BAD;
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000946
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +1000947 /* Fetch and clear interrupt bits, process abnormal ones */
948 status = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
949 iowrite32(status, priv->base + FTGMAC100_OFFSET_ISR);
950 if (unlikely(status & FTGMAC100_INT_BAD)) {
951
952 /* RX buffer unavailable */
953 if (status & FTGMAC100_INT_NO_RXBUF)
954 netdev->stats.rx_over_errors++;
955
956 /* received packet lost due to RX FIFO full */
957 if (status & FTGMAC100_INT_RPKT_LOST)
958 netdev->stats.rx_fifo_errors++;
959
960 /* sent packet lost due to excessive TX collision */
961 if (status & FTGMAC100_INT_XPKT_LOST)
962 netdev->stats.tx_fifo_errors++;
963
964 /* AHB error -> Reset the chip */
965 if (status & FTGMAC100_INT_AHB_ERR) {
966 if (net_ratelimit())
967 netdev_warn(netdev,
968 "AHB bus error ! Resetting chip.\n");
969 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
970 schedule_work(&priv->reset_task);
971 return IRQ_HANDLED;
972 }
973
974 /* We may need to restart the MAC after such errors, delay
975 * this until after we have freed some Rx buffers though
976 */
977 priv->need_mac_restart = true;
978
979 /* Disable those errors until we restart */
980 new_mask &= ~status;
981 }
982
983 /* Only enable "bad" interrupts while NAPI is on */
984 iowrite32(new_mask, priv->base + FTGMAC100_OFFSET_IER);
985
986 /* Schedule NAPI bh */
987 napi_schedule_irqoff(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +0000988
989 return IRQ_HANDLED;
990}
991
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +1000992static bool ftgmac100_check_rx(struct ftgmac100 *priv)
993{
994 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[priv->rx_pointer];
995
996 /* Do we have a packet ? */
997 return !!(rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RXPKT_RDY));
998}
999
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001000static int ftgmac100_poll(struct napi_struct *napi, int budget)
1001{
1002 struct ftgmac100 *priv = container_of(napi, struct ftgmac100, napi);
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001003 bool more, completed = true;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001004 int rx = 0;
1005
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001006 ftgmac100_tx_complete(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001007
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001008 do {
1009 more = ftgmac100_rx_packet(priv, &rx);
1010 } while (more && rx < budget);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001011
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001012 if (more && rx == budget)
1013 completed = false;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001014
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001015
1016 /* The interrupt is telling us to kick the MAC back to life
1017 * after an RX overflow
1018 */
1019 if (unlikely(priv->need_mac_restart)) {
1020 ftgmac100_start_hw(priv);
1021
1022 /* Re-enable "bad" interrupts */
1023 iowrite32(FTGMAC100_INT_BAD,
1024 priv->base + FTGMAC100_OFFSET_IER);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001025 }
1026
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001027 /* Keep NAPI going if we have still packets to reclaim */
1028 if (priv->tx_pending)
1029 return budget;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001030
1031 if (completed) {
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001032 /* We are about to re-enable all interrupts. However
1033 * the HW has been latching RX/TX packet interrupts while
1034 * they were masked. So we clear them first, then we need
1035 * to re-check if there's something to process
1036 */
1037 iowrite32(FTGMAC100_INT_RXTX,
1038 priv->base + FTGMAC100_OFFSET_ISR);
Benjamin Herrenschmidt4ca24152017-04-06 11:02:51 +10001039 if (ftgmac100_check_rx(priv) || priv->tx_pending)
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001040 return budget;
1041
1042 /* deschedule NAPI */
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001043 napi_complete(napi);
1044
1045 /* enable all interrupts */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001046 iowrite32(FTGMAC100_INT_ALL,
Gavin Shanfc6061c2016-07-19 11:54:25 +10001047 priv->base + FTGMAC100_OFFSET_IER);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001048 }
1049
1050 return rx;
1051}
1052
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001053static int ftgmac100_init_all(struct ftgmac100 *priv, bool ignore_alloc_err)
1054{
1055 int err = 0;
1056
1057 /* Re-init descriptors (adjust queue sizes) */
1058 ftgmac100_init_rings(priv);
1059
1060 /* Realloc rx descriptors */
1061 err = ftgmac100_alloc_rx_buffers(priv);
1062 if (err && !ignore_alloc_err)
1063 return err;
1064
1065 /* Reinit and restart HW */
1066 ftgmac100_init_hw(priv);
1067 ftgmac100_start_hw(priv);
1068
1069 /* Re-enable the device */
1070 napi_enable(&priv->napi);
1071 netif_start_queue(priv->netdev);
1072
1073 /* Enable all interrupts */
Benjamin Herrenschmidt10cbd642017-04-05 12:28:53 +10001074 iowrite32(FTGMAC100_INT_ALL, priv->base + FTGMAC100_OFFSET_IER);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001075
1076 return err;
1077}
1078
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001079static void ftgmac100_reset_task(struct work_struct *work)
1080{
1081 struct ftgmac100 *priv = container_of(work, struct ftgmac100,
1082 reset_task);
1083 struct net_device *netdev = priv->netdev;
1084 int err;
1085
1086 netdev_dbg(netdev, "Resetting NIC...\n");
1087
1088 /* Lock the world */
1089 rtnl_lock();
1090 if (netdev->phydev)
1091 mutex_lock(&netdev->phydev->lock);
1092 if (priv->mii_bus)
1093 mutex_lock(&priv->mii_bus->mdio_lock);
1094
1095
1096 /* Check if the interface is still up */
1097 if (!netif_running(netdev))
1098 goto bail;
1099
1100 /* Stop the network stack */
1101 netif_trans_update(netdev);
1102 napi_disable(&priv->napi);
1103 netif_tx_disable(netdev);
1104
1105 /* Stop and reset the MAC */
1106 ftgmac100_stop_hw(priv);
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +10001107 err = ftgmac100_reset_and_config_mac(priv);
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001108 if (err) {
1109 /* Not much we can do ... it might come back... */
1110 netdev_err(netdev, "attempting to continue...\n");
1111 }
1112
1113 /* Free all rx and tx buffers */
1114 ftgmac100_free_buffers(priv);
1115
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001116 /* Setup everything again and restart chip */
1117 ftgmac100_init_all(priv, true);
1118
1119 netdev_dbg(netdev, "Reset done !\n");
1120 bail:
1121 if (priv->mii_bus)
1122 mutex_unlock(&priv->mii_bus->mdio_lock);
1123 if (netdev->phydev)
1124 mutex_unlock(&netdev->phydev->lock);
1125 rtnl_unlock();
1126}
1127
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001128static int ftgmac100_open(struct net_device *netdev)
1129{
1130 struct ftgmac100 *priv = netdev_priv(netdev);
1131 int err;
1132
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001133 /* Allocate ring buffers */
1134 err = ftgmac100_alloc_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001135 if (err) {
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001136 netdev_err(netdev, "Failed to allocate descriptors\n");
1137 return err;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001138 }
1139
Benjamin Herrenschmidt51764772017-04-05 12:28:45 +10001140 /* When using NC-SI we force the speed to 100Mbit/s full duplex,
1141 *
1142 * Otherwise we leave it set to 0 (no link), the link
1143 * message from the PHY layer will handle setting it up to
1144 * something else if needed.
1145 */
1146 if (priv->use_ncsi) {
1147 priv->cur_duplex = DUPLEX_FULL;
1148 priv->cur_speed = SPEED_100;
1149 } else {
1150 priv->cur_duplex = 0;
1151 priv->cur_speed = 0;
1152 }
1153
Benjamin Herrenschmidt874b55b2017-04-05 12:28:51 +10001154 /* Reset the hardware */
1155 err = ftgmac100_reset_and_config_mac(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001156 if (err)
1157 goto err_hw;
1158
Benjamin Herrenschmidtb8dbecf2017-04-05 12:28:47 +10001159 /* Initialize NAPI */
1160 netif_napi_add(netdev, &priv->napi, ftgmac100_poll, 64);
1161
Benjamin Herrenschmidt81f1eca2017-04-05 12:28:48 +10001162 /* Grab our interrupt */
1163 err = request_irq(netdev->irq, ftgmac100_interrupt, 0, netdev->name, netdev);
1164 if (err) {
1165 netdev_err(netdev, "failed to request irq %d\n", netdev->irq);
1166 goto err_irq;
1167 }
1168
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001169 /* Start things up */
1170 err = ftgmac100_init_all(priv, false);
1171 if (err) {
1172 netdev_err(netdev, "Failed to allocate packet buffers\n");
1173 goto err_alloc;
1174 }
Gavin Shan08c9c122016-09-22 08:35:01 +09301175
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001176 if (netdev->phydev) {
1177 /* If we have a PHY, start polling */
Gavin Shanbd466c32016-07-19 11:54:23 +10001178 phy_start(netdev->phydev);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001179 } else if (priv->use_ncsi) {
1180 /* If using NC-SI, set our carrier on and start the stack */
Gavin Shanbd466c32016-07-19 11:54:23 +10001181 netif_carrier_on(netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001182
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001183 /* Start the NCSI device */
Gavin Shanbd466c32016-07-19 11:54:23 +10001184 err = ncsi_start_dev(priv->ndev);
1185 if (err)
1186 goto err_ncsi;
1187 }
1188
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001189 return 0;
1190
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001191 err_ncsi:
Gavin Shanbd466c32016-07-19 11:54:23 +10001192 napi_disable(&priv->napi);
1193 netif_stop_queue(netdev);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001194 err_alloc:
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001195 ftgmac100_free_buffers(priv);
Benjamin Herrenschmidtda40d9d2017-04-05 12:28:49 +10001196 free_irq(netdev->irq, netdev);
1197 err_irq:
1198 netif_napi_del(&priv->napi);
1199 err_hw:
1200 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001201 ftgmac100_free_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001202 return err;
1203}
1204
1205static int ftgmac100_stop(struct net_device *netdev)
1206{
1207 struct ftgmac100 *priv = netdev_priv(netdev);
1208
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001209 /* Note about the reset task: We are called with the rtnl lock
1210 * held, so we are synchronized against the core of the reset
1211 * task. We must not try to synchronously cancel it otherwise
1212 * we can deadlock. But since it will test for netif_running()
1213 * which has already been cleared by the net core, we don't
1214 * anything special to do.
1215 */
1216
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001217 /* disable all interrupts */
1218 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1219
1220 netif_stop_queue(netdev);
1221 napi_disable(&priv->napi);
Benjamin Herrenschmidtb8dbecf2017-04-05 12:28:47 +10001222 netif_napi_del(&priv->napi);
Gavin Shanbd466c32016-07-19 11:54:23 +10001223 if (netdev->phydev)
1224 phy_stop(netdev->phydev);
Gavin Shan2c15f252016-10-04 11:25:54 +11001225 else if (priv->use_ncsi)
1226 ncsi_stop_dev(priv->ndev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001227
1228 ftgmac100_stop_hw(priv);
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001229 free_irq(netdev->irq, netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001230 ftgmac100_free_buffers(priv);
Benjamin Herrenschmidt87d18752017-04-05 12:28:46 +10001231 ftgmac100_free_rings(priv);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001232
1233 return 0;
1234}
1235
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001236/* optional */
1237static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1238{
Gavin Shanbd466c32016-07-19 11:54:23 +10001239 if (!netdev->phydev)
1240 return -ENXIO;
1241
Philippe Reynesb3c40ad2016-05-16 01:35:13 +02001242 return phy_mii_ioctl(netdev->phydev, ifr, cmd);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001243}
1244
Benjamin Herrenschmidtd3ca8fb2017-04-10 11:15:15 +10001245static void ftgmac100_tx_timeout(struct net_device *netdev)
1246{
1247 struct ftgmac100 *priv = netdev_priv(netdev);
1248
1249 /* Disable all interrupts */
1250 iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1251
1252 /* Do the reset outside of interrupt context */
1253 schedule_work(&priv->reset_task);
1254}
1255
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001256static const struct net_device_ops ftgmac100_netdev_ops = {
1257 .ndo_open = ftgmac100_open,
1258 .ndo_stop = ftgmac100_stop,
1259 .ndo_start_xmit = ftgmac100_hard_start_xmit,
Gavin Shan113ce102016-07-19 11:54:22 +10001260 .ndo_set_mac_address = ftgmac100_set_mac_addr,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001261 .ndo_validate_addr = eth_validate_addr,
1262 .ndo_do_ioctl = ftgmac100_do_ioctl,
Benjamin Herrenschmidtd3ca8fb2017-04-10 11:15:15 +10001263 .ndo_tx_timeout = ftgmac100_tx_timeout,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001264};
1265
Gavin Shaneb418182016-07-19 11:54:21 +10001266static int ftgmac100_setup_mdio(struct net_device *netdev)
1267{
1268 struct ftgmac100 *priv = netdev_priv(netdev);
1269 struct platform_device *pdev = to_platform_device(priv->dev);
1270 int i, err = 0;
Joel Stanleye07dc632016-09-22 08:35:02 +09301271 u32 reg;
Gavin Shaneb418182016-07-19 11:54:21 +10001272
1273 /* initialize mdio bus */
1274 priv->mii_bus = mdiobus_alloc();
1275 if (!priv->mii_bus)
1276 return -EIO;
1277
Joel Stanleye07dc632016-09-22 08:35:02 +09301278 if (of_machine_is_compatible("aspeed,ast2400") ||
1279 of_machine_is_compatible("aspeed,ast2500")) {
1280 /* This driver supports the old MDIO interface */
1281 reg = ioread32(priv->base + FTGMAC100_OFFSET_REVR);
1282 reg &= ~FTGMAC100_REVR_NEW_MDIO_INTERFACE;
1283 iowrite32(reg, priv->base + FTGMAC100_OFFSET_REVR);
1284 };
1285
Gavin Shaneb418182016-07-19 11:54:21 +10001286 priv->mii_bus->name = "ftgmac100_mdio";
1287 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d",
1288 pdev->name, pdev->id);
1289 priv->mii_bus->priv = priv->netdev;
1290 priv->mii_bus->read = ftgmac100_mdiobus_read;
1291 priv->mii_bus->write = ftgmac100_mdiobus_write;
1292
1293 for (i = 0; i < PHY_MAX_ADDR; i++)
1294 priv->mii_bus->irq[i] = PHY_POLL;
1295
1296 err = mdiobus_register(priv->mii_bus);
1297 if (err) {
1298 dev_err(priv->dev, "Cannot register MDIO bus!\n");
1299 goto err_register_mdiobus;
1300 }
1301
1302 err = ftgmac100_mii_probe(priv);
1303 if (err) {
1304 dev_err(priv->dev, "MII Probe failed!\n");
1305 goto err_mii_probe;
1306 }
1307
1308 return 0;
1309
1310err_mii_probe:
1311 mdiobus_unregister(priv->mii_bus);
1312err_register_mdiobus:
1313 mdiobus_free(priv->mii_bus);
1314 return err;
1315}
1316
1317static void ftgmac100_destroy_mdio(struct net_device *netdev)
1318{
1319 struct ftgmac100 *priv = netdev_priv(netdev);
1320
1321 if (!netdev->phydev)
1322 return;
1323
1324 phy_disconnect(netdev->phydev);
1325 mdiobus_unregister(priv->mii_bus);
1326 mdiobus_free(priv->mii_bus);
1327}
1328
Gavin Shanbd466c32016-07-19 11:54:23 +10001329static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
1330{
1331 if (unlikely(nd->state != ncsi_dev_state_functional))
1332 return;
1333
1334 netdev_info(nd->dev, "NCSI interface %s\n",
1335 nd->link_up ? "up" : "down");
1336}
1337
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001338static int ftgmac100_probe(struct platform_device *pdev)
1339{
1340 struct resource *res;
1341 int irq;
1342 struct net_device *netdev;
1343 struct ftgmac100 *priv;
Gavin Shanbd466c32016-07-19 11:54:23 +10001344 int err = 0;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001345
1346 if (!pdev)
1347 return -ENODEV;
1348
1349 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1350 if (!res)
1351 return -ENXIO;
1352
1353 irq = platform_get_irq(pdev, 0);
1354 if (irq < 0)
1355 return irq;
1356
1357 /* setup net_device */
1358 netdev = alloc_etherdev(sizeof(*priv));
1359 if (!netdev) {
1360 err = -ENOMEM;
1361 goto err_alloc_etherdev;
1362 }
1363
1364 SET_NETDEV_DEV(netdev, &pdev->dev);
1365
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00001366 netdev->ethtool_ops = &ftgmac100_ethtool_ops;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001367 netdev->netdev_ops = &ftgmac100_netdev_ops;
Benjamin Herrenschmidtd3ca8fb2017-04-10 11:15:15 +10001368 netdev->watchdog_timeo = 5 * HZ;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001369
1370 platform_set_drvdata(pdev, netdev);
1371
1372 /* setup private data */
1373 priv = netdev_priv(netdev);
1374 priv->netdev = netdev;
1375 priv->dev = &pdev->dev;
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001376 INIT_WORK(&priv->reset_task, ftgmac100_reset_task);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001377
1378 spin_lock_init(&priv->tx_lock);
1379
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001380 /* map io memory */
1381 priv->res = request_mem_region(res->start, resource_size(res),
1382 dev_name(&pdev->dev));
1383 if (!priv->res) {
1384 dev_err(&pdev->dev, "Could not reserve memory region\n");
1385 err = -ENOMEM;
1386 goto err_req_mem;
1387 }
1388
1389 priv->base = ioremap(res->start, resource_size(res));
1390 if (!priv->base) {
1391 dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
1392 err = -EIO;
1393 goto err_ioremap;
1394 }
1395
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001396 netdev->irq = irq;
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001397
Gavin Shan113ce102016-07-19 11:54:22 +10001398 /* MAC address from chip or random one */
1399 ftgmac100_setup_mac(priv);
1400
Joel Stanley2a0ab8eb2016-09-22 08:35:00 +09301401 if (of_machine_is_compatible("aspeed,ast2400") ||
1402 of_machine_is_compatible("aspeed,ast2500")) {
1403 priv->rxdes0_edorr_mask = BIT(30);
1404 priv->txdes0_edotr_mask = BIT(30);
1405 } else {
1406 priv->rxdes0_edorr_mask = BIT(15);
1407 priv->txdes0_edotr_mask = BIT(15);
1408 }
1409
Gavin Shanbd466c32016-07-19 11:54:23 +10001410 if (pdev->dev.of_node &&
1411 of_get_property(pdev->dev.of_node, "use-ncsi", NULL)) {
1412 if (!IS_ENABLED(CONFIG_NET_NCSI)) {
1413 dev_err(&pdev->dev, "NCSI stack not enabled\n");
1414 goto err_ncsi_dev;
1415 }
1416
1417 dev_info(&pdev->dev, "Using NCSI interface\n");
1418 priv->use_ncsi = true;
1419 priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
1420 if (!priv->ndev)
1421 goto err_ncsi_dev;
1422 } else {
1423 priv->use_ncsi = false;
1424 err = ftgmac100_setup_mdio(netdev);
1425 if (err)
1426 goto err_setup_mdio;
1427 }
1428
1429 /* We have to disable on-chip IP checksum functionality
1430 * when NCSI is enabled on the interface. It doesn't work
1431 * in that case.
1432 */
Benjamin Herrenschmidt67202192017-04-06 11:02:46 +10001433 netdev->features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_GRO;
Gavin Shanbd466c32016-07-19 11:54:23 +10001434 if (priv->use_ncsi &&
1435 of_get_property(pdev->dev.of_node, "no-hw-checksum", NULL))
1436 netdev->features &= ~NETIF_F_IP_CSUM;
1437
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001438
1439 /* register network device */
1440 err = register_netdev(netdev);
1441 if (err) {
1442 dev_err(&pdev->dev, "Failed to register netdev\n");
1443 goto err_register_netdev;
1444 }
1445
Benjamin Herrenschmidt60b28a12017-04-05 12:28:41 +10001446 netdev_info(netdev, "irq %d, mapped at %p\n", netdev->irq, priv->base);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001447
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001448 return 0;
1449
Gavin Shanbd466c32016-07-19 11:54:23 +10001450err_ncsi_dev:
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001451err_register_netdev:
Gavin Shaneb418182016-07-19 11:54:21 +10001452 ftgmac100_destroy_mdio(netdev);
1453err_setup_mdio:
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001454 iounmap(priv->base);
1455err_ioremap:
1456 release_resource(priv->res);
1457err_req_mem:
1458 netif_napi_del(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001459 free_netdev(netdev);
1460err_alloc_etherdev:
1461 return err;
1462}
1463
Dmitry Torokhovbe125022017-03-01 17:24:47 -08001464static int ftgmac100_remove(struct platform_device *pdev)
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001465{
1466 struct net_device *netdev;
1467 struct ftgmac100 *priv;
1468
1469 netdev = platform_get_drvdata(pdev);
1470 priv = netdev_priv(netdev);
1471
1472 unregister_netdev(netdev);
Benjamin Herrenschmidt855944c2017-04-05 12:28:50 +10001473
1474 /* There's a small chance the reset task will have been re-queued,
1475 * during stop, make sure it's gone before we free the structure.
1476 */
1477 cancel_work_sync(&priv->reset_task);
1478
Gavin Shaneb418182016-07-19 11:54:21 +10001479 ftgmac100_destroy_mdio(netdev);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001480
1481 iounmap(priv->base);
1482 release_resource(priv->res);
1483
1484 netif_napi_del(&priv->napi);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001485 free_netdev(netdev);
1486 return 0;
1487}
1488
Gavin Shanbb168e22016-07-19 11:54:24 +10001489static const struct of_device_id ftgmac100_of_match[] = {
1490 { .compatible = "faraday,ftgmac100" },
1491 { }
1492};
1493MODULE_DEVICE_TABLE(of, ftgmac100_of_match);
1494
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001495static struct platform_driver ftgmac100_driver = {
Gavin Shanbb168e22016-07-19 11:54:24 +10001496 .probe = ftgmac100_probe,
Dmitry Torokhovbe125022017-03-01 17:24:47 -08001497 .remove = ftgmac100_remove,
Gavin Shanbb168e22016-07-19 11:54:24 +10001498 .driver = {
1499 .name = DRV_NAME,
1500 .of_match_table = ftgmac100_of_match,
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001501 },
1502};
Sachin Kamat14f645d2013-03-18 01:50:48 +00001503module_platform_driver(ftgmac100_driver);
Po-Yu Chuang69785b72011-06-08 23:32:48 +00001504
1505MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
1506MODULE_DESCRIPTION("FTGMAC100 driver");
1507MODULE_LICENSE("GPL");