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/ethtool.h>
26 #include <linux/mii.h>
27 #include <linux/phy.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/ethtool.h>
31 #include <linux/skbuff.h>
32 #include <linux/platform_device.h>
35 #include <linux/dma-mapping.h>
37 #include <asm/blackfin.h>
38 #include <asm/cacheflush.h>
39 #include <asm/portmux.h>
43 #define DRV_NAME "bfin_mac"
44 #define DRV_VERSION "1.1"
45 #define DRV_AUTHOR "Bryan Wu, Luke Yang"
46 #define DRV_DESC "Blackfin on-chip Ethernet MAC driver"
48 MODULE_AUTHOR(DRV_AUTHOR);
49 MODULE_LICENSE("GPL");
50 MODULE_DESCRIPTION(DRV_DESC);
51 MODULE_ALIAS("platform:bfin_mac");
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)
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)
63 #define PKT_BUF_SZ 1580
65 #define MAX_TIMEOUT_CNT 500
67 /* pointers to maintain transmit list */
68 static struct net_dma_desc_tx *tx_list_head;
69 static struct net_dma_desc_tx *tx_list_tail;
70 static struct net_dma_desc_rx *rx_list_head;
71 static struct net_dma_desc_rx *rx_list_tail;
72 static struct net_dma_desc_rx *current_rx_ptr;
73 static struct net_dma_desc_tx *current_tx_ptr;
74 static struct net_dma_desc_tx *tx_desc;
75 static struct net_dma_desc_rx *rx_desc;
77 #if defined(CONFIG_BFIN_MAC_RMII)
78 static u16 pin_req[] = P_RMII0;
80 static u16 pin_req[] = P_MII0;
83 static void bfin_mac_disable(void);
84 static void bfin_mac_enable(void);
86 static void desc_list_free(void)
88 struct net_dma_desc_rx *r;
89 struct net_dma_desc_tx *t;
91 #if !defined(CONFIG_BFIN_MAC_USE_L1)
92 dma_addr_t dma_handle = 0;
97 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
100 dev_kfree_skb(t->skb);
106 bfin_mac_free(dma_handle, tx_desc);
111 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
114 dev_kfree_skb(r->skb);
120 bfin_mac_free(dma_handle, rx_desc);
124 static int desc_list_init(void)
127 struct sk_buff *new_skb;
128 #if !defined(CONFIG_BFIN_MAC_USE_L1)
130 * This dma_handle is useless in Blackfin dma_alloc_coherent().
131 * The real dma handler is the return value of dma_alloc_coherent().
133 dma_addr_t dma_handle;
136 tx_desc = bfin_mac_alloc(&dma_handle,
137 sizeof(struct net_dma_desc_tx) *
138 CONFIG_BFIN_TX_DESC_NUM);
142 rx_desc = bfin_mac_alloc(&dma_handle,
143 sizeof(struct net_dma_desc_rx) *
144 CONFIG_BFIN_RX_DESC_NUM);
149 tx_list_head = tx_list_tail = tx_desc;
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);
158 * read from memory WNR = 0
159 * wordsize is 32 bits
160 * 6 half words is desc size
163 a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
164 a->start_addr = (unsigned long)t->packet;
166 a->next_dma_desc = b;
170 * write to memory WNR = 1
171 * wordsize is 32 bits
173 * 6 half words is desc size
176 b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
177 b->start_addr = (unsigned long)(&(t->status));
181 tx_list_tail->desc_b.next_dma_desc = a;
182 tx_list_tail->next = t;
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;
190 rx_list_head = rx_list_tail = rx_desc;
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);
197 /* allocate a new skb for next time receive */
198 new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
200 printk(KERN_NOTICE DRV_NAME
201 ": init: low on mem - packet dropped\n");
204 skb_reserve(new_skb, 2);
209 * write to memory WNR = 1
210 * wordsize is 32 bits
212 * 6 half words is desc size
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;
219 a->next_dma_desc = b;
223 * write to memory WNR = 1
224 * wordsize is 32 bits
226 * 6 half words is desc size
229 b->config = DMAEN | WNR | WDSIZE_32 | DI_EN |
230 NDSIZE_6 | DMAFLOW_LARGE;
231 b->start_addr = (unsigned long)(&(r->status));
234 rx_list_tail->desc_b.next_dma_desc = a;
235 rx_list_tail->next = r;
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;
246 printk(KERN_ERR DRV_NAME ": kmalloc failed\n");
251 /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
256 /* Wait until the previous MDC/MDIO transaction has completed */
257 static void mdio_poll(void)
259 int timeout_cnt = MAX_TIMEOUT_CNT;
261 /* poll the STABUSY bit */
262 while ((bfin_read_EMAC_STAADD()) & STABUSY) {
264 if (timeout_cnt-- < 0) {
265 printk(KERN_ERR DRV_NAME
266 ": wait MDC/MDIO transaction to complete timeout\n");
272 /* Read an off-chip register in a PHY through the MDC/MDIO port */
273 static int mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
278 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
279 SET_REGAD((u16) regnum) |
284 return (int) bfin_read_EMAC_STADAT();
287 /* Write an off-chip register in a PHY through the MDC/MDIO port */
288 static int mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
293 bfin_write_EMAC_STADAT((u32) value);
296 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
297 SET_REGAD((u16) regnum) |
306 static int mdiobus_reset(struct mii_bus *bus)
311 static void bfin_mac_adjust_link(struct net_device *dev)
313 struct bfin_mac_local *lp = netdev_priv(dev);
314 struct phy_device *phydev = lp->phydev;
318 spin_lock_irqsave(&lp->lock, flags);
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();
331 bfin_write_EMAC_OPMODE(opmode);
332 lp->old_duplex = phydev->duplex;
335 if (phydev->speed != lp->old_speed) {
336 #if defined(CONFIG_BFIN_MAC_RMII)
337 u32 opmode = bfin_read_EMAC_OPMODE();
338 switch (phydev->speed) {
343 opmode &= ~(RMII_10);
347 "%s: Ack! Speed (%d) is not 10/100!\n",
348 DRV_NAME, phydev->speed);
351 bfin_write_EMAC_OPMODE(opmode);
355 lp->old_speed = phydev->speed;
363 } else if (lp->old_link) {
371 u32 opmode = bfin_read_EMAC_OPMODE();
372 phy_print_status(phydev);
373 pr_debug("EMAC_OPMODE = 0x%08x\n", opmode);
376 spin_unlock_irqrestore(&lp->lock, flags);
380 #define MDC_CLK 2500000
382 static int mii_probe(struct net_device *dev)
384 struct bfin_mac_local *lp = netdev_priv(dev);
385 struct phy_device *phydev = NULL;
386 unsigned short sysctl;
390 /* Enable PHY output early */
391 if (!(bfin_read_VR_CTL() & PHYCLKOE))
392 bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
395 mdc_div = ((sclk / MDC_CLK) / 2) - 1;
397 sysctl = bfin_read_EMAC_SYSCTL();
398 sysctl = (sysctl & ~MDCDIV) | SET_MDCDIV(mdc_div);
399 bfin_write_EMAC_SYSCTL(sysctl);
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];
406 continue; /* no PHY here... */
409 break; /* found it */
412 /* now we are supposed to have a proper phydev, to attach to... */
414 printk(KERN_INFO "%s: Don't found any phy device at all\n",
419 #if defined(CONFIG_BFIN_MAC_RMII)
420 phydev = phy_connect(dev, phydev->dev.bus_id, &bfin_mac_adjust_link, 0,
421 PHY_INTERFACE_MODE_RMII);
423 phydev = phy_connect(dev, phydev->dev.bus_id, &bfin_mac_adjust_link, 0,
424 PHY_INTERFACE_MODE_MII);
427 if (IS_ERR(phydev)) {
428 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
429 return PTR_ERR(phydev);
432 /* mask with MAC supported features */
433 phydev->supported &= (SUPPORTED_10baseT_Half
434 | SUPPORTED_10baseT_Full
435 | SUPPORTED_100baseT_Half
436 | SUPPORTED_100baseT_Full
438 | SUPPORTED_Pause | SUPPORTED_Asym_Pause
442 phydev->advertising = phydev->supported;
449 printk(KERN_INFO "%s: attached PHY driver [%s] "
450 "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)"
452 DRV_NAME, phydev->drv->name, phydev->dev.bus_id, phydev->irq,
453 MDC_CLK, mdc_div, sclk/1000000);
463 bfin_mac_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
465 struct bfin_mac_local *lp = netdev_priv(dev);
468 return phy_ethtool_gset(lp->phydev, cmd);
474 bfin_mac_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
476 struct bfin_mac_local *lp = netdev_priv(dev);
478 if (!capable(CAP_NET_ADMIN))
482 return phy_ethtool_sset(lp->phydev, cmd);
487 static void bfin_mac_ethtool_getdrvinfo(struct net_device *dev,
488 struct ethtool_drvinfo *info)
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);
496 static 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,
503 /**************************************************************************/
504 void setup_system_regs(struct net_device *dev)
506 unsigned short sysctl;
509 * Odd word alignment for Receive Frame DMA word
510 * Configure checksum support and rcve frame word alignment
512 sysctl = bfin_read_EMAC_SYSCTL();
513 #if defined(BFIN_MAC_CSUM_OFFLOAD)
514 sysctl |= RXDWA | RXCKS;
518 bfin_write_EMAC_SYSCTL(sysctl);
520 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
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);
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);
535 static void setup_mac_addr(u8 *mac_addr)
537 u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
538 u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
540 /* this depends on a little-endian machine */
541 bfin_write_EMAC_ADDRLO(addr_low);
542 bfin_write_EMAC_ADDRHI(addr_hi);
545 static int bfin_mac_set_mac_address(struct net_device *dev, void *p)
547 struct sockaddr *addr = p;
548 if (netif_running(dev))
550 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
551 setup_mac_addr(dev->dev_addr);
555 static void adjust_tx_list(void)
557 int timeout_cnt = MAX_TIMEOUT_CNT;
559 if (tx_list_head->status.status_word != 0
560 && current_tx_ptr != tx_list_head) {
561 goto adjust_head; /* released something, just return; */
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
569 if (current_tx_ptr->next->next == tx_list_head) {
570 while (tx_list_head->status.status_word == 0) {
572 if (tx_list_head->status.status_word != 0
573 || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) {
576 if (timeout_cnt-- < 0) {
577 printk(KERN_ERR DRV_NAME
578 ": wait for adjust tx list head timeout\n");
582 if (tx_list_head->status.status_word != 0) {
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;
597 printk(KERN_ERR DRV_NAME
598 ": no sk_buff in a transmitted frame!\n");
600 tx_list_head = tx_list_head->next;
601 } while (tx_list_head->status.status_word != 0
602 && current_tx_ptr != tx_list_head);
607 static int bfin_mac_hard_start_xmit(struct sk_buff *skb,
608 struct net_device *dev)
612 current_tx_ptr->skb = skb;
615 * Is skb->data always 16-bit aligned?
616 * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)?
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);
627 *((unsigned short *)(current_tx_ptr->packet)) =
628 (unsigned short)(skb->len);
629 memcpy((char *)(current_tx_ptr->packet + 2), skb->data,
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->
637 (unsigned int)(current_tx_ptr->
642 /* enable this packet's dma */
643 current_tx_ptr->desc_a.config |= DMAEN;
645 /* tx dma is running, just return */
646 if (bfin_read_DMA2_IRQ_STATUS() & 0x08)
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);
658 current_tx_ptr = current_tx_ptr->next;
659 dev->trans_start = jiffies;
660 dev->stats.tx_packets++;
661 dev->stats.tx_bytes += (skb->len);
665 static void bfin_mac_rx(struct net_device *dev)
667 struct sk_buff *skb, *new_skb;
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);
674 printk(KERN_NOTICE DRV_NAME
675 ": rx: low on mem - packet dropped\n");
676 dev->stats.rx_dropped++;
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;
684 /* Invidate the data cache of skb->data range when it is write back
685 * cache. It will prevent overwritting the new data from DMA
687 blackfin_dcache_invalidate_range((unsigned long)new_skb->head,
688 (unsigned long)new_skb->end);
690 len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
692 blackfin_dcache_invalidate_range((unsigned long)skb->head,
693 (unsigned long)skb->tail);
695 dev->last_rx = jiffies;
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;
700 skb->ip_summed = CHECKSUM_COMPLETE;
704 dev->stats.rx_packets++;
705 dev->stats.rx_bytes += len;
706 current_rx_ptr->status.status_word = 0x00000000;
707 current_rx_ptr = current_rx_ptr->next;
713 /* interrupt routine to handle rx and error signal */
714 static irqreturn_t bfin_mac_interrupt(int irq, void *dev_id)
716 struct net_device *dev = dev_id;
720 if (current_rx_ptr->status.status_word == 0) {
721 /* no more new packet received */
723 if (current_rx_ptr->next->status.status_word != 0) {
724 current_rx_ptr = current_rx_ptr->next;
728 bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
739 #ifdef CONFIG_NET_POLL_CONTROLLER
740 static void bfin_mac_poll(struct net_device *dev)
742 disable_irq(IRQ_MAC_RX);
743 bfin_mac_interrupt(IRQ_MAC_RX, dev);
744 enable_irq(IRQ_MAC_RX);
746 #endif /* CONFIG_NET_POLL_CONTROLLER */
748 static void bfin_mac_disable(void)
752 opmode = bfin_read_EMAC_OPMODE();
755 /* Turn off the EMAC */
756 bfin_write_EMAC_OPMODE(opmode);
760 * Enable Interrupts, Receive, and Transmit
762 static void bfin_mac_enable(void)
766 pr_debug("%s: %s\n", DRV_NAME, __FUNCTION__);
769 bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
770 bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
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();
786 opmode |= DRO | DC | PSF;
789 #if defined(CONFIG_BFIN_MAC_RMII)
790 opmode |= RMII; /* For Now only 100MBit are supported */
791 #if (defined(CONFIG_BF537) || defined(CONFIG_BF536)) && CONFIG_BF_REV_0_2
795 /* Turn on the EMAC rx */
796 bfin_write_EMAC_OPMODE(opmode);
799 /* Our watchdog timed out. Called by the networking layer */
800 static void bfin_mac_timeout(struct net_device *dev)
802 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
807 tx_list_tail = tx_list_head->next;
811 /* We can accept TX packets again */
812 dev->trans_start = jiffies;
813 netif_wake_queue(dev);
816 static void bfin_mac_multicast_hash(struct net_device *dev)
818 u32 emac_hashhi, emac_hashlo;
819 struct dev_mc_list *dmi = dev->mc_list;
824 emac_hashhi = emac_hashlo = 0;
826 for (i = 0; i < dev->mc_count; i++) {
827 addrs = dmi->dmi_addr;
830 /* skip non-multicast addresses */
834 crc = ether_crc(ETH_ALEN, addrs);
838 emac_hashhi |= 1 << (crc & 0x1f);
840 emac_hashlo |= 1 << (crc & 0x1f);
843 bfin_write_EMAC_HASHHI(emac_hashhi);
844 bfin_write_EMAC_HASHLO(emac_hashlo);
850 * 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
855 static void bfin_mac_set_multicast_list(struct net_device *dev)
859 if (dev->flags & IFF_PROMISC) {
860 printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
861 sysctl = bfin_read_EMAC_OPMODE();
863 bfin_write_EMAC_OPMODE(sysctl);
864 } else if (dev->flags & IFF_ALLMULTI) {
865 /* accept all multicast */
866 sysctl = bfin_read_EMAC_OPMODE();
868 bfin_write_EMAC_OPMODE(sysctl);
869 } else if (dev->mc_count) {
870 /* set up multicast hash table */
871 sysctl = bfin_read_EMAC_OPMODE();
873 bfin_write_EMAC_OPMODE(sysctl);
874 bfin_mac_multicast_hash(dev);
876 /* clear promisc or multicast mode */
877 sysctl = bfin_read_EMAC_OPMODE();
878 sysctl &= ~(RAF | PAM);
879 bfin_write_EMAC_OPMODE(sysctl);
884 * this puts the device in an inactive state
886 static void bfin_mac_shutdown(struct net_device *dev)
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);
896 * Open and Initialize the interface
898 * Set up everything, reset the card, etc..
900 static int bfin_mac_open(struct net_device *dev)
902 struct bfin_mac_local *lp = netdev_priv(dev);
904 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
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
911 if (!is_valid_ether_addr(dev->dev_addr)) {
912 printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
916 /* initial rx and tx list */
917 retval = desc_list_init();
922 phy_start(lp->phydev);
923 phy_write(lp->phydev, MII_BMCR, BMCR_RESET);
924 setup_system_regs(dev);
927 pr_debug("hardware init finished\n");
928 netif_start_queue(dev);
929 netif_carrier_on(dev);
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'
940 static int bfin_mac_close(struct net_device *dev)
942 struct bfin_mac_local *lp = netdev_priv(dev);
943 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
945 netif_stop_queue(dev);
946 netif_carrier_off(dev);
948 phy_stop(lp->phydev);
949 phy_write(lp->phydev, MII_BMCR, BMCR_PDOWN);
951 /* clear everything */
952 bfin_mac_shutdown(dev);
954 /* free the rx/tx buffers */
960 static int __init bfin_mac_probe(struct platform_device *pdev)
962 struct net_device *ndev;
963 struct bfin_mac_local *lp;
966 ndev = alloc_etherdev(sizeof(struct bfin_mac_local));
968 dev_err(&pdev->dev, "Cannot allocate net device!\n");
972 SET_NETDEV_DEV(ndev, &pdev->dev);
973 platform_set_drvdata(pdev, ndev);
974 lp = netdev_priv(ndev);
976 /* Grab the MAC address in the MAC */
977 *(__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());
981 /*todo: how to proble? which is revision_register */
982 bfin_write_EMAC_ADDRLO(0x12345678);
983 if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
984 dev_err(&pdev->dev, "Cannot detect Blackfin on-chip ethernet MAC controller!\n");
986 goto out_err_probe_mac;
989 /* set the GPIO pins to Ethernet mode */
990 rc = peripheral_request_list(pin_req, DRV_NAME);
992 dev_err(&pdev->dev, "Requesting peripherals failed!\n");
994 goto out_err_setup_pin_mux;
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
1002 if (!is_valid_ether_addr(ndev->dev_addr))
1003 bfin_get_ether_addr(ndev->dev_addr);
1005 /* If still not valid, get a random one */
1006 if (!is_valid_ether_addr(ndev->dev_addr))
1007 random_ether_addr(ndev->dev_addr);
1009 setup_mac_addr(ndev->dev_addr);
1011 /* MDIO bus initial */
1012 lp->mii_bus.priv = ndev;
1013 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";
1017 snprintf(lp->mii_bus.id, MII_BUS_ID_SIZE, "0");
1018 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;
1022 rc = mdiobus_register(&lp->mii_bus);
1024 dev_err(&pdev->dev, "Cannot register MDIO bus!\n");
1025 goto out_err_mdiobus_register;
1028 rc = mii_probe(ndev);
1030 dev_err(&pdev->dev, "MII Probe failed!\n");
1031 goto out_err_mii_probe;
1034 /* Fill in the fields of the device structure with ethernet values. */
1037 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;
1043 #ifdef CONFIG_NET_POLL_CONTROLLER
1044 ndev->poll_controller = bfin_mac_poll;
1046 ndev->ethtool_ops = &bfin_mac_ethtool_ops;
1048 spin_lock_init(&lp->lock);
1050 /* now, enable interrupts */
1051 /* register irq handler */
1052 rc = request_irq(IRQ_MAC_RX, bfin_mac_interrupt,
1053 IRQF_DISABLED | IRQF_SHARED, "EMAC_RX", ndev);
1055 dev_err(&pdev->dev, "Cannot request Blackfin MAC RX IRQ!\n");
1057 goto out_err_request_irq;
1060 rc = register_netdev(ndev);
1062 dev_err(&pdev->dev, "Cannot register net device!\n");
1063 goto out_err_reg_ndev;
1066 /* now, print out the card info, in a short format.. */
1067 dev_info(&pdev->dev, "%s, Version %s\n", DRV_DESC, DRV_VERSION);
1072 free_irq(IRQ_MAC_RX, ndev);
1073 out_err_request_irq:
1075 mdiobus_unregister(&lp->mii_bus);
1076 out_err_mdiobus_register:
1077 peripheral_free_list(pin_req);
1078 out_err_setup_pin_mux:
1080 platform_set_drvdata(pdev, NULL);
1086 static int bfin_mac_remove(struct platform_device *pdev)
1088 struct net_device *ndev = platform_get_drvdata(pdev);
1089 struct bfin_mac_local *lp = netdev_priv(ndev);
1091 platform_set_drvdata(pdev, NULL);
1093 mdiobus_unregister(&lp->mii_bus);
1095 unregister_netdev(ndev);
1097 free_irq(IRQ_MAC_RX, ndev);
1101 peripheral_free_list(pin_req);
1107 static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg)
1109 struct net_device *net_dev = platform_get_drvdata(pdev);
1111 if (netif_running(net_dev))
1112 bfin_mac_close(net_dev);
1117 static int bfin_mac_resume(struct platform_device *pdev)
1119 struct net_device *net_dev = platform_get_drvdata(pdev);
1121 if (netif_running(net_dev))
1122 bfin_mac_open(net_dev);
1127 #define bfin_mac_suspend NULL
1128 #define bfin_mac_resume NULL
1129 #endif /* CONFIG_PM */
1131 static 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,
1138 .owner = THIS_MODULE,
1142 static int __init bfin_mac_init(void)
1144 return platform_driver_register(&bfin_mac_driver);
1147 module_init(bfin_mac_init);
1149 static void __exit bfin_mac_cleanup(void)
1151 platform_driver_unregister(&bfin_mac_driver);
1154 module_exit(bfin_mac_cleanup);