Blackfin EMAC Driver: add proper __devinit/__devexit markings
[deliverable/linux.git] / drivers / net / bfin_mac.c
CommitLineData
e190d6b1 1/*
2fb9d6f5 2 * Blackfin On-Chip MAC Driver
e190d6b1 3 *
2fb9d6f5 4 * Copyright 2004-2007 Analog Devices Inc.
e190d6b1 5 *
2fb9d6f5 6 * Enter bugs at http://blackfin.uclinux.org/
e190d6b1 7 *
2fb9d6f5 8 * Licensed under the GPL-2 or later.
e190d6b1
BW
9 */
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>
e190d6b1 25#include <linux/mii.h>
4ae5a3ad 26#include <linux/phy.h>
e190d6b1
BW
27#include <linux/netdevice.h>
28#include <linux/etherdevice.h>
679dce39 29#include <linux/ethtool.h>
e190d6b1 30#include <linux/skbuff.h>
e190d6b1 31#include <linux/platform_device.h>
e190d6b1
BW
32
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"
7ef0a7ee 45#define DRV_DESC "Blackfin on-chip Ethernet MAC driver"
e190d6b1
BW
46
47MODULE_AUTHOR(DRV_AUTHOR);
48MODULE_LICENSE("GPL");
49MODULE_DESCRIPTION(DRV_DESC);
72abb461 50MODULE_ALIAS("platform:bfin_mac");
e190d6b1
BW
51
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
7ef0a7ee
BW
76#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);
4ae5a3ad 84
e190d6b1
BW
85static 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
4ae5a3ad
BW
252/*
253 * MII operations
254 */
e190d6b1 255/* Wait until the previous MDC/MDIO transaction has completed */
4ae5a3ad 256static void mdio_poll(void)
e190d6b1
BW
257{
258 int timeout_cnt = MAX_TIMEOUT_CNT;
259
260 /* poll the STABUSY bit */
261 while ((bfin_read_EMAC_STAADD()) & STABUSY) {
6db9e461 262 udelay(1);
e190d6b1
BW
263 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 */
4ae5a3ad 272static int mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
e190d6b1 273{
4ae5a3ad
BW
274 mdio_poll();
275
e190d6b1 276 /* read mode */
4ae5a3ad
BW
277 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
278 SET_REGAD((u16) regnum) |
e190d6b1 279 STABUSY);
e190d6b1 280
4ae5a3ad
BW
281 mdio_poll();
282
283 return (int) bfin_read_EMAC_STADAT();
e190d6b1
BW
284}
285
286/* Write an off-chip register in a PHY through the MDC/MDIO port */
4ae5a3ad
BW
287static int mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
288 u16 value)
e190d6b1 289{
4ae5a3ad
BW
290 mdio_poll();
291
292 bfin_write_EMAC_STADAT((u32) value);
e190d6b1
BW
293
294 /* write mode */
4ae5a3ad
BW
295 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
296 SET_REGAD((u16) regnum) |
e190d6b1
BW
297 STAOP |
298 STABUSY);
299
4ae5a3ad
BW
300 mdio_poll();
301
302 return 0;
e190d6b1
BW
303}
304
4ae5a3ad 305static int mdiobus_reset(struct mii_bus *bus)
e190d6b1 306{
4ae5a3ad 307 return 0;
e190d6b1
BW
308}
309
7ef0a7ee 310static void bfin_mac_adjust_link(struct net_device *dev)
e190d6b1 311{
7ef0a7ee 312 struct bfin_mac_local *lp = netdev_priv(dev);
4ae5a3ad
BW
313 struct phy_device *phydev = lp->phydev;
314 unsigned long flags;
315 int new_state = 0;
316
317 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;
324
325 if (phydev->duplex)
326 opmode |= FDMODE;
327 else
328 opmode &= ~(FDMODE);
329
330 bfin_write_EMAC_OPMODE(opmode);
331 lp->old_duplex = phydev->duplex;
332 }
e190d6b1 333
4ae5a3ad
BW
334 if (phydev->speed != lp->old_speed) {
335#if defined(CONFIG_BFIN_MAC_RMII)
336 u32 opmode = bfin_read_EMAC_OPMODE();
4ae5a3ad
BW
337 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);
4ae5a3ad 351#endif
e190d6b1 352
4ae5a3ad
BW
353 new_state = 1;
354 lp->old_speed = phydev->speed;
355 }
e190d6b1 356
4ae5a3ad
BW
357 if (!lp->old_link) {
358 new_state = 1;
359 lp->old_link = 1;
4ae5a3ad
BW
360 }
361 } else if (lp->old_link) {
362 new_state = 1;
363 lp->old_link = 0;
364 lp->old_speed = 0;
365 lp->old_duplex = -1;
e190d6b1
BW
366 }
367
4ae5a3ad
BW
368 if (new_state) {
369 u32 opmode = bfin_read_EMAC_OPMODE();
370 phy_print_status(phydev);
371 pr_debug("EMAC_OPMODE = 0x%08x\n", opmode);
e190d6b1 372 }
4ae5a3ad
BW
373
374 spin_unlock_irqrestore(&lp->lock, flags);
e190d6b1
BW
375}
376
7cc8f381
BW
377/* MDC = 2.5 MHz */
378#define MDC_CLK 2500000
379
4ae5a3ad 380static int mii_probe(struct net_device *dev)
e190d6b1 381{
7ef0a7ee 382 struct bfin_mac_local *lp = netdev_priv(dev);
4ae5a3ad
BW
383 struct phy_device *phydev = NULL;
384 unsigned short sysctl;
385 int i;
7cc8f381 386 u32 sclk, mdc_div;
e190d6b1 387
4ae5a3ad 388 /* Enable PHY output early */
e190d6b1
BW
389 if (!(bfin_read_VR_CTL() & PHYCLKOE))
390 bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
391
7cc8f381
BW
392 sclk = get_sclk();
393 mdc_div = ((sclk / MDC_CLK) / 2) - 1;
394
4ae5a3ad 395 sysctl = bfin_read_EMAC_SYSCTL();
9dc7f30e 396 sysctl = (sysctl & ~MDCDIV) | SET_MDCDIV(mdc_div);
e190d6b1 397 bfin_write_EMAC_SYSCTL(sysctl);
e190d6b1 398
4ae5a3ad
BW
399 /* search for connect PHY device */
400 for (i = 0; i < PHY_MAX_ADDR; i++) {
401 struct phy_device *const tmp_phydev = lp->mii_bus.phy_map[i];
e190d6b1 402
4ae5a3ad
BW
403 if (!tmp_phydev)
404 continue; /* no PHY here... */
e190d6b1 405
4ae5a3ad
BW
406 phydev = tmp_phydev;
407 break; /* found it */
408 }
409
410 /* now we are supposed to have a proper phydev, to attach to... */
411 if (!phydev) {
412 printk(KERN_INFO "%s: Don't found any phy device at all\n",
413 dev->name);
414 return -ENODEV;
e190d6b1
BW
415 }
416
417#if defined(CONFIG_BFIN_MAC_RMII)
7ef0a7ee 418 phydev = phy_connect(dev, phydev->dev.bus_id, &bfin_mac_adjust_link, 0,
4ae5a3ad
BW
419 PHY_INTERFACE_MODE_RMII);
420#else
7ef0a7ee 421 phydev = phy_connect(dev, phydev->dev.bus_id, &bfin_mac_adjust_link, 0,
4ae5a3ad 422 PHY_INTERFACE_MODE_MII);
e190d6b1
BW
423#endif
424
4ae5a3ad
BW
425 if (IS_ERR(phydev)) {
426 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
427 return PTR_ERR(phydev);
428 }
429
430 /* mask with MAC supported features */
431 phydev->supported &= (SUPPORTED_10baseT_Half
432 | SUPPORTED_10baseT_Full
433 | SUPPORTED_100baseT_Half
434 | SUPPORTED_100baseT_Full
435 | SUPPORTED_Autoneg
436 | SUPPORTED_Pause | SUPPORTED_Asym_Pause
437 | SUPPORTED_MII
438 | SUPPORTED_TP);
439
440 phydev->advertising = phydev->supported;
441
442 lp->old_link = 0;
443 lp->old_speed = 0;
444 lp->old_duplex = -1;
445 lp->phydev = phydev;
446
447 printk(KERN_INFO "%s: attached PHY driver [%s] "
7cc8f381
BW
448 "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)"
449 "@sclk=%dMHz)\n",
450 DRV_NAME, phydev->drv->name, phydev->dev.bus_id, phydev->irq,
451 MDC_CLK, mdc_div, sclk/1000000);
4ae5a3ad
BW
452
453 return 0;
454}
455
679dce39
BW
456/*
457 * Ethtool support
458 */
459
460static int
461bfin_mac_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
462{
463 struct bfin_mac_local *lp = netdev_priv(dev);
464
465 if (lp->phydev)
466 return phy_ethtool_gset(lp->phydev, cmd);
467
468 return -EINVAL;
469}
470
471static int
472bfin_mac_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
473{
474 struct bfin_mac_local *lp = netdev_priv(dev);
475
476 if (!capable(CAP_NET_ADMIN))
477 return -EPERM;
478
479 if (lp->phydev)
480 return phy_ethtool_sset(lp->phydev, cmd);
481
482 return -EINVAL;
483}
484
485static void bfin_mac_ethtool_getdrvinfo(struct net_device *dev,
486 struct ethtool_drvinfo *info)
487{
488 strcpy(info->driver, DRV_NAME);
489 strcpy(info->version, DRV_VERSION);
490 strcpy(info->fw_version, "N/A");
491 strcpy(info->bus_info, dev->dev.bus_id);
492}
493
494static struct ethtool_ops bfin_mac_ethtool_ops = {
495 .get_settings = bfin_mac_ethtool_getsettings,
496 .set_settings = bfin_mac_ethtool_setsettings,
497 .get_link = ethtool_op_get_link,
498 .get_drvinfo = bfin_mac_ethtool_getdrvinfo,
499};
500
4ae5a3ad
BW
501/**************************************************************************/
502void setup_system_regs(struct net_device *dev)
503{
504 unsigned short sysctl;
505
506 /*
507 * Odd word alignment for Receive Frame DMA word
508 * Configure checksum support and rcve frame word alignment
509 */
510 sysctl = bfin_read_EMAC_SYSCTL();
511#if defined(BFIN_MAC_CSUM_OFFLOAD)
512 sysctl |= RXDWA | RXCKS;
513#else
514 sysctl |= RXDWA;
515#endif
516 bfin_write_EMAC_SYSCTL(sysctl);
e190d6b1
BW
517
518 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
519
520 /* Initialize the TX DMA channel registers */
521 bfin_write_DMA2_X_COUNT(0);
522 bfin_write_DMA2_X_MODIFY(4);
523 bfin_write_DMA2_Y_COUNT(0);
524 bfin_write_DMA2_Y_MODIFY(0);
525
526 /* Initialize the RX DMA channel registers */
527 bfin_write_DMA1_X_COUNT(0);
528 bfin_write_DMA1_X_MODIFY(4);
529 bfin_write_DMA1_Y_COUNT(0);
530 bfin_write_DMA1_Y_MODIFY(0);
531}
532
73f83182 533static void setup_mac_addr(u8 *mac_addr)
e190d6b1
BW
534{
535 u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
536 u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
537
538 /* this depends on a little-endian machine */
539 bfin_write_EMAC_ADDRLO(addr_low);
540 bfin_write_EMAC_ADDRHI(addr_hi);
541}
542
7ef0a7ee 543static int bfin_mac_set_mac_address(struct net_device *dev, void *p)
73f83182
AL
544{
545 struct sockaddr *addr = p;
546 if (netif_running(dev))
547 return -EBUSY;
548 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
549 setup_mac_addr(dev->dev_addr);
550 return 0;
551}
552
e190d6b1
BW
553static void adjust_tx_list(void)
554{
555 int timeout_cnt = MAX_TIMEOUT_CNT;
556
557 if (tx_list_head->status.status_word != 0
558 && current_tx_ptr != tx_list_head) {
559 goto adjust_head; /* released something, just return; */
560 }
561
562 /*
563 * if nothing released, check wait condition
564 * current's next can not be the head,
565 * otherwise the dma will not stop as we want
566 */
567 if (current_tx_ptr->next->next == tx_list_head) {
568 while (tx_list_head->status.status_word == 0) {
6db9e461 569 mdelay(1);
e190d6b1
BW
570 if (tx_list_head->status.status_word != 0
571 || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) {
572 goto adjust_head;
573 }
574 if (timeout_cnt-- < 0) {
575 printk(KERN_ERR DRV_NAME
576 ": wait for adjust tx list head timeout\n");
577 break;
578 }
579 }
580 if (tx_list_head->status.status_word != 0) {
581 goto adjust_head;
582 }
583 }
584
585 return;
586
587adjust_head:
588 do {
589 tx_list_head->desc_a.config &= ~DMAEN;
590 tx_list_head->status.status_word = 0;
591 if (tx_list_head->skb) {
592 dev_kfree_skb(tx_list_head->skb);
593 tx_list_head->skb = NULL;
594 } else {
595 printk(KERN_ERR DRV_NAME
596 ": no sk_buff in a transmitted frame!\n");
597 }
598 tx_list_head = tx_list_head->next;
599 } while (tx_list_head->status.status_word != 0
600 && current_tx_ptr != tx_list_head);
601 return;
602
603}
604
7ef0a7ee 605static int bfin_mac_hard_start_xmit(struct sk_buff *skb,
e190d6b1
BW
606 struct net_device *dev)
607{
e190d6b1
BW
608 unsigned int data;
609
610 current_tx_ptr->skb = skb;
611
612 /*
613 * Is skb->data always 16-bit aligned?
614 * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)?
615 */
616 if ((((unsigned int)(skb->data)) & 0x02) == 2) {
617 /* move skb->data to current_tx_ptr payload */
618 data = (unsigned int)(skb->data) - 2;
619 *((unsigned short *)data) = (unsigned short)(skb->len);
620 current_tx_ptr->desc_a.start_addr = (unsigned long)data;
621 /* this is important! */
622 blackfin_dcache_flush_range(data, (data + (skb->len)) + 2);
623
624 } else {
625 *((unsigned short *)(current_tx_ptr->packet)) =
626 (unsigned short)(skb->len);
627 memcpy((char *)(current_tx_ptr->packet + 2), skb->data,
628 (skb->len));
629 current_tx_ptr->desc_a.start_addr =
630 (unsigned long)current_tx_ptr->packet;
631 if (current_tx_ptr->status.status_word != 0)
632 current_tx_ptr->status.status_word = 0;
633 blackfin_dcache_flush_range((unsigned int)current_tx_ptr->
634 packet,
635 (unsigned int)(current_tx_ptr->
636 packet + skb->len) +
637 2);
638 }
639
640 /* enable this packet's dma */
641 current_tx_ptr->desc_a.config |= DMAEN;
642
643 /* tx dma is running, just return */
644 if (bfin_read_DMA2_IRQ_STATUS() & 0x08)
645 goto out;
646
647 /* tx dma is not running */
648 bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
649 /* dma enabled, read from memory, size is 6 */
650 bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
651 /* Turn on the EMAC tx */
652 bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
653
654out:
655 adjust_tx_list();
656 current_tx_ptr = current_tx_ptr->next;
657 dev->trans_start = jiffies;
09f75cd7
JG
658 dev->stats.tx_packets++;
659 dev->stats.tx_bytes += (skb->len);
e190d6b1
BW
660 return 0;
661}
662
7ef0a7ee 663static void bfin_mac_rx(struct net_device *dev)
e190d6b1
BW
664{
665 struct sk_buff *skb, *new_skb;
e190d6b1
BW
666 unsigned short len;
667
668 /* allocate a new skb for next time receive */
669 skb = current_rx_ptr->skb;
670 new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
671 if (!new_skb) {
672 printk(KERN_NOTICE DRV_NAME
673 ": rx: low on mem - packet dropped\n");
09f75cd7 674 dev->stats.rx_dropped++;
e190d6b1
BW
675 goto out;
676 }
677 /* reserve 2 bytes for RXDWA padding */
678 skb_reserve(new_skb, 2);
679 current_rx_ptr->skb = new_skb;
680 current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
681
6e01d1a4
AD
682 /* Invidate the data cache of skb->data range when it is write back
683 * cache. It will prevent overwritting the new data from DMA
684 */
685 blackfin_dcache_invalidate_range((unsigned long)new_skb->head,
686 (unsigned long)new_skb->end);
687
e190d6b1
BW
688 len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
689 skb_put(skb, len);
690 blackfin_dcache_invalidate_range((unsigned long)skb->head,
691 (unsigned long)skb->tail);
692
693 dev->last_rx = jiffies;
694 skb->dev = dev;
695 skb->protocol = eth_type_trans(skb, dev);
696#if defined(BFIN_MAC_CSUM_OFFLOAD)
697 skb->csum = current_rx_ptr->status.ip_payload_csum;
00ff49a9 698 skb->ip_summed = CHECKSUM_COMPLETE;
e190d6b1
BW
699#endif
700
701 netif_rx(skb);
09f75cd7
JG
702 dev->stats.rx_packets++;
703 dev->stats.rx_bytes += len;
e190d6b1
BW
704 current_rx_ptr->status.status_word = 0x00000000;
705 current_rx_ptr = current_rx_ptr->next;
706
707out:
708 return;
709}
710
711/* interrupt routine to handle rx and error signal */
7ef0a7ee 712static irqreturn_t bfin_mac_interrupt(int irq, void *dev_id)
e190d6b1
BW
713{
714 struct net_device *dev = dev_id;
715 int number = 0;
716
717get_one_packet:
718 if (current_rx_ptr->status.status_word == 0) {
719 /* no more new packet received */
720 if (number == 0) {
721 if (current_rx_ptr->next->status.status_word != 0) {
722 current_rx_ptr = current_rx_ptr->next;
723 goto real_rx;
724 }
725 }
726 bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
727 DMA_DONE | DMA_ERR);
728 return IRQ_HANDLED;
729 }
730
731real_rx:
7ef0a7ee 732 bfin_mac_rx(dev);
e190d6b1
BW
733 number++;
734 goto get_one_packet;
735}
736
737#ifdef CONFIG_NET_POLL_CONTROLLER
7ef0a7ee 738static void bfin_mac_poll(struct net_device *dev)
e190d6b1
BW
739{
740 disable_irq(IRQ_MAC_RX);
7ef0a7ee 741 bfin_mac_interrupt(IRQ_MAC_RX, dev);
e190d6b1
BW
742 enable_irq(IRQ_MAC_RX);
743}
744#endif /* CONFIG_NET_POLL_CONTROLLER */
745
7ef0a7ee 746static void bfin_mac_disable(void)
e190d6b1
BW
747{
748 unsigned int opmode;
749
750 opmode = bfin_read_EMAC_OPMODE();
751 opmode &= (~RE);
752 opmode &= (~TE);
753 /* Turn off the EMAC */
754 bfin_write_EMAC_OPMODE(opmode);
755}
756
757/*
758 * Enable Interrupts, Receive, and Transmit
759 */
7ef0a7ee 760static void bfin_mac_enable(void)
e190d6b1
BW
761{
762 u32 opmode;
763
4ae5a3ad 764 pr_debug("%s: %s\n", DRV_NAME, __FUNCTION__);
e190d6b1
BW
765
766 /* Set RX DMA */
767 bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
768 bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
769
770 /* Wait MII done */
4ae5a3ad 771 mdio_poll();
e190d6b1
BW
772
773 /* We enable only RX here */
774 /* ASTP : Enable Automatic Pad Stripping
775 PR : Promiscuous Mode for test
776 PSF : Receive frames with total length less than 64 bytes.
777 FDMODE : Full Duplex Mode
778 LB : Internal Loopback for test
779 RE : Receiver Enable */
780 opmode = bfin_read_EMAC_OPMODE();
781 if (opmode & FDMODE)
782 opmode |= PSF;
783 else
784 opmode |= DRO | DC | PSF;
785 opmode |= RE;
786
787#if defined(CONFIG_BFIN_MAC_RMII)
788 opmode |= RMII; /* For Now only 100MBit are supported */
6893ff1c 789#if (defined(CONFIG_BF537) || defined(CONFIG_BF536)) && CONFIG_BF_REV_0_2
e190d6b1
BW
790 opmode |= TE;
791#endif
792#endif
793 /* Turn on the EMAC rx */
794 bfin_write_EMAC_OPMODE(opmode);
e190d6b1
BW
795}
796
797/* Our watchdog timed out. Called by the networking layer */
7ef0a7ee 798static void bfin_mac_timeout(struct net_device *dev)
e190d6b1
BW
799{
800 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
801
7ef0a7ee 802 bfin_mac_disable();
e190d6b1
BW
803
804 /* reset tx queue */
805 tx_list_tail = tx_list_head->next;
806
7ef0a7ee 807 bfin_mac_enable();
e190d6b1
BW
808
809 /* We can accept TX packets again */
810 dev->trans_start = jiffies;
811 netif_wake_queue(dev);
812}
813
7ef0a7ee 814static void bfin_mac_multicast_hash(struct net_device *dev)
775919bc
AW
815{
816 u32 emac_hashhi, emac_hashlo;
817 struct dev_mc_list *dmi = dev->mc_list;
818 char *addrs;
819 int i;
820 u32 crc;
821
822 emac_hashhi = emac_hashlo = 0;
823
824 for (i = 0; i < dev->mc_count; i++) {
825 addrs = dmi->dmi_addr;
826 dmi = dmi->next;
827
828 /* skip non-multicast addresses */
829 if (!(*addrs & 1))
830 continue;
831
832 crc = ether_crc(ETH_ALEN, addrs);
833 crc >>= 26;
834
835 if (crc & 0x20)
836 emac_hashhi |= 1 << (crc & 0x1f);
837 else
838 emac_hashlo |= 1 << (crc & 0x1f);
839 }
840
841 bfin_write_EMAC_HASHHI(emac_hashhi);
842 bfin_write_EMAC_HASHLO(emac_hashlo);
843
844 return;
845}
846
e190d6b1
BW
847/*
848 * This routine will, depending on the values passed to it,
849 * either make it accept multicast packets, go into
850 * promiscuous mode (for TCPDUMP and cousins) or accept
851 * a select set of multicast packets
852 */
7ef0a7ee 853static void bfin_mac_set_multicast_list(struct net_device *dev)
e190d6b1
BW
854{
855 u32 sysctl;
856
857 if (dev->flags & IFF_PROMISC) {
858 printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
859 sysctl = bfin_read_EMAC_OPMODE();
860 sysctl |= RAF;
861 bfin_write_EMAC_OPMODE(sysctl);
775919bc 862 } else if (dev->flags & IFF_ALLMULTI) {
e190d6b1
BW
863 /* accept all multicast */
864 sysctl = bfin_read_EMAC_OPMODE();
865 sysctl |= PAM;
866 bfin_write_EMAC_OPMODE(sysctl);
775919bc
AW
867 } else if (dev->mc_count) {
868 /* set up multicast hash table */
869 sysctl = bfin_read_EMAC_OPMODE();
870 sysctl |= HM;
871 bfin_write_EMAC_OPMODE(sysctl);
7ef0a7ee 872 bfin_mac_multicast_hash(dev);
e190d6b1
BW
873 } else {
874 /* clear promisc or multicast mode */
875 sysctl = bfin_read_EMAC_OPMODE();
876 sysctl &= ~(RAF | PAM);
877 bfin_write_EMAC_OPMODE(sysctl);
878 }
879}
880
881/*
882 * this puts the device in an inactive state
883 */
7ef0a7ee 884static void bfin_mac_shutdown(struct net_device *dev)
e190d6b1
BW
885{
886 /* Turn off the EMAC */
887 bfin_write_EMAC_OPMODE(0x00000000);
888 /* Turn off the EMAC RX DMA */
889 bfin_write_DMA1_CONFIG(0x0000);
890 bfin_write_DMA2_CONFIG(0x0000);
891}
892
893/*
894 * Open and Initialize the interface
895 *
896 * Set up everything, reset the card, etc..
897 */
7ef0a7ee 898static int bfin_mac_open(struct net_device *dev)
e190d6b1 899{
7ef0a7ee 900 struct bfin_mac_local *lp = netdev_priv(dev);
4af4b840 901 int retval;
e190d6b1
BW
902 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
903
904 /*
905 * Check that the address is valid. If its not, refuse
906 * to bring the device up. The user must specify an
907 * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
908 */
909 if (!is_valid_ether_addr(dev->dev_addr)) {
910 printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
911 return -EINVAL;
912 }
913
914 /* initial rx and tx list */
4af4b840
MH
915 retval = desc_list_init();
916
917 if (retval)
918 return retval;
e190d6b1 919
4ae5a3ad 920 phy_start(lp->phydev);
136492b2 921 phy_write(lp->phydev, MII_BMCR, BMCR_RESET);
e190d6b1 922 setup_system_regs(dev);
7ef0a7ee
BW
923 bfin_mac_disable();
924 bfin_mac_enable();
e190d6b1
BW
925 pr_debug("hardware init finished\n");
926 netif_start_queue(dev);
927 netif_carrier_on(dev);
928
929 return 0;
930}
931
932/*
933 *
934 * this makes the board clean up everything that it can
935 * and not talk to the outside world. Caused by
936 * an 'ifconfig ethX down'
937 */
7ef0a7ee 938static int bfin_mac_close(struct net_device *dev)
e190d6b1 939{
7ef0a7ee 940 struct bfin_mac_local *lp = netdev_priv(dev);
e190d6b1
BW
941 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
942
943 netif_stop_queue(dev);
944 netif_carrier_off(dev);
945
4ae5a3ad 946 phy_stop(lp->phydev);
136492b2 947 phy_write(lp->phydev, MII_BMCR, BMCR_PDOWN);
4ae5a3ad 948
e190d6b1 949 /* clear everything */
7ef0a7ee 950 bfin_mac_shutdown(dev);
e190d6b1
BW
951
952 /* free the rx/tx buffers */
953 desc_list_free();
954
955 return 0;
956}
957
d7b843d3 958static int __devinit bfin_mac_probe(struct platform_device *pdev)
e190d6b1 959{
7ef0a7ee
BW
960 struct net_device *ndev;
961 struct bfin_mac_local *lp;
962 int rc, i;
963
964 ndev = alloc_etherdev(sizeof(struct bfin_mac_local));
965 if (!ndev) {
966 dev_err(&pdev->dev, "Cannot allocate net device!\n");
967 return -ENOMEM;
968 }
969
970 SET_NETDEV_DEV(ndev, &pdev->dev);
971 platform_set_drvdata(pdev, ndev);
972 lp = netdev_priv(ndev);
e190d6b1
BW
973
974 /* Grab the MAC address in the MAC */
7ef0a7ee
BW
975 *(__le32 *) (&(ndev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
976 *(__le16 *) (&(ndev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
e190d6b1
BW
977
978 /* probe mac */
979 /*todo: how to proble? which is revision_register */
980 bfin_write_EMAC_ADDRLO(0x12345678);
981 if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
7ef0a7ee
BW
982 dev_err(&pdev->dev, "Cannot detect Blackfin on-chip ethernet MAC controller!\n");
983 rc = -ENODEV;
984 goto out_err_probe_mac;
e190d6b1
BW
985 }
986
987 /* set the GPIO pins to Ethernet mode */
7ef0a7ee
BW
988 rc = peripheral_request_list(pin_req, DRV_NAME);
989 if (rc) {
990 dev_err(&pdev->dev, "Requesting peripherals failed!\n");
991 rc = -EFAULT;
992 goto out_err_setup_pin_mux;
e190d6b1
BW
993 }
994
7ef0a7ee
BW
995 /*
996 * Is it valid? (Did bootloader initialize it?)
997 * Grab the MAC from the board somehow
998 * this is done in the arch/blackfin/mach-bfxxx/boards/eth_mac.c
999 */
1000 if (!is_valid_ether_addr(ndev->dev_addr))
1001 bfin_get_ether_addr(ndev->dev_addr);
1002
e190d6b1 1003 /* If still not valid, get a random one */
7ef0a7ee
BW
1004 if (!is_valid_ether_addr(ndev->dev_addr))
1005 random_ether_addr(ndev->dev_addr);
e190d6b1 1006
7ef0a7ee 1007 setup_mac_addr(ndev->dev_addr);
e190d6b1 1008
4ae5a3ad 1009 /* MDIO bus initial */
7ef0a7ee 1010 lp->mii_bus.priv = ndev;
4ae5a3ad
BW
1011 lp->mii_bus.read = mdiobus_read;
1012 lp->mii_bus.write = mdiobus_write;
1013 lp->mii_bus.reset = mdiobus_reset;
1014 lp->mii_bus.name = "bfin_mac_mdio";
9d9326d3 1015 snprintf(lp->mii_bus.id, MII_BUS_ID_SIZE, "0");
4ae5a3ad
BW
1016 lp->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
1017 for (i = 0; i < PHY_MAX_ADDR; ++i)
1018 lp->mii_bus.irq[i] = PHY_POLL;
1019
7ef0a7ee
BW
1020 rc = mdiobus_register(&lp->mii_bus);
1021 if (rc) {
1022 dev_err(&pdev->dev, "Cannot register MDIO bus!\n");
1023 goto out_err_mdiobus_register;
1024 }
4ae5a3ad 1025
7ef0a7ee
BW
1026 rc = mii_probe(ndev);
1027 if (rc) {
1028 dev_err(&pdev->dev, "MII Probe failed!\n");
1029 goto out_err_mii_probe;
1030 }
4ae5a3ad 1031
e190d6b1 1032 /* Fill in the fields of the device structure with ethernet values. */
7ef0a7ee
BW
1033 ether_setup(ndev);
1034
1035 ndev->open = bfin_mac_open;
1036 ndev->stop = bfin_mac_close;
1037 ndev->hard_start_xmit = bfin_mac_hard_start_xmit;
1038 ndev->set_mac_address = bfin_mac_set_mac_address;
1039 ndev->tx_timeout = bfin_mac_timeout;
1040 ndev->set_multicast_list = bfin_mac_set_multicast_list;
e190d6b1 1041#ifdef CONFIG_NET_POLL_CONTROLLER
7ef0a7ee 1042 ndev->poll_controller = bfin_mac_poll;
e190d6b1 1043#endif
679dce39 1044 ndev->ethtool_ops = &bfin_mac_ethtool_ops;
e190d6b1 1045
e190d6b1
BW
1046 spin_lock_init(&lp->lock);
1047
1048 /* now, enable interrupts */
1049 /* register irq handler */
7ef0a7ee
BW
1050 rc = request_irq(IRQ_MAC_RX, bfin_mac_interrupt,
1051 IRQF_DISABLED | IRQF_SHARED, "EMAC_RX", ndev);
1052 if (rc) {
1053 dev_err(&pdev->dev, "Cannot request Blackfin MAC RX IRQ!\n");
1054 rc = -EBUSY;
1055 goto out_err_request_irq;
e190d6b1
BW
1056 }
1057
7ef0a7ee
BW
1058 rc = register_netdev(ndev);
1059 if (rc) {
1060 dev_err(&pdev->dev, "Cannot register net device!\n");
1061 goto out_err_reg_ndev;
e190d6b1
BW
1062 }
1063
7ef0a7ee
BW
1064 /* now, print out the card info, in a short format.. */
1065 dev_info(&pdev->dev, "%s, Version %s\n", DRV_DESC, DRV_VERSION);
e190d6b1 1066
7ef0a7ee 1067 return 0;
e190d6b1 1068
7ef0a7ee
BW
1069out_err_reg_ndev:
1070 free_irq(IRQ_MAC_RX, ndev);
1071out_err_request_irq:
1072out_err_mii_probe:
1073 mdiobus_unregister(&lp->mii_bus);
1074out_err_mdiobus_register:
1075 peripheral_free_list(pin_req);
1076out_err_setup_pin_mux:
1077out_err_probe_mac:
1078 platform_set_drvdata(pdev, NULL);
1079 free_netdev(ndev);
e190d6b1 1080
7ef0a7ee 1081 return rc;
e190d6b1
BW
1082}
1083
d7b843d3 1084static int __devexit bfin_mac_remove(struct platform_device *pdev)
e190d6b1
BW
1085{
1086 struct net_device *ndev = platform_get_drvdata(pdev);
7ef0a7ee 1087 struct bfin_mac_local *lp = netdev_priv(ndev);
e190d6b1
BW
1088
1089 platform_set_drvdata(pdev, NULL);
1090
7ef0a7ee
BW
1091 mdiobus_unregister(&lp->mii_bus);
1092
e190d6b1
BW
1093 unregister_netdev(ndev);
1094
1095 free_irq(IRQ_MAC_RX, ndev);
1096
1097 free_netdev(ndev);
1098
7ef0a7ee 1099 peripheral_free_list(pin_req);
e190d6b1
BW
1100
1101 return 0;
1102}
1103
496a34c2
BW
1104#ifdef CONFIG_PM
1105static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg)
e190d6b1 1106{
496a34c2
BW
1107 struct net_device *net_dev = platform_get_drvdata(pdev);
1108
1109 if (netif_running(net_dev))
7ef0a7ee 1110 bfin_mac_close(net_dev);
496a34c2 1111
e190d6b1
BW
1112 return 0;
1113}
1114
1115static int bfin_mac_resume(struct platform_device *pdev)
1116{
496a34c2
BW
1117 struct net_device *net_dev = platform_get_drvdata(pdev);
1118
1119 if (netif_running(net_dev))
7ef0a7ee 1120 bfin_mac_open(net_dev);
496a34c2 1121
e190d6b1
BW
1122 return 0;
1123}
496a34c2
BW
1124#else
1125#define bfin_mac_suspend NULL
1126#define bfin_mac_resume NULL
1127#endif /* CONFIG_PM */
e190d6b1
BW
1128
1129static struct platform_driver bfin_mac_driver = {
1130 .probe = bfin_mac_probe,
d7b843d3 1131 .remove = __devexit_p(bfin_mac_remove),
e190d6b1
BW
1132 .resume = bfin_mac_resume,
1133 .suspend = bfin_mac_suspend,
1134 .driver = {
72abb461
KS
1135 .name = DRV_NAME,
1136 .owner = THIS_MODULE,
1137 },
e190d6b1
BW
1138};
1139
1140static int __init bfin_mac_init(void)
1141{
1142 return platform_driver_register(&bfin_mac_driver);
1143}
1144
1145module_init(bfin_mac_init);
1146
1147static void __exit bfin_mac_cleanup(void)
1148{
1149 platform_driver_unregister(&bfin_mac_driver);
1150}
1151
1152module_exit(bfin_mac_cleanup);
72abb461 1153
This page took 0.18572 seconds and 5 git commands to generate.