blob: 9d461825bf4ce92e55a9de79921e8ffc94735617 [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/init.h>
28#include <linux/delay.h>
29#include <linux/netdevice.h>
30#include <linux/etherdevice.h>
31#include <linux/skbuff.h>
32#include <linux/spinlock.h>
33#include <linux/mii.h>
34#include <linux/ethtool.h>
35#include <linux/bitops.h>
36#include <linux/fs.h>
Marcelo Tosattif7b99962005-11-09 11:00:16 -020037#include <linux/platform_device.h>
Vitaly Bordug5b4b8452006-08-14 23:00:30 -070038#include <linux/phy.h>
Kumar Galab2191082008-06-12 08:32:13 -050039#include <linux/of_platform.h>
Benjamin Herrenschmidt8725f252008-07-22 17:12:37 +100040#include <linux/of_gpio.h>
Pantelis Antoniou48257c42005-10-28 16:25:58 -040041
42#include <linux/vmalloc.h>
43#include <asm/pgtable.h>
Pantelis Antoniou48257c42005-10-28 16:25:58 -040044#include <asm/irq.h>
45#include <asm/uaccess.h>
46
47#include "fs_enet.h"
48
49/*************************************************/
50
Pantelis Antoniou48257c42005-10-28 16:25:58 -040051MODULE_AUTHOR("Pantelis Antoniou <panto@intracom.gr>");
52MODULE_DESCRIPTION("Freescale Ethernet Driver");
53MODULE_LICENSE("GPL");
54MODULE_VERSION(DRV_MODULE_VERSION);
55
Scott Wood31a5bb02007-10-01 14:20:58 -050056static int fs_enet_debug = -1; /* -1 == use FS_ENET_DEF_MSG_ENABLE as value */
Rusty Russell8d3b33f2006-03-25 03:07:05 -080057module_param(fs_enet_debug, int, 0);
Pantelis Antoniou48257c42005-10-28 16:25:58 -040058MODULE_PARM_DESC(fs_enet_debug,
59 "Freescale bitmapped debugging message enable value");
60
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +040061#ifdef CONFIG_NET_POLL_CONTROLLER
62static void fs_enet_netpoll(struct net_device *dev);
63#endif
Pantelis Antoniou48257c42005-10-28 16:25:58 -040064
65static void fs_set_multicast_list(struct net_device *dev)
66{
67 struct fs_enet_private *fep = netdev_priv(dev);
68
69 (*fep->ops->set_multicast_list)(dev);
70}
71
Scott Wood0d0d9c12007-10-01 14:20:52 -050072static void skb_align(struct sk_buff *skb, int align)
73{
74 int off = ((unsigned long)skb->data) & (align - 1);
75
76 if (off)
77 skb_reserve(skb, align - off);
78}
79
Pantelis Antoniou48257c42005-10-28 16:25:58 -040080/* NAPI receive function */
Stephen Hemmingerbea33482007-10-03 16:41:36 -070081static int fs_enet_rx_napi(struct napi_struct *napi, int budget)
Pantelis Antoniou48257c42005-10-28 16:25:58 -040082{
Stephen Hemmingerbea33482007-10-03 16:41:36 -070083 struct fs_enet_private *fep = container_of(napi, struct fs_enet_private, napi);
Scott Woodf860f492007-10-17 12:42:43 -050084 struct net_device *dev = fep->ndev;
Pantelis Antoniou48257c42005-10-28 16:25:58 -040085 const struct fs_platform_info *fpi = fep->fpi;
Scott Wood31a5bb02007-10-01 14:20:58 -050086 cbd_t __iomem *bdp;
Pantelis Antoniou48257c42005-10-28 16:25:58 -040087 struct sk_buff *skb, *skbn, *skbt;
88 int received = 0;
89 u16 pkt_len, sc;
90 int curidx;
Pantelis Antoniou48257c42005-10-28 16:25:58 -040091
Pantelis Antoniou48257c42005-10-28 16:25:58 -040092 /*
93 * First, grab all of the stats for the incoming packet.
94 * These get messed up if we get called due to a busy condition.
95 */
96 bdp = fep->cur_rx;
97
98 /* clear RX status bits for napi*/
99 (*fep->ops->napi_clear_rx_event)(dev);
100
101 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400102 curidx = bdp - fep->rx_bd_base;
103
104 /*
105 * Since we have allocated space to hold a complete frame,
106 * the last indicator should be set.
107 */
108 if ((sc & BD_ENET_RX_LAST) == 0)
109 printk(KERN_WARNING DRV_MODULE_NAME
110 ": %s rcv is not +last\n",
111 dev->name);
112
113 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400114 * Check for errors.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400115 */
116 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
117 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
118 fep->stats.rx_errors++;
119 /* Frame too long or too short. */
120 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
121 fep->stats.rx_length_errors++;
122 /* Frame alignment */
123 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
124 fep->stats.rx_frame_errors++;
125 /* CRC Error */
126 if (sc & BD_ENET_RX_CR)
127 fep->stats.rx_crc_errors++;
128 /* FIFO overrun */
129 if (sc & BD_ENET_RX_OV)
130 fep->stats.rx_crc_errors++;
131
132 skb = fep->rx_skbuff[curidx];
133
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300134 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400135 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
136 DMA_FROM_DEVICE);
137
138 skbn = skb;
139
140 } else {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400141 skb = fep->rx_skbuff[curidx];
142
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300143 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400144 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
145 DMA_FROM_DEVICE);
146
147 /*
148 * Process the incoming frame.
149 */
150 fep->stats.rx_packets++;
151 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
152 fep->stats.rx_bytes += pkt_len + 4;
153
154 if (pkt_len <= fpi->rx_copybreak) {
155 /* +2 to make IP header L1 cache aligned */
156 skbn = dev_alloc_skb(pkt_len + 2);
157 if (skbn != NULL) {
158 skb_reserve(skbn, 2); /* align IP header */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300159 skb_copy_from_linear_data(skb,
160 skbn->data, pkt_len);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400161 /* swap */
162 skbt = skb;
163 skb = skbn;
164 skbn = skbt;
165 }
Scott Wood0d0d9c12007-10-01 14:20:52 -0500166 } else {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400167 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
168
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 {
179 printk(KERN_WARNING DRV_MODULE_NAME
180 ": %s Memory squeeze, dropping packet.\n",
181 dev->name);
182 fep->stats.rx_dropped++;
183 skbn = skb;
184 }
185 }
186
187 fep->rx_skbuff[curidx] = skbn;
188 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
189 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
190 DMA_FROM_DEVICE));
191 CBDW_DATLEN(bdp, 0);
192 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
193
194 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400195 * Update BD pointer to next entry.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400196 */
197 if ((sc & BD_ENET_RX_WRAP) == 0)
198 bdp++;
199 else
200 bdp = fep->rx_bd_base;
201
202 (*fep->ops->rx_bd_done)(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700203
204 if (received >= budget)
205 break;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400206 }
207
208 fep->cur_rx = bdp;
209
Scott Woodf860f492007-10-17 12:42:43 -0500210 if (received < budget) {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700211 /* done */
212 netif_rx_complete(dev, napi);
213 (*fep->ops->napi_enable_rx)(dev);
214 }
215 return received;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400216}
217
218/* non NAPI receive function */
219static int fs_enet_rx_non_napi(struct net_device *dev)
220{
221 struct fs_enet_private *fep = netdev_priv(dev);
222 const struct fs_platform_info *fpi = fep->fpi;
Scott Wood31a5bb02007-10-01 14:20:58 -0500223 cbd_t __iomem *bdp;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400224 struct sk_buff *skb, *skbn, *skbt;
225 int received = 0;
226 u16 pkt_len, sc;
227 int curidx;
228 /*
229 * First, grab all of the stats for the incoming packet.
230 * These get messed up if we get called due to a busy condition.
231 */
232 bdp = fep->cur_rx;
233
234 while (((sc = CBDR_SC(bdp)) & BD_ENET_RX_EMPTY) == 0) {
235
236 curidx = bdp - fep->rx_bd_base;
237
238 /*
239 * Since we have allocated space to hold a complete frame,
240 * the last indicator should be set.
241 */
242 if ((sc & BD_ENET_RX_LAST) == 0)
243 printk(KERN_WARNING DRV_MODULE_NAME
244 ": %s rcv is not +last\n",
245 dev->name);
246
247 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400248 * Check for errors.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400249 */
250 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_CL |
251 BD_ENET_RX_NO | BD_ENET_RX_CR | BD_ENET_RX_OV)) {
252 fep->stats.rx_errors++;
253 /* Frame too long or too short. */
254 if (sc & (BD_ENET_RX_LG | BD_ENET_RX_SH))
255 fep->stats.rx_length_errors++;
256 /* Frame alignment */
257 if (sc & (BD_ENET_RX_NO | BD_ENET_RX_CL))
258 fep->stats.rx_frame_errors++;
259 /* CRC Error */
260 if (sc & BD_ENET_RX_CR)
261 fep->stats.rx_crc_errors++;
262 /* FIFO overrun */
263 if (sc & BD_ENET_RX_OV)
264 fep->stats.rx_crc_errors++;
265
266 skb = fep->rx_skbuff[curidx];
267
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300268 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400269 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
270 DMA_FROM_DEVICE);
271
272 skbn = skb;
273
274 } else {
275
276 skb = fep->rx_skbuff[curidx];
277
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300278 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400279 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
280 DMA_FROM_DEVICE);
281
282 /*
283 * Process the incoming frame.
284 */
285 fep->stats.rx_packets++;
286 pkt_len = CBDR_DATLEN(bdp) - 4; /* remove CRC */
287 fep->stats.rx_bytes += pkt_len + 4;
288
289 if (pkt_len <= fpi->rx_copybreak) {
290 /* +2 to make IP header L1 cache aligned */
291 skbn = dev_alloc_skb(pkt_len + 2);
292 if (skbn != NULL) {
293 skb_reserve(skbn, 2); /* align IP header */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300294 skb_copy_from_linear_data(skb,
295 skbn->data, pkt_len);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400296 /* swap */
297 skbt = skb;
298 skb = skbn;
299 skbn = skbt;
300 }
Scott Wood0d0d9c12007-10-01 14:20:52 -0500301 } else {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400302 skbn = dev_alloc_skb(ENET_RX_FRSIZE);
303
Scott Wood0d0d9c12007-10-01 14:20:52 -0500304 if (skbn)
305 skb_align(skbn, ENET_RX_ALIGN);
306 }
307
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400308 if (skbn != NULL) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400309 skb_put(skb, pkt_len); /* Make room */
310 skb->protocol = eth_type_trans(skb, dev);
311 received++;
312 netif_rx(skb);
313 } else {
314 printk(KERN_WARNING DRV_MODULE_NAME
315 ": %s Memory squeeze, dropping packet.\n",
316 dev->name);
317 fep->stats.rx_dropped++;
318 skbn = skb;
319 }
320 }
321
322 fep->rx_skbuff[curidx] = skbn;
323 CBDW_BUFADDR(bdp, dma_map_single(fep->dev, skbn->data,
324 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
325 DMA_FROM_DEVICE));
326 CBDW_DATLEN(bdp, 0);
327 CBDW_SC(bdp, (sc & ~BD_ENET_RX_STATS) | BD_ENET_RX_EMPTY);
328
329 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400330 * Update BD pointer to next entry.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400331 */
332 if ((sc & BD_ENET_RX_WRAP) == 0)
333 bdp++;
334 else
335 bdp = fep->rx_bd_base;
336
337 (*fep->ops->rx_bd_done)(dev);
338 }
339
340 fep->cur_rx = bdp;
341
342 return 0;
343}
344
345static void fs_enet_tx(struct net_device *dev)
346{
347 struct fs_enet_private *fep = netdev_priv(dev);
Scott Wood31a5bb02007-10-01 14:20:58 -0500348 cbd_t __iomem *bdp;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400349 struct sk_buff *skb;
350 int dirtyidx, do_wake, do_restart;
351 u16 sc;
352
Vitaly Bordugaa90f502007-09-18 20:05:27 +0400353 spin_lock(&fep->tx_lock);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400354 bdp = fep->dirty_tx;
355
356 do_wake = do_restart = 0;
357 while (((sc = CBDR_SC(bdp)) & BD_ENET_TX_READY) == 0) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400358 dirtyidx = bdp - fep->tx_bd_base;
359
360 if (fep->tx_free == fep->tx_ring)
361 break;
362
363 skb = fep->tx_skbuff[dirtyidx];
364
365 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400366 * Check for errors.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400367 */
368 if (sc & (BD_ENET_TX_HB | BD_ENET_TX_LC |
369 BD_ENET_TX_RL | BD_ENET_TX_UN | BD_ENET_TX_CSL)) {
370
371 if (sc & BD_ENET_TX_HB) /* No heartbeat */
372 fep->stats.tx_heartbeat_errors++;
373 if (sc & BD_ENET_TX_LC) /* Late collision */
374 fep->stats.tx_window_errors++;
375 if (sc & BD_ENET_TX_RL) /* Retrans limit */
376 fep->stats.tx_aborted_errors++;
377 if (sc & BD_ENET_TX_UN) /* Underrun */
378 fep->stats.tx_fifo_errors++;
379 if (sc & BD_ENET_TX_CSL) /* Carrier lost */
380 fep->stats.tx_carrier_errors++;
381
382 if (sc & (BD_ENET_TX_LC | BD_ENET_TX_RL | BD_ENET_TX_UN)) {
383 fep->stats.tx_errors++;
384 do_restart = 1;
385 }
386 } else
387 fep->stats.tx_packets++;
388
389 if (sc & BD_ENET_TX_READY)
390 printk(KERN_WARNING DRV_MODULE_NAME
391 ": %s HEY! Enet xmit interrupt and TX_READY.\n",
392 dev->name);
393
394 /*
395 * Deferred means some collisions occurred during transmit,
396 * but we eventually sent the packet OK.
397 */
398 if (sc & BD_ENET_TX_DEF)
399 fep->stats.collisions++;
400
401 /* unmap */
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300402 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
403 skb->len, DMA_TO_DEVICE);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400404
405 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400406 * Free the sk buffer associated with this last transmit.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400407 */
408 dev_kfree_skb_irq(skb);
409 fep->tx_skbuff[dirtyidx] = NULL;
410
411 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400412 * Update pointer to next buffer descriptor to be transmitted.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400413 */
414 if ((sc & BD_ENET_TX_WRAP) == 0)
415 bdp++;
416 else
417 bdp = fep->tx_bd_base;
418
419 /*
420 * Since we have freed up a buffer, the ring is no longer
421 * full.
422 */
423 if (!fep->tx_free++)
424 do_wake = 1;
425 }
426
427 fep->dirty_tx = bdp;
428
429 if (do_restart)
430 (*fep->ops->tx_restart)(dev);
431
Vitaly Bordugaa90f502007-09-18 20:05:27 +0400432 spin_unlock(&fep->tx_lock);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400433
434 if (do_wake)
435 netif_wake_queue(dev);
436}
437
438/*
439 * The interrupt handler.
440 * This is called from the MPC core interrupt.
441 */
442static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100443fs_enet_interrupt(int irq, void *dev_id)
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400444{
445 struct net_device *dev = dev_id;
446 struct fs_enet_private *fep;
447 const struct fs_platform_info *fpi;
448 u32 int_events;
449 u32 int_clr_events;
450 int nr, napi_ok;
451 int handled;
452
453 fep = netdev_priv(dev);
454 fpi = fep->fpi;
455
456 nr = 0;
457 while ((int_events = (*fep->ops->get_int_events)(dev)) != 0) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400458 nr++;
459
460 int_clr_events = int_events;
461 if (fpi->use_napi)
462 int_clr_events &= ~fep->ev_napi_rx;
463
464 (*fep->ops->clear_int_events)(dev, int_clr_events);
465
466 if (int_events & fep->ev_err)
467 (*fep->ops->ev_error)(dev, int_events);
468
469 if (int_events & fep->ev_rx) {
470 if (!fpi->use_napi)
471 fs_enet_rx_non_napi(dev);
472 else {
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700473 napi_ok = napi_schedule_prep(&fep->napi);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400474
475 (*fep->ops->napi_disable_rx)(dev);
476 (*fep->ops->clear_int_events)(dev, fep->ev_napi_rx);
477
478 /* NOTE: it is possible for FCCs in NAPI mode */
479 /* to submit a spurious interrupt while in poll */
480 if (napi_ok)
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700481 __netif_rx_schedule(dev, &fep->napi);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400482 }
483 }
484
485 if (int_events & fep->ev_tx)
486 fs_enet_tx(dev);
487 }
488
489 handled = nr > 0;
490 return IRQ_RETVAL(handled);
491}
492
493void fs_init_bds(struct net_device *dev)
494{
495 struct fs_enet_private *fep = netdev_priv(dev);
Scott Wood31a5bb02007-10-01 14:20:58 -0500496 cbd_t __iomem *bdp;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400497 struct sk_buff *skb;
498 int i;
499
500 fs_cleanup_bds(dev);
501
502 fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;
503 fep->tx_free = fep->tx_ring;
504 fep->cur_rx = fep->rx_bd_base;
505
506 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400507 * Initialize the receive buffer descriptors.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400508 */
509 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
510 skb = dev_alloc_skb(ENET_RX_FRSIZE);
511 if (skb == NULL) {
512 printk(KERN_WARNING DRV_MODULE_NAME
513 ": %s Memory squeeze, unable to allocate skb\n",
514 dev->name);
515 break;
516 }
Scott Wood0d0d9c12007-10-01 14:20:52 -0500517 skb_align(skb, ENET_RX_ALIGN);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400518 fep->rx_skbuff[i] = skb;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400519 CBDW_BUFADDR(bdp,
520 dma_map_single(fep->dev, skb->data,
521 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
522 DMA_FROM_DEVICE));
523 CBDW_DATLEN(bdp, 0); /* zero */
524 CBDW_SC(bdp, BD_ENET_RX_EMPTY |
525 ((i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP));
526 }
527 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400528 * if we failed, fillup remainder
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400529 */
530 for (; i < fep->rx_ring; i++, bdp++) {
531 fep->rx_skbuff[i] = NULL;
532 CBDW_SC(bdp, (i < fep->rx_ring - 1) ? 0 : BD_SC_WRAP);
533 }
534
535 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400536 * ...and the same for transmit.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400537 */
538 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
539 fep->tx_skbuff[i] = NULL;
540 CBDW_BUFADDR(bdp, 0);
541 CBDW_DATLEN(bdp, 0);
542 CBDW_SC(bdp, (i < fep->tx_ring - 1) ? 0 : BD_SC_WRAP);
543 }
544}
545
546void fs_cleanup_bds(struct net_device *dev)
547{
548 struct fs_enet_private *fep = netdev_priv(dev);
549 struct sk_buff *skb;
Scott Wood31a5bb02007-10-01 14:20:58 -0500550 cbd_t __iomem *bdp;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400551 int i;
552
553 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400554 * Reset SKB transmit buffers.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400555 */
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300556 for (i = 0, bdp = fep->tx_bd_base; i < fep->tx_ring; i++, bdp++) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400557 if ((skb = fep->tx_skbuff[i]) == NULL)
558 continue;
559
560 /* unmap */
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300561 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
562 skb->len, DMA_TO_DEVICE);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400563
564 fep->tx_skbuff[i] = NULL;
565 dev_kfree_skb(skb);
566 }
567
568 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400569 * Reset SKB receive buffers
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400570 */
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300571 for (i = 0, bdp = fep->rx_bd_base; i < fep->rx_ring; i++, bdp++) {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400572 if ((skb = fep->rx_skbuff[i]) == NULL)
573 continue;
574
575 /* unmap */
Pantelis Antoniou34e30d62005-10-30 01:22:40 +0300576 dma_unmap_single(fep->dev, CBDR_BUFADDR(bdp),
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400577 L1_CACHE_ALIGN(PKT_MAXBUF_SIZE),
578 DMA_FROM_DEVICE);
579
580 fep->rx_skbuff[i] = NULL;
581
582 dev_kfree_skb(skb);
583 }
584}
585
586/**********************************************************************************/
587
588static int fs_enet_start_xmit(struct sk_buff *skb, struct net_device *dev)
589{
590 struct fs_enet_private *fep = netdev_priv(dev);
Scott Wood31a5bb02007-10-01 14:20:58 -0500591 cbd_t __iomem *bdp;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400592 int curidx;
593 u16 sc;
594 unsigned long flags;
595
596 spin_lock_irqsave(&fep->tx_lock, flags);
597
598 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400599 * Fill in a Tx ring entry
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400600 */
601 bdp = fep->cur_tx;
602
603 if (!fep->tx_free || (CBDR_SC(bdp) & BD_ENET_TX_READY)) {
604 netif_stop_queue(dev);
605 spin_unlock_irqrestore(&fep->tx_lock, flags);
606
607 /*
608 * Ooops. All transmit buffers are full. Bail out.
609 * This should not happen, since the tx queue should be stopped.
610 */
611 printk(KERN_WARNING DRV_MODULE_NAME
612 ": %s tx queue full!.\n", dev->name);
613 return NETDEV_TX_BUSY;
614 }
615
616 curidx = bdp - fep->tx_bd_base;
617 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400618 * Clear all of the status flags.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400619 */
620 CBDC_SC(bdp, BD_ENET_TX_STATS);
621
622 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400623 * Save skb pointer.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400624 */
625 fep->tx_skbuff[curidx] = skb;
626
627 fep->stats.tx_bytes += skb->len;
628
629 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400630 * Push the data cache so the CPM does not get stale memory data.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400631 */
632 CBDW_BUFADDR(bdp, dma_map_single(fep->dev,
633 skb->data, skb->len, DMA_TO_DEVICE));
634 CBDW_DATLEN(bdp, skb->len);
635
636 dev->trans_start = jiffies;
637
638 /*
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +0400639 * If this was the last BD in the ring, start at the beginning again.
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400640 */
641 if ((CBDR_SC(bdp) & BD_ENET_TX_WRAP) == 0)
642 fep->cur_tx++;
643 else
644 fep->cur_tx = fep->tx_bd_base;
645
646 if (!--fep->tx_free)
647 netif_stop_queue(dev);
648
649 /* Trigger transmission start */
650 sc = BD_ENET_TX_READY | BD_ENET_TX_INTR |
651 BD_ENET_TX_LAST | BD_ENET_TX_TC;
652
653 /* note that while FEC does not have this bit
654 * it marks it as available for software use
655 * yay for hw reuse :) */
656 if (skb->len <= 60)
657 sc |= BD_ENET_TX_PAD;
658 CBDS_SC(bdp, sc);
659
660 (*fep->ops->tx_kickstart)(dev);
661
662 spin_unlock_irqrestore(&fep->tx_lock, flags);
663
664 return NETDEV_TX_OK;
665}
666
667static int fs_request_irq(struct net_device *dev, int irq, const char *name,
David Howells7d12e782006-10-05 14:55:46 +0100668 irq_handler_t irqf)
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400669{
670 struct fs_enet_private *fep = netdev_priv(dev);
671
672 (*fep->ops->pre_request_irq)(dev, irq);
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700673 return request_irq(irq, irqf, IRQF_SHARED, name, dev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400674}
675
676static void fs_free_irq(struct net_device *dev, int irq)
677{
678 struct fs_enet_private *fep = netdev_priv(dev);
679
680 free_irq(irq, dev);
681 (*fep->ops->post_free_irq)(dev, irq);
682}
683
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400684static void fs_timeout(struct net_device *dev)
685{
686 struct fs_enet_private *fep = netdev_priv(dev);
687 unsigned long flags;
688 int wake = 0;
689
690 fep->stats.tx_errors++;
691
692 spin_lock_irqsave(&fep->lock, flags);
693
694 if (dev->flags & IFF_UP) {
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700695 phy_stop(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400696 (*fep->ops->stop)(dev);
697 (*fep->ops->restart)(dev);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700698 phy_start(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400699 }
700
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700701 phy_start(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400702 wake = fep->tx_free && !(CBDR_SC(fep->cur_tx) & BD_ENET_TX_READY);
703 spin_unlock_irqrestore(&fep->lock, flags);
704
705 if (wake)
706 netif_wake_queue(dev);
707}
708
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700709/*-----------------------------------------------------------------------------
710 * generic link-change handler - should be sufficient for most cases
711 *-----------------------------------------------------------------------------*/
712static void generic_adjust_link(struct net_device *dev)
713{
Scott Wood0fb300f2007-10-01 14:20:17 -0500714 struct fs_enet_private *fep = netdev_priv(dev);
715 struct phy_device *phydev = fep->phydev;
716 int new_state = 0;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700717
Scott Wood0fb300f2007-10-01 14:20:17 -0500718 if (phydev->link) {
719 /* adjust to duplex mode */
720 if (phydev->duplex != fep->oldduplex) {
721 new_state = 1;
722 fep->oldduplex = phydev->duplex;
723 }
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700724
Scott Wood0fb300f2007-10-01 14:20:17 -0500725 if (phydev->speed != fep->oldspeed) {
726 new_state = 1;
727 fep->oldspeed = phydev->speed;
728 }
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700729
Scott Wood0fb300f2007-10-01 14:20:17 -0500730 if (!fep->oldlink) {
731 new_state = 1;
732 fep->oldlink = 1;
Scott Wood0fb300f2007-10-01 14:20:17 -0500733 }
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700734
Scott Wood0fb300f2007-10-01 14:20:17 -0500735 if (new_state)
736 fep->ops->restart(dev);
737 } else if (fep->oldlink) {
738 new_state = 1;
739 fep->oldlink = 0;
740 fep->oldspeed = 0;
741 fep->oldduplex = -1;
Scott Wood0fb300f2007-10-01 14:20:17 -0500742 }
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700743
Scott Wood0fb300f2007-10-01 14:20:17 -0500744 if (new_state && netif_msg_link(fep))
745 phy_print_status(phydev);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700746}
747
748
749static void fs_adjust_link(struct net_device *dev)
750{
751 struct fs_enet_private *fep = netdev_priv(dev);
752 unsigned long flags;
753
754 spin_lock_irqsave(&fep->lock, flags);
755
756 if(fep->ops->adjust_link)
757 fep->ops->adjust_link(dev);
758 else
759 generic_adjust_link(dev);
760
761 spin_unlock_irqrestore(&fep->lock, flags);
762}
763
764static int fs_init_phy(struct net_device *dev)
765{
766 struct fs_enet_private *fep = netdev_priv(dev);
767 struct phy_device *phydev;
768
769 fep->oldlink = 0;
770 fep->oldspeed = 0;
771 fep->oldduplex = -1;
772 if(fep->fpi->bus_id)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -0600773 phydev = phy_connect(dev, fep->fpi->bus_id, &fs_adjust_link, 0,
774 PHY_INTERFACE_MODE_MII);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700775 else {
776 printk("No phy bus ID specified in BSP code\n");
777 return -EINVAL;
778 }
779 if (IS_ERR(phydev)) {
780 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
781 return PTR_ERR(phydev);
782 }
783
784 fep->phydev = phydev;
785
786 return 0;
787}
788
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400789static int fs_enet_open(struct net_device *dev)
790{
791 struct fs_enet_private *fep = netdev_priv(dev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400792 int r;
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700793 int err;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400794
Heiko Schocherf4f62302008-08-25 20:20:53 -0500795 /* to initialize the fep->cur_rx,... */
796 /* not doing this, will cause a crash in fs_enet_rx_napi */
797 fs_init_bds(fep->ndev);
798
Scott Woodf860f492007-10-17 12:42:43 -0500799 if (fep->fpi->use_napi)
800 napi_enable(&fep->napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700801
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400802 /* Install our interrupt handler. */
803 r = fs_request_irq(dev, fep->interrupt, "fs_enet-mac", fs_enet_interrupt);
804 if (r != 0) {
805 printk(KERN_ERR DRV_MODULE_NAME
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700806 ": %s Could not allocate FS_ENET IRQ!", dev->name);
Scott Woodf860f492007-10-17 12:42:43 -0500807 if (fep->fpi->use_napi)
808 napi_disable(&fep->napi);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400809 return -EINVAL;
810 }
811
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700812 err = fs_init_phy(dev);
Scott Woodf860f492007-10-17 12:42:43 -0500813 if (err) {
814 if (fep->fpi->use_napi)
815 napi_disable(&fep->napi);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700816 return err;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700817 }
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700818 phy_start(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400819
Anton Vorontsovc8f15682008-07-22 15:41:24 -0700820 netif_start_queue(dev);
821
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400822 return 0;
823}
824
825static int fs_enet_close(struct net_device *dev)
826{
827 struct fs_enet_private *fep = netdev_priv(dev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400828 unsigned long flags;
829
830 netif_stop_queue(dev);
831 netif_carrier_off(dev);
Laurent Pinchart9a3c2432008-04-11 14:05:50 +0200832 if (fep->fpi->use_napi)
833 napi_disable(&fep->napi);
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700834 phy_stop(fep->phydev);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400835
836 spin_lock_irqsave(&fep->lock, flags);
Vitaly Bordugaa90f502007-09-18 20:05:27 +0400837 spin_lock(&fep->tx_lock);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400838 (*fep->ops->stop)(dev);
Vitaly Bordugaa90f502007-09-18 20:05:27 +0400839 spin_unlock(&fep->tx_lock);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400840 spin_unlock_irqrestore(&fep->lock, flags);
841
842 /* release any irqs */
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700843 phy_disconnect(fep->phydev);
844 fep->phydev = NULL;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400845 fs_free_irq(dev, fep->interrupt);
846
847 return 0;
848}
849
850static struct net_device_stats *fs_enet_get_stats(struct net_device *dev)
851{
852 struct fs_enet_private *fep = netdev_priv(dev);
853 return &fep->stats;
854}
855
856/*************************************************************************/
857
858static void fs_get_drvinfo(struct net_device *dev,
859 struct ethtool_drvinfo *info)
860{
861 strcpy(info->driver, DRV_MODULE_NAME);
862 strcpy(info->version, DRV_MODULE_VERSION);
863}
864
865static int fs_get_regs_len(struct net_device *dev)
866{
867 struct fs_enet_private *fep = netdev_priv(dev);
868
869 return (*fep->ops->get_regs_len)(dev);
870}
871
872static void fs_get_regs(struct net_device *dev, struct ethtool_regs *regs,
873 void *p)
874{
875 struct fs_enet_private *fep = netdev_priv(dev);
876 unsigned long flags;
877 int r, len;
878
879 len = regs->len;
880
881 spin_lock_irqsave(&fep->lock, flags);
882 r = (*fep->ops->get_regs)(dev, p, &len);
883 spin_unlock_irqrestore(&fep->lock, flags);
884
885 if (r == 0)
886 regs->version = 0;
887}
888
889static int fs_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
890{
891 struct fs_enet_private *fep = netdev_priv(dev);
Anton Vorontsovdfd9a422008-01-08 22:05:55 +0300892
893 if (!fep->phydev)
894 return -ENODEV;
895
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700896 return phy_ethtool_gset(fep->phydev, cmd);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400897}
898
899static int fs_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
900{
901 struct fs_enet_private *fep = netdev_priv(dev);
Anton Vorontsovdfd9a422008-01-08 22:05:55 +0300902
903 if (!fep->phydev)
904 return -ENODEV;
905
906 return phy_ethtool_sset(fep->phydev, cmd);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400907}
908
909static int fs_nway_reset(struct net_device *dev)
910{
Vitaly Bordug5b4b8452006-08-14 23:00:30 -0700911 return 0;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400912}
913
914static u32 fs_get_msglevel(struct net_device *dev)
915{
916 struct fs_enet_private *fep = netdev_priv(dev);
917 return fep->msg_enable;
918}
919
920static void fs_set_msglevel(struct net_device *dev, u32 value)
921{
922 struct fs_enet_private *fep = netdev_priv(dev);
923 fep->msg_enable = value;
924}
925
Jeff Garzik7282d492006-09-13 14:30:00 -0400926static const struct ethtool_ops fs_ethtool_ops = {
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400927 .get_drvinfo = fs_get_drvinfo,
928 .get_regs_len = fs_get_regs_len,
929 .get_settings = fs_get_settings,
930 .set_settings = fs_set_settings,
931 .nway_reset = fs_nway_reset,
932 .get_link = ethtool_op_get_link,
933 .get_msglevel = fs_get_msglevel,
934 .set_msglevel = fs_set_msglevel,
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400935 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400936 .set_sg = ethtool_op_set_sg,
937 .get_regs = fs_get_regs,
938};
939
940static int fs_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
941{
942 struct fs_enet_private *fep = netdev_priv(dev);
943 struct mii_ioctl_data *mii = (struct mii_ioctl_data *)&rq->ifr_data;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400944
945 if (!netif_running(dev))
946 return -EINVAL;
947
Scott Wood5bd36702008-02-15 15:08:04 -0600948 return phy_mii_ioctl(fep->phydev, mii, cmd);
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400949}
950
951extern int fs_mii_connect(struct net_device *dev);
952extern void fs_mii_disconnect(struct net_device *dev);
953
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400954/**************************************************************************************/
955
956/* handy pointer to the immap */
Scott Wood31a5bb02007-10-01 14:20:58 -0500957void __iomem *fs_enet_immap = NULL;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400958
959static int setup_immap(void)
960{
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400961#ifdef CONFIG_CPM1
Scott Wood976de6a2007-10-02 10:55:58 -0500962 fs_enet_immap = ioremap(IMAP_ADDR, 0x4000);
963 WARN_ON(!fs_enet_immap);
964#elif defined(CONFIG_CPM2)
965 fs_enet_immap = cpm2_immr;
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400966#endif
967
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400968 return 0;
969}
970
971static void cleanup_immap(void)
972{
Scott Wood976de6a2007-10-02 10:55:58 -0500973#if defined(CONFIG_CPM1)
974 iounmap(fs_enet_immap);
975#endif
Pantelis Antoniou48257c42005-10-28 16:25:58 -0400976}
977
978/**************************************************************************************/
979
Scott Wood976de6a2007-10-02 10:55:58 -0500980static int __devinit find_phy(struct device_node *np,
981 struct fs_platform_info *fpi)
982{
983 struct device_node *phynode, *mdionode;
Laurent Pinchartd8b35fa2008-05-26 11:53:45 +0200984 int ret = 0, len, bus_id;
Jochen Friedrich70f782e2007-12-18 16:25:43 +0100985 const u32 *data;
Scott Wood976de6a2007-10-02 10:55:58 -0500986
Jochen Friedrich70f782e2007-12-18 16:25:43 +0100987 data = of_get_property(np, "fixed-link", NULL);
988 if (data) {
Andy Fleming9d9326d2008-04-09 19:38:13 -0500989 snprintf(fpi->bus_id, 16, "%x:%02x", 0, *data);
Jochen Friedrich70f782e2007-12-18 16:25:43 +0100990 return 0;
991 }
992
993 data = of_get_property(np, "phy-handle", &len);
Scott Wood976de6a2007-10-02 10:55:58 -0500994 if (!data || len != 4)
995 return -EINVAL;
996
997 phynode = of_find_node_by_phandle(*data);
998 if (!phynode)
999 return -EINVAL;
1000
Scott Wood976de6a2007-10-02 10:55:58 -05001001 data = of_get_property(phynode, "reg", &len);
Laurent Pinchartd8b35fa2008-05-26 11:53:45 +02001002 if (!data || len != 4) {
1003 ret = -EINVAL;
1004 goto out_put_phy;
1005 }
Scott Wood976de6a2007-10-02 10:55:58 -05001006
Laurent Pinchartd8b35fa2008-05-26 11:53:45 +02001007 mdionode = of_get_parent(phynode);
1008 if (!mdionode) {
1009 ret = -EINVAL;
1010 goto out_put_phy;
1011 }
1012
1013 bus_id = of_get_gpio(mdionode, 0);
1014 if (bus_id < 0) {
1015 struct resource res;
1016 ret = of_address_to_resource(mdionode, 0, &res);
1017 if (ret)
1018 goto out_put_mdio;
1019 bus_id = res.start;
1020 }
1021
1022 snprintf(fpi->bus_id, 16, "%x:%02x", bus_id, *data);
Scott Wood976de6a2007-10-02 10:55:58 -05001023
1024out_put_mdio:
1025 of_node_put(mdionode);
1026out_put_phy:
1027 of_node_put(phynode);
1028 return ret;
1029}
1030
1031#ifdef CONFIG_FS_ENET_HAS_FEC
1032#define IS_FEC(match) ((match)->data == &fs_fec_ops)
1033#else
1034#define IS_FEC(match) 0
1035#endif
1036
1037static int __devinit fs_enet_probe(struct of_device *ofdev,
1038 const struct of_device_id *match)
1039{
1040 struct net_device *ndev;
1041 struct fs_enet_private *fep;
1042 struct fs_platform_info *fpi;
1043 const u32 *data;
1044 const u8 *mac_addr;
1045 int privsize, len, ret = -ENODEV;
1046
1047 fpi = kzalloc(sizeof(*fpi), GFP_KERNEL);
1048 if (!fpi)
1049 return -ENOMEM;
1050
1051 if (!IS_FEC(match)) {
1052 data = of_get_property(ofdev->node, "fsl,cpm-command", &len);
1053 if (!data || len != 4)
1054 goto out_free_fpi;
1055
1056 fpi->cp_command = *data;
1057 }
1058
1059 fpi->rx_ring = 32;
1060 fpi->tx_ring = 32;
1061 fpi->rx_copybreak = 240;
Scott Woodf860f492007-10-17 12:42:43 -05001062 fpi->use_napi = 1;
Scott Wood976de6a2007-10-02 10:55:58 -05001063 fpi->napi_weight = 17;
1064
1065 ret = find_phy(ofdev->node, fpi);
1066 if (ret)
1067 goto out_free_fpi;
1068
1069 privsize = sizeof(*fep) +
1070 sizeof(struct sk_buff **) *
1071 (fpi->rx_ring + fpi->tx_ring);
1072
1073 ndev = alloc_etherdev(privsize);
1074 if (!ndev) {
1075 ret = -ENOMEM;
1076 goto out_free_fpi;
1077 }
1078
Scott Wood976de6a2007-10-02 10:55:58 -05001079 dev_set_drvdata(&ofdev->dev, ndev);
1080
1081 fep = netdev_priv(ndev);
1082 fep->dev = &ofdev->dev;
Scott Woodf860f492007-10-17 12:42:43 -05001083 fep->ndev = ndev;
Scott Wood976de6a2007-10-02 10:55:58 -05001084 fep->fpi = fpi;
1085 fep->ops = match->data;
1086
1087 ret = fep->ops->setup_data(ndev);
1088 if (ret)
1089 goto out_free_dev;
1090
1091 fep->rx_skbuff = (struct sk_buff **)&fep[1];
1092 fep->tx_skbuff = fep->rx_skbuff + fpi->rx_ring;
1093
1094 spin_lock_init(&fep->lock);
1095 spin_lock_init(&fep->tx_lock);
1096
1097 mac_addr = of_get_mac_address(ofdev->node);
1098 if (mac_addr)
1099 memcpy(ndev->dev_addr, mac_addr, 6);
1100
1101 ret = fep->ops->allocate_bd(ndev);
1102 if (ret)
1103 goto out_cleanup_data;
1104
1105 fep->rx_bd_base = fep->ring_base;
1106 fep->tx_bd_base = fep->rx_bd_base + fpi->rx_ring;
1107
1108 fep->tx_ring = fpi->tx_ring;
1109 fep->rx_ring = fpi->rx_ring;
1110
1111 ndev->open = fs_enet_open;
1112 ndev->hard_start_xmit = fs_enet_start_xmit;
1113 ndev->tx_timeout = fs_timeout;
1114 ndev->watchdog_timeo = 2 * HZ;
1115 ndev->stop = fs_enet_close;
1116 ndev->get_stats = fs_enet_get_stats;
1117 ndev->set_multicast_list = fs_set_multicast_list;
Scott Woodf860f492007-10-17 12:42:43 -05001118
1119 if (fpi->use_napi)
1120 netif_napi_add(ndev, &fep->napi, fs_enet_rx_napi,
1121 fpi->napi_weight);
1122
Scott Wood976de6a2007-10-02 10:55:58 -05001123 ndev->ethtool_ops = &fs_ethtool_ops;
1124 ndev->do_ioctl = fs_ioctl;
1125
1126 init_timer(&fep->phy_timer_list);
1127
1128 netif_carrier_off(ndev);
1129
1130 ret = register_netdev(ndev);
1131 if (ret)
1132 goto out_free_bd;
1133
1134 printk(KERN_INFO "%s: fs_enet: %02x:%02x:%02x:%02x:%02x:%02x\n",
1135 ndev->name,
1136 ndev->dev_addr[0], ndev->dev_addr[1], ndev->dev_addr[2],
1137 ndev->dev_addr[3], ndev->dev_addr[4], ndev->dev_addr[5]);
1138
1139 return 0;
1140
1141out_free_bd:
1142 fep->ops->free_bd(ndev);
1143out_cleanup_data:
1144 fep->ops->cleanup_data(ndev);
1145out_free_dev:
1146 free_netdev(ndev);
1147 dev_set_drvdata(&ofdev->dev, NULL);
1148out_free_fpi:
1149 kfree(fpi);
1150 return ret;
1151}
1152
1153static int fs_enet_remove(struct of_device *ofdev)
1154{
1155 struct net_device *ndev = dev_get_drvdata(&ofdev->dev);
1156 struct fs_enet_private *fep = netdev_priv(ndev);
1157
1158 unregister_netdev(ndev);
1159
1160 fep->ops->free_bd(ndev);
1161 fep->ops->cleanup_data(ndev);
1162 dev_set_drvdata(fep->dev, NULL);
1163
1164 free_netdev(ndev);
1165 return 0;
1166}
1167
1168static struct of_device_id fs_enet_match[] = {
1169#ifdef CONFIG_FS_ENET_HAS_SCC
1170 {
1171 .compatible = "fsl,cpm1-scc-enet",
1172 .data = (void *)&fs_scc_ops,
1173 },
Heiko Schocherf4f62302008-08-25 20:20:53 -05001174 {
1175 .compatible = "fsl,cpm2-scc-enet",
1176 .data = (void *)&fs_scc_ops,
1177 },
Scott Wood976de6a2007-10-02 10:55:58 -05001178#endif
1179#ifdef CONFIG_FS_ENET_HAS_FCC
1180 {
1181 .compatible = "fsl,cpm2-fcc-enet",
1182 .data = (void *)&fs_fcc_ops,
1183 },
1184#endif
1185#ifdef CONFIG_FS_ENET_HAS_FEC
1186 {
1187 .compatible = "fsl,pq1-fec-enet",
1188 .data = (void *)&fs_fec_ops,
1189 },
1190#endif
1191 {}
1192};
1193
1194static struct of_platform_driver fs_enet_driver = {
1195 .name = "fs_enet",
1196 .match_table = fs_enet_match,
1197 .probe = fs_enet_probe,
1198 .remove = fs_enet_remove,
1199};
1200
1201static int __init fs_init(void)
1202{
1203 int r = setup_immap();
1204 if (r != 0)
1205 return r;
1206
1207 r = of_register_platform_driver(&fs_enet_driver);
1208 if (r != 0)
1209 goto out;
1210
1211 return 0;
1212
1213out:
1214 cleanup_immap();
1215 return r;
1216}
1217
1218static void __exit fs_cleanup(void)
1219{
1220 of_unregister_platform_driver(&fs_enet_driver);
1221 cleanup_immap();
1222}
Pantelis Antoniou48257c42005-10-28 16:25:58 -04001223
Vitaly Bordug9b8ee8e2007-09-18 20:05:35 +04001224#ifdef CONFIG_NET_POLL_CONTROLLER
1225static void fs_enet_netpoll(struct net_device *dev)
1226{
1227 disable_irq(dev->irq);
1228 fs_enet_interrupt(dev->irq, dev, NULL);
1229 enable_irq(dev->irq);
1230}
1231#endif
1232
Pantelis Antoniou48257c42005-10-28 16:25:58 -04001233/**************************************************************************************/
1234
1235module_init(fs_init);
1236module_exit(fs_cleanup);