blob: cf8e54652df95266e24a5d6d64ed67c00336f67b [file] [log] [blame]
Pantelis Antoniou48257c42005-10-28 16:25:58 -04001/*
2 * Combined Ethernet driver for Motorola MPC8xx and MPC82xx.
3 *
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +04004 * Copyright (c) 2003 Intracom S.A.
Pantelis Antoniou48257c42005-10-28 16:25:58 -04005 * by Pantelis Antoniou <panto@intracom.gr>
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +04006 *
7 * 2005 (c) MontaVista Software, Inc.
Pantelis Antoniou48257c42005-10-28 16:25:58 -04008 * Vitaly Bordug <vbordug@ru.mvista.com>
9 *
10 * Heavily based on original FEC driver by Dan Malek <dan@embeddededge.com>
11 * and modifications by Joakim Tjernlund <joakim.tjernlund@lumentis.se>
12 *
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +040013 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
Pantelis Antoniou48257c42005-10-28 16:25:58 -040015 * kind, whether express or implied.
16 */
17
Pantelis Antoniou48257c42005-10-28 16:25:58 -040018#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/types.h>
Pantelis Antoniou48257c42005-10-28 16:25:58 -040021#include <linux/string.h>
22#include <linux/ptrace.h>
23#include <linux/errno.h>
24#include <linux/ioport.h>
25#include <linux/slab.h>
26#include <linux/interrupt.h>
Pantelis Antoniou48257c42005-10-28 16:25:58 -040027#include <linux/delay.h>
28#include <linux/netdevice.h>
29#include <linux/etherdevice.h>
30#include <linux/skbuff.h>
31#include <linux/spinlock.h>
32#include <linux/mii.h>
33#include <linux/ethtool.h>
34#include <linux/bitops.h>
35#include <linux/fs.h>
Marcelo Tosattif7b99962005-11-09 11:00:16 -020036#include <linux/platform_device.h>
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070037#include <linux/phy.h>
Grant Likelyaa738322009-04-25 12:53:33 +000038#include <linux/of.h>
39#include <linux/of_mdio.h>
Kumar Galab2191082008-06-12 08:32:13 -050040#include <linux/of_platform.h>
Benjamin Herrenschmidt8725f252008-07-22 17:12:37 +100041#include <linux/of_gpio.h>
David Daney4b6ba8a2010-10-26 15:07:13 -070042#include <linux/of_net.h>
Pantelis Antoniou48257c42005-10-28 16:25:58 -040043
44#include <linux/vmalloc.h>
45#include <asm/pgtable.h>
Pantelis Antoniou48257c42005-10-28 16:25:58 -040046#include <asm/irq.h>
47#include <asm/uaccess.h>
48
49#include "fs_enet.h"
50
51/*************************************************/
52
Pantelis Antoniou48257c42005-10-28 16:25:58 -040053MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
54MODULE_DESCRIPTION("Freescale Ethernet Driver");
55MODULE_LICENSE("GPL");
56MODULE_VERSION(DRV_MODULE_VERSION);
57
Scott Wood31a5bb02007-10-01 14:20:58 -050058static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
Rusty Russell8d3b33f2006-03-25 03:07:05 -080059module_param(fs_enet_debug, int, 0);
Pantelis Antoniou48257c42005-10-28 16:25:58 -040060MODULE_PARM_DESC(fs_enet_debug,
61 "Freescale bitmapped debugging message enable value");
62
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +040063#ifdef CONFIG_NET_POLL_CONTROLLER
64static void fs_enet_netpoll(struct net_device *dev);
65#endif
Pantelis Antoniou48257c42005-10-28 16:25:58 -040066
67static void fs_set_multicast_list(struct net_device *dev)
68{
69 struct fs_enet_private *fep = netdev_priv(dev);
70
71 (*fep->ops->set_multicast_list)(dev);
72}
73
Scott Wood0d0d9c12007-10-01 14:20:52 -050074static void skb_align(struct sk_buff *skb, int align)
75{
76 int off = ((unsigned long)skb->data) & (align - 1);
77
78 if (off)
79 skb_reserve(skb, align - off);
80}
81
Pantelis Antoniou48257c42005-10-28 16:25:58 -040082/* NAPI receive function */
Stephen Hemmingerbea33482007-10-03 16:41:36 -070083static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
Pantelis Antoniou48257c42005-10-28 16:25:58 -040084{
Stephen Hemmingerbea33482007-10-03 16:41:36 -070085 struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
Scott Woodf860f492007-10-17 12:42:43 -050086 struct net_device *dev = fep->ndev;
Pantelis Antoniou48257c42005-10-28 16:25:58 -040087 const struct fs_platform_info *fpi = fep->fpi;
Scott Wood31a5bb02007-10-01 14:20:58 -050088 cbd_t __iomem *bdp;
Fabian Frederickd0cc1142015-06-10 18:33:19 +020089 struct sk_buff *skb, *skbn;
Pantelis Antoniou48257c42005-10-28 16:25:58 -040090 int received = 0;
91 u16 pkt_len, sc;
92 int curidx;
Pantelis Antoniou48257c42005-10-28 16:25:58 -040093
Eric W. Biederman9b2c0572014-03-14 18:03:23 -070094 if (budget <= 0)
95 return received;
96
Pantelis Antoniou48257c42005-10-28 16:25:58 -040097 /*
98 * First, grab all of the stats for the incoming packet.
99 * These get messed up if we get called due to a busy condition.
100 */
101 bdp = fep->cur_rx;
102
103 /* clear RX status bits for napi*/
104 (*fep->ops->napi_clear_rx_event)(dev);
105
106 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400107 curidx = bdp - fep->rx_bd_base;
108
109 /*
110 * Since we have allocated space to hold a complete frame,
111 * the last indicator should be set.
112 */
113 if ((sc & BD_ENET_RX_LAST) == 0)
Anatolij Gustschinfcb6a1c2010-02-26 12:00:47 +0000114 dev_warn(fep->dev, "rcv is not +last\n");
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400115
116 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400117 * Check for errors.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400118 */
119 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
120 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
121 fep->stats.rx_errors++;
122 /* Frame too long or too short. */
123 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
124 fep->stats.rx_length_errors++;
125 /* Frame alignment */
126 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
127 fep->stats.rx_frame_errors++;
128 /* CRC Error */
129 if (sc & BD_ENET_RX_CR)
130 fep->stats.rx_crc_errors++;
131 /* FIFO overrun */
132 if (sc & BD_ENET_RX_OV)
133 fep->stats.rx_crc_errors++;
134
135 skb = fep->rx_skbuff[curidx];
136
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300137 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400138 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
139 DMA_FROM_DEVICE);
140
141 skbn = skb;
142
143 } else {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400144 skb = fep->rx_skbuff[curidx];
145
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300146 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400147 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
148 DMA_FROM_DEVICE);
149
150 /*
151 * Process the incoming frame.
152 */
153 fep->stats.rx_packets++;
154 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
155 fep->stats.rx_bytes += pkt_len + 4;
156
157 if (pkt_len <= fpi->rx_copybreak) {
158 /* +2 to make IP header L1 cache aligned */
Pradeep A Dalvi21a4e462012-02-05 02:50:10 +0000159 skbn = netdev_alloc_skb(dev, pkt_len + 2);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400160 if (skbn != NULL) {
161 skb_reserve(skbn, 2); /* align IP header */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300162 skb_copy_from_linear_data(skb,
163 skbn->data, pkt_len);
Fabian Frederickd0cc1142015-06-10 18:33:19 +0200164 swap(skb, skbn);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400165 }
Scott Wood0d0d9c12007-10-01 14:20:52 -0500166 } else {
Pradeep A Dalvi21a4e462012-02-05 02:50:10 +0000167 skbn = netdev_alloc_skb(dev, ENET_RX_FRSIZE);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400168
Scott Wood0d0d9c12007-10-01 14:20:52 -0500169 if (skbn)
170 skb_align(skbn, ENET_RX_ALIGN);
171 }
172
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400173 if (skbn != NULL) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400174 skb_put(skb, pkt_len); /* Make room */
175 skb->protocol = eth_type_trans(skb, dev);
176 received++;
177 netif_receive_skb(skb);
178 } else {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400179 fep->stats.rx_dropped++;
180 skbn = skb;
181 }
182 }
183
184 fep->rx_skbuff[curidx] = skbn;
185 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
186 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
187 DMA_FROM_DEVICE));
188 CBDW_DATLEN(bdp, 0);
189 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
190
191 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400192 * Update BD pointer to next entry.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400193 */
194 if ((sc & BD_ENET_RX_WRAP) == 0)
195 bdp++;
196 else
197 bdp = fep->rx_bd_base;
198
199 (*fep->ops->rx_bd_done)(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700200
201 if (received >= budget)
202 break;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400203 }
204
205 fep->cur_rx = bdp;
206
Scott Woodf860f492007-10-17 12:42:43 -0500207 if (received < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700208 /* done */
Ben Hutchings288379f2009-01-19 16:43:59 -0800209 napi_complete(napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700210 (*fep->ops->napi_enable_rx)(dev);
211 }
212 return received;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400213}
214
LEROY Christophed43a3962014-10-07 15:05:02 +0200215static int fs_enet_tx_napi(struct napi_struct *napi, int budget)
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400216{
LEROY Christophed43a3962014-10-07 15:05:02 +0200217 struct fs_enet_private *fep = container_of(napi, struct fs_enet_private,
218 napi_tx);
219 struct net_device *dev = fep->ndev;
Scott Wood31a5bb02007-10-01 14:20:58 -0500220 cbd_t __iomem *bdp;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400221 struct sk_buff *skb;
222 int dirtyidx, do_wake, do_restart;
223 u16 sc;
LEROY Christophed43a3962014-10-07 15:05:02 +0200224 int has_tx_work = 0;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400225
Vitaly Bordugaa90f502007-09-18 20:05:27 +0400226 spin_lock(&fep->tx_lock);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400227 bdp = fep->dirty_tx;
228
LEROY Christophed43a3962014-10-07 15:05:02 +0200229 /* clear TX status bits for napi*/
230 (*fep->ops->napi_clear_tx_event)(dev);
231
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400232 do_wake = do_restart = 0;
233 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400234 dirtyidx = bdp - fep->tx_bd_base;
235
236 if (fep->tx_free == fep->tx_ring)
237 break;
238
239 skb = fep->tx_skbuff[dirtyidx];
240
241 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400242 * Check for errors.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400243 */
244 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
245 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
246
247 if (sc & BD_ENET_TX_HB) /* No heartbeat */
248 fep->stats.tx_heartbeat_errors++;
249 if (sc & BD_ENET_TX_LC) /* Late collision */
250 fep->stats.tx_window_errors++;
251 if (sc & BD_ENET_TX_RL) /* Retrans limit */
252 fep->stats.tx_aborted_errors++;
253 if (sc & BD_ENET_TX_UN) /* Underrun */
254 fep->stats.tx_fifo_errors++;
255 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
256 fep->stats.tx_carrier_errors++;
257
258 if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
259 fep->stats.tx_errors++;
260 do_restart = 1;
261 }
262 } else
263 fep->stats.tx_packets++;
264
Anatolij Gustschinfcb6a1c2010-02-26 12:00:47 +0000265 if (sc & BD_ENET_TX_READY) {
266 dev_warn(fep->dev,
267 "HEY! Enet xmit interrupt and TX_READY.\n");
268 }
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400269
270 /*
271 * Deferred means some collisions occurred during transmit,
272 * but we eventually sent the packet OK.
273 */
274 if (sc & BD_ENET_TX_DEF)
275 fep->stats.collisions++;
276
277 /* unmap */
LEROY Christophe4fc9b872015-02-02 18:06:54 +0100278 if (fep->mapped_as_page[dirtyidx])
279 dma_unmap_page(fep->dev, CBDR_BUFADDR(bdp),
280 CBDR_DATLEN(bdp), DMA_TO_DEVICE);
281 else
282 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
283 CBDR_DATLEN(bdp), DMA_TO_DEVICE);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400284
285 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400286 * Free the sk buffer associated with this last transmit.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400287 */
LEROY Christophe4fc9b872015-02-02 18:06:54 +0100288 if (skb) {
289 dev_kfree_skb(skb);
290 fep->tx_skbuff[dirtyidx] = NULL;
291 }
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400292
293 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400294 * Update pointer to next buffer descriptor to be transmitted.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400295 */
296 if ((sc & BD_ENET_TX_WRAP) == 0)
297 bdp++;
298 else
299 bdp = fep->tx_bd_base;
300
301 /*
302 * Since we have freed up a buffer, the ring is no longer
303 * full.
304 */
LEROY Christophe4fc9b872015-02-02 18:06:54 +0100305 if (++fep->tx_free >= MAX_SKB_FRAGS)
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400306 do_wake = 1;
LEROY Christophed43a3962014-10-07 15:05:02 +0200307 has_tx_work = 1;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400308 }
309
310 fep->dirty_tx = bdp;
311
312 if (do_restart)
313 (*fep->ops->tx_restart)(dev);
314
LEROY Christophed43a3962014-10-07 15:05:02 +0200315 if (!has_tx_work) {
316 napi_complete(napi);
317 (*fep->ops->napi_enable_tx)(dev);
318 }
319
Vitaly Bordugaa90f502007-09-18 20:05:27 +0400320 spin_unlock(&fep->tx_lock);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400321
322 if (do_wake)
323 netif_wake_queue(dev);
LEROY Christophed43a3962014-10-07 15:05:02 +0200324
325 if (has_tx_work)
326 return budget;
327 return 0;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400328}
329
330/*
331 * The interrupt handler.
332 * This is called from the MPC core interrupt.
333 */
334static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100335fs_enet_interrupt(int irq, void *dev_id)
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400336{
337 struct net_device *dev = dev_id;
338 struct fs_enet_private *fep;
339 const struct fs_platform_info *fpi;
340 u32 int_events;
341 u32 int_clr_events;
342 int nr, napi_ok;
343 int handled;
344
345 fep = netdev_priv(dev);
346 fpi = fep->fpi;
347
348 nr = 0;
349 while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400350 nr++;
351
352 int_clr_events = int_events;
LEROY Christophe583d4a62014-10-07 15:04:57 +0200353 int_clr_events &= ~fep->ev_napi_rx;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400354
355 (*fep->ops->clear_int_events)(dev, int_clr_events);
356
357 if (int_events & fep->ev_err)
358 (*fep->ops->ev_error)(dev, int_events);
359
360 if (int_events & fep->ev_rx) {
LEROY Christophe583d4a62014-10-07 15:04:57 +0200361 napi_ok = napi_schedule_prep(&fep->napi);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400362
LEROY Christophe583d4a62014-10-07 15:04:57 +0200363 (*fep->ops->napi_disable_rx)(dev);
364 (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400365
LEROY Christophe583d4a62014-10-07 15:04:57 +0200366 /* NOTE: it is possible for FCCs in NAPI mode */
367 /* to submit a spurious interrupt while in poll */
368 if (napi_ok)
369 __napi_schedule(&fep->napi);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400370 }
371
LEROY Christophed43a3962014-10-07 15:05:02 +0200372 if (int_events & fep->ev_tx) {
373 napi_ok = napi_schedule_prep(&fep->napi_tx);
374
375 (*fep->ops->napi_disable_tx)(dev);
376 (*fep->ops->clear_int_events)(dev, fep->ev_napi_tx);
377
378 /* NOTE: it is possible for FCCs in NAPI mode */
379 /* to submit a spurious interrupt while in poll */
380 if (napi_ok)
381 __napi_schedule(&fep->napi_tx);
382 }
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400383 }
384
385 handled = nr > 0;
386 return IRQ_RETVAL(handled);
387}
388
389void fs_init_bds(struct net_device *dev)
390{
391 struct fs_enet_private *fep = netdev_priv(dev);
Scott Wood31a5bb02007-10-01 14:20:58 -0500392 cbd_t __iomem *bdp;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400393 struct sk_buff *skb;
394 int i;
395
396 fs_cleanup_bds(dev);
397
398 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
399 fep->tx_free = fep->tx_ring;
400 fep->cur_rx = fep->rx_bd_base;
401
402 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400403 * Initialize the receive buffer descriptors.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400404 */
405 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
Pradeep A Dalvi21a4e462012-02-05 02:50:10 +0000406 skb = netdev_alloc_skb(dev, ENET_RX_FRSIZE);
Joe Perches720a43e2013-03-08 15:03:25 +0000407 if (skb == NULL)
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400408 break;
Joe Perches720a43e2013-03-08 15:03:25 +0000409
Scott Wood0d0d9c12007-10-01 14:20:52 -0500410 skb_align(skb, ENET_RX_ALIGN);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400411 fep->rx_skbuff[i] = skb;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400412 CBDW_BUFADDR(bdp,
413 dma_map_single(fep->dev, skb->data,
414 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
415 DMA_FROM_DEVICE));
416 CBDW_DATLEN(bdp, 0); /* zero */
417 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
418 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
419 }
420 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400421 * if we failed, fillup remainder
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400422 */
423 for (; i < fep->rx_ring; i++, bdp++) {
424 fep->rx_skbuff[i] = NULL;
425 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
426 }
427
428 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400429 * ...and the same for transmit.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400430 */
431 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
432 fep->tx_skbuff[i] = NULL;
433 CBDW_BUFADDR(bdp, 0);
434 CBDW_DATLEN(bdp, 0);
435 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
436 }
437}
438
439void fs_cleanup_bds(struct net_device *dev)
440{
441 struct fs_enet_private *fep = netdev_priv(dev);
442 struct sk_buff *skb;
Scott Wood31a5bb02007-10-01 14:20:58 -0500443 cbd_t __iomem *bdp;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400444 int i;
445
446 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400447 * Reset SKB transmit buffers.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400448 */
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300449 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400450 if ((skb = fep->tx_skbuff[i]) == NULL)
451 continue;
452
453 /* unmap */
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300454 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
455 skb->len, DMA_TO_DEVICE);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400456
457 fep->tx_skbuff[i] = NULL;
458 dev_kfree_skb(skb);
459 }
460
461 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400462 * Reset SKB receive buffers
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400463 */
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300464 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400465 if ((skb = fep->rx_skbuff[i]) == NULL)
466 continue;
467
468 /* unmap */
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300469 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400470 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
471 DMA_FROM_DEVICE);
472
473 fep->rx_skbuff[i] = NULL;
474
475 dev_kfree_skb(skb);
476 }
477}
478
479/**********************************************************************************/
480
Anatolij Gustschincb395ea2010-02-26 12:00:49 +0000481#ifdef CONFIG_FS_ENET_MPC5121_FEC
482/*
483 * MPC5121 FEC requeries 4-byte alignment for TX data buffer!
484 */
485static struct sk_buff *tx_skb_align_workaround(struct net_device *dev,
486 struct sk_buff *skb)
487{
488 struct sk_buff *new_skb;
Anatolij Gustschincb395ea2010-02-26 12:00:49 +0000489
Alexander Popov26c0a142015-06-21 01:32:46 +0300490 if (skb_linearize(skb))
491 return NULL;
492
Anatolij Gustschincb395ea2010-02-26 12:00:49 +0000493 /* Alloc new skb */
Pradeep A Dalvi21a4e462012-02-05 02:50:10 +0000494 new_skb = netdev_alloc_skb(dev, skb->len + 4);
Joe Perches720a43e2013-03-08 15:03:25 +0000495 if (!new_skb)
Anatolij Gustschincb395ea2010-02-26 12:00:49 +0000496 return NULL;
Anatolij Gustschincb395ea2010-02-26 12:00:49 +0000497
498 /* Make sure new skb is properly aligned */
499 skb_align(new_skb, 4);
500
501 /* Copy data to new skb ... */
502 skb_copy_from_linear_data(skb, new_skb->data, skb->len);
503 skb_put(new_skb, skb->len);
504
505 /* ... and free an old one */
506 dev_kfree_skb_any(skb);
507
508 return new_skb;
509}
510#endif
511
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400512static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
513{
514 struct fs_enet_private *fep = netdev_priv(dev);
Scott Wood31a5bb02007-10-01 14:20:58 -0500515 cbd_t __iomem *bdp;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400516 int curidx;
517 u16 sc;
Alexander Popov26c0a142015-06-21 01:32:46 +0300518 int nr_frags;
LEROY Christophe4fc9b872015-02-02 18:06:54 +0100519 skb_frag_t *frag;
520 int len;
Anatolij Gustschincb395ea2010-02-26 12:00:49 +0000521#ifdef CONFIG_FS_ENET_MPC5121_FEC
Alexander Popov26c0a142015-06-21 01:32:46 +0300522 int is_aligned = 1;
523 int i;
524
525 if (!IS_ALIGNED((unsigned long)skb->data, 4)) {
526 is_aligned = 0;
527 } else {
528 nr_frags = skb_shinfo(skb)->nr_frags;
529 frag = skb_shinfo(skb)->frags;
530 for (i = 0; i < nr_frags; i++, frag++) {
531 if (!IS_ALIGNED(frag->page_offset, 4)) {
532 is_aligned = 0;
533 break;
534 }
535 }
536 }
537
538 if (!is_aligned) {
Anatolij Gustschincb395ea2010-02-26 12:00:49 +0000539 skb = tx_skb_align_workaround(dev, skb);
540 if (!skb) {
541 /*
542 * We have lost packet due to memory allocation error
543 * in tx_skb_align_workaround(). Hopefully original
544 * skb is still valid, so try transmit it later.
545 */
546 return NETDEV_TX_BUSY;
547 }
548 }
549#endif
Alexander Popov26c0a142015-06-21 01:32:46 +0300550
LEROY Christophed43a3962014-10-07 15:05:02 +0200551 spin_lock(&fep->tx_lock);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400552
553 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400554 * Fill in a Tx ring entry
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400555 */
556 bdp = fep->cur_tx;
557
Alexander Popov26c0a142015-06-21 01:32:46 +0300558 nr_frags = skb_shinfo(skb)->nr_frags;
LEROY Christophe4fc9b872015-02-02 18:06:54 +0100559 if (fep->tx_free <= nr_frags || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400560 netif_stop_queue(dev);
LEROY Christophed43a3962014-10-07 15:05:02 +0200561 spin_unlock(&fep->tx_lock);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400562
563 /*
564 * Ooops. All transmit buffers are full. Bail out.
565 * This should not happen, since the tx queue should be stopped.
566 */
Anatolij Gustschinfcb6a1c2010-02-26 12:00:47 +0000567 dev_warn(fep->dev, "tx queue full!.\n");
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400568 return NETDEV_TX_BUSY;
569 }
570
571 curidx = bdp - fep->tx_bd_base;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400572
LEROY Christophe4fc9b872015-02-02 18:06:54 +0100573 len = skb->len;
574 fep->stats.tx_bytes += len;
575 if (nr_frags)
576 len -= skb->data_len;
577 fep->tx_free -= nr_frags + 1;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400578 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400579 * Push the data cache so the CPM does not get stale memory data.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400580 */
581 CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
LEROY Christophe4fc9b872015-02-02 18:06:54 +0100582 skb->data, len, DMA_TO_DEVICE));
583 CBDW_DATLEN(bdp, len);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400584
LEROY Christophe4fc9b872015-02-02 18:06:54 +0100585 fep->mapped_as_page[curidx] = 0;
586 frag = skb_shinfo(skb)->frags;
587 while (nr_frags) {
588 CBDC_SC(bdp,
LEROY Christophe89618222015-08-11 12:11:00 +0200589 BD_ENET_TX_STATS | BD_ENET_TX_INTR | BD_ENET_TX_LAST |
590 BD_ENET_TX_TC);
LEROY Christophe4fc9b872015-02-02 18:06:54 +0100591 CBDS_SC(bdp, BD_ENET_TX_READY);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400592
LEROY Christophe4fc9b872015-02-02 18:06:54 +0100593 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
594 bdp++, curidx++;
595 else
596 bdp = fep->tx_bd_base, curidx = 0;
597
598 len = skb_frag_size(frag);
599 CBDW_BUFADDR(bdp, skb_frag_dma_map(fep->dev, frag, 0, len,
600 DMA_TO_DEVICE));
601 CBDW_DATLEN(bdp, len);
602
603 fep->tx_skbuff[curidx] = NULL;
604 fep->mapped_as_page[curidx] = 1;
605
606 frag++;
607 nr_frags--;
608 }
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400609
610 /* Trigger transmission start */
611 sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
612 BD_ENET_TX_LAST | BD_ENET_TX_TC;
613
614 /* note that while FEC does not have this bit
615 * it marks it as available for software use
616 * yay for hw reuse :) */
617 if (skb->len <= 60)
618 sc |= BD_ENET_TX_PAD;
LEROY Christophe4fc9b872015-02-02 18:06:54 +0100619 CBDC_SC(bdp, BD_ENET_TX_STATS);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400620 CBDS_SC(bdp, sc);
621
LEROY Christophe4fc9b872015-02-02 18:06:54 +0100622 /* Save skb pointer. */
623 fep->tx_skbuff[curidx] = skb;
624
625 /* If this was the last BD in the ring, start at the beginning again. */
626 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
627 bdp++;
628 else
629 bdp = fep->tx_bd_base;
630 fep->cur_tx = bdp;
631
632 if (fep->tx_free < MAX_SKB_FRAGS)
633 netif_stop_queue(dev);
634
Richard Cochrane3097212011-06-19 21:51:29 +0000635 skb_tx_timestamp(skb);
636
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400637 (*fep->ops->tx_kickstart)(dev);
638
LEROY Christophed43a3962014-10-07 15:05:02 +0200639 spin_unlock(&fep->tx_lock);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400640
641 return NETDEV_TX_OK;
642}
643
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400644static void fs_timeout(struct net_device *dev)
645{
646 struct fs_enet_private *fep = netdev_priv(dev);
647 unsigned long flags;
648 int wake = 0;
649
650 fep->stats.tx_errors++;
651
652 spin_lock_irqsave(&fep->lock, flags);
653
654 if (dev->flags & IFF_UP) {
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700655 phy_stop(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400656 (*fep->ops->stop)(dev);
657 (*fep->ops->restart)(dev);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700658 phy_start(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400659 }
660
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700661 phy_start(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400662 wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
663 spin_unlock_irqrestore(&fep->lock, flags);
664
665 if (wake)
666 netif_wake_queue(dev);
667}
668
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700669/*-----------------------------------------------------------------------------
670 * generic link-change handler - should be sufficient for most cases
671 *-----------------------------------------------------------------------------*/
672static void generic_adjust_link(struct net_device *dev)
673{
Scott Wood0fb300f2007-10-01 14:20:17 -0500674 struct fs_enet_private *fep = netdev_priv(dev);
675 struct phy_device *phydev = fep->phydev;
676 int new_state = 0;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700677
Scott Wood0fb300f2007-10-01 14:20:17 -0500678 if (phydev->link) {
679 /* adjust to duplex mode */
680 if (phydev->duplex != fep->oldduplex) {
681 new_state = 1;
682 fep->oldduplex = phydev->duplex;
683 }
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700684
Scott Wood0fb300f2007-10-01 14:20:17 -0500685 if (phydev->speed != fep->oldspeed) {
686 new_state = 1;
687 fep->oldspeed = phydev->speed;
688 }
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700689
Scott Wood0fb300f2007-10-01 14:20:17 -0500690 if (!fep->oldlink) {
691 new_state = 1;
692 fep->oldlink = 1;
Scott Wood0fb300f2007-10-01 14:20:17 -0500693 }
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700694
Scott Wood0fb300f2007-10-01 14:20:17 -0500695 if (new_state)
696 fep->ops->restart(dev);
697 } else if (fep->oldlink) {
698 new_state = 1;
699 fep->oldlink = 0;
700 fep->oldspeed = 0;
701 fep->oldduplex = -1;
Scott Wood0fb300f2007-10-01 14:20:17 -0500702 }
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700703
Scott Wood0fb300f2007-10-01 14:20:17 -0500704 if (new_state && netif_msg_link(fep))
705 phy_print_status(phydev);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700706}
707
708
709static void fs_adjust_link(struct net_device *dev)
710{
711 struct fs_enet_private *fep = netdev_priv(dev);
712 unsigned long flags;
713
714 spin_lock_irqsave(&fep->lock, flags);
715
716 if(fep->ops->adjust_link)
717 fep->ops->adjust_link(dev);
718 else
719 generic_adjust_link(dev);
720
721 spin_unlock_irqrestore(&fep->lock, flags);
722}
723
724static int fs_init_phy(struct net_device *dev)
725{
726 struct fs_enet_private *fep = netdev_priv(dev);
727 struct phy_device *phydev;
Vladimir Ermakovba568332012-03-17 13:10:50 +0000728 phy_interface_t iface;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700729
730 fep->oldlink = 0;
731 fep->oldspeed = 0;
732 fep->oldduplex = -1;
Anton Vorontsoveedbc702009-07-16 21:31:36 +0000733
Vladimir Ermakovba568332012-03-17 13:10:50 +0000734 iface = fep->fpi->use_rmii ?
735 PHY_INTERFACE_MODE_RMII : PHY_INTERFACE_MODE_MII;
736
Anton Vorontsoveedbc702009-07-16 21:31:36 +0000737 phydev = of_phy_connect(dev, fep->fpi->phy_node, &fs_adjust_link, 0,
Vladimir Ermakovba568332012-03-17 13:10:50 +0000738 iface);
Anton Vorontsoveedbc702009-07-16 21:31:36 +0000739 if (!phydev) {
Anton Vorontsoveedbc702009-07-16 21:31:36 +0000740 dev_err(&dev->dev, "Could not attach to PHY\n");
741 return -ENODEV;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700742 }
743
744 fep->phydev = phydev;
745
746 return 0;
747}
748
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400749static int fs_enet_open(struct net_device *dev)
750{
751 struct fs_enet_private *fep = netdev_priv(dev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400752 int r;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700753 int err;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400754
Heiko Schocherf4f62302008-08-25 20:20:53 -0500755 /* to initialize the fep->cur_rx,... */
756 /* not doing this, will cause a crash in fs_enet_rx_napi */
757 fs_init_bds(fep->ndev);
758
LEROY Christophe583d4a62014-10-07 15:04:57 +0200759 napi_enable(&fep->napi);
LEROY Christophed43a3962014-10-07 15:05:02 +0200760 napi_enable(&fep->napi_tx);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700761
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400762 /* Install our interrupt handler. */
Kumar Gala315781402008-09-22 14:52:18 -0700763 r = request_irq(fep->interrupt, fs_enet_interrupt, IRQF_SHARED,
764 "fs_enet-mac", dev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400765 if (r != 0) {
Anatolij Gustschinfcb6a1c2010-02-26 12:00:47 +0000766 dev_err(fep->dev, "Could not allocate FS_ENET IRQ!");
LEROY Christophe583d4a62014-10-07 15:04:57 +0200767 napi_disable(&fep->napi);
LEROY Christophed43a3962014-10-07 15:05:02 +0200768 napi_disable(&fep->napi_tx);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400769 return -EINVAL;
770 }
771
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700772 err = fs_init_phy(dev);
Scott Woodf860f492007-10-17 12:42:43 -0500773 if (err) {
Mike Dittod7e094d2009-01-14 20:43:43 -0800774 free_irq(fep->interrupt, dev);
LEROY Christophe583d4a62014-10-07 15:04:57 +0200775 napi_disable(&fep->napi);
LEROY Christophed43a3962014-10-07 15:05:02 +0200776 napi_disable(&fep->napi_tx);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700777 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700778 }
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700779 phy_start(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400780
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700781 netif_start_queue(dev);
782
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400783 return 0;
784}
785
786static int fs_enet_close(struct net_device *dev)
787{
788 struct fs_enet_private *fep = netdev_priv(dev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400789 unsigned long flags;
790
791 netif_stop_queue(dev);
792 netif_carrier_off(dev);
LEROY Christophe583d4a62014-10-07 15:04:57 +0200793 napi_disable(&fep->napi);
LEROY Christophed43a3962014-10-07 15:05:02 +0200794 napi_disable(&fep->napi_tx);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700795 phy_stop(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400796
797 spin_lock_irqsave(&fep->lock, flags);
Vitaly Bordugaa90f502007-09-18 20:05:27 +0400798 spin_lock(&fep->tx_lock);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400799 (*fep->ops->stop)(dev);
Vitaly Bordugaa90f502007-09-18 20:05:27 +0400800 spin_unlock(&fep->tx_lock);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400801 spin_unlock_irqrestore(&fep->lock, flags);
802
803 /* release any irqs */
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700804 phy_disconnect(fep->phydev);
805 fep->phydev = NULL;
Kumar Gala315781402008-09-22 14:52:18 -0700806 free_irq(fep->interrupt, dev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400807
808 return 0;
809}
810
811static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
812{
813 struct fs_enet_private *fep = netdev_priv(dev);
814 return &fep->stats;
815}
816
817/*************************************************************************/
818
819static void fs_get_drvinfo(struct net_device *dev,
820 struct ethtool_drvinfo *info)
821{
Jiri Pirko7826d432013-01-06 00:44:26 +0000822 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
823 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400824}
825
826static int fs_get_regs_len(struct net_device *dev)
827{
828 struct fs_enet_private *fep = netdev_priv(dev);
829
830 return (*fep->ops->get_regs_len)(dev);
831}
832
833static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
834 void *p)
835{
836 struct fs_enet_private *fep = netdev_priv(dev);
837 unsigned long flags;
838 int r, len;
839
840 len = regs->len;
841
842 spin_lock_irqsave(&fep->lock, flags);
843 r = (*fep->ops->get_regs)(dev, p, &len);
844 spin_unlock_irqrestore(&fep->lock, flags);
845
846 if (r == 0)
847 regs->version = 0;
848}
849
850static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
851{
852 struct fs_enet_private *fep = netdev_priv(dev);
Anton Vorontsovdfd9a4212008-01-08 22:05:55 +0300853
854 if (!fep->phydev)
855 return -ENODEV;
856
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700857 return phy_ethtool_gset(fep->phydev, cmd);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400858}
859
860static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
861{
862 struct fs_enet_private *fep = netdev_priv(dev);
Anton Vorontsovdfd9a4212008-01-08 22:05:55 +0300863
864 if (!fep->phydev)
865 return -ENODEV;
866
867 return phy_ethtool_sset(fep->phydev, cmd);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400868}
869
870static int fs_nway_reset(struct net_device *dev)
871{
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700872 return 0;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400873}
874
875static u32 fs_get_msglevel(struct net_device *dev)
876{
877 struct fs_enet_private *fep = netdev_priv(dev);
878 return fep->msg_enable;
879}
880
881static void fs_set_msglevel(struct net_device *dev, u32 value)
882{
883 struct fs_enet_private *fep = netdev_priv(dev);
884 fep->msg_enable = value;
885}
886
Jeff Garzik7282d492006-09-13 14:30:00 -0400887static const struct ethtool_ops fs_ethtool_ops = {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400888 .get_drvinfo = fs_get_drvinfo,
889 .get_regs_len = fs_get_regs_len,
890 .get_settings = fs_get_settings,
891 .set_settings = fs_set_settings,
892 .nway_reset = fs_nway_reset,
893 .get_link = ethtool_op_get_link,
894 .get_msglevel = fs_get_msglevel,
895 .set_msglevel = fs_set_msglevel,
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400896 .get_regs = fs_get_regs,
Richard Cochran3bf2ca12012-04-03 22:59:29 +0000897 .get_ts_info = ethtool_op_get_ts_info,
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400898};
899
900static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
901{
902 struct fs_enet_private *fep = netdev_priv(dev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400903
904 if (!netif_running(dev))
905 return -EINVAL;
906
Richard Cochran28b04112010-07-17 08:48:55 +0000907 return phy_mii_ioctl(fep->phydev, rq, cmd);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400908}
909
910extern int fs_mii_connect(struct net_device *dev);
911extern void fs_mii_disconnect(struct net_device *dev);
912
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400913/**************************************************************************************/
914
Scott Wood976de6a2007-10-02 10:55:58 -0500915#ifdef CONFIG_FS_ENET_HAS_FEC
916#define IS_FEC(match) ((match)->data == &fs_fec_ops)
917#else
918#define IS_FEC(match) 0
919#endif
920
Alexander Beregalov8c02acd2009-04-09 04:46:53 +0000921static const struct net_device_ops fs_enet_netdev_ops = {
922 .ndo_open = fs_enet_open,
923 .ndo_stop = fs_enet_close,
924 .ndo_get_stats = fs_enet_get_stats,
925 .ndo_start_xmit = fs_enet_start_xmit,
926 .ndo_tx_timeout = fs_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000927 .ndo_set_rx_mode = fs_set_multicast_list,
Alexander Beregalov8c02acd2009-04-09 04:46:53 +0000928 .ndo_do_ioctl = fs_ioctl,
929 .ndo_validate_addr = eth_validate_addr,
930 .ndo_set_mac_address = eth_mac_addr,
931 .ndo_change_mtu = eth_change_mtu,
932#ifdef CONFIG_NET_POLL_CONTROLLER
933 .ndo_poll_controller = fs_enet_netpoll,
934#endif
935};
936
Fabian Frederick94e5a2a2015-03-17 19:37:34 +0100937static const struct of_device_id fs_enet_match[];
Bill Pemberton4f2c53e2012-12-03 09:23:08 -0500938static int fs_enet_probe(struct platform_device *ofdev)
Scott Wood976de6a2007-10-02 10:55:58 -0500939{
Grant Likelyb1608d62011-05-18 11:19:24 -0600940 const struct of_device_id *match;
Scott Wood976de6a2007-10-02 10:55:58 -0500941 struct net_device *ndev;
942 struct fs_enet_private *fep;
943 struct fs_platform_info *fpi;
944 const u32 *data;
Gerhard Sittig27713992013-08-22 21:55:13 +0200945 struct clk *clk;
946 int err;
Scott Wood976de6a2007-10-02 10:55:58 -0500947 const u8 *mac_addr;
Vladimir Ermakovba568332012-03-17 13:10:50 +0000948 const char *phy_connection_type;
Scott Wood976de6a2007-10-02 10:55:58 -0500949 int privsize, len, ret = -ENODEV;
950
Grant Likelyb1608d62011-05-18 11:19:24 -0600951 match = of_match_device(fs_enet_match, &ofdev->dev);
952 if (!match)
Grant Likely74888762011-02-22 21:05:51 -0700953 return -EINVAL;
954
Scott Wood976de6a2007-10-02 10:55:58 -0500955 fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
956 if (!fpi)
957 return -ENOMEM;
958
Grant Likelyb1608d62011-05-18 11:19:24 -0600959 if (!IS_FEC(match)) {
Grant Likely61c7a082010-04-13 16:12:29 -0700960 data = of_get_property(ofdev->dev.of_node, "fsl,cpm-command", &len);
Scott Wood976de6a2007-10-02 10:55:58 -0500961 if (!data || len != 4)
962 goto out_free_fpi;
963
964 fpi->cp_command = *data;
965 }
966
967 fpi->rx_ring = 32;
LEROY Christophe4fc9b872015-02-02 18:06:54 +0100968 fpi->tx_ring = 64;
Scott Wood976de6a2007-10-02 10:55:58 -0500969 fpi->rx_copybreak = 240;
Scott Wood976de6a2007-10-02 10:55:58 -0500970 fpi->napi_weight = 17;
Grant Likely61c7a082010-04-13 16:12:29 -0700971 fpi->phy_node = of_parse_phandle(ofdev->dev.of_node, "phy-handle", 0);
Florian Fainellibb74d9a2014-05-22 09:47:47 -0700972 if (!fpi->phy_node && of_phy_is_fixed_link(ofdev->dev.of_node)) {
973 err = of_phy_register_fixed_link(ofdev->dev.of_node);
974 if (err)
975 goto out_free_fpi;
976
977 /* In the case of a fixed PHY, the DT node associated
978 * to the PHY is the Ethernet MAC DT node.
979 */
Uwe Kleine-König129cc832014-08-07 23:06:00 +0200980 fpi->phy_node = of_node_get(ofdev->dev.of_node);
Florian Fainellibb74d9a2014-05-22 09:47:47 -0700981 }
Scott Wood976de6a2007-10-02 10:55:58 -0500982
Vladimir Ermakovba568332012-03-17 13:10:50 +0000983 if (of_device_is_compatible(ofdev->dev.of_node, "fsl,mpc5125-fec")) {
984 phy_connection_type = of_get_property(ofdev->dev.of_node,
985 "phy-connection-type", NULL);
986 if (phy_connection_type && !strcmp("rmii", phy_connection_type))
987 fpi->use_rmii = 1;
988 }
989
Gerhard Sittig27713992013-08-22 21:55:13 +0200990 /* make clock lookup non-fatal (the driver is shared among platforms),
991 * but require enable to succeed when a clock was specified/found,
992 * keep a reference to the clock upon successful acquisition
993 */
994 clk = devm_clk_get(&ofdev->dev, "per");
995 if (!IS_ERR(clk)) {
996 err = clk_prepare_enable(clk);
997 if (err) {
998 ret = err;
999 goto out_free_fpi;
1000 }
1001 fpi->clk_per = clk;
1002 }
1003
Scott Wood976de6a2007-10-02 10:55:58 -05001004 privsize = sizeof(*fep) +
1005 sizeof(struct sk_buff **) *
LEROY Christophe4fc9b872015-02-02 18:06:54 +01001006 (fpi->rx_ring + fpi->tx_ring) +
1007 sizeof(char) * fpi->tx_ring;
Scott Wood976de6a2007-10-02 10:55:58 -05001008
1009 ndev = alloc_etherdev(privsize);
1010 if (!ndev) {
1011 ret = -ENOMEM;
Julia Lawalle8f7f432010-09-04 00:12:43 +00001012 goto out_put;
Scott Wood976de6a2007-10-02 10:55:58 -05001013 }
1014
Anton Vorontsoveedbc702009-07-16 21:31:36 +00001015 SET_NETDEV_DEV(ndev, &ofdev->dev);
Jingoo Han8513fbd2013-05-23 00:52:31 +00001016 platform_set_drvdata(ofdev, ndev);
Scott Wood976de6a2007-10-02 10:55:58 -05001017
1018 fep = netdev_priv(ndev);
1019 fep->dev = &ofdev->dev;
Scott Woodf860f492007-10-17 12:42:43 -05001020 fep->ndev = ndev;
Scott Wood976de6a2007-10-02 10:55:58 -05001021 fep->fpi = fpi;
Grant Likelyb1608d62011-05-18 11:19:24 -06001022 fep->ops = match->data;
Scott Wood976de6a2007-10-02 10:55:58 -05001023
1024 ret = fep->ops->setup_data(ndev);
1025 if (ret)
1026 goto out_free_dev;
1027
1028 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1029 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
LEROY Christophe4fc9b872015-02-02 18:06:54 +01001030 fep->mapped_as_page = (char *)(fep->rx_skbuff + fpi->rx_ring +
1031 fpi->tx_ring);
Scott Wood976de6a2007-10-02 10:55:58 -05001032
1033 spin_lock_init(&fep->lock);
1034 spin_lock_init(&fep->tx_lock);
1035
Grant Likely61c7a082010-04-13 16:12:29 -07001036 mac_addr = of_get_mac_address(ofdev->dev.of_node);
Scott Wood976de6a2007-10-02 10:55:58 -05001037 if (mac_addr)
Joe Perchesd458cdf2013-10-01 19:04:40 -07001038 memcpy(ndev->dev_addr, mac_addr, ETH_ALEN);
Scott Wood976de6a2007-10-02 10:55:58 -05001039
1040 ret = fep->ops->allocate_bd(ndev);
1041 if (ret)
1042 goto out_cleanup_data;
1043
1044 fep->rx_bd_base = fep->ring_base;
1045 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1046
1047 fep->tx_ring = fpi->tx_ring;
1048 fep->rx_ring = fpi->rx_ring;
1049
Alexander Beregalov8c02acd2009-04-09 04:46:53 +00001050 ndev->netdev_ops = &fs_enet_netdev_ops;
Scott Wood976de6a2007-10-02 10:55:58 -05001051 ndev->watchdog_timeo = 2 * HZ;
LEROY Christophe583d4a62014-10-07 15:04:57 +02001052 netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi, fpi->napi_weight);
LEROY Christophed43a3962014-10-07 15:05:02 +02001053 netif_napi_add(ndev, &fep->napi_tx, fs_enet_tx_napi, 2);
Scott Woodf860f492007-10-17 12:42:43 -05001054
Scott Wood976de6a2007-10-02 10:55:58 -05001055 ndev->ethtool_ops = &fs_ethtool_ops;
Scott Wood976de6a2007-10-02 10:55:58 -05001056
1057 init_timer(&fep->phy_timer_list);
1058
1059 netif_carrier_off(ndev);
1060
LEROY Christophe4fc9b872015-02-02 18:06:54 +01001061 ndev->features |= NETIF_F_SG;
1062
Scott Wood976de6a2007-10-02 10:55:58 -05001063 ret = register_netdev(ndev);
1064 if (ret)
1065 goto out_free_bd;
1066
Anatolij Gustschinfcb6a1c2010-02-26 12:00:47 +00001067 pr_info("%s: fs_enet: %pM\n", ndev->name, ndev->dev_addr);
Scott Wood976de6a2007-10-02 10:55:58 -05001068
1069 return 0;
1070
1071out_free_bd:
1072 fep->ops->free_bd(ndev);
1073out_cleanup_data:
1074 fep->ops->cleanup_data(ndev);
1075out_free_dev:
1076 free_netdev(ndev);
Julia Lawalle8f7f432010-09-04 00:12:43 +00001077out_put:
Grant Likelyaa738322009-04-25 12:53:33 +00001078 of_node_put(fpi->phy_node);
Gerhard Sittig27713992013-08-22 21:55:13 +02001079 if (fpi->clk_per)
1080 clk_disable_unprepare(fpi->clk_per);
Scott Wood976de6a2007-10-02 10:55:58 -05001081out_free_fpi:
1082 kfree(fpi);
1083 return ret;
1084}
1085
Grant Likely2dc11582010-08-06 09:25:50 -06001086static int fs_enet_remove(struct platform_device *ofdev)
Scott Wood976de6a2007-10-02 10:55:58 -05001087{
Jingoo Han8513fbd2013-05-23 00:52:31 +00001088 struct net_device *ndev = platform_get_drvdata(ofdev);
Scott Wood976de6a2007-10-02 10:55:58 -05001089 struct fs_enet_private *fep = netdev_priv(ndev);
1090
1091 unregister_netdev(ndev);
1092
1093 fep->ops->free_bd(ndev);
1094 fep->ops->cleanup_data(ndev);
1095 dev_set_drvdata(fep->dev, NULL);
Grant Likelyaa738322009-04-25 12:53:33 +00001096 of_node_put(fep->fpi->phy_node);
Gerhard Sittig27713992013-08-22 21:55:13 +02001097 if (fep->fpi->clk_per)
1098 clk_disable_unprepare(fep->fpi->clk_per);
Scott Wood976de6a2007-10-02 10:55:58 -05001099 free_netdev(ndev);
1100 return 0;
1101}
1102
Fabian Frederick94e5a2a2015-03-17 19:37:34 +01001103static const struct of_device_id fs_enet_match[] = {
Scott Wood976de6a2007-10-02 10:55:58 -05001104#ifdef CONFIG_FS_ENET_HAS_SCC
1105 {
1106 .compatible = "fsl,cpm1-scc-enet",
1107 .data = (void *)&fs_scc_ops,
1108 },
Heiko Schocherf4f62302008-08-25 20:20:53 -05001109 {
1110 .compatible = "fsl,cpm2-scc-enet",
1111 .data = (void *)&fs_scc_ops,
1112 },
Scott Wood976de6a2007-10-02 10:55:58 -05001113#endif
1114#ifdef CONFIG_FS_ENET_HAS_FCC
1115 {
1116 .compatible = "fsl,cpm2-fcc-enet",
1117 .data = (void *)&fs_fcc_ops,
1118 },
1119#endif
1120#ifdef CONFIG_FS_ENET_HAS_FEC
Anatolij Gustschin60ab4362010-02-26 12:00:48 +00001121#ifdef CONFIG_FS_ENET_MPC5121_FEC
1122 {
1123 .compatible = "fsl,mpc5121-fec",
1124 .data = (void *)&fs_fec_ops,
1125 },
Vladimir Ermakovba568332012-03-17 13:10:50 +00001126 {
1127 .compatible = "fsl,mpc5125-fec",
1128 .data = (void *)&fs_fec_ops,
1129 },
Anatolij Gustschin60ab4362010-02-26 12:00:48 +00001130#else
Scott Wood976de6a2007-10-02 10:55:58 -05001131 {
1132 .compatible = "fsl,pq1-fec-enet",
1133 .data = (void *)&fs_fec_ops,
1134 },
1135#endif
Anatolij Gustschin60ab4362010-02-26 12:00:48 +00001136#endif
Scott Wood976de6a2007-10-02 10:55:58 -05001137 {}
1138};
Anton Vorontsove72701a2009-10-14 14:54:52 -07001139MODULE_DEVICE_TABLE(of, fs_enet_match);
Scott Wood976de6a2007-10-02 10:55:58 -05001140
Grant Likely74888762011-02-22 21:05:51 -07001141static struct platform_driver fs_enet_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001142 .driver = {
Grant Likely40182942010-04-13 16:13:02 -07001143 .name = "fs_enet",
1144 .of_match_table = fs_enet_match,
1145 },
Scott Wood976de6a2007-10-02 10:55:58 -05001146 .probe = fs_enet_probe,
1147 .remove = fs_enet_remove,
1148};
1149
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +04001150#ifdef CONFIG_NET_POLL_CONTROLLER
1151static void fs_enet_netpoll(struct net_device *dev)
1152{
1153 disable_irq(dev->irq);
Alexey Dobriyan7385d592008-11-02 17:49:59 +03001154 fs_enet_interrupt(dev->irq, dev);
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +04001155 enable_irq(dev->irq);
1156}
1157#endif
1158
Axel Lindb62f682011-11-27 16:44:17 +00001159module_platform_driver(fs_enet_driver);