blob: 848e8407ea8f5407a53eb5464a2f487e131e2ee9 [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>
22#include <linux/errno.h>
23#include <linux/init.h>
24#include <linux/crc32.h>
25#include <linux/hardirq.h>
26#include <linux/delay.h>
27#include <linux/of_device.h>
Grant Likelyca816d92009-04-25 12:53:02 +000028#include <linux/of_mdio.h>
Domen Puncer5d031e92007-10-26 18:07:49 +020029#include <linux/of_platform.h>
30
31#include <linux/netdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/ethtool.h>
34#include <linux/skbuff.h>
35
36#include <asm/io.h>
37#include <asm/delay.h>
38#include <asm/mpc52xx.h>
39
40#include <sysdev/bestcomm/bestcomm.h>
41#include <sysdev/bestcomm/fec.h>
42
43#include "fec_mpc52xx.h"
44
45#define DRIVER_NAME "mpc52xx-fec"
46
Grant Likely80791be2008-05-01 11:05:58 -060047/* Private driver data structure */
48struct mpc52xx_fec_priv {
Grant Likelyca816d92009-04-25 12:53:02 +000049 struct net_device *ndev;
Grant Likely80791be2008-05-01 11:05:58 -060050 int duplex;
51 int speed;
52 int r_irq;
53 int t_irq;
54 struct mpc52xx_fec __iomem *fec;
55 struct bcom_task *rx_dmatsk;
56 struct bcom_task *tx_dmatsk;
57 spinlock_t lock;
58 int msg_enable;
59
60 /* MDIO link details */
Grant Likelyca816d92009-04-25 12:53:02 +000061 unsigned int mdio_speed;
62 struct device_node *phy_node;
Grant Likely80791be2008-05-01 11:05:58 -060063 struct phy_device *phydev;
64 enum phy_state link;
Grant Likelyca816d92009-04-25 12:53:02 +000065 int seven_wire_mode;
Grant Likely80791be2008-05-01 11:05:58 -060066};
67
68
Domen Puncer5d031e92007-10-26 18:07:49 +020069static irqreturn_t mpc52xx_fec_interrupt(int, void *);
70static irqreturn_t mpc52xx_fec_rx_interrupt(int, void *);
71static irqreturn_t mpc52xx_fec_tx_interrupt(int, void *);
72static void mpc52xx_fec_stop(struct net_device *dev);
73static void mpc52xx_fec_start(struct net_device *dev);
74static void mpc52xx_fec_reset(struct net_device *dev);
75
76static u8 mpc52xx_fec_mac_addr[6];
77module_param_array_named(mac, mpc52xx_fec_mac_addr, byte, NULL, 0);
78MODULE_PARM_DESC(mac, "six hex digits, ie. 0x1,0x2,0xc0,0x01,0xba,0xbe");
79
80#define MPC52xx_MESSAGES_DEFAULT ( NETIF_MSG_DRV | NETIF_MSG_PROBE | \
Roel Kluin8b983512008-06-09 16:33:51 -070081 NETIF_MSG_LINK | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP)
Domen Puncer5d031e92007-10-26 18:07:49 +020082static int debug = -1; /* the above default */
83module_param(debug, int, 0);
84MODULE_PARM_DESC(debug, "debugging messages level");
85
86static void mpc52xx_fec_tx_timeout(struct net_device *dev)
87{
Asier Llano1e4e0762009-12-08 20:29:10 -080088 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
89 unsigned long flags;
90
Domen Puncer5d031e92007-10-26 18:07:49 +020091 dev_warn(&dev->dev, "transmit timed out\n");
92
Asier Llano1e4e0762009-12-08 20:29:10 -080093 spin_lock_irqsave(&priv->lock, flags);
Domen Puncer5d031e92007-10-26 18:07:49 +020094 mpc52xx_fec_reset(dev);
Domen Puncer5d031e92007-10-26 18:07:49 +020095 dev->stats.tx_errors++;
Asier Llano1e4e0762009-12-08 20:29:10 -080096 spin_unlock_irqrestore(&priv->lock, flags);
Domen Puncer5d031e92007-10-26 18:07:49 +020097
98 netif_wake_queue(dev);
99}
100
101static void mpc52xx_fec_set_paddr(struct net_device *dev, u8 *mac)
102{
103 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
104 struct mpc52xx_fec __iomem *fec = priv->fec;
105
106 out_be32(&fec->paddr1, *(u32 *)(&mac[0]));
107 out_be32(&fec->paddr2, (*(u16 *)(&mac[4]) << 16) | FEC_PADDR2_TYPE);
108}
109
110static void mpc52xx_fec_get_paddr(struct net_device *dev, u8 *mac)
111{
112 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
113 struct mpc52xx_fec __iomem *fec = priv->fec;
114
115 *(u32 *)(&mac[0]) = in_be32(&fec->paddr1);
116 *(u16 *)(&mac[4]) = in_be32(&fec->paddr2) >> 16;
117}
118
119static int mpc52xx_fec_set_mac_address(struct net_device *dev, void *addr)
120{
121 struct sockaddr *sock = addr;
122
123 memcpy(dev->dev_addr, sock->sa_data, dev->addr_len);
124
125 mpc52xx_fec_set_paddr(dev, sock->sa_data);
126 return 0;
127}
128
129static void mpc52xx_fec_free_rx_buffers(struct net_device *dev, struct bcom_task *s)
130{
131 while (!bcom_queue_empty(s)) {
132 struct bcom_fec_bd *bd;
133 struct sk_buff *skb;
134
135 skb = bcom_retrieve_buffer(s, NULL, (struct bcom_bd **)&bd);
Grant Likely461cadb2009-03-31 20:16:52 +0000136 dma_unmap_single(dev->dev.parent, bd->skb_pa, skb->len,
137 DMA_FROM_DEVICE);
Domen Puncer5d031e92007-10-26 18:07:49 +0200138 kfree_skb(skb);
139 }
140}
141
Asier Llano1e4e0762009-12-08 20:29:10 -0800142static void
143mpc52xx_fec_rx_submit(struct net_device *dev, struct sk_buff *rskb)
144{
145 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
146 struct bcom_fec_bd *bd;
147
148 bd = (struct bcom_fec_bd *) bcom_prepare_next_buffer(priv->rx_dmatsk);
149 bd->status = FEC_RX_BUFFER_SIZE;
150 bd->skb_pa = dma_map_single(dev->dev.parent, rskb->data,
151 FEC_RX_BUFFER_SIZE, DMA_FROM_DEVICE);
152 bcom_submit_next_buffer(priv->rx_dmatsk, rskb);
153}
154
Domen Puncer5d031e92007-10-26 18:07:49 +0200155static int mpc52xx_fec_alloc_rx_buffers(struct net_device *dev, struct bcom_task *rxtsk)
156{
Asier Llano1e4e0762009-12-08 20:29:10 -0800157 struct sk_buff *skb;
Domen Puncer5d031e92007-10-26 18:07:49 +0200158
Asier Llano1e4e0762009-12-08 20:29:10 -0800159 while (!bcom_queue_full(rxtsk)) {
Domen Puncer5d031e92007-10-26 18:07:49 +0200160 skb = dev_alloc_skb(FEC_RX_BUFFER_SIZE);
Asier Llano1e4e0762009-12-08 20:29:10 -0800161 if (!skb)
Domen Puncer5d031e92007-10-26 18:07:49 +0200162 return -EAGAIN;
163
164 /* zero out the initial receive buffers to aid debugging */
165 memset(skb->data, 0, FEC_RX_BUFFER_SIZE);
Asier Llano1e4e0762009-12-08 20:29:10 -0800166 mpc52xx_fec_rx_submit(dev, skb);
Domen Puncer5d031e92007-10-26 18:07:49 +0200167 }
Domen Puncer5d031e92007-10-26 18:07:49 +0200168 return 0;
169}
170
171/* based on generic_adjust_link from fs_enet-main.c */
172static void mpc52xx_fec_adjust_link(struct net_device *dev)
173{
174 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
175 struct phy_device *phydev = priv->phydev;
176 int new_state = 0;
177
178 if (phydev->link != PHY_DOWN) {
179 if (phydev->duplex != priv->duplex) {
180 struct mpc52xx_fec __iomem *fec = priv->fec;
181 u32 rcntrl;
182 u32 tcntrl;
183
184 new_state = 1;
185 priv->duplex = phydev->duplex;
186
187 rcntrl = in_be32(&fec->r_cntrl);
188 tcntrl = in_be32(&fec->x_cntrl);
189
190 rcntrl &= ~FEC_RCNTRL_DRT;
191 tcntrl &= ~FEC_TCNTRL_FDEN;
192 if (phydev->duplex == DUPLEX_FULL)
193 tcntrl |= FEC_TCNTRL_FDEN; /* FD enable */
194 else
195 rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
196
197 out_be32(&fec->r_cntrl, rcntrl);
198 out_be32(&fec->x_cntrl, tcntrl);
199 }
200
201 if (phydev->speed != priv->speed) {
202 new_state = 1;
203 priv->speed = phydev->speed;
204 }
205
206 if (priv->link == PHY_DOWN) {
207 new_state = 1;
208 priv->link = phydev->link;
Domen Puncer5d031e92007-10-26 18:07:49 +0200209 }
210
211 } else if (priv->link) {
212 new_state = 1;
213 priv->link = PHY_DOWN;
214 priv->speed = 0;
215 priv->duplex = -1;
Domen Puncer5d031e92007-10-26 18:07:49 +0200216 }
217
218 if (new_state && netif_msg_link(priv))
219 phy_print_status(phydev);
220}
221
Domen Puncer5d031e92007-10-26 18:07:49 +0200222static int mpc52xx_fec_open(struct net_device *dev)
223{
224 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
225 int err = -EBUSY;
226
Grant Likelyca816d92009-04-25 12:53:02 +0000227 if (priv->phy_node) {
228 priv->phydev = of_phy_connect(priv->ndev, priv->phy_node,
229 mpc52xx_fec_adjust_link, 0, 0);
230 if (!priv->phydev) {
231 dev_err(&dev->dev, "of_phy_connect failed\n");
232 return -ENODEV;
233 }
234 phy_start(priv->phydev);
235 }
236
Joe Perchesa0607fd2009-11-18 23:29:17 -0800237 if (request_irq(dev->irq, mpc52xx_fec_interrupt, IRQF_SHARED,
Domen Puncer5d031e92007-10-26 18:07:49 +0200238 DRIVER_NAME "_ctrl", dev)) {
239 dev_err(&dev->dev, "ctrl interrupt request failed\n");
Grant Likelyca816d92009-04-25 12:53:02 +0000240 goto free_phy;
Domen Puncer5d031e92007-10-26 18:07:49 +0200241 }
Joe Perchesa0607fd2009-11-18 23:29:17 -0800242 if (request_irq(priv->r_irq, mpc52xx_fec_rx_interrupt, 0,
Domen Puncer5d031e92007-10-26 18:07:49 +0200243 DRIVER_NAME "_rx", dev)) {
244 dev_err(&dev->dev, "rx interrupt request failed\n");
245 goto free_ctrl_irq;
246 }
Joe Perchesa0607fd2009-11-18 23:29:17 -0800247 if (request_irq(priv->t_irq, mpc52xx_fec_tx_interrupt, 0,
Domen Puncer5d031e92007-10-26 18:07:49 +0200248 DRIVER_NAME "_tx", dev)) {
249 dev_err(&dev->dev, "tx interrupt request failed\n");
250 goto free_2irqs;
251 }
252
253 bcom_fec_rx_reset(priv->rx_dmatsk);
254 bcom_fec_tx_reset(priv->tx_dmatsk);
255
256 err = mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
257 if (err) {
258 dev_err(&dev->dev, "mpc52xx_fec_alloc_rx_buffers failed\n");
259 goto free_irqs;
260 }
261
Domen Puncer5d031e92007-10-26 18:07:49 +0200262 bcom_enable(priv->rx_dmatsk);
263 bcom_enable(priv->tx_dmatsk);
264
265 mpc52xx_fec_start(dev);
266
267 netif_start_queue(dev);
268
269 return 0;
270
Domen Puncer5d031e92007-10-26 18:07:49 +0200271 free_irqs:
272 free_irq(priv->t_irq, dev);
273 free_2irqs:
274 free_irq(priv->r_irq, dev);
275 free_ctrl_irq:
276 free_irq(dev->irq, dev);
Grant Likelyca816d92009-04-25 12:53:02 +0000277 free_phy:
278 if (priv->phydev) {
279 phy_stop(priv->phydev);
280 phy_disconnect(priv->phydev);
281 priv->phydev = NULL;
282 }
Domen Puncer5d031e92007-10-26 18:07:49 +0200283
284 return err;
285}
286
287static int mpc52xx_fec_close(struct net_device *dev)
288{
289 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
290
291 netif_stop_queue(dev);
292
293 mpc52xx_fec_stop(dev);
294
295 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
296
297 free_irq(dev->irq, dev);
298 free_irq(priv->r_irq, dev);
299 free_irq(priv->t_irq, dev);
300
Grant Likelyca816d92009-04-25 12:53:02 +0000301 if (priv->phydev) {
302 /* power down phy */
303 phy_stop(priv->phydev);
304 phy_disconnect(priv->phydev);
305 priv->phydev = NULL;
306 }
Domen Puncer5d031e92007-10-26 18:07:49 +0200307
308 return 0;
309}
310
311/* This will only be invoked if your driver is _not_ in XOFF state.
312 * What this means is that you need not check it, and that this
313 * invariant will hold if you make sure that the netif_*_queue()
314 * calls are done at the proper times.
315 */
Henk Stegemand3600092009-03-31 20:16:57 +0000316static int mpc52xx_fec_start_xmit(struct sk_buff *skb, struct net_device *dev)
Domen Puncer5d031e92007-10-26 18:07:49 +0200317{
318 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
319 struct bcom_fec_bd *bd;
Dongdong Deng48719532009-08-23 19:49:07 -0700320 unsigned long flags;
Domen Puncer5d031e92007-10-26 18:07:49 +0200321
322 if (bcom_queue_full(priv->tx_dmatsk)) {
323 if (net_ratelimit())
324 dev_err(&dev->dev, "transmit queue overrun\n");
Henk Stegemand3600092009-03-31 20:16:57 +0000325 return NETDEV_TX_BUSY;
Domen Puncer5d031e92007-10-26 18:07:49 +0200326 }
327
Dongdong Deng48719532009-08-23 19:49:07 -0700328 spin_lock_irqsave(&priv->lock, flags);
Domen Puncer5d031e92007-10-26 18:07:49 +0200329 dev->trans_start = jiffies;
330
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 */
438 rskb->dev = dev;
439 rskb->protocol = eth_type_trans(rskb, dev);
440 netif_rx(rskb);
441
442 spin_lock_irqsave(&priv->lock, flags);
Domen Puncer5d031e92007-10-26 18:07:49 +0200443 }
444
Asier Llano1e4e0762009-12-08 20:29:10 -0800445 spin_unlock_irqrestore(&priv->lock, flags);
446
Domen Puncer5d031e92007-10-26 18:07:49 +0200447 return IRQ_HANDLED;
448}
449
450static irqreturn_t mpc52xx_fec_interrupt(int irq, void *dev_id)
451{
452 struct net_device *dev = dev_id;
453 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
454 struct mpc52xx_fec __iomem *fec = priv->fec;
455 u32 ievent;
Asier Llano1e4e0762009-12-08 20:29:10 -0800456 unsigned long flags;
Domen Puncer5d031e92007-10-26 18:07:49 +0200457
458 ievent = in_be32(&fec->ievent);
459
460 ievent &= ~FEC_IEVENT_MII; /* mii is handled separately */
461 if (!ievent)
462 return IRQ_NONE;
463
464 out_be32(&fec->ievent, ievent); /* clear pending events */
465
Sascha Hauer8f3ba2d2008-04-16 01:44:39 +1000466 /* on fifo error, soft-reset fec */
467 if (ievent & (FEC_IEVENT_RFIFO_ERROR | FEC_IEVENT_XFIFO_ERROR)) {
468
469 if (net_ratelimit() && (ievent & FEC_IEVENT_RFIFO_ERROR))
470 dev_warn(&dev->dev, "FEC_IEVENT_RFIFO_ERROR\n");
471 if (net_ratelimit() && (ievent & FEC_IEVENT_XFIFO_ERROR))
472 dev_warn(&dev->dev, "FEC_IEVENT_XFIFO_ERROR\n");
473
Asier Llano1e4e0762009-12-08 20:29:10 -0800474 spin_lock_irqsave(&priv->lock, flags);
Sascha Hauer8f3ba2d2008-04-16 01:44:39 +1000475 mpc52xx_fec_reset(dev);
Asier Llano1e4e0762009-12-08 20:29:10 -0800476 spin_unlock_irqrestore(&priv->lock, flags);
Sascha Hauer8f3ba2d2008-04-16 01:44:39 +1000477
Domen Puncer5d031e92007-10-26 18:07:49 +0200478 return IRQ_HANDLED;
479 }
480
Sascha Hauer8f3ba2d2008-04-16 01:44:39 +1000481 if (ievent & ~FEC_IEVENT_TFINT)
482 dev_dbg(&dev->dev, "ievent: %08x\n", ievent);
Domen Puncer5d031e92007-10-26 18:07:49 +0200483
Domen Puncer5d031e92007-10-26 18:07:49 +0200484 return IRQ_HANDLED;
485}
486
487/*
488 * Get the current statistics.
489 * This may be called with the card open or closed.
490 */
491static struct net_device_stats *mpc52xx_fec_get_stats(struct net_device *dev)
492{
493 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
494 struct net_device_stats *stats = &dev->stats;
495 struct mpc52xx_fec __iomem *fec = priv->fec;
496
497 stats->rx_bytes = in_be32(&fec->rmon_r_octets);
498 stats->rx_packets = in_be32(&fec->rmon_r_packets);
499 stats->rx_errors = in_be32(&fec->rmon_r_crc_align) +
500 in_be32(&fec->rmon_r_undersize) +
501 in_be32(&fec->rmon_r_oversize) +
502 in_be32(&fec->rmon_r_frag) +
503 in_be32(&fec->rmon_r_jab);
504
505 stats->tx_bytes = in_be32(&fec->rmon_t_octets);
506 stats->tx_packets = in_be32(&fec->rmon_t_packets);
507 stats->tx_errors = in_be32(&fec->rmon_t_crc_align) +
508 in_be32(&fec->rmon_t_undersize) +
509 in_be32(&fec->rmon_t_oversize) +
510 in_be32(&fec->rmon_t_frag) +
511 in_be32(&fec->rmon_t_jab);
512
513 stats->multicast = in_be32(&fec->rmon_r_mc_pkt);
514 stats->collisions = in_be32(&fec->rmon_t_col);
515
516 /* detailed rx_errors: */
517 stats->rx_length_errors = in_be32(&fec->rmon_r_undersize)
518 + in_be32(&fec->rmon_r_oversize)
519 + in_be32(&fec->rmon_r_frag)
520 + in_be32(&fec->rmon_r_jab);
521 stats->rx_over_errors = in_be32(&fec->r_macerr);
522 stats->rx_crc_errors = in_be32(&fec->ieee_r_crc);
523 stats->rx_frame_errors = in_be32(&fec->ieee_r_align);
524 stats->rx_fifo_errors = in_be32(&fec->rmon_r_drop);
525 stats->rx_missed_errors = in_be32(&fec->rmon_r_drop);
526
527 /* detailed tx_errors: */
528 stats->tx_aborted_errors = 0;
529 stats->tx_carrier_errors = in_be32(&fec->ieee_t_cserr);
530 stats->tx_fifo_errors = in_be32(&fec->rmon_t_drop);
531 stats->tx_heartbeat_errors = in_be32(&fec->ieee_t_sqe);
532 stats->tx_window_errors = in_be32(&fec->ieee_t_lcol);
533
534 return stats;
535}
536
537/*
538 * Read MIB counters in order to reset them,
539 * then zero all the stats fields in memory
540 */
541static void mpc52xx_fec_reset_stats(struct net_device *dev)
542{
543 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
544 struct mpc52xx_fec __iomem *fec = priv->fec;
545
546 out_be32(&fec->mib_control, FEC_MIB_DISABLE);
Al Virocc154ac2007-12-22 18:56:53 +0000547 memset_io(&fec->rmon_t_drop, 0,
548 offsetof(struct mpc52xx_fec, reserved10) -
549 offsetof(struct mpc52xx_fec, rmon_t_drop));
Domen Puncer5d031e92007-10-26 18:07:49 +0200550 out_be32(&fec->mib_control, 0);
551
552 memset(&dev->stats, 0, sizeof(dev->stats));
553}
554
555/*
556 * Set or clear the multicast filter for this adaptor.
557 */
558static void mpc52xx_fec_set_multicast_list(struct net_device *dev)
559{
560 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
561 struct mpc52xx_fec __iomem *fec = priv->fec;
562 u32 rx_control;
563
564 rx_control = in_be32(&fec->r_cntrl);
565
566 if (dev->flags & IFF_PROMISC) {
567 rx_control |= FEC_RCNTRL_PROM;
568 out_be32(&fec->r_cntrl, rx_control);
569 } else {
570 rx_control &= ~FEC_RCNTRL_PROM;
571 out_be32(&fec->r_cntrl, rx_control);
572
573 if (dev->flags & IFF_ALLMULTI) {
574 out_be32(&fec->gaddr1, 0xffffffff);
575 out_be32(&fec->gaddr2, 0xffffffff);
576 } else {
577 u32 crc;
578 int i;
579 struct dev_mc_list *dmi;
580 u32 gaddr1 = 0x00000000;
581 u32 gaddr2 = 0x00000000;
582
583 dmi = dev->mc_list;
584 for (i=0; i<dev->mc_count; i++) {
585 crc = ether_crc_le(6, dmi->dmi_addr) >> 26;
586 if (crc >= 32)
587 gaddr1 |= 1 << (crc-32);
588 else
589 gaddr2 |= 1 << crc;
590 dmi = dmi->next;
591 }
592 out_be32(&fec->gaddr1, gaddr1);
593 out_be32(&fec->gaddr2, gaddr2);
594 }
595 }
596}
597
598/**
599 * mpc52xx_fec_hw_init
600 * @dev: network device
601 *
602 * Setup various hardware setting, only needed once on start
603 */
604static void mpc52xx_fec_hw_init(struct net_device *dev)
605{
606 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
607 struct mpc52xx_fec __iomem *fec = priv->fec;
608 int i;
609
610 /* Whack a reset. We should wait for this. */
611 out_be32(&fec->ecntrl, FEC_ECNTRL_RESET);
612 for (i = 0; i < FEC_RESET_DELAY; ++i) {
613 if ((in_be32(&fec->ecntrl) & FEC_ECNTRL_RESET) == 0)
614 break;
615 udelay(1);
616 }
617 if (i == FEC_RESET_DELAY)
618 dev_err(&dev->dev, "FEC Reset timeout!\n");
619
620 /* set pause to 0x20 frames */
621 out_be32(&fec->op_pause, FEC_OP_PAUSE_OPCODE | 0x20);
622
623 /* high service request will be deasserted when there's < 7 bytes in fifo
624 * low service request will be deasserted when there's < 4*7 bytes in fifo
625 */
626 out_be32(&fec->rfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
627 out_be32(&fec->tfifo_cntrl, FEC_FIFO_CNTRL_FRAME | FEC_FIFO_CNTRL_LTG_7);
628
629 /* alarm when <= x bytes in FIFO */
630 out_be32(&fec->rfifo_alarm, 0x0000030c);
631 out_be32(&fec->tfifo_alarm, 0x00000100);
632
633 /* begin transmittion when 256 bytes are in FIFO (or EOF or FIFO full) */
634 out_be32(&fec->x_wmrk, FEC_FIFO_WMRK_256B);
635
636 /* enable crc generation */
637 out_be32(&fec->xmit_fsm, FEC_XMIT_FSM_APPEND_CRC | FEC_XMIT_FSM_ENABLE_CRC);
638 out_be32(&fec->iaddr1, 0x00000000); /* No individual filter */
639 out_be32(&fec->iaddr2, 0x00000000); /* No individual filter */
640
641 /* set phy speed.
642 * this can't be done in phy driver, since it needs to be called
643 * before fec stuff (even on resume) */
Grant Likelyca816d92009-04-25 12:53:02 +0000644 out_be32(&fec->mii_speed, priv->mdio_speed);
Domen Puncer5d031e92007-10-26 18:07:49 +0200645}
646
647/**
648 * mpc52xx_fec_start
649 * @dev: network device
650 *
651 * This function is called to start or restart the FEC during a link
652 * change. This happens on fifo errors or when switching between half
653 * and full duplex.
654 */
655static void mpc52xx_fec_start(struct net_device *dev)
656{
657 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
658 struct mpc52xx_fec __iomem *fec = priv->fec;
659 u32 rcntrl;
660 u32 tcntrl;
661 u32 tmp;
662
663 /* clear sticky error bits */
664 tmp = FEC_FIFO_STATUS_ERR | FEC_FIFO_STATUS_UF | FEC_FIFO_STATUS_OF;
665 out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status) & tmp);
666 out_be32(&fec->tfifo_status, in_be32(&fec->tfifo_status) & tmp);
667
668 /* FIFOs will reset on mpc52xx_fec_enable */
669 out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_ENABLE_IS_RESET);
670
671 /* Set station address. */
672 mpc52xx_fec_set_paddr(dev, dev->dev_addr);
673
674 mpc52xx_fec_set_multicast_list(dev);
675
676 /* set max frame len, enable flow control, select mii mode */
677 rcntrl = FEC_RX_BUFFER_SIZE << 16; /* max frame length */
678 rcntrl |= FEC_RCNTRL_FCE;
679
Grant Likelyca816d92009-04-25 12:53:02 +0000680 if (!priv->seven_wire_mode)
Domen Puncer5d031e92007-10-26 18:07:49 +0200681 rcntrl |= FEC_RCNTRL_MII_MODE;
682
683 if (priv->duplex == DUPLEX_FULL)
684 tcntrl = FEC_TCNTRL_FDEN; /* FD enable */
685 else {
686 rcntrl |= FEC_RCNTRL_DRT; /* disable Rx on Tx (HD) */
687 tcntrl = 0;
688 }
689 out_be32(&fec->r_cntrl, rcntrl);
690 out_be32(&fec->x_cntrl, tcntrl);
691
692 /* Clear any outstanding interrupt. */
693 out_be32(&fec->ievent, 0xffffffff);
694
695 /* Enable interrupts we wish to service. */
696 out_be32(&fec->imask, FEC_IMASK_ENABLE);
697
698 /* And last, enable the transmit and receive processing. */
699 out_be32(&fec->ecntrl, FEC_ECNTRL_ETHER_EN);
700 out_be32(&fec->r_des_active, 0x01000000);
701}
702
703/**
704 * mpc52xx_fec_stop
705 * @dev: network device
706 *
707 * stop all activity on fec and empty dma buffers
708 */
709static void mpc52xx_fec_stop(struct net_device *dev)
710{
711 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
712 struct mpc52xx_fec __iomem *fec = priv->fec;
713 unsigned long timeout;
714
715 /* disable all interrupts */
716 out_be32(&fec->imask, 0);
717
718 /* Disable the rx task. */
719 bcom_disable(priv->rx_dmatsk);
720
721 /* Wait for tx queue to drain, but only if we're in process context */
722 if (!in_interrupt()) {
723 timeout = jiffies + msecs_to_jiffies(2000);
724 while (time_before(jiffies, timeout) &&
725 !bcom_queue_empty(priv->tx_dmatsk))
726 msleep(100);
727
728 if (time_after_eq(jiffies, timeout))
729 dev_err(&dev->dev, "queues didn't drain\n");
730#if 1
731 if (time_after_eq(jiffies, timeout)) {
732 dev_err(&dev->dev, " tx: index: %i, outdex: %i\n",
733 priv->tx_dmatsk->index,
734 priv->tx_dmatsk->outdex);
735 dev_err(&dev->dev, " rx: index: %i, outdex: %i\n",
736 priv->rx_dmatsk->index,
737 priv->rx_dmatsk->outdex);
738 }
739#endif
740 }
741
742 bcom_disable(priv->tx_dmatsk);
743
744 /* Stop FEC */
745 out_be32(&fec->ecntrl, in_be32(&fec->ecntrl) & ~FEC_ECNTRL_ETHER_EN);
Domen Puncer5d031e92007-10-26 18:07:49 +0200746}
747
748/* reset fec and bestcomm tasks */
749static void mpc52xx_fec_reset(struct net_device *dev)
750{
751 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
752 struct mpc52xx_fec __iomem *fec = priv->fec;
753
754 mpc52xx_fec_stop(dev);
755
756 out_be32(&fec->rfifo_status, in_be32(&fec->rfifo_status));
757 out_be32(&fec->reset_cntrl, FEC_RESET_CNTRL_RESET_FIFO);
758
759 mpc52xx_fec_free_rx_buffers(dev, priv->rx_dmatsk);
760
761 mpc52xx_fec_hw_init(dev);
762
Domen Puncer5d031e92007-10-26 18:07:49 +0200763 bcom_fec_rx_reset(priv->rx_dmatsk);
764 bcom_fec_tx_reset(priv->tx_dmatsk);
765
766 mpc52xx_fec_alloc_rx_buffers(dev, priv->rx_dmatsk);
767
768 bcom_enable(priv->rx_dmatsk);
769 bcom_enable(priv->tx_dmatsk);
770
771 mpc52xx_fec_start(dev);
Asier Llano1e4e0762009-12-08 20:29:10 -0800772
773 netif_wake_queue(dev);
Domen Puncer5d031e92007-10-26 18:07:49 +0200774}
775
776
777/* ethtool interface */
778static void mpc52xx_fec_get_drvinfo(struct net_device *dev,
779 struct ethtool_drvinfo *info)
780{
781 strcpy(info->driver, DRIVER_NAME);
782}
783
784static int mpc52xx_fec_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
785{
786 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
Grant Likely9404c822009-03-31 20:17:03 +0000787
788 if (!priv->phydev)
789 return -ENODEV;
790
Domen Puncer5d031e92007-10-26 18:07:49 +0200791 return phy_ethtool_gset(priv->phydev, cmd);
792}
793
794static int mpc52xx_fec_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
795{
796 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
Grant Likely9404c822009-03-31 20:17:03 +0000797
798 if (!priv->phydev)
799 return -ENODEV;
800
Domen Puncer5d031e92007-10-26 18:07:49 +0200801 return phy_ethtool_sset(priv->phydev, cmd);
802}
803
804static u32 mpc52xx_fec_get_msglevel(struct net_device *dev)
805{
806 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
807 return priv->msg_enable;
808}
809
810static void mpc52xx_fec_set_msglevel(struct net_device *dev, u32 level)
811{
812 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
813 priv->msg_enable = level;
814}
815
816static const struct ethtool_ops mpc52xx_fec_ethtool_ops = {
817 .get_drvinfo = mpc52xx_fec_get_drvinfo,
818 .get_settings = mpc52xx_fec_get_settings,
819 .set_settings = mpc52xx_fec_set_settings,
820 .get_link = ethtool_op_get_link,
821 .get_msglevel = mpc52xx_fec_get_msglevel,
822 .set_msglevel = mpc52xx_fec_set_msglevel,
823};
824
825
826static int mpc52xx_fec_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
827{
828 struct mpc52xx_fec_priv *priv = netdev_priv(dev);
829
Grant Likely9404c822009-03-31 20:17:03 +0000830 if (!priv->phydev)
831 return -ENOTSUPP;
832
833 return phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
Domen Puncer5d031e92007-10-26 18:07:49 +0200834}
835
Henk Stegemand3600092009-03-31 20:16:57 +0000836static const struct net_device_ops mpc52xx_fec_netdev_ops = {
837 .ndo_open = mpc52xx_fec_open,
838 .ndo_stop = mpc52xx_fec_close,
839 .ndo_start_xmit = mpc52xx_fec_start_xmit,
840 .ndo_set_multicast_list = mpc52xx_fec_set_multicast_list,
841 .ndo_set_mac_address = mpc52xx_fec_set_mac_address,
842 .ndo_validate_addr = eth_validate_addr,
843 .ndo_do_ioctl = mpc52xx_fec_ioctl,
844 .ndo_change_mtu = eth_change_mtu,
845 .ndo_tx_timeout = mpc52xx_fec_tx_timeout,
846 .ndo_get_stats = mpc52xx_fec_get_stats,
847#ifdef CONFIG_NET_POLL_CONTROLLER
848 .ndo_poll_controller = mpc52xx_fec_poll_controller,
849#endif
850};
851
Domen Puncer5d031e92007-10-26 18:07:49 +0200852/* ======================================================================== */
853/* OF Driver */
854/* ======================================================================== */
855
856static int __devinit
857mpc52xx_fec_probe(struct of_device *op, const struct of_device_id *match)
858{
859 int rv;
860 struct net_device *ndev;
861 struct mpc52xx_fec_priv *priv = NULL;
862 struct resource mem;
Grant Likely80791be2008-05-01 11:05:58 -0600863 const u32 *prop;
864 int prop_size;
Domen Puncer5d031e92007-10-26 18:07:49 +0200865
866 phys_addr_t rx_fifo;
867 phys_addr_t tx_fifo;
868
869 /* Get the ether ndev & it's private zone */
870 ndev = alloc_etherdev(sizeof(struct mpc52xx_fec_priv));
871 if (!ndev)
872 return -ENOMEM;
873
874 priv = netdev_priv(ndev);
Grant Likelyca816d92009-04-25 12:53:02 +0000875 priv->ndev = ndev;
Domen Puncer5d031e92007-10-26 18:07:49 +0200876
877 /* Reserve FEC control zone */
878 rv = of_address_to_resource(op->node, 0, &mem);
879 if (rv) {
880 printk(KERN_ERR DRIVER_NAME ": "
881 "Error while parsing device node resource\n" );
882 return rv;
883 }
Grant Likely48d58452007-11-01 08:22:35 -0600884 if ((mem.end - mem.start + 1) < sizeof(struct mpc52xx_fec)) {
Domen Puncer5d031e92007-10-26 18:07:49 +0200885 printk(KERN_ERR DRIVER_NAME
Grant Likely48d58452007-11-01 08:22:35 -0600886 " - invalid resource size (%lx < %x), check mpc52xx_devices.c\n",
Domen Puncer5d031e92007-10-26 18:07:49 +0200887 (unsigned long)(mem.end - mem.start + 1), sizeof(struct mpc52xx_fec));
888 return -EINVAL;
889 }
890
891 if (!request_mem_region(mem.start, sizeof(struct mpc52xx_fec), DRIVER_NAME))
892 return -EBUSY;
893
894 /* Init ether ndev with what we have */
Henk Stegemand3600092009-03-31 20:16:57 +0000895 ndev->netdev_ops = &mpc52xx_fec_netdev_ops;
Domen Puncer5d031e92007-10-26 18:07:49 +0200896 ndev->ethtool_ops = &mpc52xx_fec_ethtool_ops;
Domen Puncer5d031e92007-10-26 18:07:49 +0200897 ndev->watchdog_timeo = FEC_WATCHDOG_TIMEOUT;
898 ndev->base_addr = mem.start;
Grant Likelyca816d92009-04-25 12:53:02 +0000899 SET_NETDEV_DEV(ndev, &op->dev);
Domen Puncer5d031e92007-10-26 18:07:49 +0200900
901 spin_lock_init(&priv->lock);
902
903 /* ioremap the zones */
904 priv->fec = ioremap(mem.start, sizeof(struct mpc52xx_fec));
905
906 if (!priv->fec) {
907 rv = -ENOMEM;
908 goto probe_error;
909 }
910
911 /* Bestcomm init */
912 rx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, rfifo_data);
913 tx_fifo = ndev->base_addr + offsetof(struct mpc52xx_fec, tfifo_data);
914
915 priv->rx_dmatsk = bcom_fec_rx_init(FEC_RX_NUM_BD, rx_fifo, FEC_RX_BUFFER_SIZE);
916 priv->tx_dmatsk = bcom_fec_tx_init(FEC_TX_NUM_BD, tx_fifo);
917
918 if (!priv->rx_dmatsk || !priv->tx_dmatsk) {
919 printk(KERN_ERR DRIVER_NAME ": Can not init SDMA tasks\n" );
920 rv = -ENOMEM;
921 goto probe_error;
922 }
923
924 /* Get the IRQ we need one by one */
925 /* Control */
926 ndev->irq = irq_of_parse_and_map(op->node, 0);
927
928 /* RX */
929 priv->r_irq = bcom_get_task_irq(priv->rx_dmatsk);
930
931 /* TX */
932 priv->t_irq = bcom_get_task_irq(priv->tx_dmatsk);
933
934 /* MAC address init */
935 if (!is_zero_ether_addr(mpc52xx_fec_mac_addr))
936 memcpy(ndev->dev_addr, mpc52xx_fec_mac_addr, 6);
937 else
938 mpc52xx_fec_get_paddr(ndev, ndev->dev_addr);
939
940 priv->msg_enable = netif_msg_init(debug, MPC52xx_MESSAGES_DEFAULT);
Domen Puncer5d031e92007-10-26 18:07:49 +0200941
Grant Likely80791be2008-05-01 11:05:58 -0600942 /*
943 * Link mode configuration
944 */
Domen Puncer5d031e92007-10-26 18:07:49 +0200945
Grant Likely80791be2008-05-01 11:05:58 -0600946 /* Start with safe defaults for link connection */
Grant Likely80791be2008-05-01 11:05:58 -0600947 priv->speed = 100;
948 priv->duplex = DUPLEX_HALF;
Benjamin Herrenschmidtb71a1072009-06-18 11:22:08 +1000949 priv->mdio_speed = ((mpc5xxx_get_bus_frequency(op->node) >> 20) / 5) << 1;
Domen Puncer5d031e92007-10-26 18:07:49 +0200950
Grant Likely80791be2008-05-01 11:05:58 -0600951 /* The current speed preconfigures the speed of the MII link */
952 prop = of_get_property(op->node, "current-speed", &prop_size);
953 if (prop && (prop_size >= sizeof(u32) * 2)) {
954 priv->speed = prop[0];
955 priv->duplex = prop[1] ? DUPLEX_FULL : DUPLEX_HALF;
956 }
957
Grant Likelyca816d92009-04-25 12:53:02 +0000958 /* If there is a phy handle, then get the PHY node */
959 priv->phy_node = of_parse_phandle(op->node, "phy-handle", 0);
960
961 /* the 7-wire property means don't use MII mode */
962 if (of_find_property(op->node, "fsl,7-wire-mode", NULL)) {
963 priv->seven_wire_mode = 1;
964 dev_info(&ndev->dev, "using 7-wire PHY mode\n");
Domen Puncer5d031e92007-10-26 18:07:49 +0200965 }
966
967 /* Hardware init */
968 mpc52xx_fec_hw_init(ndev);
Domen Puncer5d031e92007-10-26 18:07:49 +0200969 mpc52xx_fec_reset_stats(ndev);
970
Domen Puncer5d031e92007-10-26 18:07:49 +0200971 rv = register_netdev(ndev);
972 if (rv < 0)
973 goto probe_error;
974
975 /* We're done ! */
976 dev_set_drvdata(&op->dev, ndev);
977
978 return 0;
979
980
981 /* Error handling - free everything that might be allocated */
982probe_error:
983
Grant Likelyca816d92009-04-25 12:53:02 +0000984 if (priv->phy_node)
985 of_node_put(priv->phy_node);
986 priv->phy_node = NULL;
987
Domen Puncer5d031e92007-10-26 18:07:49 +0200988 irq_dispose_mapping(ndev->irq);
989
990 if (priv->rx_dmatsk)
991 bcom_fec_rx_release(priv->rx_dmatsk);
992 if (priv->tx_dmatsk)
993 bcom_fec_tx_release(priv->tx_dmatsk);
994
995 if (priv->fec)
996 iounmap(priv->fec);
997
998 release_mem_region(mem.start, sizeof(struct mpc52xx_fec));
999
1000 free_netdev(ndev);
1001
1002 return rv;
1003}
1004
1005static int
1006mpc52xx_fec_remove(struct of_device *op)
1007{
1008 struct net_device *ndev;
1009 struct mpc52xx_fec_priv *priv;
1010
1011 ndev = dev_get_drvdata(&op->dev);
1012 priv = netdev_priv(ndev);
1013
1014 unregister_netdev(ndev);
1015
Grant Likelyca816d92009-04-25 12:53:02 +00001016 if (priv->phy_node)
1017 of_node_put(priv->phy_node);
1018 priv->phy_node = NULL;
1019
Domen Puncer5d031e92007-10-26 18:07:49 +02001020 irq_dispose_mapping(ndev->irq);
1021
1022 bcom_fec_rx_release(priv->rx_dmatsk);
1023 bcom_fec_tx_release(priv->tx_dmatsk);
1024
1025 iounmap(priv->fec);
1026
1027 release_mem_region(ndev->base_addr, sizeof(struct mpc52xx_fec));
1028
1029 free_netdev(ndev);
1030
1031 dev_set_drvdata(&op->dev, NULL);
1032 return 0;
1033}
1034
1035#ifdef CONFIG_PM
1036static int mpc52xx_fec_of_suspend(struct of_device *op, pm_message_t state)
1037{
1038 struct net_device *dev = dev_get_drvdata(&op->dev);
1039
1040 if (netif_running(dev))
1041 mpc52xx_fec_close(dev);
1042
1043 return 0;
1044}
1045
1046static int mpc52xx_fec_of_resume(struct of_device *op)
1047{
1048 struct net_device *dev = dev_get_drvdata(&op->dev);
1049
1050 mpc52xx_fec_hw_init(dev);
1051 mpc52xx_fec_reset_stats(dev);
1052
1053 if (netif_running(dev))
1054 mpc52xx_fec_open(dev);
1055
1056 return 0;
1057}
1058#endif
1059
1060static struct of_device_id mpc52xx_fec_match[] = {
Grant Likely3b5ebf82009-02-03 12:30:25 -07001061 { .compatible = "fsl,mpc5200b-fec", },
1062 { .compatible = "fsl,mpc5200-fec", },
1063 { .compatible = "mpc5200-fec", },
Domen Puncer5d031e92007-10-26 18:07:49 +02001064 { }
1065};
1066
1067MODULE_DEVICE_TABLE(of, mpc52xx_fec_match);
1068
1069static struct of_platform_driver mpc52xx_fec_driver = {
1070 .owner = THIS_MODULE,
1071 .name = DRIVER_NAME,
1072 .match_table = mpc52xx_fec_match,
1073 .probe = mpc52xx_fec_probe,
1074 .remove = mpc52xx_fec_remove,
1075#ifdef CONFIG_PM
1076 .suspend = mpc52xx_fec_of_suspend,
1077 .resume = mpc52xx_fec_of_resume,
1078#endif
1079};
1080
1081
1082/* ======================================================================== */
1083/* Module */
1084/* ======================================================================== */
1085
1086static int __init
1087mpc52xx_fec_init(void)
1088{
1089#ifdef CONFIG_FEC_MPC52xx_MDIO
1090 int ret;
1091 ret = of_register_platform_driver(&mpc52xx_fec_mdio_driver);
1092 if (ret) {
1093 printk(KERN_ERR DRIVER_NAME ": failed to register mdio driver\n");
1094 return ret;
1095 }
1096#endif
1097 return of_register_platform_driver(&mpc52xx_fec_driver);
1098}
1099
1100static void __exit
1101mpc52xx_fec_exit(void)
1102{
1103 of_unregister_platform_driver(&mpc52xx_fec_driver);
1104#ifdef CONFIG_FEC_MPC52xx_MDIO
1105 of_unregister_platform_driver(&mpc52xx_fec_mdio_driver);
1106#endif
1107}
1108
1109
1110module_init(mpc52xx_fec_init);
1111module_exit(mpc52xx_fec_exit);
1112
1113MODULE_LICENSE("GPL");
1114MODULE_AUTHOR("Dale Farnsworth");
1115MODULE_DESCRIPTION("Ethernet driver for the Freescale MPC52xx FEC");