blob: 609748b84e83e131e28d5fcb227c88fc8d62c2ef [file] [log] [blame]
Bryan Wue190d6b2007-07-17 14:43:44 +08001/*
Mike Frysinger2fb9d6f2008-01-30 16:52:24 +08002 * Blackfin On-Chip MAC Driver
Bryan Wue190d6b2007-07-17 14:43:44 +08003 *
Mike Frysinger2fb9d6f2008-01-30 16:52:24 +08004 * Copyright 2004-2007 Analog Devices Inc.
Bryan Wue190d6b2007-07-17 14:43:44 +08005 *
Mike Frysinger2fb9d6f2008-01-30 16:52:24 +08006 * Enter bugs at http://blackfin.uclinux.org/
Bryan Wue190d6b2007-07-17 14:43:44 +08007 *
Mike Frysinger2fb9d6f2008-01-30 16:52:24 +08008 * Licensed under the GPL-2 or later.
Bryan Wue190d6b2007-07-17 14:43:44 +08009 */
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/sched.h>
15#include <linux/slab.h>
16#include <linux/delay.h>
17#include <linux/timer.h>
18#include <linux/errno.h>
19#include <linux/irq.h>
20#include <linux/io.h>
21#include <linux/ioport.h>
22#include <linux/crc32.h>
23#include <linux/device.h>
24#include <linux/spinlock.h>
25#include <linux/ethtool.h>
26#include <linux/mii.h>
Bryan Wu4ae5a3a2007-09-19 23:37:36 +080027#include <linux/phy.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080028#include <linux/netdevice.h>
29#include <linux/etherdevice.h>
30#include <linux/skbuff.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080031#include <linux/platform_device.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080032
33#include <asm/dma.h>
34#include <linux/dma-mapping.h>
35
36#include <asm/blackfin.h>
37#include <asm/cacheflush.h>
38#include <asm/portmux.h>
39
40#include "bfin_mac.h"
41
42#define DRV_NAME "bfin_mac"
43#define DRV_VERSION "1.1"
44#define DRV_AUTHOR "Bryan Wu, Luke Yang"
Bryan Wu7ef0a7e2008-04-25 11:53:10 +080045#define DRV_DESC "Blackfin on-chip Ethernet MAC driver"
Bryan Wue190d6b2007-07-17 14:43:44 +080046
47MODULE_AUTHOR(DRV_AUTHOR);
48MODULE_LICENSE("GPL");
49MODULE_DESCRIPTION(DRV_DESC);
Kay Sievers72abb462008-04-18 13:50:44 -070050MODULE_ALIAS("platform:bfin_mac");
Bryan Wue190d6b2007-07-17 14:43:44 +080051
52#if defined(CONFIG_BFIN_MAC_USE_L1)
53# define bfin_mac_alloc(dma_handle, size) l1_data_sram_zalloc(size)
54# define bfin_mac_free(dma_handle, ptr) l1_data_sram_free(ptr)
55#else
56# define bfin_mac_alloc(dma_handle, size) \
57 dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL)
58# define bfin_mac_free(dma_handle, ptr) \
59 dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle)
60#endif
61
62#define PKT_BUF_SZ 1580
63
64#define MAX_TIMEOUT_CNT 500
65
66/* pointers to maintain transmit list */
67static struct net_dma_desc_tx *tx_list_head;
68static struct net_dma_desc_tx *tx_list_tail;
69static struct net_dma_desc_rx *rx_list_head;
70static struct net_dma_desc_rx *rx_list_tail;
71static struct net_dma_desc_rx *current_rx_ptr;
72static struct net_dma_desc_tx *current_tx_ptr;
73static struct net_dma_desc_tx *tx_desc;
74static struct net_dma_desc_rx *rx_desc;
75
Bryan Wu7ef0a7e2008-04-25 11:53:10 +080076#if defined(CONFIG_BFIN_MAC_RMII)
77static u16 pin_req[] = P_RMII0;
78#else
79static u16 pin_req[] = P_MII0;
80#endif
81
82static void bfin_mac_disable(void);
83static void bfin_mac_enable(void);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +080084
Bryan Wue190d6b2007-07-17 14:43:44 +080085static void desc_list_free(void)
86{
87 struct net_dma_desc_rx *r;
88 struct net_dma_desc_tx *t;
89 int i;
90#if !defined(CONFIG_BFIN_MAC_USE_L1)
91 dma_addr_t dma_handle = 0;
92#endif
93
94 if (tx_desc) {
95 t = tx_list_head;
96 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
97 if (t) {
98 if (t->skb) {
99 dev_kfree_skb(t->skb);
100 t->skb = NULL;
101 }
102 t = t->next;
103 }
104 }
105 bfin_mac_free(dma_handle, tx_desc);
106 }
107
108 if (rx_desc) {
109 r = rx_list_head;
110 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
111 if (r) {
112 if (r->skb) {
113 dev_kfree_skb(r->skb);
114 r->skb = NULL;
115 }
116 r = r->next;
117 }
118 }
119 bfin_mac_free(dma_handle, rx_desc);
120 }
121}
122
123static int desc_list_init(void)
124{
125 int i;
126 struct sk_buff *new_skb;
127#if !defined(CONFIG_BFIN_MAC_USE_L1)
128 /*
129 * This dma_handle is useless in Blackfin dma_alloc_coherent().
130 * The real dma handler is the return value of dma_alloc_coherent().
131 */
132 dma_addr_t dma_handle;
133#endif
134
135 tx_desc = bfin_mac_alloc(&dma_handle,
136 sizeof(struct net_dma_desc_tx) *
137 CONFIG_BFIN_TX_DESC_NUM);
138 if (tx_desc == NULL)
139 goto init_error;
140
141 rx_desc = bfin_mac_alloc(&dma_handle,
142 sizeof(struct net_dma_desc_rx) *
143 CONFIG_BFIN_RX_DESC_NUM);
144 if (rx_desc == NULL)
145 goto init_error;
146
147 /* init tx_list */
148 tx_list_head = tx_list_tail = tx_desc;
149
150 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
151 struct net_dma_desc_tx *t = tx_desc + i;
152 struct dma_descriptor *a = &(t->desc_a);
153 struct dma_descriptor *b = &(t->desc_b);
154
155 /*
156 * disable DMA
157 * read from memory WNR = 0
158 * wordsize is 32 bits
159 * 6 half words is desc size
160 * large desc flow
161 */
162 a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
163 a->start_addr = (unsigned long)t->packet;
164 a->x_count = 0;
165 a->next_dma_desc = b;
166
167 /*
168 * enabled DMA
169 * write to memory WNR = 1
170 * wordsize is 32 bits
171 * disable interrupt
172 * 6 half words is desc size
173 * large desc flow
174 */
175 b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
176 b->start_addr = (unsigned long)(&(t->status));
177 b->x_count = 0;
178
179 t->skb = NULL;
180 tx_list_tail->desc_b.next_dma_desc = a;
181 tx_list_tail->next = t;
182 tx_list_tail = t;
183 }
184 tx_list_tail->next = tx_list_head; /* tx_list is a circle */
185 tx_list_tail->desc_b.next_dma_desc = &(tx_list_head->desc_a);
186 current_tx_ptr = tx_list_head;
187
188 /* init rx_list */
189 rx_list_head = rx_list_tail = rx_desc;
190
191 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
192 struct net_dma_desc_rx *r = rx_desc + i;
193 struct dma_descriptor *a = &(r->desc_a);
194 struct dma_descriptor *b = &(r->desc_b);
195
196 /* allocate a new skb for next time receive */
197 new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
198 if (!new_skb) {
199 printk(KERN_NOTICE DRV_NAME
200 ": init: low on mem - packet dropped\n");
201 goto init_error;
202 }
203 skb_reserve(new_skb, 2);
204 r->skb = new_skb;
205
206 /*
207 * enabled DMA
208 * write to memory WNR = 1
209 * wordsize is 32 bits
210 * disable interrupt
211 * 6 half words is desc size
212 * large desc flow
213 */
214 a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
215 /* since RXDWA is enabled */
216 a->start_addr = (unsigned long)new_skb->data - 2;
217 a->x_count = 0;
218 a->next_dma_desc = b;
219
220 /*
221 * enabled DMA
222 * write to memory WNR = 1
223 * wordsize is 32 bits
224 * enable interrupt
225 * 6 half words is desc size
226 * large desc flow
227 */
228 b->config = DMAEN | WNR | WDSIZE_32 | DI_EN |
229 NDSIZE_6 | DMAFLOW_LARGE;
230 b->start_addr = (unsigned long)(&(r->status));
231 b->x_count = 0;
232
233 rx_list_tail->desc_b.next_dma_desc = a;
234 rx_list_tail->next = r;
235 rx_list_tail = r;
236 }
237 rx_list_tail->next = rx_list_head; /* rx_list is a circle */
238 rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a);
239 current_rx_ptr = rx_list_head;
240
241 return 0;
242
243init_error:
244 desc_list_free();
245 printk(KERN_ERR DRV_NAME ": kmalloc failed\n");
246 return -ENOMEM;
247}
248
249
250/*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
251
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800252/*
253 * MII operations
254 */
Bryan Wue190d6b2007-07-17 14:43:44 +0800255/* Wait until the previous MDC/MDIO transaction has completed */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800256static void mdio_poll(void)
Bryan Wue190d6b2007-07-17 14:43:44 +0800257{
258 int timeout_cnt = MAX_TIMEOUT_CNT;
259
260 /* poll the STABUSY bit */
261 while ((bfin_read_EMAC_STAADD()) & STABUSY) {
Bryan Wu6db9e462008-01-30 16:52:21 +0800262 udelay(1);
Bryan Wue190d6b2007-07-17 14:43:44 +0800263 if (timeout_cnt-- < 0) {
264 printk(KERN_ERR DRV_NAME
265 ": wait MDC/MDIO transaction to complete timeout\n");
266 break;
267 }
268 }
269}
270
271/* Read an off-chip register in a PHY through the MDC/MDIO port */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800272static int mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
Bryan Wue190d6b2007-07-17 14:43:44 +0800273{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800274 mdio_poll();
Bryan Wue190d6b2007-07-17 14:43:44 +0800275
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800276 /* read mode */
277 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
278 SET_REGAD((u16) regnum) |
279 STABUSY);
280
281 mdio_poll();
282
283 return (int) bfin_read_EMAC_STADAT();
Bryan Wue190d6b2007-07-17 14:43:44 +0800284}
285
286/* Write an off-chip register in a PHY through the MDC/MDIO port */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800287static int mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
288 u16 value)
Bryan Wue190d6b2007-07-17 14:43:44 +0800289{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800290 mdio_poll();
291
292 bfin_write_EMAC_STADAT((u32) value);
Bryan Wue190d6b2007-07-17 14:43:44 +0800293
294 /* write mode */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800295 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
296 SET_REGAD((u16) regnum) |
Bryan Wue190d6b2007-07-17 14:43:44 +0800297 STAOP |
298 STABUSY);
299
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800300 mdio_poll();
301
302 return 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800303}
304
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800305static int mdiobus_reset(struct mii_bus *bus)
Bryan Wue190d6b2007-07-17 14:43:44 +0800306{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800307 return 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800308}
309
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800310static void bfin_mac_adjust_link(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800311{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800312 struct bfin_mac_local *lp = netdev_priv(dev);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800313 struct phy_device *phydev = lp->phydev;
314 unsigned long flags;
315 int new_state = 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800316
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800317 spin_lock_irqsave(&lp->lock, flags);
318 if (phydev->link) {
319 /* Now we make sure that we can be in full duplex mode.
320 * If not, we operate in half-duplex mode. */
321 if (phydev->duplex != lp->old_duplex) {
322 u32 opmode = bfin_read_EMAC_OPMODE();
323 new_state = 1;
Bryan Wue190d6b2007-07-17 14:43:44 +0800324
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800325 if (phydev->duplex)
326 opmode |= FDMODE;
327 else
328 opmode &= ~(FDMODE);
Bryan Wue190d6b2007-07-17 14:43:44 +0800329
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800330 bfin_write_EMAC_OPMODE(opmode);
331 lp->old_duplex = phydev->duplex;
332 }
Bryan Wue190d6b2007-07-17 14:43:44 +0800333
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800334 if (phydev->speed != lp->old_speed) {
335#if defined(CONFIG_BFIN_MAC_RMII)
336 u32 opmode = bfin_read_EMAC_OPMODE();
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800337 switch (phydev->speed) {
338 case 10:
339 opmode |= RMII_10;
340 break;
341 case 100:
342 opmode &= ~(RMII_10);
343 break;
344 default:
345 printk(KERN_WARNING
346 "%s: Ack! Speed (%d) is not 10/100!\n",
347 DRV_NAME, phydev->speed);
348 break;
349 }
350 bfin_write_EMAC_OPMODE(opmode);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800351#endif
Bryan Wue190d6b2007-07-17 14:43:44 +0800352
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800353 new_state = 1;
354 lp->old_speed = phydev->speed;
355 }
Bryan Wue190d6b2007-07-17 14:43:44 +0800356
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800357 if (!lp->old_link) {
358 new_state = 1;
359 lp->old_link = 1;
360 netif_schedule(dev);
361 }
362 } else if (lp->old_link) {
363 new_state = 1;
364 lp->old_link = 0;
365 lp->old_speed = 0;
366 lp->old_duplex = -1;
Bryan Wue190d6b2007-07-17 14:43:44 +0800367 }
368
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800369 if (new_state) {
370 u32 opmode = bfin_read_EMAC_OPMODE();
371 phy_print_status(phydev);
372 pr_debug("EMAC_OPMODE = 0x%08x\n", opmode);
Bryan Wue190d6b2007-07-17 14:43:44 +0800373 }
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800374
375 spin_unlock_irqrestore(&lp->lock, flags);
376}
377
Bryan Wu7cc8f382008-01-30 16:52:22 +0800378/* MDC = 2.5 MHz */
379#define MDC_CLK 2500000
380
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800381static int mii_probe(struct net_device *dev)
382{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800383 struct bfin_mac_local *lp = netdev_priv(dev);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800384 struct phy_device *phydev = NULL;
385 unsigned short sysctl;
386 int i;
Bryan Wu7cc8f382008-01-30 16:52:22 +0800387 u32 sclk, mdc_div;
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800388
389 /* Enable PHY output early */
390 if (!(bfin_read_VR_CTL() & PHYCLKOE))
391 bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
392
Bryan Wu7cc8f382008-01-30 16:52:22 +0800393 sclk = get_sclk();
394 mdc_div = ((sclk / MDC_CLK) / 2) - 1;
395
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800396 sysctl = bfin_read_EMAC_SYSCTL();
Bryan Wu9dc7f302008-01-30 16:52:28 +0800397 sysctl = (sysctl & ~MDCDIV) | SET_MDCDIV(mdc_div);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800398 bfin_write_EMAC_SYSCTL(sysctl);
399
400 /* search for connect PHY device */
401 for (i = 0; i < PHY_MAX_ADDR; i++) {
402 struct phy_device *const tmp_phydev = lp->mii_bus.phy_map[i];
403
404 if (!tmp_phydev)
405 continue; /* no PHY here... */
406
407 phydev = tmp_phydev;
408 break; /* found it */
409 }
410
411 /* now we are supposed to have a proper phydev, to attach to... */
412 if (!phydev) {
413 printk(KERN_INFO "%s: Don't found any phy device at all\n",
414 dev->name);
415 return -ENODEV;
416 }
417
418#if defined(CONFIG_BFIN_MAC_RMII)
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800419 phydev = phy_connect(dev, phydev->dev.bus_id, &bfin_mac_adjust_link, 0,
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800420 PHY_INTERFACE_MODE_RMII);
421#else
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800422 phydev = phy_connect(dev, phydev->dev.bus_id, &bfin_mac_adjust_link, 0,
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800423 PHY_INTERFACE_MODE_MII);
424#endif
425
426 if (IS_ERR(phydev)) {
427 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
428 return PTR_ERR(phydev);
429 }
430
431 /* mask with MAC supported features */
432 phydev->supported &= (SUPPORTED_10baseT_Half
433 | SUPPORTED_10baseT_Full
434 | SUPPORTED_100baseT_Half
435 | SUPPORTED_100baseT_Full
436 | SUPPORTED_Autoneg
437 | SUPPORTED_Pause | SUPPORTED_Asym_Pause
438 | SUPPORTED_MII
439 | SUPPORTED_TP);
440
441 phydev->advertising = phydev->supported;
442
443 lp->old_link = 0;
444 lp->old_speed = 0;
445 lp->old_duplex = -1;
446 lp->phydev = phydev;
447
448 printk(KERN_INFO "%s: attached PHY driver [%s] "
Bryan Wu7cc8f382008-01-30 16:52:22 +0800449 "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)"
450 "@sclk=%dMHz)\n",
451 DRV_NAME, phydev->drv->name, phydev->dev.bus_id, phydev->irq,
452 MDC_CLK, mdc_div, sclk/1000000);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800453
454 return 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800455}
456
457/**************************************************************************/
458void setup_system_regs(struct net_device *dev)
459{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800460 unsigned short sysctl;
Bryan Wue190d6b2007-07-17 14:43:44 +0800461
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800462 /*
463 * Odd word alignment for Receive Frame DMA word
464 * Configure checksum support and rcve frame word alignment
465 */
466 sysctl = bfin_read_EMAC_SYSCTL();
Bryan Wue190d6b2007-07-17 14:43:44 +0800467#if defined(BFIN_MAC_CSUM_OFFLOAD)
468 sysctl |= RXDWA | RXCKS;
469#else
470 sysctl |= RXDWA;
471#endif
472 bfin_write_EMAC_SYSCTL(sysctl);
Bryan Wue190d6b2007-07-17 14:43:44 +0800473
474 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
475
476 /* Initialize the TX DMA channel registers */
477 bfin_write_DMA2_X_COUNT(0);
478 bfin_write_DMA2_X_MODIFY(4);
479 bfin_write_DMA2_Y_COUNT(0);
480 bfin_write_DMA2_Y_MODIFY(0);
481
482 /* Initialize the RX DMA channel registers */
483 bfin_write_DMA1_X_COUNT(0);
484 bfin_write_DMA1_X_MODIFY(4);
485 bfin_write_DMA1_Y_COUNT(0);
486 bfin_write_DMA1_Y_MODIFY(0);
487}
488
Alex Landau73f83182007-09-19 23:14:18 +0800489static void setup_mac_addr(u8 *mac_addr)
Bryan Wue190d6b2007-07-17 14:43:44 +0800490{
491 u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
492 u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
493
494 /* this depends on a little-endian machine */
495 bfin_write_EMAC_ADDRLO(addr_low);
496 bfin_write_EMAC_ADDRHI(addr_hi);
497}
498
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800499static int bfin_mac_set_mac_address(struct net_device *dev, void *p)
Alex Landau73f83182007-09-19 23:14:18 +0800500{
501 struct sockaddr *addr = p;
502 if (netif_running(dev))
503 return -EBUSY;
504 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
505 setup_mac_addr(dev->dev_addr);
506 return 0;
507}
508
Bryan Wue190d6b2007-07-17 14:43:44 +0800509static void adjust_tx_list(void)
510{
511 int timeout_cnt = MAX_TIMEOUT_CNT;
512
513 if (tx_list_head->status.status_word != 0
514 && current_tx_ptr != tx_list_head) {
515 goto adjust_head; /* released something, just return; */
516 }
517
518 /*
519 * if nothing released, check wait condition
520 * current's next can not be the head,
521 * otherwise the dma will not stop as we want
522 */
523 if (current_tx_ptr->next->next == tx_list_head) {
524 while (tx_list_head->status.status_word == 0) {
Bryan Wu6db9e462008-01-30 16:52:21 +0800525 mdelay(1);
Bryan Wue190d6b2007-07-17 14:43:44 +0800526 if (tx_list_head->status.status_word != 0
527 || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) {
528 goto adjust_head;
529 }
530 if (timeout_cnt-- < 0) {
531 printk(KERN_ERR DRV_NAME
532 ": wait for adjust tx list head timeout\n");
533 break;
534 }
535 }
536 if (tx_list_head->status.status_word != 0) {
537 goto adjust_head;
538 }
539 }
540
541 return;
542
543adjust_head:
544 do {
545 tx_list_head->desc_a.config &= ~DMAEN;
546 tx_list_head->status.status_word = 0;
547 if (tx_list_head->skb) {
548 dev_kfree_skb(tx_list_head->skb);
549 tx_list_head->skb = NULL;
550 } else {
551 printk(KERN_ERR DRV_NAME
552 ": no sk_buff in a transmitted frame!\n");
553 }
554 tx_list_head = tx_list_head->next;
555 } while (tx_list_head->status.status_word != 0
556 && current_tx_ptr != tx_list_head);
557 return;
558
559}
560
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800561static int bfin_mac_hard_start_xmit(struct sk_buff *skb,
Bryan Wue190d6b2007-07-17 14:43:44 +0800562 struct net_device *dev)
563{
Bryan Wue190d6b2007-07-17 14:43:44 +0800564 unsigned int data;
565
566 current_tx_ptr->skb = skb;
567
568 /*
569 * Is skb->data always 16-bit aligned?
570 * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)?
571 */
572 if ((((unsigned int)(skb->data)) & 0x02) == 2) {
573 /* move skb->data to current_tx_ptr payload */
574 data = (unsigned int)(skb->data) - 2;
575 *((unsigned short *)data) = (unsigned short)(skb->len);
576 current_tx_ptr->desc_a.start_addr = (unsigned long)data;
577 /* this is important! */
578 blackfin_dcache_flush_range(data, (data + (skb->len)) + 2);
579
580 } else {
581 *((unsigned short *)(current_tx_ptr->packet)) =
582 (unsigned short)(skb->len);
583 memcpy((char *)(current_tx_ptr->packet + 2), skb->data,
584 (skb->len));
585 current_tx_ptr->desc_a.start_addr =
586 (unsigned long)current_tx_ptr->packet;
587 if (current_tx_ptr->status.status_word != 0)
588 current_tx_ptr->status.status_word = 0;
589 blackfin_dcache_flush_range((unsigned int)current_tx_ptr->
590 packet,
591 (unsigned int)(current_tx_ptr->
592 packet + skb->len) +
593 2);
594 }
595
596 /* enable this packet's dma */
597 current_tx_ptr->desc_a.config |= DMAEN;
598
599 /* tx dma is running, just return */
600 if (bfin_read_DMA2_IRQ_STATUS() & 0x08)
601 goto out;
602
603 /* tx dma is not running */
604 bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
605 /* dma enabled, read from memory, size is 6 */
606 bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
607 /* Turn on the EMAC tx */
608 bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
609
610out:
611 adjust_tx_list();
612 current_tx_ptr = current_tx_ptr->next;
613 dev->trans_start = jiffies;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700614 dev->stats.tx_packets++;
615 dev->stats.tx_bytes += (skb->len);
Bryan Wue190d6b2007-07-17 14:43:44 +0800616 return 0;
617}
618
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800619static void bfin_mac_rx(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800620{
621 struct sk_buff *skb, *new_skb;
Bryan Wue190d6b2007-07-17 14:43:44 +0800622 unsigned short len;
623
624 /* allocate a new skb for next time receive */
625 skb = current_rx_ptr->skb;
626 new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
627 if (!new_skb) {
628 printk(KERN_NOTICE DRV_NAME
629 ": rx: low on mem - packet dropped\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700630 dev->stats.rx_dropped++;
Bryan Wue190d6b2007-07-17 14:43:44 +0800631 goto out;
632 }
633 /* reserve 2 bytes for RXDWA padding */
634 skb_reserve(new_skb, 2);
635 current_rx_ptr->skb = new_skb;
636 current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
637
Alexey Demin6e01d1a2008-01-30 16:52:27 +0800638 /* Invidate the data cache of skb->data range when it is write back
639 * cache. It will prevent overwritting the new data from DMA
640 */
641 blackfin_dcache_invalidate_range((unsigned long)new_skb->head,
642 (unsigned long)new_skb->end);
643
Bryan Wue190d6b2007-07-17 14:43:44 +0800644 len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
645 skb_put(skb, len);
646 blackfin_dcache_invalidate_range((unsigned long)skb->head,
647 (unsigned long)skb->tail);
648
649 dev->last_rx = jiffies;
650 skb->dev = dev;
651 skb->protocol = eth_type_trans(skb, dev);
652#if defined(BFIN_MAC_CSUM_OFFLOAD)
653 skb->csum = current_rx_ptr->status.ip_payload_csum;
Vitja Makarov00ff49a2007-11-23 17:55:51 +0800654 skb->ip_summed = CHECKSUM_COMPLETE;
Bryan Wue190d6b2007-07-17 14:43:44 +0800655#endif
656
657 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700658 dev->stats.rx_packets++;
659 dev->stats.rx_bytes += len;
Bryan Wue190d6b2007-07-17 14:43:44 +0800660 current_rx_ptr->status.status_word = 0x00000000;
661 current_rx_ptr = current_rx_ptr->next;
662
663out:
664 return;
665}
666
667/* interrupt routine to handle rx and error signal */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800668static irqreturn_t bfin_mac_interrupt(int irq, void *dev_id)
Bryan Wue190d6b2007-07-17 14:43:44 +0800669{
670 struct net_device *dev = dev_id;
671 int number = 0;
672
673get_one_packet:
674 if (current_rx_ptr->status.status_word == 0) {
675 /* no more new packet received */
676 if (number == 0) {
677 if (current_rx_ptr->next->status.status_word != 0) {
678 current_rx_ptr = current_rx_ptr->next;
679 goto real_rx;
680 }
681 }
682 bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
683 DMA_DONE | DMA_ERR);
684 return IRQ_HANDLED;
685 }
686
687real_rx:
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800688 bfin_mac_rx(dev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800689 number++;
690 goto get_one_packet;
691}
692
693#ifdef CONFIG_NET_POLL_CONTROLLER
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800694static void bfin_mac_poll(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800695{
696 disable_irq(IRQ_MAC_RX);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800697 bfin_mac_interrupt(IRQ_MAC_RX, dev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800698 enable_irq(IRQ_MAC_RX);
699}
700#endif /* CONFIG_NET_POLL_CONTROLLER */
701
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800702static void bfin_mac_disable(void)
Bryan Wue190d6b2007-07-17 14:43:44 +0800703{
704 unsigned int opmode;
705
706 opmode = bfin_read_EMAC_OPMODE();
707 opmode &= (~RE);
708 opmode &= (~TE);
709 /* Turn off the EMAC */
710 bfin_write_EMAC_OPMODE(opmode);
711}
712
713/*
714 * Enable Interrupts, Receive, and Transmit
715 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800716static void bfin_mac_enable(void)
Bryan Wue190d6b2007-07-17 14:43:44 +0800717{
718 u32 opmode;
719
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800720 pr_debug("%s: %s\n", DRV_NAME, __FUNCTION__);
Bryan Wue190d6b2007-07-17 14:43:44 +0800721
722 /* Set RX DMA */
723 bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
724 bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
725
726 /* Wait MII done */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800727 mdio_poll();
Bryan Wue190d6b2007-07-17 14:43:44 +0800728
729 /* We enable only RX here */
730 /* ASTP : Enable Automatic Pad Stripping
731 PR : Promiscuous Mode for test
732 PSF : Receive frames with total length less than 64 bytes.
733 FDMODE : Full Duplex Mode
734 LB : Internal Loopback for test
735 RE : Receiver Enable */
736 opmode = bfin_read_EMAC_OPMODE();
737 if (opmode & FDMODE)
738 opmode |= PSF;
739 else
740 opmode |= DRO | DC | PSF;
741 opmode |= RE;
742
743#if defined(CONFIG_BFIN_MAC_RMII)
744 opmode |= RMII; /* For Now only 100MBit are supported */
Michael Hennerich6893ff12008-01-30 16:52:25 +0800745#if (defined(CONFIG_BF537) || defined(CONFIG_BF536)) && CONFIG_BF_REV_0_2
Bryan Wue190d6b2007-07-17 14:43:44 +0800746 opmode |= TE;
747#endif
748#endif
749 /* Turn on the EMAC rx */
750 bfin_write_EMAC_OPMODE(opmode);
Bryan Wue190d6b2007-07-17 14:43:44 +0800751}
752
753/* Our watchdog timed out. Called by the networking layer */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800754static void bfin_mac_timeout(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800755{
756 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
757
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800758 bfin_mac_disable();
Bryan Wue190d6b2007-07-17 14:43:44 +0800759
760 /* reset tx queue */
761 tx_list_tail = tx_list_head->next;
762
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800763 bfin_mac_enable();
Bryan Wue190d6b2007-07-17 14:43:44 +0800764
765 /* We can accept TX packets again */
766 dev->trans_start = jiffies;
767 netif_wake_queue(dev);
768}
769
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800770static void bfin_mac_multicast_hash(struct net_device *dev)
Aidan Williams775919b2008-01-30 16:52:23 +0800771{
772 u32 emac_hashhi, emac_hashlo;
773 struct dev_mc_list *dmi = dev->mc_list;
774 char *addrs;
775 int i;
776 u32 crc;
777
778 emac_hashhi = emac_hashlo = 0;
779
780 for (i = 0; i < dev->mc_count; i++) {
781 addrs = dmi->dmi_addr;
782 dmi = dmi->next;
783
784 /* skip non-multicast addresses */
785 if (!(*addrs & 1))
786 continue;
787
788 crc = ether_crc(ETH_ALEN, addrs);
789 crc >>= 26;
790
791 if (crc & 0x20)
792 emac_hashhi |= 1 << (crc & 0x1f);
793 else
794 emac_hashlo |= 1 << (crc & 0x1f);
795 }
796
797 bfin_write_EMAC_HASHHI(emac_hashhi);
798 bfin_write_EMAC_HASHLO(emac_hashlo);
799
800 return;
801}
802
Bryan Wue190d6b2007-07-17 14:43:44 +0800803/*
Bryan Wue190d6b2007-07-17 14:43:44 +0800804 * This routine will, depending on the values passed to it,
805 * either make it accept multicast packets, go into
806 * promiscuous mode (for TCPDUMP and cousins) or accept
807 * a select set of multicast packets
808 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800809static void bfin_mac_set_multicast_list(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800810{
811 u32 sysctl;
812
813 if (dev->flags & IFF_PROMISC) {
814 printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
815 sysctl = bfin_read_EMAC_OPMODE();
816 sysctl |= RAF;
817 bfin_write_EMAC_OPMODE(sysctl);
Aidan Williams775919b2008-01-30 16:52:23 +0800818 } else if (dev->flags & IFF_ALLMULTI) {
Bryan Wue190d6b2007-07-17 14:43:44 +0800819 /* accept all multicast */
820 sysctl = bfin_read_EMAC_OPMODE();
821 sysctl |= PAM;
822 bfin_write_EMAC_OPMODE(sysctl);
Aidan Williams775919b2008-01-30 16:52:23 +0800823 } else if (dev->mc_count) {
824 /* set up multicast hash table */
825 sysctl = bfin_read_EMAC_OPMODE();
826 sysctl |= HM;
827 bfin_write_EMAC_OPMODE(sysctl);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800828 bfin_mac_multicast_hash(dev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800829 } else {
830 /* clear promisc or multicast mode */
831 sysctl = bfin_read_EMAC_OPMODE();
832 sysctl &= ~(RAF | PAM);
833 bfin_write_EMAC_OPMODE(sysctl);
834 }
835}
836
837/*
838 * this puts the device in an inactive state
839 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800840static void bfin_mac_shutdown(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800841{
842 /* Turn off the EMAC */
843 bfin_write_EMAC_OPMODE(0x00000000);
844 /* Turn off the EMAC RX DMA */
845 bfin_write_DMA1_CONFIG(0x0000);
846 bfin_write_DMA2_CONFIG(0x0000);
847}
848
849/*
850 * Open and Initialize the interface
851 *
852 * Set up everything, reset the card, etc..
853 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800854static int bfin_mac_open(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800855{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800856 struct bfin_mac_local *lp = netdev_priv(dev);
Michael Hennerich4af4b842007-07-25 14:09:54 +0800857 int retval;
Bryan Wue190d6b2007-07-17 14:43:44 +0800858 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
859
860 /*
861 * Check that the address is valid. If its not, refuse
862 * to bring the device up. The user must specify an
863 * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
864 */
865 if (!is_valid_ether_addr(dev->dev_addr)) {
866 printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
867 return -EINVAL;
868 }
869
870 /* initial rx and tx list */
Michael Hennerich4af4b842007-07-25 14:09:54 +0800871 retval = desc_list_init();
872
873 if (retval)
874 return retval;
Bryan Wue190d6b2007-07-17 14:43:44 +0800875
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800876 phy_start(lp->phydev);
Vitja Makarov136492b2008-01-30 16:52:26 +0800877 phy_write(lp->phydev, MII_BMCR, BMCR_RESET);
Bryan Wue190d6b2007-07-17 14:43:44 +0800878 setup_system_regs(dev);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800879 bfin_mac_disable();
880 bfin_mac_enable();
Bryan Wue190d6b2007-07-17 14:43:44 +0800881 pr_debug("hardware init finished\n");
882 netif_start_queue(dev);
883 netif_carrier_on(dev);
884
885 return 0;
886}
887
888/*
889 *
890 * this makes the board clean up everything that it can
891 * and not talk to the outside world. Caused by
892 * an 'ifconfig ethX down'
893 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800894static int bfin_mac_close(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800895{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800896 struct bfin_mac_local *lp = netdev_priv(dev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800897 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
898
899 netif_stop_queue(dev);
900 netif_carrier_off(dev);
901
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800902 phy_stop(lp->phydev);
Vitja Makarov136492b2008-01-30 16:52:26 +0800903 phy_write(lp->phydev, MII_BMCR, BMCR_PDOWN);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800904
Bryan Wue190d6b2007-07-17 14:43:44 +0800905 /* clear everything */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800906 bfin_mac_shutdown(dev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800907
908 /* free the rx/tx buffers */
909 desc_list_free();
910
911 return 0;
912}
913
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800914static int __init bfin_mac_probe(struct platform_device *pdev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800915{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800916 struct net_device *ndev;
917 struct bfin_mac_local *lp;
918 int rc, i;
919
920 ndev = alloc_etherdev(sizeof(struct bfin_mac_local));
921 if (!ndev) {
922 dev_err(&pdev->dev, "Cannot allocate net device!\n");
923 return -ENOMEM;
924 }
925
926 SET_NETDEV_DEV(ndev, &pdev->dev);
927 platform_set_drvdata(pdev, ndev);
928 lp = netdev_priv(ndev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800929
930 /* Grab the MAC address in the MAC */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800931 *(__le32 *) (&(ndev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
932 *(__le16 *) (&(ndev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
Bryan Wue190d6b2007-07-17 14:43:44 +0800933
934 /* probe mac */
935 /*todo: how to proble? which is revision_register */
936 bfin_write_EMAC_ADDRLO(0x12345678);
937 if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800938 dev_err(&pdev->dev, "Cannot detect Blackfin on-chip ethernet MAC controller!\n");
939 rc = -ENODEV;
940 goto out_err_probe_mac;
Bryan Wue190d6b2007-07-17 14:43:44 +0800941 }
942
943 /* set the GPIO pins to Ethernet mode */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800944 rc = peripheral_request_list(pin_req, DRV_NAME);
945 if (rc) {
946 dev_err(&pdev->dev, "Requesting peripherals failed!\n");
947 rc = -EFAULT;
948 goto out_err_setup_pin_mux;
Bryan Wue190d6b2007-07-17 14:43:44 +0800949 }
950
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800951 /*
952 * Is it valid? (Did bootloader initialize it?)
953 * Grab the MAC from the board somehow
954 * this is done in the arch/blackfin/mach-bfxxx/boards/eth_mac.c
955 */
956 if (!is_valid_ether_addr(ndev->dev_addr))
957 bfin_get_ether_addr(ndev->dev_addr);
958
Bryan Wue190d6b2007-07-17 14:43:44 +0800959 /* If still not valid, get a random one */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800960 if (!is_valid_ether_addr(ndev->dev_addr))
961 random_ether_addr(ndev->dev_addr);
Bryan Wue190d6b2007-07-17 14:43:44 +0800962
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800963 setup_mac_addr(ndev->dev_addr);
Bryan Wue190d6b2007-07-17 14:43:44 +0800964
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800965 /* MDIO bus initial */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800966 lp->mii_bus.priv = ndev;
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800967 lp->mii_bus.read = mdiobus_read;
968 lp->mii_bus.write = mdiobus_write;
969 lp->mii_bus.reset = mdiobus_reset;
970 lp->mii_bus.name = "bfin_mac_mdio";
Andy Fleming9d9326d32008-04-09 19:38:13 -0500971 snprintf(lp->mii_bus.id, MII_BUS_ID_SIZE, "0");
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800972 lp->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
973 for (i = 0; i < PHY_MAX_ADDR; ++i)
974 lp->mii_bus.irq[i] = PHY_POLL;
975
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800976 rc = mdiobus_register(&lp->mii_bus);
977 if (rc) {
978 dev_err(&pdev->dev, "Cannot register MDIO bus!\n");
979 goto out_err_mdiobus_register;
980 }
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800981
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800982 rc = mii_probe(ndev);
983 if (rc) {
984 dev_err(&pdev->dev, "MII Probe failed!\n");
985 goto out_err_mii_probe;
986 }
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800987
Bryan Wue190d6b2007-07-17 14:43:44 +0800988 /* Fill in the fields of the device structure with ethernet values. */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800989 ether_setup(ndev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800990
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800991 ndev->open = bfin_mac_open;
992 ndev->stop = bfin_mac_close;
993 ndev->hard_start_xmit = bfin_mac_hard_start_xmit;
994 ndev->set_mac_address = bfin_mac_set_mac_address;
995 ndev->tx_timeout = bfin_mac_timeout;
996 ndev->set_multicast_list = bfin_mac_set_multicast_list;
Bryan Wue190d6b2007-07-17 14:43:44 +0800997#ifdef CONFIG_NET_POLL_CONTROLLER
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800998 ndev->poll_controller = bfin_mac_poll;
Bryan Wue190d6b2007-07-17 14:43:44 +0800999#endif
1000
Bryan Wue190d6b2007-07-17 14:43:44 +08001001 spin_lock_init(&lp->lock);
1002
1003 /* now, enable interrupts */
1004 /* register irq handler */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001005 rc = request_irq(IRQ_MAC_RX, bfin_mac_interrupt,
1006 IRQF_DISABLED | IRQF_SHARED, "EMAC_RX", ndev);
1007 if (rc) {
1008 dev_err(&pdev->dev, "Cannot request Blackfin MAC RX IRQ!\n");
1009 rc = -EBUSY;
1010 goto out_err_request_irq;
Bryan Wue190d6b2007-07-17 14:43:44 +08001011 }
1012
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001013 rc = register_netdev(ndev);
1014 if (rc) {
1015 dev_err(&pdev->dev, "Cannot register net device!\n");
1016 goto out_err_reg_ndev;
Bryan Wue190d6b2007-07-17 14:43:44 +08001017 }
1018
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001019 /* now, print out the card info, in a short format.. */
1020 dev_info(&pdev->dev, "%s, Version %s\n", DRV_DESC, DRV_VERSION);
Bryan Wue190d6b2007-07-17 14:43:44 +08001021
1022 return 0;
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001023
1024out_err_reg_ndev:
1025 free_irq(IRQ_MAC_RX, ndev);
1026out_err_request_irq:
1027out_err_mii_probe:
1028 mdiobus_unregister(&lp->mii_bus);
1029out_err_mdiobus_register:
1030 peripheral_free_list(pin_req);
1031out_err_setup_pin_mux:
1032out_err_probe_mac:
1033 platform_set_drvdata(pdev, NULL);
1034 free_netdev(ndev);
1035
1036 return rc;
Bryan Wue190d6b2007-07-17 14:43:44 +08001037}
1038
1039static int bfin_mac_remove(struct platform_device *pdev)
1040{
1041 struct net_device *ndev = platform_get_drvdata(pdev);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001042 struct bfin_mac_local *lp = netdev_priv(ndev);
Bryan Wue190d6b2007-07-17 14:43:44 +08001043
1044 platform_set_drvdata(pdev, NULL);
1045
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001046 mdiobus_unregister(&lp->mii_bus);
1047
Bryan Wue190d6b2007-07-17 14:43:44 +08001048 unregister_netdev(ndev);
1049
1050 free_irq(IRQ_MAC_RX, ndev);
1051
1052 free_netdev(ndev);
1053
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001054 peripheral_free_list(pin_req);
Bryan Wue190d6b2007-07-17 14:43:44 +08001055
1056 return 0;
1057}
1058
Bryan Wu496a34c2007-09-19 23:37:14 +08001059#ifdef CONFIG_PM
1060static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg)
Bryan Wue190d6b2007-07-17 14:43:44 +08001061{
Bryan Wu496a34c2007-09-19 23:37:14 +08001062 struct net_device *net_dev = platform_get_drvdata(pdev);
1063
1064 if (netif_running(net_dev))
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001065 bfin_mac_close(net_dev);
Bryan Wu496a34c2007-09-19 23:37:14 +08001066
Bryan Wue190d6b2007-07-17 14:43:44 +08001067 return 0;
1068}
1069
1070static int bfin_mac_resume(struct platform_device *pdev)
1071{
Bryan Wu496a34c2007-09-19 23:37:14 +08001072 struct net_device *net_dev = platform_get_drvdata(pdev);
1073
1074 if (netif_running(net_dev))
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001075 bfin_mac_open(net_dev);
Bryan Wu496a34c2007-09-19 23:37:14 +08001076
Bryan Wue190d6b2007-07-17 14:43:44 +08001077 return 0;
1078}
Bryan Wu496a34c2007-09-19 23:37:14 +08001079#else
1080#define bfin_mac_suspend NULL
1081#define bfin_mac_resume NULL
1082#endif /* CONFIG_PM */
Bryan Wue190d6b2007-07-17 14:43:44 +08001083
1084static struct platform_driver bfin_mac_driver = {
1085 .probe = bfin_mac_probe,
1086 .remove = bfin_mac_remove,
1087 .resume = bfin_mac_resume,
1088 .suspend = bfin_mac_suspend,
1089 .driver = {
Kay Sievers72abb462008-04-18 13:50:44 -07001090 .name = DRV_NAME,
1091 .owner = THIS_MODULE,
1092 },
Bryan Wue190d6b2007-07-17 14:43:44 +08001093};
1094
1095static int __init bfin_mac_init(void)
1096{
1097 return platform_driver_register(&bfin_mac_driver);
1098}
1099
1100module_init(bfin_mac_init);
1101
1102static void __exit bfin_mac_cleanup(void)
1103{
1104 platform_driver_unregister(&bfin_mac_driver);
1105}
1106
1107module_exit(bfin_mac_cleanup);
Kay Sievers72abb462008-04-18 13:50:44 -07001108