blob: 89c0018132ec7e8137a4877da7166d7758b5e93b [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>
Bryan Wu679dce32008-04-25 11:53:11 +080030#include <linux/ethtool.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080031#include <linux/skbuff.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080032#include <linux/platform_device.h>
Bryan Wue190d6b2007-07-17 14:43:44 +080033
34#include <asm/dma.h>
35#include <linux/dma-mapping.h>
36
37#include <asm/blackfin.h>
38#include <asm/cacheflush.h>
39#include <asm/portmux.h>
40
41#include "bfin_mac.h"
42
43#define DRV_NAME "bfin_mac"
44#define DRV_VERSION "1.1"
45#define DRV_AUTHOR "Bryan Wu, Luke Yang"
Bryan Wu7ef0a7e2008-04-25 11:53:10 +080046#define DRV_DESC "Blackfin on-chip Ethernet MAC driver"
Bryan Wue190d6b2007-07-17 14:43:44 +080047
48MODULE_AUTHOR(DRV_AUTHOR);
49MODULE_LICENSE("GPL");
50MODULE_DESCRIPTION(DRV_DESC);
Kay Sievers72abb462008-04-18 13:50:44 -070051MODULE_ALIAS("platform:bfin_mac");
Bryan Wue190d6b2007-07-17 14:43:44 +080052
53#if defined(CONFIG_BFIN_MAC_USE_L1)
54# define bfin_mac_alloc(dma_handle, size) l1_data_sram_zalloc(size)
55# define bfin_mac_free(dma_handle, ptr) l1_data_sram_free(ptr)
56#else
57# define bfin_mac_alloc(dma_handle, size) \
58 dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL)
59# define bfin_mac_free(dma_handle, ptr) \
60 dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle)
61#endif
62
63#define PKT_BUF_SZ 1580
64
65#define MAX_TIMEOUT_CNT 500
66
67/* pointers to maintain transmit list */
68static struct net_dma_desc_tx *tx_list_head;
69static struct net_dma_desc_tx *tx_list_tail;
70static struct net_dma_desc_rx *rx_list_head;
71static struct net_dma_desc_rx *rx_list_tail;
72static struct net_dma_desc_rx *current_rx_ptr;
73static struct net_dma_desc_tx *current_tx_ptr;
74static struct net_dma_desc_tx *tx_desc;
75static struct net_dma_desc_rx *rx_desc;
76
Bryan Wu7ef0a7e2008-04-25 11:53:10 +080077#if defined(CONFIG_BFIN_MAC_RMII)
78static u16 pin_req[] = P_RMII0;
79#else
80static u16 pin_req[] = P_MII0;
81#endif
82
83static void bfin_mac_disable(void);
84static void bfin_mac_enable(void);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +080085
Bryan Wue190d6b2007-07-17 14:43:44 +080086static void desc_list_free(void)
87{
88 struct net_dma_desc_rx *r;
89 struct net_dma_desc_tx *t;
90 int i;
91#if !defined(CONFIG_BFIN_MAC_USE_L1)
92 dma_addr_t dma_handle = 0;
93#endif
94
95 if (tx_desc) {
96 t = tx_list_head;
97 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
98 if (t) {
99 if (t->skb) {
100 dev_kfree_skb(t->skb);
101 t->skb = NULL;
102 }
103 t = t->next;
104 }
105 }
106 bfin_mac_free(dma_handle, tx_desc);
107 }
108
109 if (rx_desc) {
110 r = rx_list_head;
111 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
112 if (r) {
113 if (r->skb) {
114 dev_kfree_skb(r->skb);
115 r->skb = NULL;
116 }
117 r = r->next;
118 }
119 }
120 bfin_mac_free(dma_handle, rx_desc);
121 }
122}
123
124static int desc_list_init(void)
125{
126 int i;
127 struct sk_buff *new_skb;
128#if !defined(CONFIG_BFIN_MAC_USE_L1)
129 /*
130 * This dma_handle is useless in Blackfin dma_alloc_coherent().
131 * The real dma handler is the return value of dma_alloc_coherent().
132 */
133 dma_addr_t dma_handle;
134#endif
135
136 tx_desc = bfin_mac_alloc(&dma_handle,
137 sizeof(struct net_dma_desc_tx) *
138 CONFIG_BFIN_TX_DESC_NUM);
139 if (tx_desc == NULL)
140 goto init_error;
141
142 rx_desc = bfin_mac_alloc(&dma_handle,
143 sizeof(struct net_dma_desc_rx) *
144 CONFIG_BFIN_RX_DESC_NUM);
145 if (rx_desc == NULL)
146 goto init_error;
147
148 /* init tx_list */
149 tx_list_head = tx_list_tail = tx_desc;
150
151 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
152 struct net_dma_desc_tx *t = tx_desc + i;
153 struct dma_descriptor *a = &(t->desc_a);
154 struct dma_descriptor *b = &(t->desc_b);
155
156 /*
157 * disable DMA
158 * read from memory WNR = 0
159 * wordsize is 32 bits
160 * 6 half words is desc size
161 * large desc flow
162 */
163 a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
164 a->start_addr = (unsigned long)t->packet;
165 a->x_count = 0;
166 a->next_dma_desc = b;
167
168 /*
169 * enabled DMA
170 * write to memory WNR = 1
171 * wordsize is 32 bits
172 * disable interrupt
173 * 6 half words is desc size
174 * large desc flow
175 */
176 b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
177 b->start_addr = (unsigned long)(&(t->status));
178 b->x_count = 0;
179
180 t->skb = NULL;
181 tx_list_tail->desc_b.next_dma_desc = a;
182 tx_list_tail->next = t;
183 tx_list_tail = t;
184 }
185 tx_list_tail->next = tx_list_head; /* tx_list is a circle */
186 tx_list_tail->desc_b.next_dma_desc = &(tx_list_head->desc_a);
187 current_tx_ptr = tx_list_head;
188
189 /* init rx_list */
190 rx_list_head = rx_list_tail = rx_desc;
191
192 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
193 struct net_dma_desc_rx *r = rx_desc + i;
194 struct dma_descriptor *a = &(r->desc_a);
195 struct dma_descriptor *b = &(r->desc_b);
196
197 /* allocate a new skb for next time receive */
198 new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
199 if (!new_skb) {
200 printk(KERN_NOTICE DRV_NAME
201 ": init: low on mem - packet dropped\n");
202 goto init_error;
203 }
204 skb_reserve(new_skb, 2);
205 r->skb = new_skb;
206
207 /*
208 * enabled DMA
209 * write to memory WNR = 1
210 * wordsize is 32 bits
211 * disable interrupt
212 * 6 half words is desc size
213 * large desc flow
214 */
215 a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
216 /* since RXDWA is enabled */
217 a->start_addr = (unsigned long)new_skb->data - 2;
218 a->x_count = 0;
219 a->next_dma_desc = b;
220
221 /*
222 * enabled DMA
223 * write to memory WNR = 1
224 * wordsize is 32 bits
225 * enable interrupt
226 * 6 half words is desc size
227 * large desc flow
228 */
229 b->config = DMAEN | WNR | WDSIZE_32 | DI_EN |
230 NDSIZE_6 | DMAFLOW_LARGE;
231 b->start_addr = (unsigned long)(&(r->status));
232 b->x_count = 0;
233
234 rx_list_tail->desc_b.next_dma_desc = a;
235 rx_list_tail->next = r;
236 rx_list_tail = r;
237 }
238 rx_list_tail->next = rx_list_head; /* rx_list is a circle */
239 rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a);
240 current_rx_ptr = rx_list_head;
241
242 return 0;
243
244init_error:
245 desc_list_free();
246 printk(KERN_ERR DRV_NAME ": kmalloc failed\n");
247 return -ENOMEM;
248}
249
250
251/*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
252
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800253/*
254 * MII operations
255 */
Bryan Wue190d6b2007-07-17 14:43:44 +0800256/* Wait until the previous MDC/MDIO transaction has completed */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800257static void mdio_poll(void)
Bryan Wue190d6b2007-07-17 14:43:44 +0800258{
259 int timeout_cnt = MAX_TIMEOUT_CNT;
260
261 /* poll the STABUSY bit */
262 while ((bfin_read_EMAC_STAADD()) & STABUSY) {
Bryan Wu6db9e462008-01-30 16:52:21 +0800263 udelay(1);
Bryan Wue190d6b2007-07-17 14:43:44 +0800264 if (timeout_cnt-- < 0) {
265 printk(KERN_ERR DRV_NAME
266 ": wait MDC/MDIO transaction to complete timeout\n");
267 break;
268 }
269 }
270}
271
272/* Read an off-chip register in a PHY through the MDC/MDIO port */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800273static int mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
Bryan Wue190d6b2007-07-17 14:43:44 +0800274{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800275 mdio_poll();
Bryan Wue190d6b2007-07-17 14:43:44 +0800276
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800277 /* read mode */
278 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
279 SET_REGAD((u16) regnum) |
280 STABUSY);
281
282 mdio_poll();
283
284 return (int) bfin_read_EMAC_STADAT();
Bryan Wue190d6b2007-07-17 14:43:44 +0800285}
286
287/* Write an off-chip register in a PHY through the MDC/MDIO port */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800288static int mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
289 u16 value)
Bryan Wue190d6b2007-07-17 14:43:44 +0800290{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800291 mdio_poll();
292
293 bfin_write_EMAC_STADAT((u32) value);
Bryan Wue190d6b2007-07-17 14:43:44 +0800294
295 /* write mode */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800296 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
297 SET_REGAD((u16) regnum) |
Bryan Wue190d6b2007-07-17 14:43:44 +0800298 STAOP |
299 STABUSY);
300
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800301 mdio_poll();
302
303 return 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800304}
305
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800306static int mdiobus_reset(struct mii_bus *bus)
Bryan Wue190d6b2007-07-17 14:43:44 +0800307{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800308 return 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800309}
310
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800311static void bfin_mac_adjust_link(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800312{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800313 struct bfin_mac_local *lp = netdev_priv(dev);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800314 struct phy_device *phydev = lp->phydev;
315 unsigned long flags;
316 int new_state = 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800317
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800318 spin_lock_irqsave(&lp->lock, flags);
319 if (phydev->link) {
320 /* Now we make sure that we can be in full duplex mode.
321 * If not, we operate in half-duplex mode. */
322 if (phydev->duplex != lp->old_duplex) {
323 u32 opmode = bfin_read_EMAC_OPMODE();
324 new_state = 1;
Bryan Wue190d6b2007-07-17 14:43:44 +0800325
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800326 if (phydev->duplex)
327 opmode |= FDMODE;
328 else
329 opmode &= ~(FDMODE);
Bryan Wue190d6b2007-07-17 14:43:44 +0800330
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800331 bfin_write_EMAC_OPMODE(opmode);
332 lp->old_duplex = phydev->duplex;
333 }
Bryan Wue190d6b2007-07-17 14:43:44 +0800334
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800335 if (phydev->speed != lp->old_speed) {
336#if defined(CONFIG_BFIN_MAC_RMII)
337 u32 opmode = bfin_read_EMAC_OPMODE();
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800338 switch (phydev->speed) {
339 case 10:
340 opmode |= RMII_10;
341 break;
342 case 100:
343 opmode &= ~(RMII_10);
344 break;
345 default:
346 printk(KERN_WARNING
347 "%s: Ack! Speed (%d) is not 10/100!\n",
348 DRV_NAME, phydev->speed);
349 break;
350 }
351 bfin_write_EMAC_OPMODE(opmode);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800352#endif
Bryan Wue190d6b2007-07-17 14:43:44 +0800353
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800354 new_state = 1;
355 lp->old_speed = phydev->speed;
356 }
Bryan Wue190d6b2007-07-17 14:43:44 +0800357
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800358 if (!lp->old_link) {
359 new_state = 1;
360 lp->old_link = 1;
361 netif_schedule(dev);
362 }
363 } else if (lp->old_link) {
364 new_state = 1;
365 lp->old_link = 0;
366 lp->old_speed = 0;
367 lp->old_duplex = -1;
Bryan Wue190d6b2007-07-17 14:43:44 +0800368 }
369
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800370 if (new_state) {
371 u32 opmode = bfin_read_EMAC_OPMODE();
372 phy_print_status(phydev);
373 pr_debug("EMAC_OPMODE = 0x%08x\n", opmode);
Bryan Wue190d6b2007-07-17 14:43:44 +0800374 }
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800375
376 spin_unlock_irqrestore(&lp->lock, flags);
377}
378
Bryan Wu7cc8f382008-01-30 16:52:22 +0800379/* MDC = 2.5 MHz */
380#define MDC_CLK 2500000
381
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800382static int mii_probe(struct net_device *dev)
383{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800384 struct bfin_mac_local *lp = netdev_priv(dev);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800385 struct phy_device *phydev = NULL;
386 unsigned short sysctl;
387 int i;
Bryan Wu7cc8f382008-01-30 16:52:22 +0800388 u32 sclk, mdc_div;
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800389
390 /* Enable PHY output early */
391 if (!(bfin_read_VR_CTL() & PHYCLKOE))
392 bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
393
Bryan Wu7cc8f382008-01-30 16:52:22 +0800394 sclk = get_sclk();
395 mdc_div = ((sclk / MDC_CLK) / 2) - 1;
396
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800397 sysctl = bfin_read_EMAC_SYSCTL();
Bryan Wu9dc7f302008-01-30 16:52:28 +0800398 sysctl = (sysctl & ~MDCDIV) | SET_MDCDIV(mdc_div);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800399 bfin_write_EMAC_SYSCTL(sysctl);
400
401 /* search for connect PHY device */
402 for (i = 0; i < PHY_MAX_ADDR; i++) {
403 struct phy_device *const tmp_phydev = lp->mii_bus.phy_map[i];
404
405 if (!tmp_phydev)
406 continue; /* no PHY here... */
407
408 phydev = tmp_phydev;
409 break; /* found it */
410 }
411
412 /* now we are supposed to have a proper phydev, to attach to... */
413 if (!phydev) {
414 printk(KERN_INFO "%s: Don't found any phy device at all\n",
415 dev->name);
416 return -ENODEV;
417 }
418
419#if defined(CONFIG_BFIN_MAC_RMII)
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800420 phydev = phy_connect(dev, phydev->dev.bus_id, &bfin_mac_adjust_link, 0,
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800421 PHY_INTERFACE_MODE_RMII);
422#else
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800423 phydev = phy_connect(dev, phydev->dev.bus_id, &bfin_mac_adjust_link, 0,
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800424 PHY_INTERFACE_MODE_MII);
425#endif
426
427 if (IS_ERR(phydev)) {
428 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
429 return PTR_ERR(phydev);
430 }
431
432 /* mask with MAC supported features */
433 phydev->supported &= (SUPPORTED_10baseT_Half
434 | SUPPORTED_10baseT_Full
435 | SUPPORTED_100baseT_Half
436 | SUPPORTED_100baseT_Full
437 | SUPPORTED_Autoneg
438 | SUPPORTED_Pause | SUPPORTED_Asym_Pause
439 | SUPPORTED_MII
440 | SUPPORTED_TP);
441
442 phydev->advertising = phydev->supported;
443
444 lp->old_link = 0;
445 lp->old_speed = 0;
446 lp->old_duplex = -1;
447 lp->phydev = phydev;
448
449 printk(KERN_INFO "%s: attached PHY driver [%s] "
Bryan Wu7cc8f382008-01-30 16:52:22 +0800450 "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)"
451 "@sclk=%dMHz)\n",
452 DRV_NAME, phydev->drv->name, phydev->dev.bus_id, phydev->irq,
453 MDC_CLK, mdc_div, sclk/1000000);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800454
455 return 0;
Bryan Wue190d6b2007-07-17 14:43:44 +0800456}
457
Bryan Wu679dce32008-04-25 11:53:11 +0800458/*
459 * Ethtool support
460 */
461
462static int
463bfin_mac_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
464{
465 struct bfin_mac_local *lp = netdev_priv(dev);
466
467 if (lp->phydev)
468 return phy_ethtool_gset(lp->phydev, cmd);
469
470 return -EINVAL;
471}
472
473static int
474bfin_mac_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
475{
476 struct bfin_mac_local *lp = netdev_priv(dev);
477
478 if (!capable(CAP_NET_ADMIN))
479 return -EPERM;
480
481 if (lp->phydev)
482 return phy_ethtool_sset(lp->phydev, cmd);
483
484 return -EINVAL;
485}
486
487static void bfin_mac_ethtool_getdrvinfo(struct net_device *dev,
488 struct ethtool_drvinfo *info)
489{
490 strcpy(info->driver, DRV_NAME);
491 strcpy(info->version, DRV_VERSION);
492 strcpy(info->fw_version, "N/A");
493 strcpy(info->bus_info, dev->dev.bus_id);
494}
495
496static struct ethtool_ops bfin_mac_ethtool_ops = {
497 .get_settings = bfin_mac_ethtool_getsettings,
498 .set_settings = bfin_mac_ethtool_setsettings,
499 .get_link = ethtool_op_get_link,
500 .get_drvinfo = bfin_mac_ethtool_getdrvinfo,
501};
502
Bryan Wue190d6b2007-07-17 14:43:44 +0800503/**************************************************************************/
504void setup_system_regs(struct net_device *dev)
505{
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800506 unsigned short sysctl;
Bryan Wue190d6b2007-07-17 14:43:44 +0800507
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800508 /*
509 * Odd word alignment for Receive Frame DMA word
510 * Configure checksum support and rcve frame word alignment
511 */
512 sysctl = bfin_read_EMAC_SYSCTL();
Bryan Wue190d6b2007-07-17 14:43:44 +0800513#if defined(BFIN_MAC_CSUM_OFFLOAD)
514 sysctl |= RXDWA | RXCKS;
515#else
516 sysctl |= RXDWA;
517#endif
518 bfin_write_EMAC_SYSCTL(sysctl);
Bryan Wue190d6b2007-07-17 14:43:44 +0800519
520 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
521
522 /* Initialize the TX DMA channel registers */
523 bfin_write_DMA2_X_COUNT(0);
524 bfin_write_DMA2_X_MODIFY(4);
525 bfin_write_DMA2_Y_COUNT(0);
526 bfin_write_DMA2_Y_MODIFY(0);
527
528 /* Initialize the RX DMA channel registers */
529 bfin_write_DMA1_X_COUNT(0);
530 bfin_write_DMA1_X_MODIFY(4);
531 bfin_write_DMA1_Y_COUNT(0);
532 bfin_write_DMA1_Y_MODIFY(0);
533}
534
Alex Landau73f83182007-09-19 23:14:18 +0800535static void setup_mac_addr(u8 *mac_addr)
Bryan Wue190d6b2007-07-17 14:43:44 +0800536{
537 u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
538 u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
539
540 /* this depends on a little-endian machine */
541 bfin_write_EMAC_ADDRLO(addr_low);
542 bfin_write_EMAC_ADDRHI(addr_hi);
543}
544
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800545static int bfin_mac_set_mac_address(struct net_device *dev, void *p)
Alex Landau73f83182007-09-19 23:14:18 +0800546{
547 struct sockaddr *addr = p;
548 if (netif_running(dev))
549 return -EBUSY;
550 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
551 setup_mac_addr(dev->dev_addr);
552 return 0;
553}
554
Bryan Wue190d6b2007-07-17 14:43:44 +0800555static void adjust_tx_list(void)
556{
557 int timeout_cnt = MAX_TIMEOUT_CNT;
558
559 if (tx_list_head->status.status_word != 0
560 && current_tx_ptr != tx_list_head) {
561 goto adjust_head; /* released something, just return; */
562 }
563
564 /*
565 * if nothing released, check wait condition
566 * current's next can not be the head,
567 * otherwise the dma will not stop as we want
568 */
569 if (current_tx_ptr->next->next == tx_list_head) {
570 while (tx_list_head->status.status_word == 0) {
Bryan Wu6db9e462008-01-30 16:52:21 +0800571 mdelay(1);
Bryan Wue190d6b2007-07-17 14:43:44 +0800572 if (tx_list_head->status.status_word != 0
573 || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) {
574 goto adjust_head;
575 }
576 if (timeout_cnt-- < 0) {
577 printk(KERN_ERR DRV_NAME
578 ": wait for adjust tx list head timeout\n");
579 break;
580 }
581 }
582 if (tx_list_head->status.status_word != 0) {
583 goto adjust_head;
584 }
585 }
586
587 return;
588
589adjust_head:
590 do {
591 tx_list_head->desc_a.config &= ~DMAEN;
592 tx_list_head->status.status_word = 0;
593 if (tx_list_head->skb) {
594 dev_kfree_skb(tx_list_head->skb);
595 tx_list_head->skb = NULL;
596 } else {
597 printk(KERN_ERR DRV_NAME
598 ": no sk_buff in a transmitted frame!\n");
599 }
600 tx_list_head = tx_list_head->next;
601 } while (tx_list_head->status.status_word != 0
602 && current_tx_ptr != tx_list_head);
603 return;
604
605}
606
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800607static int bfin_mac_hard_start_xmit(struct sk_buff *skb,
Bryan Wue190d6b2007-07-17 14:43:44 +0800608 struct net_device *dev)
609{
Bryan Wue190d6b2007-07-17 14:43:44 +0800610 unsigned int data;
611
612 current_tx_ptr->skb = skb;
613
614 /*
615 * Is skb->data always 16-bit aligned?
616 * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)?
617 */
618 if ((((unsigned int)(skb->data)) & 0x02) == 2) {
619 /* move skb->data to current_tx_ptr payload */
620 data = (unsigned int)(skb->data) - 2;
621 *((unsigned short *)data) = (unsigned short)(skb->len);
622 current_tx_ptr->desc_a.start_addr = (unsigned long)data;
623 /* this is important! */
624 blackfin_dcache_flush_range(data, (data + (skb->len)) + 2);
625
626 } else {
627 *((unsigned short *)(current_tx_ptr->packet)) =
628 (unsigned short)(skb->len);
629 memcpy((char *)(current_tx_ptr->packet + 2), skb->data,
630 (skb->len));
631 current_tx_ptr->desc_a.start_addr =
632 (unsigned long)current_tx_ptr->packet;
633 if (current_tx_ptr->status.status_word != 0)
634 current_tx_ptr->status.status_word = 0;
635 blackfin_dcache_flush_range((unsigned int)current_tx_ptr->
636 packet,
637 (unsigned int)(current_tx_ptr->
638 packet + skb->len) +
639 2);
640 }
641
642 /* enable this packet's dma */
643 current_tx_ptr->desc_a.config |= DMAEN;
644
645 /* tx dma is running, just return */
646 if (bfin_read_DMA2_IRQ_STATUS() & 0x08)
647 goto out;
648
649 /* tx dma is not running */
650 bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
651 /* dma enabled, read from memory, size is 6 */
652 bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
653 /* Turn on the EMAC tx */
654 bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
655
656out:
657 adjust_tx_list();
658 current_tx_ptr = current_tx_ptr->next;
659 dev->trans_start = jiffies;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700660 dev->stats.tx_packets++;
661 dev->stats.tx_bytes += (skb->len);
Bryan Wue190d6b2007-07-17 14:43:44 +0800662 return 0;
663}
664
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800665static void bfin_mac_rx(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800666{
667 struct sk_buff *skb, *new_skb;
Bryan Wue190d6b2007-07-17 14:43:44 +0800668 unsigned short len;
669
670 /* allocate a new skb for next time receive */
671 skb = current_rx_ptr->skb;
672 new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
673 if (!new_skb) {
674 printk(KERN_NOTICE DRV_NAME
675 ": rx: low on mem - packet dropped\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700676 dev->stats.rx_dropped++;
Bryan Wue190d6b2007-07-17 14:43:44 +0800677 goto out;
678 }
679 /* reserve 2 bytes for RXDWA padding */
680 skb_reserve(new_skb, 2);
681 current_rx_ptr->skb = new_skb;
682 current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
683
Alexey Demin6e01d1a2008-01-30 16:52:27 +0800684 /* Invidate the data cache of skb->data range when it is write back
685 * cache. It will prevent overwritting the new data from DMA
686 */
687 blackfin_dcache_invalidate_range((unsigned long)new_skb->head,
688 (unsigned long)new_skb->end);
689
Bryan Wue190d6b2007-07-17 14:43:44 +0800690 len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
691 skb_put(skb, len);
692 blackfin_dcache_invalidate_range((unsigned long)skb->head,
693 (unsigned long)skb->tail);
694
695 dev->last_rx = jiffies;
696 skb->dev = dev;
697 skb->protocol = eth_type_trans(skb, dev);
698#if defined(BFIN_MAC_CSUM_OFFLOAD)
699 skb->csum = current_rx_ptr->status.ip_payload_csum;
Vitja Makarov00ff49a2007-11-23 17:55:51 +0800700 skb->ip_summed = CHECKSUM_COMPLETE;
Bryan Wue190d6b2007-07-17 14:43:44 +0800701#endif
702
703 netif_rx(skb);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700704 dev->stats.rx_packets++;
705 dev->stats.rx_bytes += len;
Bryan Wue190d6b2007-07-17 14:43:44 +0800706 current_rx_ptr->status.status_word = 0x00000000;
707 current_rx_ptr = current_rx_ptr->next;
708
709out:
710 return;
711}
712
713/* interrupt routine to handle rx and error signal */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800714static irqreturn_t bfin_mac_interrupt(int irq, void *dev_id)
Bryan Wue190d6b2007-07-17 14:43:44 +0800715{
716 struct net_device *dev = dev_id;
717 int number = 0;
718
719get_one_packet:
720 if (current_rx_ptr->status.status_word == 0) {
721 /* no more new packet received */
722 if (number == 0) {
723 if (current_rx_ptr->next->status.status_word != 0) {
724 current_rx_ptr = current_rx_ptr->next;
725 goto real_rx;
726 }
727 }
728 bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
729 DMA_DONE | DMA_ERR);
730 return IRQ_HANDLED;
731 }
732
733real_rx:
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800734 bfin_mac_rx(dev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800735 number++;
736 goto get_one_packet;
737}
738
739#ifdef CONFIG_NET_POLL_CONTROLLER
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800740static void bfin_mac_poll(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800741{
742 disable_irq(IRQ_MAC_RX);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800743 bfin_mac_interrupt(IRQ_MAC_RX, dev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800744 enable_irq(IRQ_MAC_RX);
745}
746#endif /* CONFIG_NET_POLL_CONTROLLER */
747
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800748static void bfin_mac_disable(void)
Bryan Wue190d6b2007-07-17 14:43:44 +0800749{
750 unsigned int opmode;
751
752 opmode = bfin_read_EMAC_OPMODE();
753 opmode &= (~RE);
754 opmode &= (~TE);
755 /* Turn off the EMAC */
756 bfin_write_EMAC_OPMODE(opmode);
757}
758
759/*
760 * Enable Interrupts, Receive, and Transmit
761 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800762static void bfin_mac_enable(void)
Bryan Wue190d6b2007-07-17 14:43:44 +0800763{
764 u32 opmode;
765
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800766 pr_debug("%s: %s\n", DRV_NAME, __FUNCTION__);
Bryan Wue190d6b2007-07-17 14:43:44 +0800767
768 /* Set RX DMA */
769 bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
770 bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
771
772 /* Wait MII done */
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800773 mdio_poll();
Bryan Wue190d6b2007-07-17 14:43:44 +0800774
775 /* We enable only RX here */
776 /* ASTP : Enable Automatic Pad Stripping
777 PR : Promiscuous Mode for test
778 PSF : Receive frames with total length less than 64 bytes.
779 FDMODE : Full Duplex Mode
780 LB : Internal Loopback for test
781 RE : Receiver Enable */
782 opmode = bfin_read_EMAC_OPMODE();
783 if (opmode & FDMODE)
784 opmode |= PSF;
785 else
786 opmode |= DRO | DC | PSF;
787 opmode |= RE;
788
789#if defined(CONFIG_BFIN_MAC_RMII)
790 opmode |= RMII; /* For Now only 100MBit are supported */
Michael Hennerich6893ff12008-01-30 16:52:25 +0800791#if (defined(CONFIG_BF537) || defined(CONFIG_BF536)) && CONFIG_BF_REV_0_2
Bryan Wue190d6b2007-07-17 14:43:44 +0800792 opmode |= TE;
793#endif
794#endif
795 /* Turn on the EMAC rx */
796 bfin_write_EMAC_OPMODE(opmode);
Bryan Wue190d6b2007-07-17 14:43:44 +0800797}
798
799/* Our watchdog timed out. Called by the networking layer */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800800static void bfin_mac_timeout(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800801{
802 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
803
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800804 bfin_mac_disable();
Bryan Wue190d6b2007-07-17 14:43:44 +0800805
806 /* reset tx queue */
807 tx_list_tail = tx_list_head->next;
808
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800809 bfin_mac_enable();
Bryan Wue190d6b2007-07-17 14:43:44 +0800810
811 /* We can accept TX packets again */
812 dev->trans_start = jiffies;
813 netif_wake_queue(dev);
814}
815
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800816static void bfin_mac_multicast_hash(struct net_device *dev)
Aidan Williams775919b2008-01-30 16:52:23 +0800817{
818 u32 emac_hashhi, emac_hashlo;
819 struct dev_mc_list *dmi = dev->mc_list;
820 char *addrs;
821 int i;
822 u32 crc;
823
824 emac_hashhi = emac_hashlo = 0;
825
826 for (i = 0; i < dev->mc_count; i++) {
827 addrs = dmi->dmi_addr;
828 dmi = dmi->next;
829
830 /* skip non-multicast addresses */
831 if (!(*addrs & 1))
832 continue;
833
834 crc = ether_crc(ETH_ALEN, addrs);
835 crc >>= 26;
836
837 if (crc & 0x20)
838 emac_hashhi |= 1 << (crc & 0x1f);
839 else
840 emac_hashlo |= 1 << (crc & 0x1f);
841 }
842
843 bfin_write_EMAC_HASHHI(emac_hashhi);
844 bfin_write_EMAC_HASHLO(emac_hashlo);
845
846 return;
847}
848
Bryan Wue190d6b2007-07-17 14:43:44 +0800849/*
Bryan Wue190d6b2007-07-17 14:43:44 +0800850 * This routine will, depending on the values passed to it,
851 * either make it accept multicast packets, go into
852 * promiscuous mode (for TCPDUMP and cousins) or accept
853 * a select set of multicast packets
854 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800855static void bfin_mac_set_multicast_list(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800856{
857 u32 sysctl;
858
859 if (dev->flags & IFF_PROMISC) {
860 printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
861 sysctl = bfin_read_EMAC_OPMODE();
862 sysctl |= RAF;
863 bfin_write_EMAC_OPMODE(sysctl);
Aidan Williams775919b2008-01-30 16:52:23 +0800864 } else if (dev->flags & IFF_ALLMULTI) {
Bryan Wue190d6b2007-07-17 14:43:44 +0800865 /* accept all multicast */
866 sysctl = bfin_read_EMAC_OPMODE();
867 sysctl |= PAM;
868 bfin_write_EMAC_OPMODE(sysctl);
Aidan Williams775919b2008-01-30 16:52:23 +0800869 } else if (dev->mc_count) {
870 /* set up multicast hash table */
871 sysctl = bfin_read_EMAC_OPMODE();
872 sysctl |= HM;
873 bfin_write_EMAC_OPMODE(sysctl);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800874 bfin_mac_multicast_hash(dev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800875 } else {
876 /* clear promisc or multicast mode */
877 sysctl = bfin_read_EMAC_OPMODE();
878 sysctl &= ~(RAF | PAM);
879 bfin_write_EMAC_OPMODE(sysctl);
880 }
881}
882
883/*
884 * this puts the device in an inactive state
885 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800886static void bfin_mac_shutdown(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800887{
888 /* Turn off the EMAC */
889 bfin_write_EMAC_OPMODE(0x00000000);
890 /* Turn off the EMAC RX DMA */
891 bfin_write_DMA1_CONFIG(0x0000);
892 bfin_write_DMA2_CONFIG(0x0000);
893}
894
895/*
896 * Open and Initialize the interface
897 *
898 * Set up everything, reset the card, etc..
899 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800900static int bfin_mac_open(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800901{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800902 struct bfin_mac_local *lp = netdev_priv(dev);
Michael Hennerich4af4b842007-07-25 14:09:54 +0800903 int retval;
Bryan Wue190d6b2007-07-17 14:43:44 +0800904 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
905
906 /*
907 * Check that the address is valid. If its not, refuse
908 * to bring the device up. The user must specify an
909 * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
910 */
911 if (!is_valid_ether_addr(dev->dev_addr)) {
912 printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
913 return -EINVAL;
914 }
915
916 /* initial rx and tx list */
Michael Hennerich4af4b842007-07-25 14:09:54 +0800917 retval = desc_list_init();
918
919 if (retval)
920 return retval;
Bryan Wue190d6b2007-07-17 14:43:44 +0800921
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800922 phy_start(lp->phydev);
Vitja Makarov136492b2008-01-30 16:52:26 +0800923 phy_write(lp->phydev, MII_BMCR, BMCR_RESET);
Bryan Wue190d6b2007-07-17 14:43:44 +0800924 setup_system_regs(dev);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800925 bfin_mac_disable();
926 bfin_mac_enable();
Bryan Wue190d6b2007-07-17 14:43:44 +0800927 pr_debug("hardware init finished\n");
928 netif_start_queue(dev);
929 netif_carrier_on(dev);
930
931 return 0;
932}
933
934/*
935 *
936 * this makes the board clean up everything that it can
937 * and not talk to the outside world. Caused by
938 * an 'ifconfig ethX down'
939 */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800940static int bfin_mac_close(struct net_device *dev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800941{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800942 struct bfin_mac_local *lp = netdev_priv(dev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800943 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
944
945 netif_stop_queue(dev);
946 netif_carrier_off(dev);
947
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800948 phy_stop(lp->phydev);
Vitja Makarov136492b2008-01-30 16:52:26 +0800949 phy_write(lp->phydev, MII_BMCR, BMCR_PDOWN);
Bryan Wu4ae5a3a2007-09-19 23:37:36 +0800950
Bryan Wue190d6b2007-07-17 14:43:44 +0800951 /* clear everything */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800952 bfin_mac_shutdown(dev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800953
954 /* free the rx/tx buffers */
955 desc_list_free();
956
957 return 0;
958}
959
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800960static int __init bfin_mac_probe(struct platform_device *pdev)
Bryan Wue190d6b2007-07-17 14:43:44 +0800961{
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800962 struct net_device *ndev;
963 struct bfin_mac_local *lp;
964 int rc, i;
965
966 ndev = alloc_etherdev(sizeof(struct bfin_mac_local));
967 if (!ndev) {
968 dev_err(&pdev->dev, "Cannot allocate net device!\n");
969 return -ENOMEM;
970 }
971
972 SET_NETDEV_DEV(ndev, &pdev->dev);
973 platform_set_drvdata(pdev, ndev);
974 lp = netdev_priv(ndev);
Bryan Wue190d6b2007-07-17 14:43:44 +0800975
976 /* Grab the MAC address in the MAC */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800977 *(__le32 *) (&(ndev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
978 *(__le16 *) (&(ndev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
Bryan Wue190d6b2007-07-17 14:43:44 +0800979
980 /* probe mac */
981 /*todo: how to proble? which is revision_register */
982 bfin_write_EMAC_ADDRLO(0x12345678);
983 if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800984 dev_err(&pdev->dev, "Cannot detect Blackfin on-chip ethernet MAC controller!\n");
985 rc = -ENODEV;
986 goto out_err_probe_mac;
Bryan Wue190d6b2007-07-17 14:43:44 +0800987 }
988
989 /* set the GPIO pins to Ethernet mode */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800990 rc = peripheral_request_list(pin_req, DRV_NAME);
991 if (rc) {
992 dev_err(&pdev->dev, "Requesting peripherals failed!\n");
993 rc = -EFAULT;
994 goto out_err_setup_pin_mux;
Bryan Wue190d6b2007-07-17 14:43:44 +0800995 }
996
Bryan Wu7ef0a7e2008-04-25 11:53:10 +0800997 /*
998 * Is it valid? (Did bootloader initialize it?)
999 * Grab the MAC from the board somehow
1000 * this is done in the arch/blackfin/mach-bfxxx/boards/eth_mac.c
1001 */
1002 if (!is_valid_ether_addr(ndev->dev_addr))
1003 bfin_get_ether_addr(ndev->dev_addr);
1004
Bryan Wue190d6b2007-07-17 14:43:44 +08001005 /* If still not valid, get a random one */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001006 if (!is_valid_ether_addr(ndev->dev_addr))
1007 random_ether_addr(ndev->dev_addr);
Bryan Wue190d6b2007-07-17 14:43:44 +08001008
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001009 setup_mac_addr(ndev->dev_addr);
Bryan Wue190d6b2007-07-17 14:43:44 +08001010
Bryan Wu4ae5a3a2007-09-19 23:37:36 +08001011 /* MDIO bus initial */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001012 lp->mii_bus.priv = ndev;
Bryan Wu4ae5a3a2007-09-19 23:37:36 +08001013 lp->mii_bus.read = mdiobus_read;
1014 lp->mii_bus.write = mdiobus_write;
1015 lp->mii_bus.reset = mdiobus_reset;
1016 lp->mii_bus.name = "bfin_mac_mdio";
Andy Fleming9d9326d32008-04-09 19:38:13 -05001017 snprintf(lp->mii_bus.id, MII_BUS_ID_SIZE, "0");
Bryan Wu4ae5a3a2007-09-19 23:37:36 +08001018 lp->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
1019 for (i = 0; i < PHY_MAX_ADDR; ++i)
1020 lp->mii_bus.irq[i] = PHY_POLL;
1021
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001022 rc = mdiobus_register(&lp->mii_bus);
1023 if (rc) {
1024 dev_err(&pdev->dev, "Cannot register MDIO bus!\n");
1025 goto out_err_mdiobus_register;
1026 }
Bryan Wu4ae5a3a2007-09-19 23:37:36 +08001027
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001028 rc = mii_probe(ndev);
1029 if (rc) {
1030 dev_err(&pdev->dev, "MII Probe failed!\n");
1031 goto out_err_mii_probe;
1032 }
Bryan Wu4ae5a3a2007-09-19 23:37:36 +08001033
Bryan Wue190d6b2007-07-17 14:43:44 +08001034 /* Fill in the fields of the device structure with ethernet values. */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001035 ether_setup(ndev);
Bryan Wue190d6b2007-07-17 14:43:44 +08001036
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001037 ndev->open = bfin_mac_open;
1038 ndev->stop = bfin_mac_close;
1039 ndev->hard_start_xmit = bfin_mac_hard_start_xmit;
1040 ndev->set_mac_address = bfin_mac_set_mac_address;
1041 ndev->tx_timeout = bfin_mac_timeout;
1042 ndev->set_multicast_list = bfin_mac_set_multicast_list;
Bryan Wue190d6b2007-07-17 14:43:44 +08001043#ifdef CONFIG_NET_POLL_CONTROLLER
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001044 ndev->poll_controller = bfin_mac_poll;
Bryan Wue190d6b2007-07-17 14:43:44 +08001045#endif
Bryan Wu679dce32008-04-25 11:53:11 +08001046 ndev->ethtool_ops = &bfin_mac_ethtool_ops;
Bryan Wue190d6b2007-07-17 14:43:44 +08001047
Bryan Wue190d6b2007-07-17 14:43:44 +08001048 spin_lock_init(&lp->lock);
1049
1050 /* now, enable interrupts */
1051 /* register irq handler */
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001052 rc = request_irq(IRQ_MAC_RX, bfin_mac_interrupt,
1053 IRQF_DISABLED | IRQF_SHARED, "EMAC_RX", ndev);
1054 if (rc) {
1055 dev_err(&pdev->dev, "Cannot request Blackfin MAC RX IRQ!\n");
1056 rc = -EBUSY;
1057 goto out_err_request_irq;
Bryan Wue190d6b2007-07-17 14:43:44 +08001058 }
1059
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001060 rc = register_netdev(ndev);
1061 if (rc) {
1062 dev_err(&pdev->dev, "Cannot register net device!\n");
1063 goto out_err_reg_ndev;
Bryan Wue190d6b2007-07-17 14:43:44 +08001064 }
1065
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001066 /* now, print out the card info, in a short format.. */
1067 dev_info(&pdev->dev, "%s, Version %s\n", DRV_DESC, DRV_VERSION);
Bryan Wue190d6b2007-07-17 14:43:44 +08001068
1069 return 0;
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001070
1071out_err_reg_ndev:
1072 free_irq(IRQ_MAC_RX, ndev);
1073out_err_request_irq:
1074out_err_mii_probe:
1075 mdiobus_unregister(&lp->mii_bus);
1076out_err_mdiobus_register:
1077 peripheral_free_list(pin_req);
1078out_err_setup_pin_mux:
1079out_err_probe_mac:
1080 platform_set_drvdata(pdev, NULL);
1081 free_netdev(ndev);
1082
1083 return rc;
Bryan Wue190d6b2007-07-17 14:43:44 +08001084}
1085
1086static int bfin_mac_remove(struct platform_device *pdev)
1087{
1088 struct net_device *ndev = platform_get_drvdata(pdev);
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001089 struct bfin_mac_local *lp = netdev_priv(ndev);
Bryan Wue190d6b2007-07-17 14:43:44 +08001090
1091 platform_set_drvdata(pdev, NULL);
1092
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001093 mdiobus_unregister(&lp->mii_bus);
1094
Bryan Wue190d6b2007-07-17 14:43:44 +08001095 unregister_netdev(ndev);
1096
1097 free_irq(IRQ_MAC_RX, ndev);
1098
1099 free_netdev(ndev);
1100
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001101 peripheral_free_list(pin_req);
Bryan Wue190d6b2007-07-17 14:43:44 +08001102
1103 return 0;
1104}
1105
Bryan Wu496a34c2007-09-19 23:37:14 +08001106#ifdef CONFIG_PM
1107static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg)
Bryan Wue190d6b2007-07-17 14:43:44 +08001108{
Bryan Wu496a34c2007-09-19 23:37:14 +08001109 struct net_device *net_dev = platform_get_drvdata(pdev);
1110
1111 if (netif_running(net_dev))
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001112 bfin_mac_close(net_dev);
Bryan Wu496a34c2007-09-19 23:37:14 +08001113
Bryan Wue190d6b2007-07-17 14:43:44 +08001114 return 0;
1115}
1116
1117static int bfin_mac_resume(struct platform_device *pdev)
1118{
Bryan Wu496a34c2007-09-19 23:37:14 +08001119 struct net_device *net_dev = platform_get_drvdata(pdev);
1120
1121 if (netif_running(net_dev))
Bryan Wu7ef0a7e2008-04-25 11:53:10 +08001122 bfin_mac_open(net_dev);
Bryan Wu496a34c2007-09-19 23:37:14 +08001123
Bryan Wue190d6b2007-07-17 14:43:44 +08001124 return 0;
1125}
Bryan Wu496a34c2007-09-19 23:37:14 +08001126#else
1127#define bfin_mac_suspend NULL
1128#define bfin_mac_resume NULL
1129#endif /* CONFIG_PM */
Bryan Wue190d6b2007-07-17 14:43:44 +08001130
1131static struct platform_driver bfin_mac_driver = {
1132 .probe = bfin_mac_probe,
1133 .remove = bfin_mac_remove,
1134 .resume = bfin_mac_resume,
1135 .suspend = bfin_mac_suspend,
1136 .driver = {
Kay Sievers72abb462008-04-18 13:50:44 -07001137 .name = DRV_NAME,
1138 .owner = THIS_MODULE,
1139 },
Bryan Wue190d6b2007-07-17 14:43:44 +08001140};
1141
1142static int __init bfin_mac_init(void)
1143{
1144 return platform_driver_register(&bfin_mac_driver);
1145}
1146
1147module_init(bfin_mac_init);
1148
1149static void __exit bfin_mac_cleanup(void)
1150{
1151 platform_driver_unregister(&bfin_mac_driver);
1152}
1153
1154module_exit(bfin_mac_cleanup);
Kay Sievers72abb462008-04-18 13:50:44 -07001155