blob: e9f5d030bc267923acb405d394f1ba67be1101ae [file] [log] [blame]
Domen Puncer5d031e92007-10-26 18:07:49 +02001/*
2 * Driver for the MPC5200 Fast Ethernet Controller
3 *
4 * Originally written by Dale Farnsworth <dfarnsworth@mvista.com> and
5 * now maintained by Sylvain Munaut <tnt@246tNt.com>
6 *
7 * Copyright (C) 2007 Domen Puncer, Telargo, Inc.
8 * Copyright (C) 2007 Sylvain Munaut <tnt@246tNt.com>
9 * Copyright (C) 2003-2004 MontaVista, Software, Inc.
10 *
11 * This file is licensed under the terms of the GNU General Public License
12 * version 2. This program is licensed "as is" without any warranty of any
13 * kind, whether express or implied.
14 *
15 */
16
17#include <linux/module.h>
18
19#include <linux/kernel.h>
20#include <linux/types.h>
21#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Domen Puncer5d031e92007-10-26 18:07:49 +020023#include <linux/errno.h>
24#include <linux/init.h>
25#include <linux/crc32.h>
26#include <linux/hardirq.h>
27#include <linux/delay.h>
28#include <linux/of_device.h>
Grant Likelyca816d92009-04-25 12:53:02 +000029#include <linux/of_mdio.h>
Domen Puncer5d031e92007-10-26 18:07:49 +020030#include <linux/of_platform.h>
31
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/ethtool.h>
35#include <linux/skbuff.h>
36
37#include <asm/io.h>
38#include <asm/delay.h>
39#include <asm/mpc52xx.h>
40
41#include <sysdev/bestcomm/bestcomm.h>
42#include <sysdev/bestcomm/fec.h>
43
44#include "fec_mpc52xx.h"
45
46#define DRIVER_NAME "mpc52xx-fec"
47
Grant Likely80791be2008-05-01 11:05:58 -060048/* Private driver data structure */
49struct mpc52xx_fec_priv {
Grant Likelyca816d92009-04-25 12:53:02 +000050 struct net_device *ndev;
Grant Likely80791be2008-05-01 11:05:58 -060051 int duplex;
52 int speed;
53 int r_irq;
54 int t_irq;
55 struct mpc52xx_fec __iomem *fec;
56 struct bcom_task *rx_dmatsk;
57 struct bcom_task *tx_dmatsk;
58 spinlock_t lock;
59 int msg_enable;
60
61 /* MDIO link details */
Grant Likelyca816d92009-04-25 12:53:02 +000062 unsigned int mdio_speed;
63 struct device_node *phy_node;
Grant Likely80791be2008-05-01 11:05:58 -060064 struct phy_device *phydev;
65 enum phy_state link;
Grant Likelyca816d92009-04-25 12:53:02 +000066 int seven_wire_mode;
Grant Likely80791be2008-05-01 11:05:58 -060067};
68
69
Domen Puncer5d031e92007-10-26 18:07:49 +020070static irqreturn_t mpc52xx_fec_interrupt(int, void *);
71static irqreturn_t mpc52xx_fec_rx_interrupt(int, void *);
72static irqreturn_t mpc52xx_fec_tx_interrupt(int, void *);
73static void mpc52xx_fec_stop(struct net_device *dev);
74static void mpc52xx_fec_start(struct net_device *dev);
75static void mpc52xx_fec_reset(struct net_device *dev);
76
77static u8 mpc52xx_fec_mac_addr[6];
78module_param_array_named(mac, mpc52xx_fec_mac_addr, byte, NULL, 0);
79MODULE_PARM_DESC(mac, "six hex digits, ie. 0x1,0x2,0xc0,0x01,0xba,0xbe");
80
81#define MPC52xx_MESSAGES_DEFAULT ( NETIF_MSG_DRV | NETIF_MSG_PROBE | \
Roel Kluin8b983512008-06-09 16:33:51 -070082 NETIF_MSG_LINK | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
Domen Puncer5d031e92007-10-26 18:07:49 +020083static int debug = -1; /* the above default */
84module_param(debug, int, 0);
85MODULE_PARM_DESC(debug, "debugging messages level");
86
87static void mpc52xx_fec_tx_timeout(struct net_device *dev)
88{
Asier Llano1e4e0762009-12-08 20:29:10 -080089 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
90 unsigned long flags;
91
Domen Puncer5d031e92007-10-26 18:07:49 +020092 dev_warn(&dev->dev, "transmit timed out\n");
93
Asier Llano1e4e0762009-12-08 20:29:10 -080094 spin_lock_irqsave(&priv->lock, flags);
Domen Puncer5d031e92007-10-26 18:07:49 +020095 mpc52xx_fec_reset(dev);
Domen Puncer5d031e92007-10-26 18:07:49 +020096 dev->stats.tx_errors++;
Asier Llano1e4e0762009-12-08 20:29:10 -080097 spin_unlock_irqrestore(&priv->lock, flags);
Domen Puncer5d031e92007-10-26 18:07:49 +020098
99 netif_wake_queue(dev);
100}
101
102static void mpc52xx_fec_set_paddr(struct net_device *dev, u8 *mac)
103{
104 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
105 struct mpc52xx_fec __iomem *fec = priv->fec;
106
107 out_be32(&fec->paddr1, *(u32 *)(&mac[0]));
108 out_be32(&fec->paddr2, (*(u16 *)(&mac[4]) << 16) | FEC_PADDR2_TYPE);
109}
110
111static void mpc52xx_fec_get_paddr(struct net_device *dev, u8 *mac)
112{
113 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
114 struct mpc52xx_fec __iomem *fec = priv->fec;
115
116 *(u32 *)(&mac[0]) = in_be32(&fec->paddr1);
117 *(u16 *)(&mac[4]) = in_be32(&fec->paddr2) >> 16;
118}
119
120static int mpc52xx_fec_set_mac_address(struct net_device *dev, void *addr)
121{
122 struct sockaddr *sock = addr;
123
124 memcpy(dev->dev_addr, sock->sa_data, dev->addr_len);
125
126 mpc52xx_fec_set_paddr(dev, sock->sa_data);
127 return 0;
128}
129
130static void mpc52xx_fec_free_rx_buffers(struct net_device *dev, struct bcom_task *s)
131{
132 while (!bcom_queue_empty(s)) {
133 struct bcom_fec_bd *bd;
134 struct sk_buff *skb;
135
136 skb = bcom_retrieve_buffer(s, NULL, (struct bcom_bd **)&bd);
Grant Likely461cadb2009-03-31 20:16:52 +0000137 dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
138 DMA_FROM_DEVICE);
Domen Puncer5d031e92007-10-26 18:07:49 +0200139 kfree_skb(skb);
140 }
141}
142
Asier Llano1e4e0762009-12-08 20:29:10 -0800143static void
144mpc52xx_fec_rx_submit(struct net_device *dev, struct sk_buff *rskb)
145{
146 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
147 struct bcom_fec_bd *bd;
148
149 bd = (struct bcom_fec_bd *) bcom_prepare_next_buffer(priv->rx_dmatsk);
150 bd->status = FEC_RX_BUFFER_SIZE;
151 bd->skb_pa = dma_map_single(dev->dev.parent, rskb->data,
152 FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);
153 bcom_submit_next_buffer(priv->rx_dmatsk, rskb);
154}
155
Domen Puncer5d031e92007-10-26 18:07:49 +0200156static int mpc52xx_fec_alloc_rx_buffers(struct net_device *dev, struct bcom_task *rxtsk)
157{
Asier Llano1e4e0762009-12-08 20:29:10 -0800158 struct sk_buff *skb;
Domen Puncer5d031e92007-10-26 18:07:49 +0200159
Asier Llano1e4e0762009-12-08 20:29:10 -0800160 while (!bcom_queue_full(rxtsk)) {
Domen Puncer5d031e92007-10-26 18:07:49 +0200161 skb = dev_alloc_skb(FEC_RX_BUFFER_SIZE);
Asier Llano1e4e0762009-12-08 20:29:10 -0800162 if (!skb)
Domen Puncer5d031e92007-10-26 18:07:49 +0200163 return -EAGAIN;
164
165 /* zero out the initial receive buffers to aid debugging */
166 memset(skb->data, 0, FEC_RX_BUFFER_SIZE);
Asier Llano1e4e0762009-12-08 20:29:10 -0800167 mpc52xx_fec_rx_submit(dev, skb);
Domen Puncer5d031e92007-10-26 18:07:49 +0200168 }
Domen Puncer5d031e92007-10-26 18:07:49 +0200169 return 0;
170}
171
172/* based on generic_adjust_link from fs_enet-main.c */
173static void mpc52xx_fec_adjust_link(struct net_device *dev)
174{
175 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
176 struct phy_device *phydev = priv->phydev;
177 int new_state = 0;
178
179 if (phydev->link != PHY_DOWN) {
180 if (phydev->duplex != priv->duplex) {
181 struct mpc52xx_fec __iomem *fec = priv->fec;
182 u32 rcntrl;
183 u32 tcntrl;
184
185 new_state = 1;
186 priv->duplex = phydev->duplex;
187
188 rcntrl = in_be32(&fec->r_cntrl);
189 tcntrl = in_be32(&fec->x_cntrl);
190
191 rcntrl &= ~FEC_RCNTRL_DRT;
192 tcntrl &= ~FEC_TCNTRL_FDEN;
193 if (phydev->duplex == DUPLEX_FULL)
194 tcntrl |= FEC_TCNTRL_FDEN; /* FD enable */
195 else
196 rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
197
198 out_be32(&fec->r_cntrl, rcntrl);
199 out_be32(&fec->x_cntrl, tcntrl);
200 }
201
202 if (phydev->speed != priv->speed) {
203 new_state = 1;
204 priv->speed = phydev->speed;
205 }
206
207 if (priv->link == PHY_DOWN) {
208 new_state = 1;
209 priv->link = phydev->link;
Domen Puncer5d031e92007-10-26 18:07:49 +0200210 }
211
212 } else if (priv->link) {
213 new_state = 1;
214 priv->link = PHY_DOWN;
215 priv->speed = 0;
216 priv->duplex = -1;
Domen Puncer5d031e92007-10-26 18:07:49 +0200217 }
218
219 if (new_state && netif_msg_link(priv))
220 phy_print_status(phydev);
221}
222
Domen Puncer5d031e92007-10-26 18:07:49 +0200223static int mpc52xx_fec_open(struct net_device *dev)
224{
225 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
226 int err = -EBUSY;
227
Grant Likelyca816d92009-04-25 12:53:02 +0000228 if (priv->phy_node) {
229 priv->phydev = of_phy_connect(priv->ndev, priv->phy_node,
230 mpc52xx_fec_adjust_link, 0, 0);
231 if (!priv->phydev) {
232 dev_err(&dev->dev, "of_phy_connect failed\n");
233 return -ENODEV;
234 }
235 phy_start(priv->phydev);
236 }
237
Joe Perchesa0607fd2009-11-18 23:29:17 -0800238 if (request_irq(dev->irq, mpc52xx_fec_interrupt, IRQF_SHARED,
Domen Puncer5d031e92007-10-26 18:07:49 +0200239 DRIVER_NAME "_ctrl", dev)) {
240 dev_err(&dev->dev, "ctrl interrupt request failed\n");
Grant Likelyca816d92009-04-25 12:53:02 +0000241 goto free_phy;
Domen Puncer5d031e92007-10-26 18:07:49 +0200242 }
Joe Perchesa0607fd2009-11-18 23:29:17 -0800243 if (request_irq(priv->r_irq, mpc52xx_fec_rx_interrupt, 0,
Domen Puncer5d031e92007-10-26 18:07:49 +0200244 DRIVER_NAME "_rx", dev)) {
245 dev_err(&dev->dev, "rx interrupt request failed\n");
246 goto free_ctrl_irq;
247 }
Joe Perchesa0607fd2009-11-18 23:29:17 -0800248 if (request_irq(priv->t_irq, mpc52xx_fec_tx_interrupt, 0,
Domen Puncer5d031e92007-10-26 18:07:49 +0200249 DRIVER_NAME "_tx", dev)) {
250 dev_err(&dev->dev, "tx interrupt request failed\n");
251 goto free_2irqs;
252 }
253
254 bcom_fec_rx_reset(priv->rx_dmatsk);
255 bcom_fec_tx_reset(priv->tx_dmatsk);
256
257 err = mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
258 if (err) {
259 dev_err(&dev->dev, "mpc52xx_fec_alloc_rx_buffers failed\n");
260 goto free_irqs;
261 }
262
Domen Puncer5d031e92007-10-26 18:07:49 +0200263 bcom_enable(priv->rx_dmatsk);
264 bcom_enable(priv->tx_dmatsk);
265
266 mpc52xx_fec_start(dev);
267
268 netif_start_queue(dev);
269
270 return 0;
271
Domen Puncer5d031e92007-10-26 18:07:49 +0200272 free_irqs:
273 free_irq(priv->t_irq, dev);
274 free_2irqs:
275 free_irq(priv->r_irq, dev);
276 free_ctrl_irq:
277 free_irq(dev->irq, dev);
Grant Likelyca816d92009-04-25 12:53:02 +0000278 free_phy:
279 if (priv->phydev) {
280 phy_stop(priv->phydev);
281 phy_disconnect(priv->phydev);
282 priv->phydev = NULL;
283 }
Domen Puncer5d031e92007-10-26 18:07:49 +0200284
285 return err;
286}
287
288static int mpc52xx_fec_close(struct net_device *dev)
289{
290 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
291
292 netif_stop_queue(dev);
293
294 mpc52xx_fec_stop(dev);
295
296 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
297
298 free_irq(dev->irq, dev);
299 free_irq(priv->r_irq, dev);
300 free_irq(priv->t_irq, dev);
301
Grant Likelyca816d92009-04-25 12:53:02 +0000302 if (priv->phydev) {
303 /* power down phy */
304 phy_stop(priv->phydev);
305 phy_disconnect(priv->phydev);
306 priv->phydev = NULL;
307 }
Domen Puncer5d031e92007-10-26 18:07:49 +0200308
309 return 0;
310}
311
312/* This will only be invoked if your driver is _not_ in XOFF state.
313 * What this means is that you need not check it, and that this
314 * invariant will hold if you make sure that the netif_*_queue()
315 * calls are done at the proper times.
316 */
Henk Stegemand3600092009-03-31 20:16:57 +0000317static int mpc52xx_fec_start_xmit(struct sk_buff *skb, struct net_device *dev)
Domen Puncer5d031e92007-10-26 18:07:49 +0200318{
319 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
320 struct bcom_fec_bd *bd;
Dongdong Deng48719532009-08-23 19:49:07 -0700321 unsigned long flags;
Domen Puncer5d031e92007-10-26 18:07:49 +0200322
323 if (bcom_queue_full(priv->tx_dmatsk)) {
324 if (net_ratelimit())
325 dev_err(&dev->dev, "transmit queue overrun\n");
Henk Stegemand3600092009-03-31 20:16:57 +0000326 return NETDEV_TX_BUSY;
Domen Puncer5d031e92007-10-26 18:07:49 +0200327 }
328
Dongdong Deng48719532009-08-23 19:49:07 -0700329 spin_lock_irqsave(&priv->lock, flags);
Domen Puncer5d031e92007-10-26 18:07:49 +0200330
331 bd = (struct bcom_fec_bd *)
332 bcom_prepare_next_buffer(priv->tx_dmatsk);
333
334 bd->status = skb->len | BCOM_FEC_TX_BD_TFD | BCOM_FEC_TX_BD_TC;
Grant Likely461cadb2009-03-31 20:16:52 +0000335 bd->skb_pa = dma_map_single(dev->dev.parent, skb->data, skb->len,
336 DMA_TO_DEVICE);
Domen Puncer5d031e92007-10-26 18:07:49 +0200337
338 bcom_submit_next_buffer(priv->tx_dmatsk, skb);
Asier Llano1e4e0762009-12-08 20:29:10 -0800339 spin_unlock_irqrestore(&priv->lock, flags);
Domen Puncer5d031e92007-10-26 18:07:49 +0200340
341 if (bcom_queue_full(priv->tx_dmatsk)) {
342 netif_stop_queue(dev);
343 }
344
Henk Stegemand3600092009-03-31 20:16:57 +0000345 return NETDEV_TX_OK;
Domen Puncer5d031e92007-10-26 18:07:49 +0200346}
347
Jon Smirlbd28bdb2008-10-18 11:40:59 +0000348#ifdef CONFIG_NET_POLL_CONTROLLER
349static void mpc52xx_fec_poll_controller(struct net_device *dev)
350{
351 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
352
353 disable_irq(priv->t_irq);
354 mpc52xx_fec_tx_interrupt(priv->t_irq, dev);
355 enable_irq(priv->t_irq);
356 disable_irq(priv->r_irq);
357 mpc52xx_fec_rx_interrupt(priv->r_irq, dev);
358 enable_irq(priv->r_irq);
359}
360#endif
361
362
Domen Puncer5d031e92007-10-26 18:07:49 +0200363/* This handles BestComm transmit task interrupts
364 */
365static irqreturn_t mpc52xx_fec_tx_interrupt(int irq, void *dev_id)
366{
367 struct net_device *dev = dev_id;
368 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
Asier Llano1e4e0762009-12-08 20:29:10 -0800369 unsigned long flags;
Domen Puncer5d031e92007-10-26 18:07:49 +0200370
Asier Llano1e4e0762009-12-08 20:29:10 -0800371 spin_lock_irqsave(&priv->lock, flags);
Domen Puncer5d031e92007-10-26 18:07:49 +0200372 while (bcom_buffer_done(priv->tx_dmatsk)) {
373 struct sk_buff *skb;
374 struct bcom_fec_bd *bd;
375 skb = bcom_retrieve_buffer(priv->tx_dmatsk, NULL,
376 (struct bcom_bd **)&bd);
Grant Likely461cadb2009-03-31 20:16:52 +0000377 dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
378 DMA_TO_DEVICE);
Domen Puncer5d031e92007-10-26 18:07:49 +0200379
380 dev_kfree_skb_irq(skb);
381 }
Asier Llano1e4e0762009-12-08 20:29:10 -0800382 spin_unlock_irqrestore(&priv->lock, flags);
Domen Puncer5d031e92007-10-26 18:07:49 +0200383
384 netif_wake_queue(dev);
385
Domen Puncer5d031e92007-10-26 18:07:49 +0200386 return IRQ_HANDLED;
387}
388
389static irqreturn_t mpc52xx_fec_rx_interrupt(int irq, void *dev_id)
390{
391 struct net_device *dev = dev_id;
392 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
Asier Llano1e4e0762009-12-08 20:29:10 -0800393 struct sk_buff *rskb; /* received sk_buff */
394 struct sk_buff *skb; /* new sk_buff to enqueue in its place */
395 struct bcom_fec_bd *bd;
396 u32 status, physaddr;
397 int length;
398 unsigned long flags;
399
400 spin_lock_irqsave(&priv->lock, flags);
Domen Puncer5d031e92007-10-26 18:07:49 +0200401
402 while (bcom_buffer_done(priv->rx_dmatsk)) {
Domen Puncer5d031e92007-10-26 18:07:49 +0200403
404 rskb = bcom_retrieve_buffer(priv->rx_dmatsk, &status,
Asier Llano1e4e0762009-12-08 20:29:10 -0800405 (struct bcom_bd **)&bd);
406 physaddr = bd->skb_pa;
Domen Puncer5d031e92007-10-26 18:07:49 +0200407
408 /* Test for errors in received frame */
409 if (status & BCOM_FEC_RX_BD_ERRORS) {
410 /* Drop packet and reuse the buffer */
Asier Llano1e4e0762009-12-08 20:29:10 -0800411 mpc52xx_fec_rx_submit(dev, rskb);
Domen Puncer5d031e92007-10-26 18:07:49 +0200412 dev->stats.rx_dropped++;
Domen Puncer5d031e92007-10-26 18:07:49 +0200413 continue;
414 }
415
416 /* skbs are allocated on open, so now we allocate a new one,
417 * and remove the old (with the packet) */
418 skb = dev_alloc_skb(FEC_RX_BUFFER_SIZE);
Asier Llano1e4e0762009-12-08 20:29:10 -0800419 if (!skb) {
Domen Puncer5d031e92007-10-26 18:07:49 +0200420 /* Can't get a new one : reuse the same & drop pkt */
Asier Llano1e4e0762009-12-08 20:29:10 -0800421 dev_notice(&dev->dev, "Low memory - dropped packet.\n");
422 mpc52xx_fec_rx_submit(dev, rskb);
Domen Puncer5d031e92007-10-26 18:07:49 +0200423 dev->stats.rx_dropped++;
Asier Llano1e4e0762009-12-08 20:29:10 -0800424 continue;
Domen Puncer5d031e92007-10-26 18:07:49 +0200425 }
426
Asier Llano1e4e0762009-12-08 20:29:10 -0800427 /* Enqueue the new sk_buff back on the hardware */
428 mpc52xx_fec_rx_submit(dev, skb);
Domen Puncer5d031e92007-10-26 18:07:49 +0200429
Asier Llano1e4e0762009-12-08 20:29:10 -0800430 /* Process the received skb - Drop the spin lock while
431 * calling into the network stack */
432 spin_unlock_irqrestore(&priv->lock, flags);
Domen Puncer5d031e92007-10-26 18:07:49 +0200433
Asier Llano1e4e0762009-12-08 20:29:10 -0800434 dma_unmap_single(dev->dev.parent, physaddr, rskb->len,
435 DMA_FROM_DEVICE);
436 length = status & BCOM_FEC_RX_BD_LEN_MASK;
437 skb_put(rskb, length - 4); /* length without CRC32 */
Asier Llano1e4e0762009-12-08 20:29:10 -0800438 rskb->protocol = eth_type_trans(rskb, dev);
439 netif_rx(rskb);
440
441 spin_lock_irqsave(&priv->lock, flags);
Domen Puncer5d031e92007-10-26 18:07:49 +0200442 }
443
Asier Llano1e4e0762009-12-08 20:29:10 -0800444 spin_unlock_irqrestore(&priv->lock, flags);
445
Domen Puncer5d031e92007-10-26 18:07:49 +0200446 return IRQ_HANDLED;
447}
448
449static irqreturn_t mpc52xx_fec_interrupt(int irq, void *dev_id)
450{
451 struct net_device *dev = dev_id;
452 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
453 struct mpc52xx_fec __iomem *fec = priv->fec;
454 u32 ievent;
Asier Llano1e4e0762009-12-08 20:29:10 -0800455 unsigned long flags;
Domen Puncer5d031e92007-10-26 18:07:49 +0200456
457 ievent = in_be32(&fec->ievent);
458
459 ievent &= ~FEC_IEVENT_MII; /* mii is handled separately */
460 if (!ievent)
461 return IRQ_NONE;
462
463 out_be32(&fec->ievent, ievent); /* clear pending events */
464
Sascha Hauer8f3ba2d2008-04-16 01:44:39 +1000465 /* on fifo error, soft-reset fec */
466 if (ievent & (FEC_IEVENT_RFIFO_ERROR | FEC_IEVENT_XFIFO_ERROR)) {
467
468 if (net_ratelimit() && (ievent & FEC_IEVENT_RFIFO_ERROR))
469 dev_warn(&dev->dev, "FEC_IEVENT_RFIFO_ERROR\n");
470 if (net_ratelimit() && (ievent & FEC_IEVENT_XFIFO_ERROR))
471 dev_warn(&dev->dev, "FEC_IEVENT_XFIFO_ERROR\n");
472
Asier Llano1e4e0762009-12-08 20:29:10 -0800473 spin_lock_irqsave(&priv->lock, flags);
Sascha Hauer8f3ba2d2008-04-16 01:44:39 +1000474 mpc52xx_fec_reset(dev);
Asier Llano1e4e0762009-12-08 20:29:10 -0800475 spin_unlock_irqrestore(&priv->lock, flags);
Sascha Hauer8f3ba2d2008-04-16 01:44:39 +1000476
Domen Puncer5d031e92007-10-26 18:07:49 +0200477 return IRQ_HANDLED;
478 }
479
Sascha Hauer8f3ba2d2008-04-16 01:44:39 +1000480 if (ievent & ~FEC_IEVENT_TFINT)
481 dev_dbg(&dev->dev, "ievent: %08x\n", ievent);
Domen Puncer5d031e92007-10-26 18:07:49 +0200482
Domen Puncer5d031e92007-10-26 18:07:49 +0200483 return IRQ_HANDLED;
484}
485
486/*
487 * Get the current statistics.
488 * This may be called with the card open or closed.
489 */
490static struct net_device_stats *mpc52xx_fec_get_stats(struct net_device *dev)
491{
492 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
493 struct net_device_stats *stats = &dev->stats;
494 struct mpc52xx_fec __iomem *fec = priv->fec;
495
496 stats->rx_bytes = in_be32(&fec->rmon_r_octets);
497 stats->rx_packets = in_be32(&fec->rmon_r_packets);
498 stats->rx_errors = in_be32(&fec->rmon_r_crc_align) +
499 in_be32(&fec->rmon_r_undersize) +
500 in_be32(&fec->rmon_r_oversize) +
501 in_be32(&fec->rmon_r_frag) +
502 in_be32(&fec->rmon_r_jab);
503
504 stats->tx_bytes = in_be32(&fec->rmon_t_octets);
505 stats->tx_packets = in_be32(&fec->rmon_t_packets);
506 stats->tx_errors = in_be32(&fec->rmon_t_crc_align) +
507 in_be32(&fec->rmon_t_undersize) +
508 in_be32(&fec->rmon_t_oversize) +
509 in_be32(&fec->rmon_t_frag) +
510 in_be32(&fec->rmon_t_jab);
511
512 stats->multicast = in_be32(&fec->rmon_r_mc_pkt);
513 stats->collisions = in_be32(&fec->rmon_t_col);
514
515 /* detailed rx_errors: */
516 stats->rx_length_errors = in_be32(&fec->rmon_r_undersize)
517 + in_be32(&fec->rmon_r_oversize)
518 + in_be32(&fec->rmon_r_frag)
519 + in_be32(&fec->rmon_r_jab);
520 stats->rx_over_errors = in_be32(&fec->r_macerr);
521 stats->rx_crc_errors = in_be32(&fec->ieee_r_crc);
522 stats->rx_frame_errors = in_be32(&fec->ieee_r_align);
523 stats->rx_fifo_errors = in_be32(&fec->rmon_r_drop);
524 stats->rx_missed_errors = in_be32(&fec->rmon_r_drop);
525
526 /* detailed tx_errors: */
527 stats->tx_aborted_errors = 0;
528 stats->tx_carrier_errors = in_be32(&fec->ieee_t_cserr);
529 stats->tx_fifo_errors = in_be32(&fec->rmon_t_drop);
530 stats->tx_heartbeat_errors = in_be32(&fec->ieee_t_sqe);
531 stats->tx_window_errors = in_be32(&fec->ieee_t_lcol);
532
533 return stats;
534}
535
536/*
537 * Read MIB counters in order to reset them,
538 * then zero all the stats fields in memory
539 */
540static void mpc52xx_fec_reset_stats(struct net_device *dev)
541{
542 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
543 struct mpc52xx_fec __iomem *fec = priv->fec;
544
545 out_be32(&fec->mib_control, FEC_MIB_DISABLE);
Al Virocc154ac2007-12-22 18:56:53 +0000546 memset_io(&fec->rmon_t_drop, 0,
547 offsetof(struct mpc52xx_fec, reserved10) -
548 offsetof(struct mpc52xx_fec, rmon_t_drop));
Domen Puncer5d031e92007-10-26 18:07:49 +0200549 out_be32(&fec->mib_control, 0);
550
551 memset(&dev->stats, 0, sizeof(dev->stats));
552}
553
554/*
555 * Set or clear the multicast filter for this adaptor.
556 */
557static void mpc52xx_fec_set_multicast_list(struct net_device *dev)
558{
559 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
560 struct mpc52xx_fec __iomem *fec = priv->fec;
561 u32 rx_control;
562
563 rx_control = in_be32(&fec->r_cntrl);
564
565 if (dev->flags & IFF_PROMISC) {
566 rx_control |= FEC_RCNTRL_PROM;
567 out_be32(&fec->r_cntrl, rx_control);
568 } else {
569 rx_control &= ~FEC_RCNTRL_PROM;
570 out_be32(&fec->r_cntrl, rx_control);
571
572 if (dev->flags & IFF_ALLMULTI) {
573 out_be32(&fec->gaddr1, 0xffffffff);
574 out_be32(&fec->gaddr2, 0xffffffff);
575 } else {
576 u32 crc;
Jiri Pirko22bedad2010-04-01 21:22:57 +0000577 struct netdev_hw_addr *ha;
Domen Puncer5d031e92007-10-26 18:07:49 +0200578 u32 gaddr1 = 0x00000000;
579 u32 gaddr2 = 0x00000000;
580
Jiri Pirko22bedad2010-04-01 21:22:57 +0000581 netdev_for_each_mc_addr(ha, dev) {
582 crc = ether_crc_le(6, ha->addr) >> 26;
Domen Puncer5d031e92007-10-26 18:07:49 +0200583 if (crc >= 32)
584 gaddr1 |= 1 << (crc-32);
585 else
586 gaddr2 |= 1 << crc;
Domen Puncer5d031e92007-10-26 18:07:49 +0200587 }
588 out_be32(&fec->gaddr1, gaddr1);
589 out_be32(&fec->gaddr2, gaddr2);
590 }
591 }
592}
593
594/**
595 * mpc52xx_fec_hw_init
596 * @dev: network device
597 *
598 * Setup various hardware setting, only needed once on start
599 */
600static void mpc52xx_fec_hw_init(struct net_device *dev)
601{
602 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
603 struct mpc52xx_fec __iomem *fec = priv->fec;
604 int i;
605
606 /* Whack a reset. We should wait for this. */
607 out_be32(&fec->ecntrl, FEC_ECNTRL_RESET);
608 for (i = 0; i < FEC_RESET_DELAY; ++i) {
609 if ((in_be32(&fec->ecntrl) & FEC_ECNTRL_RESET) == 0)
610 break;
611 udelay(1);
612 }
613 if (i == FEC_RESET_DELAY)
614 dev_err(&dev->dev, "FEC Reset timeout!\n");
615
616 /* set pause to 0x20 frames */
617 out_be32(&fec->op_pause, FEC_OP_PAUSE_OPCODE | 0x20);
618
619 /* high service request will be deasserted when there's < 7 bytes in fifo
620 * low service request will be deasserted when there's < 4*7 bytes in fifo
621 */
622 out_be32(&fec->rfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
623 out_be32(&fec->tfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
624
625 /* alarm when <= x bytes in FIFO */
626 out_be32(&fec->rfifo_alarm, 0x0000030c);
627 out_be32(&fec->tfifo_alarm, 0x00000100);
628
629 /* begin transmittion when 256 bytes are in FIFO (or EOF or FIFO full) */
630 out_be32(&fec->x_wmrk, FEC_FIFO_WMRK_256B);
631
632 /* enable crc generation */
633 out_be32(&fec->xmit_fsm, FEC_XMIT_FSM_APPEND_CRC | FEC_XMIT_FSM_ENABLE_CRC);
634 out_be32(&fec->iaddr1, 0x00000000); /* No individual filter */
635 out_be32(&fec->iaddr2, 0x00000000); /* No individual filter */
636
637 /* set phy speed.
638 * this can't be done in phy driver, since it needs to be called
639 * before fec stuff (even on resume) */
Grant Likelyca816d92009-04-25 12:53:02 +0000640 out_be32(&fec->mii_speed, priv->mdio_speed);
Domen Puncer5d031e92007-10-26 18:07:49 +0200641}
642
643/**
644 * mpc52xx_fec_start
645 * @dev: network device
646 *
647 * This function is called to start or restart the FEC during a link
648 * change. This happens on fifo errors or when switching between half
649 * and full duplex.
650 */
651static void mpc52xx_fec_start(struct net_device *dev)
652{
653 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
654 struct mpc52xx_fec __iomem *fec = priv->fec;
655 u32 rcntrl;
656 u32 tcntrl;
657 u32 tmp;
658
659 /* clear sticky error bits */
660 tmp = FEC_FIFO_STATUS_ERR | FEC_FIFO_STATUS_UF | FEC_FIFO_STATUS_OF;
661 out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status) & tmp);
662 out_be32(&fec->tfifo_status, in_be32(&fec->tfifo_status) & tmp);
663
664 /* FIFOs will reset on mpc52xx_fec_enable */
665 out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_ENABLE_IS_RESET);
666
667 /* Set station address. */
668 mpc52xx_fec_set_paddr(dev, dev->dev_addr);
669
670 mpc52xx_fec_set_multicast_list(dev);
671
672 /* set max frame len, enable flow control, select mii mode */
673 rcntrl = FEC_RX_BUFFER_SIZE << 16; /* max frame length */
674 rcntrl |= FEC_RCNTRL_FCE;
675
Grant Likelyca816d92009-04-25 12:53:02 +0000676 if (!priv->seven_wire_mode)
Domen Puncer5d031e92007-10-26 18:07:49 +0200677 rcntrl |= FEC_RCNTRL_MII_MODE;
678
679 if (priv->duplex == DUPLEX_FULL)
680 tcntrl = FEC_TCNTRL_FDEN; /* FD enable */
681 else {
682 rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
683 tcntrl = 0;
684 }
685 out_be32(&fec->r_cntrl, rcntrl);
686 out_be32(&fec->x_cntrl, tcntrl);
687
688 /* Clear any outstanding interrupt. */
689 out_be32(&fec->ievent, 0xffffffff);
690
691 /* Enable interrupts we wish to service. */
692 out_be32(&fec->imask, FEC_IMASK_ENABLE);
693
694 /* And last, enable the transmit and receive processing. */
695 out_be32(&fec->ecntrl, FEC_ECNTRL_ETHER_EN);
696 out_be32(&fec->r_des_active, 0x01000000);
697}
698
699/**
700 * mpc52xx_fec_stop
701 * @dev: network device
702 *
703 * stop all activity on fec and empty dma buffers
704 */
705static void mpc52xx_fec_stop(struct net_device *dev)
706{
707 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
708 struct mpc52xx_fec __iomem *fec = priv->fec;
709 unsigned long timeout;
710
711 /* disable all interrupts */
712 out_be32(&fec->imask, 0);
713
714 /* Disable the rx task. */
715 bcom_disable(priv->rx_dmatsk);
716
717 /* Wait for tx queue to drain, but only if we're in process context */
718 if (!in_interrupt()) {
719 timeout = jiffies + msecs_to_jiffies(2000);
720 while (time_before(jiffies, timeout) &&
721 !bcom_queue_empty(priv->tx_dmatsk))
722 msleep(100);
723
724 if (time_after_eq(jiffies, timeout))
725 dev_err(&dev->dev, "queues didn't drain\n");
726#if 1
727 if (time_after_eq(jiffies, timeout)) {
728 dev_err(&dev->dev, " tx: index: %i, outdex: %i\n",
729 priv->tx_dmatsk->index,
730 priv->tx_dmatsk->outdex);
731 dev_err(&dev->dev, " rx: index: %i, outdex: %i\n",
732 priv->rx_dmatsk->index,
733 priv->rx_dmatsk->outdex);
734 }
735#endif
736 }
737
738 bcom_disable(priv->tx_dmatsk);
739
740 /* Stop FEC */
741 out_be32(&fec->ecntrl, in_be32(&fec->ecntrl) & ~FEC_ECNTRL_ETHER_EN);
Domen Puncer5d031e92007-10-26 18:07:49 +0200742}
743
744/* reset fec and bestcomm tasks */
745static void mpc52xx_fec_reset(struct net_device *dev)
746{
747 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
748 struct mpc52xx_fec __iomem *fec = priv->fec;
749
750 mpc52xx_fec_stop(dev);
751
752 out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status));
753 out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_RESET_FIFO);
754
755 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
756
757 mpc52xx_fec_hw_init(dev);
758
Domen Puncer5d031e92007-10-26 18:07:49 +0200759 bcom_fec_rx_reset(priv->rx_dmatsk);
760 bcom_fec_tx_reset(priv->tx_dmatsk);
761
762 mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
763
764 bcom_enable(priv->rx_dmatsk);
765 bcom_enable(priv->tx_dmatsk);
766
767 mpc52xx_fec_start(dev);
Asier Llano1e4e0762009-12-08 20:29:10 -0800768
769 netif_wake_queue(dev);
Domen Puncer5d031e92007-10-26 18:07:49 +0200770}
771
772
773/* ethtool interface */
Domen Puncer5d031e92007-10-26 18:07:49 +0200774
775static int mpc52xx_fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
776{
777 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
Grant Likely9404c822009-03-31 20:17:03 +0000778
779 if (!priv->phydev)
780 return -ENODEV;
781
Domen Puncer5d031e92007-10-26 18:07:49 +0200782 return phy_ethtool_gset(priv->phydev, cmd);
783}
784
785static int mpc52xx_fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
786{
787 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
Grant Likely9404c822009-03-31 20:17:03 +0000788
789 if (!priv->phydev)
790 return -ENODEV;
791
Domen Puncer5d031e92007-10-26 18:07:49 +0200792 return phy_ethtool_sset(priv->phydev, cmd);
793}
794
795static u32 mpc52xx_fec_get_msglevel(struct net_device *dev)
796{
797 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
798 return priv->msg_enable;
799}
800
801static void mpc52xx_fec_set_msglevel(struct net_device *dev, u32 level)
802{
803 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
804 priv->msg_enable = level;
805}
806
807static const struct ethtool_ops mpc52xx_fec_ethtool_ops = {
Domen Puncer5d031e92007-10-26 18:07:49 +0200808 .get_settings = mpc52xx_fec_get_settings,
809 .set_settings = mpc52xx_fec_set_settings,
810 .get_link = ethtool_op_get_link,
811 .get_msglevel = mpc52xx_fec_get_msglevel,
812 .set_msglevel = mpc52xx_fec_set_msglevel,
813};
814
815
816static int mpc52xx_fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
817{
818 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
819
Grant Likely9404c822009-03-31 20:17:03 +0000820 if (!priv->phydev)
821 return -ENOTSUPP;
822
Richard Cochran28b04112010-07-17 08:48:55 +0000823 return phy_mii_ioctl(priv->phydev, rq, cmd);
Domen Puncer5d031e92007-10-26 18:07:49 +0200824}
825
Henk Stegemand3600092009-03-31 20:16:57 +0000826static const struct net_device_ops mpc52xx_fec_netdev_ops = {
827 .ndo_open = mpc52xx_fec_open,
828 .ndo_stop = mpc52xx_fec_close,
829 .ndo_start_xmit = mpc52xx_fec_start_xmit,
830 .ndo_set_multicast_list = mpc52xx_fec_set_multicast_list,
831 .ndo_set_mac_address = mpc52xx_fec_set_mac_address,
832 .ndo_validate_addr = eth_validate_addr,
833 .ndo_do_ioctl = mpc52xx_fec_ioctl,
834 .ndo_change_mtu = eth_change_mtu,
835 .ndo_tx_timeout = mpc52xx_fec_tx_timeout,
836 .ndo_get_stats = mpc52xx_fec_get_stats,
837#ifdef CONFIG_NET_POLL_CONTROLLER
838 .ndo_poll_controller = mpc52xx_fec_poll_controller,
839#endif
840};
841
Domen Puncer5d031e92007-10-26 18:07:49 +0200842/* ======================================================================== */
843/* OF Driver */
844/* ======================================================================== */
845
846static int __devinit
Grant Likely2dc11582010-08-06 09:25:50 -0600847mpc52xx_fec_probe(struct platform_device *op, const struct of_device_id *match)
Domen Puncer5d031e92007-10-26 18:07:49 +0200848{
849 int rv;
850 struct net_device *ndev;
851 struct mpc52xx_fec_priv *priv = NULL;
852 struct resource mem;
Grant Likely80791be2008-05-01 11:05:58 -0600853 const u32 *prop;
854 int prop_size;
Domen Puncer5d031e92007-10-26 18:07:49 +0200855
856 phys_addr_t rx_fifo;
857 phys_addr_t tx_fifo;
858
859 /* Get the ether ndev & it's private zone */
860 ndev = alloc_etherdev(sizeof(struct mpc52xx_fec_priv));
861 if (!ndev)
862 return -ENOMEM;
863
864 priv = netdev_priv(ndev);
Grant Likelyca816d92009-04-25 12:53:02 +0000865 priv->ndev = ndev;
Domen Puncer5d031e92007-10-26 18:07:49 +0200866
867 /* Reserve FEC control zone */
Grant Likely61c7a082010-04-13 16:12:29 -0700868 rv = of_address_to_resource(op->dev.of_node, 0, &mem);
Domen Puncer5d031e92007-10-26 18:07:49 +0200869 if (rv) {
870 printk(KERN_ERR DRIVER_NAME ": "
871 "Error while parsing device node resource\n" );
Kulikov Vasiliyfabc51a2010-07-08 23:44:26 -0700872 goto err_netdev;
Domen Puncer5d031e92007-10-26 18:07:49 +0200873 }
Grant Likely48d58452007-11-01 08:22:35 -0600874 if ((mem.end - mem.start + 1) < sizeof(struct mpc52xx_fec)) {
Domen Puncer5d031e92007-10-26 18:07:49 +0200875 printk(KERN_ERR DRIVER_NAME
Grant Likely48d58452007-11-01 08:22:35 -0600876 " - invalid resource size (%lx < %x), check mpc52xx_devices.c\n",
Domen Puncer5d031e92007-10-26 18:07:49 +0200877 (unsigned long)(mem.end - mem.start + 1), sizeof(struct mpc52xx_fec));
Kulikov Vasiliyfabc51a2010-07-08 23:44:26 -0700878 rv = -EINVAL;
879 goto err_netdev;
Domen Puncer5d031e92007-10-26 18:07:49 +0200880 }
881
Kulikov Vasiliyfabc51a2010-07-08 23:44:26 -0700882 if (!request_mem_region(mem.start, sizeof(struct mpc52xx_fec),
883 DRIVER_NAME)) {
884 rv = -EBUSY;
885 goto err_netdev;
886 }
Domen Puncer5d031e92007-10-26 18:07:49 +0200887
888 /* Init ether ndev with what we have */
Henk Stegemand3600092009-03-31 20:16:57 +0000889 ndev->netdev_ops = &mpc52xx_fec_netdev_ops;
Domen Puncer5d031e92007-10-26 18:07:49 +0200890 ndev->ethtool_ops = &mpc52xx_fec_ethtool_ops;
Domen Puncer5d031e92007-10-26 18:07:49 +0200891 ndev->watchdog_timeo = FEC_WATCHDOG_TIMEOUT;
892 ndev->base_addr = mem.start;
Grant Likelyca816d92009-04-25 12:53:02 +0000893 SET_NETDEV_DEV(ndev, &op->dev);
Domen Puncer5d031e92007-10-26 18:07:49 +0200894
895 spin_lock_init(&priv->lock);
896
897 /* ioremap the zones */
898 priv->fec = ioremap(mem.start, sizeof(struct mpc52xx_fec));
899
900 if (!priv->fec) {
901 rv = -ENOMEM;
Kulikov Vasiliyfabc51a2010-07-08 23:44:26 -0700902 goto err_mem_region;
Domen Puncer5d031e92007-10-26 18:07:49 +0200903 }
904
905 /* Bestcomm init */
906 rx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, rfifo_data);
907 tx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, tfifo_data);
908
909 priv->rx_dmatsk = bcom_fec_rx_init(FEC_RX_NUM_BD, rx_fifo, FEC_RX_BUFFER_SIZE);
910 priv->tx_dmatsk = bcom_fec_tx_init(FEC_TX_NUM_BD, tx_fifo);
911
912 if (!priv->rx_dmatsk || !priv->tx_dmatsk) {
913 printk(KERN_ERR DRIVER_NAME ": Can not init SDMA tasks\n" );
914 rv = -ENOMEM;
Kulikov Vasiliyfabc51a2010-07-08 23:44:26 -0700915 goto err_rx_tx_dmatsk;
Domen Puncer5d031e92007-10-26 18:07:49 +0200916 }
917
918 /* Get the IRQ we need one by one */
919 /* Control */
Grant Likely61c7a082010-04-13 16:12:29 -0700920 ndev->irq = irq_of_parse_and_map(op->dev.of_node, 0);
Domen Puncer5d031e92007-10-26 18:07:49 +0200921
922 /* RX */
923 priv->r_irq = bcom_get_task_irq(priv->rx_dmatsk);
924
925 /* TX */
926 priv->t_irq = bcom_get_task_irq(priv->tx_dmatsk);
927
928 /* MAC address init */
929 if (!is_zero_ether_addr(mpc52xx_fec_mac_addr))
930 memcpy(ndev->dev_addr, mpc52xx_fec_mac_addr, 6);
931 else
932 mpc52xx_fec_get_paddr(ndev, ndev->dev_addr);
933
934 priv->msg_enable = netif_msg_init(debug, MPC52xx_MESSAGES_DEFAULT);
Domen Puncer5d031e92007-10-26 18:07:49 +0200935
Grant Likely80791be2008-05-01 11:05:58 -0600936 /*
937 * Link mode configuration
938 */
Domen Puncer5d031e92007-10-26 18:07:49 +0200939
Grant Likely80791be2008-05-01 11:05:58 -0600940 /* Start with safe defaults for link connection */
Grant Likely80791be2008-05-01 11:05:58 -0600941 priv->speed = 100;
942 priv->duplex = DUPLEX_HALF;
Grant Likely61c7a082010-04-13 16:12:29 -0700943 priv->mdio_speed = ((mpc5xxx_get_bus_frequency(op->dev.of_node) >> 20) / 5) << 1;
Domen Puncer5d031e92007-10-26 18:07:49 +0200944
Grant Likely80791be2008-05-01 11:05:58 -0600945 /* The current speed preconfigures the speed of the MII link */
Grant Likely61c7a082010-04-13 16:12:29 -0700946 prop = of_get_property(op->dev.of_node, "current-speed", &prop_size);
Grant Likely80791be2008-05-01 11:05:58 -0600947 if (prop && (prop_size >= sizeof(u32) * 2)) {
948 priv->speed = prop[0];
949 priv->duplex = prop[1] ? DUPLEX_FULL : DUPLEX_HALF;
950 }
951
Grant Likelyca816d92009-04-25 12:53:02 +0000952 /* If there is a phy handle, then get the PHY node */
Grant Likely61c7a082010-04-13 16:12:29 -0700953 priv->phy_node = of_parse_phandle(op->dev.of_node, "phy-handle", 0);
Grant Likelyca816d92009-04-25 12:53:02 +0000954
955 /* the 7-wire property means don't use MII mode */
Grant Likely61c7a082010-04-13 16:12:29 -0700956 if (of_find_property(op->dev.of_node, "fsl,7-wire-mode", NULL)) {
Grant Likelyca816d92009-04-25 12:53:02 +0000957 priv->seven_wire_mode = 1;
958 dev_info(&ndev->dev, "using 7-wire PHY mode\n");
Domen Puncer5d031e92007-10-26 18:07:49 +0200959 }
960
961 /* Hardware init */
962 mpc52xx_fec_hw_init(ndev);
Domen Puncer5d031e92007-10-26 18:07:49 +0200963 mpc52xx_fec_reset_stats(ndev);
964
Domen Puncer5d031e92007-10-26 18:07:49 +0200965 rv = register_netdev(ndev);
966 if (rv < 0)
Kulikov Vasiliyfabc51a2010-07-08 23:44:26 -0700967 goto err_node;
Domen Puncer5d031e92007-10-26 18:07:49 +0200968
969 /* We're done ! */
970 dev_set_drvdata(&op->dev, ndev);
971
972 return 0;
973
Kulikov Vasiliyfabc51a2010-07-08 23:44:26 -0700974err_node:
975 of_node_put(priv->phy_node);
Domen Puncer5d031e92007-10-26 18:07:49 +0200976 irq_dispose_mapping(ndev->irq);
Kulikov Vasiliyfabc51a2010-07-08 23:44:26 -0700977err_rx_tx_dmatsk:
Domen Puncer5d031e92007-10-26 18:07:49 +0200978 if (priv->rx_dmatsk)
979 bcom_fec_rx_release(priv->rx_dmatsk);
980 if (priv->tx_dmatsk)
981 bcom_fec_tx_release(priv->tx_dmatsk);
Kulikov Vasiliyfabc51a2010-07-08 23:44:26 -0700982 iounmap(priv->fec);
983err_mem_region:
Domen Puncer5d031e92007-10-26 18:07:49 +0200984 release_mem_region(mem.start, sizeof(struct mpc52xx_fec));
Kulikov Vasiliyfabc51a2010-07-08 23:44:26 -0700985err_netdev:
Domen Puncer5d031e92007-10-26 18:07:49 +0200986 free_netdev(ndev);
987
988 return rv;
989}
990
991static int
Grant Likely2dc11582010-08-06 09:25:50 -0600992mpc52xx_fec_remove(struct platform_device *op)
Domen Puncer5d031e92007-10-26 18:07:49 +0200993{
994 struct net_device *ndev;
995 struct mpc52xx_fec_priv *priv;
996
997 ndev = dev_get_drvdata(&op->dev);
998 priv = netdev_priv(ndev);
999
1000 unregister_netdev(ndev);
1001
Grant Likelyca816d92009-04-25 12:53:02 +00001002 if (priv->phy_node)
1003 of_node_put(priv->phy_node);
1004 priv->phy_node = NULL;
1005
Domen Puncer5d031e92007-10-26 18:07:49 +02001006 irq_dispose_mapping(ndev->irq);
1007
1008 bcom_fec_rx_release(priv->rx_dmatsk);
1009 bcom_fec_tx_release(priv->tx_dmatsk);
1010
1011 iounmap(priv->fec);
1012
1013 release_mem_region(ndev->base_addr, sizeof(struct mpc52xx_fec));
1014
1015 free_netdev(ndev);
1016
1017 dev_set_drvdata(&op->dev, NULL);
1018 return 0;
1019}
1020
1021#ifdef CONFIG_PM
Grant Likely2dc11582010-08-06 09:25:50 -06001022static int mpc52xx_fec_of_suspend(struct platform_device *op, pm_message_t state)
Domen Puncer5d031e92007-10-26 18:07:49 +02001023{
1024 struct net_device *dev = dev_get_drvdata(&op->dev);
1025
1026 if (netif_running(dev))
1027 mpc52xx_fec_close(dev);
1028
1029 return 0;
1030}
1031
Grant Likely2dc11582010-08-06 09:25:50 -06001032static int mpc52xx_fec_of_resume(struct platform_device *op)
Domen Puncer5d031e92007-10-26 18:07:49 +02001033{
1034 struct net_device *dev = dev_get_drvdata(&op->dev);
1035
1036 mpc52xx_fec_hw_init(dev);
1037 mpc52xx_fec_reset_stats(dev);
1038
1039 if (netif_running(dev))
1040 mpc52xx_fec_open(dev);
1041
1042 return 0;
1043}
1044#endif
1045
1046static struct of_device_id mpc52xx_fec_match[] = {
Grant Likely3b5ebf82009-02-03 12:30:25 -07001047 { .compatible = "fsl,mpc5200b-fec", },
1048 { .compatible = "fsl,mpc5200-fec", },
1049 { .compatible = "mpc5200-fec", },
Domen Puncer5d031e92007-10-26 18:07:49 +02001050 { }
1051};
1052
1053MODULE_DEVICE_TABLE(of, mpc52xx_fec_match);
1054
1055static struct of_platform_driver mpc52xx_fec_driver = {
Grant Likely40182942010-04-13 16:13:02 -07001056 .driver = {
1057 .name = DRIVER_NAME,
1058 .owner = THIS_MODULE,
1059 .of_match_table = mpc52xx_fec_match,
1060 },
Domen Puncer5d031e92007-10-26 18:07:49 +02001061 .probe = mpc52xx_fec_probe,
1062 .remove = mpc52xx_fec_remove,
1063#ifdef CONFIG_PM
1064 .suspend = mpc52xx_fec_of_suspend,
1065 .resume = mpc52xx_fec_of_resume,
1066#endif
1067};
1068
1069
1070/* ======================================================================== */
1071/* Module */
1072/* ======================================================================== */
1073
1074static int __init
1075mpc52xx_fec_init(void)
1076{
1077#ifdef CONFIG_FEC_MPC52xx_MDIO
1078 int ret;
1079 ret = of_register_platform_driver(&mpc52xx_fec_mdio_driver);
1080 if (ret) {
1081 printk(KERN_ERR DRIVER_NAME ": failed to register mdio driver\n");
1082 return ret;
1083 }
1084#endif
1085 return of_register_platform_driver(&mpc52xx_fec_driver);
1086}
1087
1088static void __exit
1089mpc52xx_fec_exit(void)
1090{
1091 of_unregister_platform_driver(&mpc52xx_fec_driver);
1092#ifdef CONFIG_FEC_MPC52xx_MDIO
1093 of_unregister_platform_driver(&mpc52xx_fec_mdio_driver);
1094#endif
1095}
1096
1097
1098module_init(mpc52xx_fec_init);
1099module_exit(mpc52xx_fec_exit);
1100
1101MODULE_LICENSE("GPL");
1102MODULE_AUTHOR("Dale Farnsworth");
1103MODULE_DESCRIPTION("Ethernet driver for the Freescale MPC52xx FEC");