2 * Blackfin On-Chip MAC Driver
4 * Copyright 2004-2007 Analog Devices Inc.
6 * Enter bugs at http://blackfin.uclinux.org/
8 * Licensed under the GPL-2 or later.
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>
21 #include <linux/ioport.h>
22 #include <linux/crc32.h>
23 #include <linux/device.h>
24 #include <linux/spinlock.h>
25 #include <linux/mii.h>
26 #include <linux/phy.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ethtool.h>
30 #include <linux/skbuff.h>
31 #include <linux/platform_device.h>
34 #include <linux/dma-mapping.h>
36 #include <asm/blackfin.h>
37 #include <asm/cacheflush.h>
38 #include <asm/portmux.h>
42 #define DRV_NAME "bfin_mac"
43 #define DRV_VERSION "1.1"
44 #define DRV_AUTHOR "Bryan Wu, Luke Yang"
45 #define DRV_DESC "Blackfin on-chip Ethernet MAC driver"
47 MODULE_AUTHOR(DRV_AUTHOR
);
48 MODULE_LICENSE("GPL");
49 MODULE_DESCRIPTION(DRV_DESC
);
50 MODULE_ALIAS("platform:bfin_mac");
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)
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)
62 #define PKT_BUF_SZ 1580
64 #define MAX_TIMEOUT_CNT 500
66 /* pointers to maintain transmit list */
67 static struct net_dma_desc_tx
*tx_list_head
;
68 static struct net_dma_desc_tx
*tx_list_tail
;
69 static struct net_dma_desc_rx
*rx_list_head
;
70 static struct net_dma_desc_rx
*rx_list_tail
;
71 static struct net_dma_desc_rx
*current_rx_ptr
;
72 static struct net_dma_desc_tx
*current_tx_ptr
;
73 static struct net_dma_desc_tx
*tx_desc
;
74 static struct net_dma_desc_rx
*rx_desc
;
76 #if defined(CONFIG_BFIN_MAC_RMII)
77 static u16 pin_req
[] = P_RMII0
;
79 static u16 pin_req
[] = P_MII0
;
82 static void bfin_mac_disable(void);
83 static void bfin_mac_enable(void);
85 static void desc_list_free(void)
87 struct net_dma_desc_rx
*r
;
88 struct net_dma_desc_tx
*t
;
90 #if !defined(CONFIG_BFIN_MAC_USE_L1)
91 dma_addr_t dma_handle
= 0;
96 for (i
= 0; i
< CONFIG_BFIN_TX_DESC_NUM
; i
++) {
99 dev_kfree_skb(t
->skb
);
105 bfin_mac_free(dma_handle
, tx_desc
);
110 for (i
= 0; i
< CONFIG_BFIN_RX_DESC_NUM
; i
++) {
113 dev_kfree_skb(r
->skb
);
119 bfin_mac_free(dma_handle
, rx_desc
);
123 static int desc_list_init(void)
126 struct sk_buff
*new_skb
;
127 #if !defined(CONFIG_BFIN_MAC_USE_L1)
129 * This dma_handle is useless in Blackfin dma_alloc_coherent().
130 * The real dma handler is the return value of dma_alloc_coherent().
132 dma_addr_t dma_handle
;
135 tx_desc
= bfin_mac_alloc(&dma_handle
,
136 sizeof(struct net_dma_desc_tx
) *
137 CONFIG_BFIN_TX_DESC_NUM
);
141 rx_desc
= bfin_mac_alloc(&dma_handle
,
142 sizeof(struct net_dma_desc_rx
) *
143 CONFIG_BFIN_RX_DESC_NUM
);
148 tx_list_head
= tx_list_tail
= tx_desc
;
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
);
157 * read from memory WNR = 0
158 * wordsize is 32 bits
159 * 6 half words is desc size
162 a
->config
= WDSIZE_32
| NDSIZE_6
| DMAFLOW_LARGE
;
163 a
->start_addr
= (unsigned long)t
->packet
;
165 a
->next_dma_desc
= b
;
169 * write to memory WNR = 1
170 * wordsize is 32 bits
172 * 6 half words is desc size
175 b
->config
= DMAEN
| WNR
| WDSIZE_32
| NDSIZE_6
| DMAFLOW_LARGE
;
176 b
->start_addr
= (unsigned long)(&(t
->status
));
180 tx_list_tail
->desc_b
.next_dma_desc
= a
;
181 tx_list_tail
->next
= t
;
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
;
189 rx_list_head
= rx_list_tail
= rx_desc
;
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
);
196 /* allocate a new skb for next time receive */
197 new_skb
= dev_alloc_skb(PKT_BUF_SZ
+ 2);
199 printk(KERN_NOTICE DRV_NAME
200 ": init: low on mem - packet dropped\n");
203 skb_reserve(new_skb
, 2);
208 * write to memory WNR = 1
209 * wordsize is 32 bits
211 * 6 half words is desc size
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;
218 a
->next_dma_desc
= b
;
222 * write to memory WNR = 1
223 * wordsize is 32 bits
225 * 6 half words is desc size
228 b
->config
= DMAEN
| WNR
| WDSIZE_32
| DI_EN
|
229 NDSIZE_6
| DMAFLOW_LARGE
;
230 b
->start_addr
= (unsigned long)(&(r
->status
));
233 rx_list_tail
->desc_b
.next_dma_desc
= a
;
234 rx_list_tail
->next
= r
;
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
;
245 printk(KERN_ERR DRV_NAME
": kmalloc failed\n");
250 /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
255 /* Wait until the previous MDC/MDIO transaction has completed */
256 static void bfin_mdio_poll(void)
258 int timeout_cnt
= MAX_TIMEOUT_CNT
;
260 /* poll the STABUSY bit */
261 while ((bfin_read_EMAC_STAADD()) & STABUSY
) {
263 if (timeout_cnt
-- < 0) {
264 printk(KERN_ERR DRV_NAME
265 ": wait MDC/MDIO transaction to complete timeout\n");
271 /* Read an off-chip register in a PHY through the MDC/MDIO port */
272 static int bfin_mdiobus_read(struct mii_bus
*bus
, int phy_addr
, int regnum
)
277 bfin_write_EMAC_STAADD(SET_PHYAD((u16
) phy_addr
) |
278 SET_REGAD((u16
) regnum
) |
283 return (int) bfin_read_EMAC_STADAT();
286 /* Write an off-chip register in a PHY through the MDC/MDIO port */
287 static int bfin_mdiobus_write(struct mii_bus
*bus
, int phy_addr
, int regnum
,
292 bfin_write_EMAC_STADAT((u32
) value
);
295 bfin_write_EMAC_STAADD(SET_PHYAD((u16
) phy_addr
) |
296 SET_REGAD((u16
) regnum
) |
305 static int bfin_mdiobus_reset(struct mii_bus
*bus
)
310 static void bfin_mac_adjust_link(struct net_device
*dev
)
312 struct bfin_mac_local
*lp
= netdev_priv(dev
);
313 struct phy_device
*phydev
= lp
->phydev
;
317 spin_lock_irqsave(&lp
->lock
, flags
);
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();
330 bfin_write_EMAC_OPMODE(opmode
);
331 lp
->old_duplex
= phydev
->duplex
;
334 if (phydev
->speed
!= lp
->old_speed
) {
335 #if defined(CONFIG_BFIN_MAC_RMII)
336 u32 opmode
= bfin_read_EMAC_OPMODE();
337 switch (phydev
->speed
) {
342 opmode
&= ~(RMII_10
);
346 "%s: Ack! Speed (%d) is not 10/100!\n",
347 DRV_NAME
, phydev
->speed
);
350 bfin_write_EMAC_OPMODE(opmode
);
354 lp
->old_speed
= phydev
->speed
;
361 } else if (lp
->old_link
) {
369 u32 opmode
= bfin_read_EMAC_OPMODE();
370 phy_print_status(phydev
);
371 pr_debug("EMAC_OPMODE = 0x%08x\n", opmode
);
374 spin_unlock_irqrestore(&lp
->lock
, flags
);
378 #define MDC_CLK 2500000
380 static int mii_probe(struct net_device
*dev
)
382 struct bfin_mac_local
*lp
= netdev_priv(dev
);
383 struct phy_device
*phydev
= NULL
;
384 unsigned short sysctl
;
388 /* Enable PHY output early */
389 if (!(bfin_read_VR_CTL() & PHYCLKOE
))
390 bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE
);
393 mdc_div
= ((sclk
/ MDC_CLK
) / 2) - 1;
395 sysctl
= bfin_read_EMAC_SYSCTL();
396 sysctl
= (sysctl
& ~MDCDIV
) | SET_MDCDIV(mdc_div
);
397 bfin_write_EMAC_SYSCTL(sysctl
);
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
];
404 continue; /* no PHY here... */
407 break; /* found it */
410 /* now we are supposed to have a proper phydev, to attach to... */
412 printk(KERN_INFO
"%s: Don't found any phy device at all\n",
417 #if defined(CONFIG_BFIN_MAC_RMII)
418 phydev
= phy_connect(dev
, dev_name(&phydev
->dev
), &bfin_mac_adjust_link
,
419 0, PHY_INTERFACE_MODE_RMII
);
421 phydev
= phy_connect(dev
, dev_name(&phydev
->dev
), &bfin_mac_adjust_link
,
422 0, PHY_INTERFACE_MODE_MII
);
425 if (IS_ERR(phydev
)) {
426 printk(KERN_ERR
"%s: Could not attach to PHY\n", dev
->name
);
427 return PTR_ERR(phydev
);
430 /* mask with MAC supported features */
431 phydev
->supported
&= (SUPPORTED_10baseT_Half
432 | SUPPORTED_10baseT_Full
433 | SUPPORTED_100baseT_Half
434 | SUPPORTED_100baseT_Full
436 | SUPPORTED_Pause
| SUPPORTED_Asym_Pause
440 phydev
->advertising
= phydev
->supported
;
447 printk(KERN_INFO
"%s: attached PHY driver [%s] "
448 "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)"
450 DRV_NAME
, phydev
->drv
->name
, dev_name(&phydev
->dev
), phydev
->irq
,
451 MDC_CLK
, mdc_div
, sclk
/1000000);
461 bfin_mac_ethtool_getsettings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
463 struct bfin_mac_local
*lp
= netdev_priv(dev
);
466 return phy_ethtool_gset(lp
->phydev
, cmd
);
472 bfin_mac_ethtool_setsettings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
474 struct bfin_mac_local
*lp
= netdev_priv(dev
);
476 if (!capable(CAP_NET_ADMIN
))
480 return phy_ethtool_sset(lp
->phydev
, cmd
);
485 static void bfin_mac_ethtool_getdrvinfo(struct net_device
*dev
,
486 struct ethtool_drvinfo
*info
)
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_name(&dev
->dev
));
494 static 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
,
501 /**************************************************************************/
502 void setup_system_regs(struct net_device
*dev
)
504 unsigned short sysctl
;
507 * Odd word alignment for Receive Frame DMA word
508 * Configure checksum support and rcve frame word alignment
510 sysctl
= bfin_read_EMAC_SYSCTL();
511 #if defined(BFIN_MAC_CSUM_OFFLOAD)
512 sysctl
|= RXDWA
| RXCKS
;
516 bfin_write_EMAC_SYSCTL(sysctl
);
518 bfin_write_EMAC_MMC_CTL(RSTC
| CROLL
);
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);
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);
533 static void setup_mac_addr(u8
*mac_addr
)
535 u32 addr_low
= le32_to_cpu(*(__le32
*) & mac_addr
[0]);
536 u16 addr_hi
= le16_to_cpu(*(__le16
*) & mac_addr
[4]);
538 /* this depends on a little-endian machine */
539 bfin_write_EMAC_ADDRLO(addr_low
);
540 bfin_write_EMAC_ADDRHI(addr_hi
);
543 static int bfin_mac_set_mac_address(struct net_device
*dev
, void *p
)
545 struct sockaddr
*addr
= p
;
546 if (netif_running(dev
))
548 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
549 setup_mac_addr(dev
->dev_addr
);
553 static void adjust_tx_list(void)
555 int timeout_cnt
= MAX_TIMEOUT_CNT
;
557 if (tx_list_head
->status
.status_word
!= 0
558 && current_tx_ptr
!= tx_list_head
) {
559 goto adjust_head
; /* released something, just return; */
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
567 if (current_tx_ptr
->next
->next
== tx_list_head
) {
568 while (tx_list_head
->status
.status_word
== 0) {
570 if (tx_list_head
->status
.status_word
!= 0
571 || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) {
574 if (timeout_cnt
-- < 0) {
575 printk(KERN_ERR DRV_NAME
576 ": wait for adjust tx list head timeout\n");
580 if (tx_list_head
->status
.status_word
!= 0) {
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
;
595 printk(KERN_ERR DRV_NAME
596 ": no sk_buff in a transmitted frame!\n");
598 tx_list_head
= tx_list_head
->next
;
599 } while (tx_list_head
->status
.status_word
!= 0
600 && current_tx_ptr
!= tx_list_head
);
605 static int bfin_mac_hard_start_xmit(struct sk_buff
*skb
,
606 struct net_device
*dev
)
610 current_tx_ptr
->skb
= skb
;
612 if (ANOMALY_05000285
) {
614 * TXDWA feature is not avaible to older revision < 0.3 silicon
617 * Only if data buffer is ODD WORD alignment, we do not
620 u32 data_align
= (u32
)(skb
->data
) & 0x3;
621 if (data_align
== 0x2) {
622 /* move skb->data to current_tx_ptr payload */
623 data
= (u16
*)(skb
->data
) - 1;
624 *data
= (u16
)(skb
->len
);
625 current_tx_ptr
->desc_a
.start_addr
= (u32
)data
;
626 /* this is important! */
627 blackfin_dcache_flush_range((u32
)data
,
628 (u32
)((u8
*)data
+ skb
->len
+ 4));
630 *((u16
*)(current_tx_ptr
->packet
)) = (u16
)(skb
->len
);
631 memcpy((u8
*)(current_tx_ptr
->packet
+ 2), skb
->data
,
633 current_tx_ptr
->desc_a
.start_addr
=
634 (u32
)current_tx_ptr
->packet
;
635 if (current_tx_ptr
->status
.status_word
!= 0)
636 current_tx_ptr
->status
.status_word
= 0;
637 blackfin_dcache_flush_range(
638 (u32
)current_tx_ptr
->packet
,
639 (u32
)(current_tx_ptr
->packet
+ skb
->len
+ 2));
643 * TXDWA feature is avaible to revision < 0.3 silicon of
644 * BF537 and always avaible to BF52x
646 u32 data_align
= (u32
)(skb
->data
) & 0x3;
647 if (data_align
== 0x0) {
648 u16 sysctl
= bfin_read_EMAC_SYSCTL();
650 bfin_write_EMAC_SYSCTL(sysctl
);
652 /* move skb->data to current_tx_ptr payload */
653 data
= (u16
*)(skb
->data
) - 2;
654 *data
= (u16
)(skb
->len
);
655 current_tx_ptr
->desc_a
.start_addr
= (u32
)data
;
656 /* this is important! */
657 blackfin_dcache_flush_range(
659 (u32
)((u8
*)data
+ skb
->len
+ 4));
660 } else if (data_align
== 0x2) {
661 u16 sysctl
= bfin_read_EMAC_SYSCTL();
663 bfin_write_EMAC_SYSCTL(sysctl
);
665 /* move skb->data to current_tx_ptr payload */
666 data
= (u16
*)(skb
->data
) - 1;
667 *data
= (u16
)(skb
->len
);
668 current_tx_ptr
->desc_a
.start_addr
= (u32
)data
;
669 /* this is important! */
670 blackfin_dcache_flush_range(
672 (u32
)((u8
*)data
+ skb
->len
+ 4));
674 u16 sysctl
= bfin_read_EMAC_SYSCTL();
676 bfin_write_EMAC_SYSCTL(sysctl
);
678 *((u16
*)(current_tx_ptr
->packet
)) = (u16
)(skb
->len
);
679 memcpy((u8
*)(current_tx_ptr
->packet
+ 2), skb
->data
,
681 current_tx_ptr
->desc_a
.start_addr
=
682 (u32
)current_tx_ptr
->packet
;
683 if (current_tx_ptr
->status
.status_word
!= 0)
684 current_tx_ptr
->status
.status_word
= 0;
685 blackfin_dcache_flush_range(
686 (u32
)current_tx_ptr
->packet
,
687 (u32
)(current_tx_ptr
->packet
+ skb
->len
+ 2));
691 /* enable this packet's dma */
692 current_tx_ptr
->desc_a
.config
|= DMAEN
;
694 /* tx dma is running, just return */
695 if (bfin_read_DMA2_IRQ_STATUS() & 0x08)
698 /* tx dma is not running */
699 bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr
->desc_a
));
700 /* dma enabled, read from memory, size is 6 */
701 bfin_write_DMA2_CONFIG(current_tx_ptr
->desc_a
.config
);
702 /* Turn on the EMAC tx */
703 bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE
);
707 current_tx_ptr
= current_tx_ptr
->next
;
708 dev
->trans_start
= jiffies
;
709 dev
->stats
.tx_packets
++;
710 dev
->stats
.tx_bytes
+= (skb
->len
);
714 static void bfin_mac_rx(struct net_device
*dev
)
716 struct sk_buff
*skb
, *new_skb
;
719 /* allocate a new skb for next time receive */
720 skb
= current_rx_ptr
->skb
;
721 new_skb
= dev_alloc_skb(PKT_BUF_SZ
+ 2);
723 printk(KERN_NOTICE DRV_NAME
724 ": rx: low on mem - packet dropped\n");
725 dev
->stats
.rx_dropped
++;
728 /* reserve 2 bytes for RXDWA padding */
729 skb_reserve(new_skb
, 2);
730 current_rx_ptr
->skb
= new_skb
;
731 current_rx_ptr
->desc_a
.start_addr
= (unsigned long)new_skb
->data
- 2;
733 /* Invidate the data cache of skb->data range when it is write back
734 * cache. It will prevent overwritting the new data from DMA
736 blackfin_dcache_invalidate_range((unsigned long)new_skb
->head
,
737 (unsigned long)new_skb
->end
);
739 len
= (unsigned short)((current_rx_ptr
->status
.status_word
) & RX_FRLEN
);
741 blackfin_dcache_invalidate_range((unsigned long)skb
->head
,
742 (unsigned long)skb
->tail
);
744 skb
->protocol
= eth_type_trans(skb
, dev
);
745 #if defined(BFIN_MAC_CSUM_OFFLOAD)
746 skb
->csum
= current_rx_ptr
->status
.ip_payload_csum
;
747 skb
->ip_summed
= CHECKSUM_COMPLETE
;
751 dev
->stats
.rx_packets
++;
752 dev
->stats
.rx_bytes
+= len
;
753 current_rx_ptr
->status
.status_word
= 0x00000000;
754 current_rx_ptr
= current_rx_ptr
->next
;
760 /* interrupt routine to handle rx and error signal */
761 static irqreturn_t
bfin_mac_interrupt(int irq
, void *dev_id
)
763 struct net_device
*dev
= dev_id
;
767 if (current_rx_ptr
->status
.status_word
== 0) {
768 /* no more new packet received */
770 if (current_rx_ptr
->next
->status
.status_word
!= 0) {
771 current_rx_ptr
= current_rx_ptr
->next
;
775 bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
786 #ifdef CONFIG_NET_POLL_CONTROLLER
787 static void bfin_mac_poll(struct net_device
*dev
)
789 disable_irq(IRQ_MAC_RX
);
790 bfin_mac_interrupt(IRQ_MAC_RX
, dev
);
791 enable_irq(IRQ_MAC_RX
);
793 #endif /* CONFIG_NET_POLL_CONTROLLER */
795 static void bfin_mac_disable(void)
799 opmode
= bfin_read_EMAC_OPMODE();
802 /* Turn off the EMAC */
803 bfin_write_EMAC_OPMODE(opmode
);
807 * Enable Interrupts, Receive, and Transmit
809 static void bfin_mac_enable(void)
813 pr_debug("%s: %s\n", DRV_NAME
, __func__
);
816 bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head
->desc_a
));
817 bfin_write_DMA1_CONFIG(rx_list_head
->desc_a
.config
);
822 /* We enable only RX here */
823 /* ASTP : Enable Automatic Pad Stripping
824 PR : Promiscuous Mode for test
825 PSF : Receive frames with total length less than 64 bytes.
826 FDMODE : Full Duplex Mode
827 LB : Internal Loopback for test
828 RE : Receiver Enable */
829 opmode
= bfin_read_EMAC_OPMODE();
833 opmode
|= DRO
| DC
| PSF
;
836 #if defined(CONFIG_BFIN_MAC_RMII)
837 opmode
|= RMII
; /* For Now only 100MBit are supported */
838 #if (defined(CONFIG_BF537) || defined(CONFIG_BF536)) && CONFIG_BF_REV_0_2
842 /* Turn on the EMAC rx */
843 bfin_write_EMAC_OPMODE(opmode
);
846 /* Our watchdog timed out. Called by the networking layer */
847 static void bfin_mac_timeout(struct net_device
*dev
)
849 pr_debug("%s: %s\n", dev
->name
, __func__
);
854 tx_list_tail
= tx_list_head
->next
;
858 /* We can accept TX packets again */
859 dev
->trans_start
= jiffies
;
860 netif_wake_queue(dev
);
863 static void bfin_mac_multicast_hash(struct net_device
*dev
)
865 u32 emac_hashhi
, emac_hashlo
;
866 struct dev_mc_list
*dmi
= dev
->mc_list
;
871 emac_hashhi
= emac_hashlo
= 0;
873 for (i
= 0; i
< dev
->mc_count
; i
++) {
874 addrs
= dmi
->dmi_addr
;
877 /* skip non-multicast addresses */
881 crc
= ether_crc(ETH_ALEN
, addrs
);
885 emac_hashhi
|= 1 << (crc
& 0x1f);
887 emac_hashlo
|= 1 << (crc
& 0x1f);
890 bfin_write_EMAC_HASHHI(emac_hashhi
);
891 bfin_write_EMAC_HASHLO(emac_hashlo
);
897 * This routine will, depending on the values passed to it,
898 * either make it accept multicast packets, go into
899 * promiscuous mode (for TCPDUMP and cousins) or accept
900 * a select set of multicast packets
902 static void bfin_mac_set_multicast_list(struct net_device
*dev
)
906 if (dev
->flags
& IFF_PROMISC
) {
907 printk(KERN_INFO
"%s: set to promisc mode\n", dev
->name
);
908 sysctl
= bfin_read_EMAC_OPMODE();
910 bfin_write_EMAC_OPMODE(sysctl
);
911 } else if (dev
->flags
& IFF_ALLMULTI
) {
912 /* accept all multicast */
913 sysctl
= bfin_read_EMAC_OPMODE();
915 bfin_write_EMAC_OPMODE(sysctl
);
916 } else if (dev
->mc_count
) {
917 /* set up multicast hash table */
918 sysctl
= bfin_read_EMAC_OPMODE();
920 bfin_write_EMAC_OPMODE(sysctl
);
921 bfin_mac_multicast_hash(dev
);
923 /* clear promisc or multicast mode */
924 sysctl
= bfin_read_EMAC_OPMODE();
925 sysctl
&= ~(RAF
| PAM
);
926 bfin_write_EMAC_OPMODE(sysctl
);
931 * this puts the device in an inactive state
933 static void bfin_mac_shutdown(struct net_device
*dev
)
935 /* Turn off the EMAC */
936 bfin_write_EMAC_OPMODE(0x00000000);
937 /* Turn off the EMAC RX DMA */
938 bfin_write_DMA1_CONFIG(0x0000);
939 bfin_write_DMA2_CONFIG(0x0000);
943 * Open and Initialize the interface
945 * Set up everything, reset the card, etc..
947 static int bfin_mac_open(struct net_device
*dev
)
949 struct bfin_mac_local
*lp
= netdev_priv(dev
);
951 pr_debug("%s: %s\n", dev
->name
, __func__
);
954 * Check that the address is valid. If its not, refuse
955 * to bring the device up. The user must specify an
956 * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
958 if (!is_valid_ether_addr(dev
->dev_addr
)) {
959 printk(KERN_WARNING DRV_NAME
": no valid ethernet hw addr\n");
963 /* initial rx and tx list */
964 retval
= desc_list_init();
969 phy_start(lp
->phydev
);
970 phy_write(lp
->phydev
, MII_BMCR
, BMCR_RESET
);
971 setup_system_regs(dev
);
972 setup_mac_addr(dev
->dev_addr
);
975 pr_debug("hardware init finished\n");
976 netif_start_queue(dev
);
977 netif_carrier_on(dev
);
982 static const struct net_device_ops bfin_mac_netdev_ops
= {
983 .ndo_open
= bfin_mac_open
,
984 .ndo_stop
= bfin_mac_close
,
985 .ndo_start_xmit
= bfin_mac_hard_start_xmit
,
986 .ndo_set_mac_address
= bfin_mac_set_mac_address
,
987 .ndo_tx_timeout
= bfin_mac_timeout
,
988 .ndo_set_multicast_list
= bfin_mac_set_multicast_list
,
989 .ndo_validate_addr
= eth_validate_addr
,
990 .ndo_change_mtu
= eth_change_mtu
,
991 #ifdef CONFIG_NET_POLL_CONTROLLER
992 .ndo_poll_controller
= bfin_mac_poll
,
998 * this makes the board clean up everything that it can
999 * and not talk to the outside world. Caused by
1000 * an 'ifconfig ethX down'
1002 static int bfin_mac_close(struct net_device
*dev
)
1004 struct bfin_mac_local
*lp
= netdev_priv(dev
);
1005 pr_debug("%s: %s\n", dev
->name
, __func__
);
1007 netif_stop_queue(dev
);
1008 netif_carrier_off(dev
);
1010 phy_stop(lp
->phydev
);
1011 phy_write(lp
->phydev
, MII_BMCR
, BMCR_PDOWN
);
1013 /* clear everything */
1014 bfin_mac_shutdown(dev
);
1016 /* free the rx/tx buffers */
1022 static int __devinit
bfin_mac_probe(struct platform_device
*pdev
)
1024 struct net_device
*ndev
;
1025 struct bfin_mac_local
*lp
;
1028 ndev
= alloc_etherdev(sizeof(struct bfin_mac_local
));
1030 dev_err(&pdev
->dev
, "Cannot allocate net device!\n");
1034 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
1035 platform_set_drvdata(pdev
, ndev
);
1036 lp
= netdev_priv(ndev
);
1038 /* Grab the MAC address in the MAC */
1039 *(__le32
*) (&(ndev
->dev_addr
[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
1040 *(__le16
*) (&(ndev
->dev_addr
[4])) = cpu_to_le16((u16
) bfin_read_EMAC_ADDRHI());
1043 /*todo: how to proble? which is revision_register */
1044 bfin_write_EMAC_ADDRLO(0x12345678);
1045 if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
1046 dev_err(&pdev
->dev
, "Cannot detect Blackfin on-chip ethernet MAC controller!\n");
1048 goto out_err_probe_mac
;
1051 /* set the GPIO pins to Ethernet mode */
1052 rc
= peripheral_request_list(pin_req
, DRV_NAME
);
1054 dev_err(&pdev
->dev
, "Requesting peripherals failed!\n");
1056 goto out_err_setup_pin_mux
;
1060 * Is it valid? (Did bootloader initialize it?)
1061 * Grab the MAC from the board somehow
1062 * this is done in the arch/blackfin/mach-bfxxx/boards/eth_mac.c
1064 if (!is_valid_ether_addr(ndev
->dev_addr
))
1065 bfin_get_ether_addr(ndev
->dev_addr
);
1067 /* If still not valid, get a random one */
1068 if (!is_valid_ether_addr(ndev
->dev_addr
))
1069 random_ether_addr(ndev
->dev_addr
);
1071 setup_mac_addr(ndev
->dev_addr
);
1073 /* MDIO bus initial */
1074 lp
->mii_bus
= mdiobus_alloc();
1075 if (lp
->mii_bus
== NULL
)
1076 goto out_err_mdiobus_alloc
;
1078 lp
->mii_bus
->priv
= ndev
;
1079 lp
->mii_bus
->read
= bfin_mdiobus_read
;
1080 lp
->mii_bus
->write
= bfin_mdiobus_write
;
1081 lp
->mii_bus
->reset
= bfin_mdiobus_reset
;
1082 lp
->mii_bus
->name
= "bfin_mac_mdio";
1083 snprintf(lp
->mii_bus
->id
, MII_BUS_ID_SIZE
, "0");
1084 lp
->mii_bus
->irq
= kmalloc(sizeof(int)*PHY_MAX_ADDR
, GFP_KERNEL
);
1085 for (i
= 0; i
< PHY_MAX_ADDR
; ++i
)
1086 lp
->mii_bus
->irq
[i
] = PHY_POLL
;
1088 rc
= mdiobus_register(lp
->mii_bus
);
1090 dev_err(&pdev
->dev
, "Cannot register MDIO bus!\n");
1091 goto out_err_mdiobus_register
;
1094 rc
= mii_probe(ndev
);
1096 dev_err(&pdev
->dev
, "MII Probe failed!\n");
1097 goto out_err_mii_probe
;
1100 /* Fill in the fields of the device structure with ethernet values. */
1103 ndev
->netdev_ops
= &bfin_mac_netdev_ops
;
1104 ndev
->ethtool_ops
= &bfin_mac_ethtool_ops
;
1106 spin_lock_init(&lp
->lock
);
1108 /* now, enable interrupts */
1109 /* register irq handler */
1110 rc
= request_irq(IRQ_MAC_RX
, bfin_mac_interrupt
,
1111 IRQF_DISABLED
| IRQF_SHARED
, "EMAC_RX", ndev
);
1113 dev_err(&pdev
->dev
, "Cannot request Blackfin MAC RX IRQ!\n");
1115 goto out_err_request_irq
;
1118 rc
= register_netdev(ndev
);
1120 dev_err(&pdev
->dev
, "Cannot register net device!\n");
1121 goto out_err_reg_ndev
;
1124 /* now, print out the card info, in a short format.. */
1125 dev_info(&pdev
->dev
, "%s, Version %s\n", DRV_DESC
, DRV_VERSION
);
1130 free_irq(IRQ_MAC_RX
, ndev
);
1131 out_err_request_irq
:
1133 mdiobus_unregister(lp
->mii_bus
);
1134 out_err_mdiobus_register
:
1135 mdiobus_free(lp
->mii_bus
);
1136 out_err_mdiobus_alloc
:
1137 peripheral_free_list(pin_req
);
1138 out_err_setup_pin_mux
:
1140 platform_set_drvdata(pdev
, NULL
);
1146 static int __devexit
bfin_mac_remove(struct platform_device
*pdev
)
1148 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1149 struct bfin_mac_local
*lp
= netdev_priv(ndev
);
1151 platform_set_drvdata(pdev
, NULL
);
1153 mdiobus_unregister(lp
->mii_bus
);
1154 mdiobus_free(lp
->mii_bus
);
1156 unregister_netdev(ndev
);
1158 free_irq(IRQ_MAC_RX
, ndev
);
1162 peripheral_free_list(pin_req
);
1168 static int bfin_mac_suspend(struct platform_device
*pdev
, pm_message_t mesg
)
1170 struct net_device
*net_dev
= platform_get_drvdata(pdev
);
1172 if (netif_running(net_dev
))
1173 bfin_mac_close(net_dev
);
1178 static int bfin_mac_resume(struct platform_device
*pdev
)
1180 struct net_device
*net_dev
= platform_get_drvdata(pdev
);
1182 if (netif_running(net_dev
))
1183 bfin_mac_open(net_dev
);
1188 #define bfin_mac_suspend NULL
1189 #define bfin_mac_resume NULL
1190 #endif /* CONFIG_PM */
1192 static struct platform_driver bfin_mac_driver
= {
1193 .probe
= bfin_mac_probe
,
1194 .remove
= __devexit_p(bfin_mac_remove
),
1195 .resume
= bfin_mac_resume
,
1196 .suspend
= bfin_mac_suspend
,
1199 .owner
= THIS_MODULE
,
1203 static int __init
bfin_mac_init(void)
1205 return platform_driver_register(&bfin_mac_driver
);
1208 module_init(bfin_mac_init
);
1210 static void __exit
bfin_mac_cleanup(void)
1212 platform_driver_unregister(&bfin_mac_driver
);
1215 module_exit(bfin_mac_cleanup
);