2 * File: drivers/net/bfin_mac.c
14 * 2006-12-19 Aidan Williams, multicast hash support
15 * Copyright 2004-2006 Analog Devices Inc.
17 * Bugs: Enter bugs at http://blackfin.uclinux.org/
19 * This program is free software ; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation ; either version 2, or (at your option)
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program ; see the file COPYING.
31 * If not, write to the Free Software Foundation,
32 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/delay.h>
41 #include <linux/timer.h>
42 #include <linux/errno.h>
43 #include <linux/irq.h>
45 #include <linux/ioport.h>
46 #include <linux/crc32.h>
47 #include <linux/device.h>
48 #include <linux/spinlock.h>
49 #include <linux/ethtool.h>
50 #include <linux/mii.h>
51 #include <linux/phy.h>
52 #include <linux/netdevice.h>
53 #include <linux/etherdevice.h>
54 #include <linux/skbuff.h>
55 #include <linux/platform_device.h>
58 #include <linux/dma-mapping.h>
60 #include <asm/blackfin.h>
61 #include <asm/cacheflush.h>
62 #include <asm/portmux.h>
66 #define DRV_NAME "bfin_mac"
67 #define DRV_VERSION "1.1"
68 #define DRV_AUTHOR "Bryan Wu, Luke Yang"
69 #define DRV_DESC "Blackfin BF53[67] on-chip Ethernet MAC driver"
71 MODULE_AUTHOR(DRV_AUTHOR);
72 MODULE_LICENSE("GPL");
73 MODULE_DESCRIPTION(DRV_DESC);
75 #if defined(CONFIG_BFIN_MAC_USE_L1)
76 # define bfin_mac_alloc(dma_handle, size) l1_data_sram_zalloc(size)
77 # define bfin_mac_free(dma_handle, ptr) l1_data_sram_free(ptr)
79 # define bfin_mac_alloc(dma_handle, size) \
80 dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL)
81 # define bfin_mac_free(dma_handle, ptr) \
82 dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle)
85 #define PKT_BUF_SZ 1580
87 #define MAX_TIMEOUT_CNT 500
89 /* pointers to maintain transmit list */
90 static struct net_dma_desc_tx *tx_list_head;
91 static struct net_dma_desc_tx *tx_list_tail;
92 static struct net_dma_desc_rx *rx_list_head;
93 static struct net_dma_desc_rx *rx_list_tail;
94 static struct net_dma_desc_rx *current_rx_ptr;
95 static struct net_dma_desc_tx *current_tx_ptr;
96 static struct net_dma_desc_tx *tx_desc;
97 static struct net_dma_desc_rx *rx_desc;
99 static void bf537mac_disable(void);
100 static void bf537mac_enable(void);
102 static void desc_list_free(void)
104 struct net_dma_desc_rx *r;
105 struct net_dma_desc_tx *t;
107 #if !defined(CONFIG_BFIN_MAC_USE_L1)
108 dma_addr_t dma_handle = 0;
113 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
116 dev_kfree_skb(t->skb);
122 bfin_mac_free(dma_handle, tx_desc);
127 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
130 dev_kfree_skb(r->skb);
136 bfin_mac_free(dma_handle, rx_desc);
140 static int desc_list_init(void)
143 struct sk_buff *new_skb;
144 #if !defined(CONFIG_BFIN_MAC_USE_L1)
146 * This dma_handle is useless in Blackfin dma_alloc_coherent().
147 * The real dma handler is the return value of dma_alloc_coherent().
149 dma_addr_t dma_handle;
152 tx_desc = bfin_mac_alloc(&dma_handle,
153 sizeof(struct net_dma_desc_tx) *
154 CONFIG_BFIN_TX_DESC_NUM);
158 rx_desc = bfin_mac_alloc(&dma_handle,
159 sizeof(struct net_dma_desc_rx) *
160 CONFIG_BFIN_RX_DESC_NUM);
165 tx_list_head = tx_list_tail = tx_desc;
167 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
168 struct net_dma_desc_tx *t = tx_desc + i;
169 struct dma_descriptor *a = &(t->desc_a);
170 struct dma_descriptor *b = &(t->desc_b);
174 * read from memory WNR = 0
175 * wordsize is 32 bits
176 * 6 half words is desc size
179 a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
180 a->start_addr = (unsigned long)t->packet;
182 a->next_dma_desc = b;
186 * write to memory WNR = 1
187 * wordsize is 32 bits
189 * 6 half words is desc size
192 b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
193 b->start_addr = (unsigned long)(&(t->status));
197 tx_list_tail->desc_b.next_dma_desc = a;
198 tx_list_tail->next = t;
201 tx_list_tail->next = tx_list_head; /* tx_list is a circle */
202 tx_list_tail->desc_b.next_dma_desc = &(tx_list_head->desc_a);
203 current_tx_ptr = tx_list_head;
206 rx_list_head = rx_list_tail = rx_desc;
208 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
209 struct net_dma_desc_rx *r = rx_desc + i;
210 struct dma_descriptor *a = &(r->desc_a);
211 struct dma_descriptor *b = &(r->desc_b);
213 /* allocate a new skb for next time receive */
214 new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
216 printk(KERN_NOTICE DRV_NAME
217 ": init: low on mem - packet dropped\n");
220 skb_reserve(new_skb, 2);
225 * write to memory WNR = 1
226 * wordsize is 32 bits
228 * 6 half words is desc size
231 a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
232 /* since RXDWA is enabled */
233 a->start_addr = (unsigned long)new_skb->data - 2;
235 a->next_dma_desc = b;
239 * write to memory WNR = 1
240 * wordsize is 32 bits
242 * 6 half words is desc size
245 b->config = DMAEN | WNR | WDSIZE_32 | DI_EN |
246 NDSIZE_6 | DMAFLOW_LARGE;
247 b->start_addr = (unsigned long)(&(r->status));
250 rx_list_tail->desc_b.next_dma_desc = a;
251 rx_list_tail->next = r;
254 rx_list_tail->next = rx_list_head; /* rx_list is a circle */
255 rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a);
256 current_rx_ptr = rx_list_head;
262 printk(KERN_ERR DRV_NAME ": kmalloc failed\n");
267 /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
269 /* Set FER regs to MUX in Ethernet pins */
270 static int setup_pin_mux(int action)
272 #if defined(CONFIG_BFIN_MAC_RMII)
273 u16 pin_req[] = P_RMII0;
275 u16 pin_req[] = P_MII0;
279 if (peripheral_request_list(pin_req, DRV_NAME)) {
280 printk(KERN_ERR DRV_NAME
281 ": Requesting Peripherals failed\n");
285 peripheral_free_list(pin_req);
293 /* Wait until the previous MDC/MDIO transaction has completed */
294 static void mdio_poll(void)
296 int timeout_cnt = MAX_TIMEOUT_CNT;
298 /* poll the STABUSY bit */
299 while ((bfin_read_EMAC_STAADD()) & STABUSY) {
301 if (timeout_cnt-- < 0) {
302 printk(KERN_ERR DRV_NAME
303 ": wait MDC/MDIO transaction to complete timeout\n");
309 /* Read an off-chip register in a PHY through the MDC/MDIO port */
310 static int mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
315 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
316 SET_REGAD((u16) regnum) |
321 return (int) bfin_read_EMAC_STADAT();
324 /* Write an off-chip register in a PHY through the MDC/MDIO port */
325 static int mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
330 bfin_write_EMAC_STADAT((u32) value);
333 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
334 SET_REGAD((u16) regnum) |
343 static int mdiobus_reset(struct mii_bus *bus)
348 static void bf537_adjust_link(struct net_device *dev)
350 struct bf537mac_local *lp = netdev_priv(dev);
351 struct phy_device *phydev = lp->phydev;
355 spin_lock_irqsave(&lp->lock, flags);
357 /* Now we make sure that we can be in full duplex mode.
358 * If not, we operate in half-duplex mode. */
359 if (phydev->duplex != lp->old_duplex) {
360 u32 opmode = bfin_read_EMAC_OPMODE();
368 bfin_write_EMAC_OPMODE(opmode);
369 lp->old_duplex = phydev->duplex;
372 if (phydev->speed != lp->old_speed) {
373 #if defined(CONFIG_BFIN_MAC_RMII)
374 u32 opmode = bfin_read_EMAC_OPMODE();
375 switch (phydev->speed) {
380 opmode &= ~(RMII_10);
384 "%s: Ack! Speed (%d) is not 10/100!\n",
385 DRV_NAME, phydev->speed);
388 bfin_write_EMAC_OPMODE(opmode);
392 lp->old_speed = phydev->speed;
400 } else if (lp->old_link) {
408 u32 opmode = bfin_read_EMAC_OPMODE();
409 phy_print_status(phydev);
410 pr_debug("EMAC_OPMODE = 0x%08x\n", opmode);
413 spin_unlock_irqrestore(&lp->lock, flags);
417 #define MDC_CLK 2500000
419 static int mii_probe(struct net_device *dev)
421 struct bf537mac_local *lp = netdev_priv(dev);
422 struct phy_device *phydev = NULL;
423 unsigned short sysctl;
427 /* Enable PHY output early */
428 if (!(bfin_read_VR_CTL() & PHYCLKOE))
429 bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
432 mdc_div = ((sclk / MDC_CLK) / 2) - 1;
434 sysctl = bfin_read_EMAC_SYSCTL();
435 sysctl |= SET_MDCDIV(mdc_div);
436 bfin_write_EMAC_SYSCTL(sysctl);
438 /* search for connect PHY device */
439 for (i = 0; i < PHY_MAX_ADDR; i++) {
440 struct phy_device *const tmp_phydev = lp->mii_bus.phy_map[i];
443 continue; /* no PHY here... */
446 break; /* found it */
449 /* now we are supposed to have a proper phydev, to attach to... */
451 printk(KERN_INFO "%s: Don't found any phy device at all\n",
456 #if defined(CONFIG_BFIN_MAC_RMII)
457 phydev = phy_connect(dev, phydev->dev.bus_id, &bf537_adjust_link, 0,
458 PHY_INTERFACE_MODE_RMII);
460 phydev = phy_connect(dev, phydev->dev.bus_id, &bf537_adjust_link, 0,
461 PHY_INTERFACE_MODE_MII);
464 if (IS_ERR(phydev)) {
465 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
466 return PTR_ERR(phydev);
469 /* mask with MAC supported features */
470 phydev->supported &= (SUPPORTED_10baseT_Half
471 | SUPPORTED_10baseT_Full
472 | SUPPORTED_100baseT_Half
473 | SUPPORTED_100baseT_Full
475 | SUPPORTED_Pause | SUPPORTED_Asym_Pause
479 phydev->advertising = phydev->supported;
486 printk(KERN_INFO "%s: attached PHY driver [%s] "
487 "(mii_bus:phy_addr=%s, irq=%d, mdc_clk=%dHz(mdc_div=%d)"
489 DRV_NAME, phydev->drv->name, phydev->dev.bus_id, phydev->irq,
490 MDC_CLK, mdc_div, sclk/1000000);
495 /**************************************************************************/
496 void setup_system_regs(struct net_device *dev)
498 unsigned short sysctl;
501 * Odd word alignment for Receive Frame DMA word
502 * Configure checksum support and rcve frame word alignment
504 sysctl = bfin_read_EMAC_SYSCTL();
505 #if defined(BFIN_MAC_CSUM_OFFLOAD)
506 sysctl |= RXDWA | RXCKS;
510 bfin_write_EMAC_SYSCTL(sysctl);
512 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
514 /* Initialize the TX DMA channel registers */
515 bfin_write_DMA2_X_COUNT(0);
516 bfin_write_DMA2_X_MODIFY(4);
517 bfin_write_DMA2_Y_COUNT(0);
518 bfin_write_DMA2_Y_MODIFY(0);
520 /* Initialize the RX DMA channel registers */
521 bfin_write_DMA1_X_COUNT(0);
522 bfin_write_DMA1_X_MODIFY(4);
523 bfin_write_DMA1_Y_COUNT(0);
524 bfin_write_DMA1_Y_MODIFY(0);
527 static void setup_mac_addr(u8 *mac_addr)
529 u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
530 u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
532 /* this depends on a little-endian machine */
533 bfin_write_EMAC_ADDRLO(addr_low);
534 bfin_write_EMAC_ADDRHI(addr_hi);
537 static int bf537mac_set_mac_address(struct net_device *dev, void *p)
539 struct sockaddr *addr = p;
540 if (netif_running(dev))
542 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
543 setup_mac_addr(dev->dev_addr);
547 static void adjust_tx_list(void)
549 int timeout_cnt = MAX_TIMEOUT_CNT;
551 if (tx_list_head->status.status_word != 0
552 && current_tx_ptr != tx_list_head) {
553 goto adjust_head; /* released something, just return; */
557 * if nothing released, check wait condition
558 * current's next can not be the head,
559 * otherwise the dma will not stop as we want
561 if (current_tx_ptr->next->next == tx_list_head) {
562 while (tx_list_head->status.status_word == 0) {
564 if (tx_list_head->status.status_word != 0
565 || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) {
568 if (timeout_cnt-- < 0) {
569 printk(KERN_ERR DRV_NAME
570 ": wait for adjust tx list head timeout\n");
574 if (tx_list_head->status.status_word != 0) {
583 tx_list_head->desc_a.config &= ~DMAEN;
584 tx_list_head->status.status_word = 0;
585 if (tx_list_head->skb) {
586 dev_kfree_skb(tx_list_head->skb);
587 tx_list_head->skb = NULL;
589 printk(KERN_ERR DRV_NAME
590 ": no sk_buff in a transmitted frame!\n");
592 tx_list_head = tx_list_head->next;
593 } while (tx_list_head->status.status_word != 0
594 && current_tx_ptr != tx_list_head);
599 static int bf537mac_hard_start_xmit(struct sk_buff *skb,
600 struct net_device *dev)
602 struct bf537mac_local *lp = netdev_priv(dev);
605 current_tx_ptr->skb = skb;
608 * Is skb->data always 16-bit aligned?
609 * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)?
611 if ((((unsigned int)(skb->data)) & 0x02) == 2) {
612 /* move skb->data to current_tx_ptr payload */
613 data = (unsigned int)(skb->data) - 2;
614 *((unsigned short *)data) = (unsigned short)(skb->len);
615 current_tx_ptr->desc_a.start_addr = (unsigned long)data;
616 /* this is important! */
617 blackfin_dcache_flush_range(data, (data + (skb->len)) + 2);
620 *((unsigned short *)(current_tx_ptr->packet)) =
621 (unsigned short)(skb->len);
622 memcpy((char *)(current_tx_ptr->packet + 2), skb->data,
624 current_tx_ptr->desc_a.start_addr =
625 (unsigned long)current_tx_ptr->packet;
626 if (current_tx_ptr->status.status_word != 0)
627 current_tx_ptr->status.status_word = 0;
628 blackfin_dcache_flush_range((unsigned int)current_tx_ptr->
630 (unsigned int)(current_tx_ptr->
635 /* enable this packet's dma */
636 current_tx_ptr->desc_a.config |= DMAEN;
638 /* tx dma is running, just return */
639 if (bfin_read_DMA2_IRQ_STATUS() & 0x08)
642 /* tx dma is not running */
643 bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
644 /* dma enabled, read from memory, size is 6 */
645 bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
646 /* Turn on the EMAC tx */
647 bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
651 current_tx_ptr = current_tx_ptr->next;
652 dev->trans_start = jiffies;
653 dev->stats.tx_packets++;
654 dev->stats.tx_bytes += (skb->len);
658 static void bf537mac_rx(struct net_device *dev)
660 struct sk_buff *skb, *new_skb;
661 struct bf537mac_local *lp = netdev_priv(dev);
664 /* allocate a new skb for next time receive */
665 skb = current_rx_ptr->skb;
666 new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
668 printk(KERN_NOTICE DRV_NAME
669 ": rx: low on mem - packet dropped\n");
670 dev->stats.rx_dropped++;
673 /* reserve 2 bytes for RXDWA padding */
674 skb_reserve(new_skb, 2);
675 current_rx_ptr->skb = new_skb;
676 current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
678 len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
680 blackfin_dcache_invalidate_range((unsigned long)skb->head,
681 (unsigned long)skb->tail);
683 dev->last_rx = jiffies;
685 skb->protocol = eth_type_trans(skb, dev);
686 #if defined(BFIN_MAC_CSUM_OFFLOAD)
687 skb->csum = current_rx_ptr->status.ip_payload_csum;
688 skb->ip_summed = CHECKSUM_COMPLETE;
692 dev->stats.rx_packets++;
693 dev->stats.rx_bytes += len;
694 current_rx_ptr->status.status_word = 0x00000000;
695 current_rx_ptr = current_rx_ptr->next;
701 /* interrupt routine to handle rx and error signal */
702 static irqreturn_t bf537mac_interrupt(int irq, void *dev_id)
704 struct net_device *dev = dev_id;
708 if (current_rx_ptr->status.status_word == 0) {
709 /* no more new packet received */
711 if (current_rx_ptr->next->status.status_word != 0) {
712 current_rx_ptr = current_rx_ptr->next;
716 bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
727 #ifdef CONFIG_NET_POLL_CONTROLLER
728 static void bf537mac_poll(struct net_device *dev)
730 disable_irq(IRQ_MAC_RX);
731 bf537mac_interrupt(IRQ_MAC_RX, dev);
732 enable_irq(IRQ_MAC_RX);
734 #endif /* CONFIG_NET_POLL_CONTROLLER */
736 static void bf537mac_disable(void)
740 opmode = bfin_read_EMAC_OPMODE();
743 /* Turn off the EMAC */
744 bfin_write_EMAC_OPMODE(opmode);
748 * Enable Interrupts, Receive, and Transmit
750 static void bf537mac_enable(void)
754 pr_debug("%s: %s\n", DRV_NAME, __FUNCTION__);
757 bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
758 bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
763 /* We enable only RX here */
764 /* ASTP : Enable Automatic Pad Stripping
765 PR : Promiscuous Mode for test
766 PSF : Receive frames with total length less than 64 bytes.
767 FDMODE : Full Duplex Mode
768 LB : Internal Loopback for test
769 RE : Receiver Enable */
770 opmode = bfin_read_EMAC_OPMODE();
774 opmode |= DRO | DC | PSF;
777 #if defined(CONFIG_BFIN_MAC_RMII)
778 opmode |= RMII; /* For Now only 100MBit are supported */
779 #ifdef CONFIG_BF_REV_0_2
783 /* Turn on the EMAC rx */
784 bfin_write_EMAC_OPMODE(opmode);
787 /* Our watchdog timed out. Called by the networking layer */
788 static void bf537mac_timeout(struct net_device *dev)
790 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
795 tx_list_tail = tx_list_head->next;
799 /* We can accept TX packets again */
800 dev->trans_start = jiffies;
801 netif_wake_queue(dev);
804 static void bf537mac_multicast_hash(struct net_device *dev)
806 u32 emac_hashhi, emac_hashlo;
807 struct dev_mc_list *dmi = dev->mc_list;
812 emac_hashhi = emac_hashlo = 0;
814 for (i = 0; i < dev->mc_count; i++) {
815 addrs = dmi->dmi_addr;
818 /* skip non-multicast addresses */
822 crc = ether_crc(ETH_ALEN, addrs);
826 emac_hashhi |= 1 << (crc & 0x1f);
828 emac_hashlo |= 1 << (crc & 0x1f);
831 bfin_write_EMAC_HASHHI(emac_hashhi);
832 bfin_write_EMAC_HASHLO(emac_hashlo);
838 * This routine will, depending on the values passed to it,
839 * either make it accept multicast packets, go into
840 * promiscuous mode (for TCPDUMP and cousins) or accept
841 * a select set of multicast packets
843 static void bf537mac_set_multicast_list(struct net_device *dev)
847 if (dev->flags & IFF_PROMISC) {
848 printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
849 sysctl = bfin_read_EMAC_OPMODE();
851 bfin_write_EMAC_OPMODE(sysctl);
852 } else if (dev->flags & IFF_ALLMULTI) {
853 /* accept all multicast */
854 sysctl = bfin_read_EMAC_OPMODE();
856 bfin_write_EMAC_OPMODE(sysctl);
857 } else if (dev->mc_count) {
858 /* set up multicast hash table */
859 sysctl = bfin_read_EMAC_OPMODE();
861 bfin_write_EMAC_OPMODE(sysctl);
862 bf537mac_multicast_hash(dev);
864 /* clear promisc or multicast mode */
865 sysctl = bfin_read_EMAC_OPMODE();
866 sysctl &= ~(RAF | PAM);
867 bfin_write_EMAC_OPMODE(sysctl);
872 * this puts the device in an inactive state
874 static void bf537mac_shutdown(struct net_device *dev)
876 /* Turn off the EMAC */
877 bfin_write_EMAC_OPMODE(0x00000000);
878 /* Turn off the EMAC RX DMA */
879 bfin_write_DMA1_CONFIG(0x0000);
880 bfin_write_DMA2_CONFIG(0x0000);
884 * Open and Initialize the interface
886 * Set up everything, reset the card, etc..
888 static int bf537mac_open(struct net_device *dev)
890 struct bf537mac_local *lp = netdev_priv(dev);
892 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
895 * Check that the address is valid. If its not, refuse
896 * to bring the device up. The user must specify an
897 * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
899 if (!is_valid_ether_addr(dev->dev_addr)) {
900 printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
904 /* initial rx and tx list */
905 retval = desc_list_init();
910 phy_start(lp->phydev);
911 setup_system_regs(dev);
915 pr_debug("hardware init finished\n");
916 netif_start_queue(dev);
917 netif_carrier_on(dev);
924 * this makes the board clean up everything that it can
925 * and not talk to the outside world. Caused by
926 * an 'ifconfig ethX down'
928 static int bf537mac_close(struct net_device *dev)
930 struct bf537mac_local *lp = netdev_priv(dev);
931 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
933 netif_stop_queue(dev);
934 netif_carrier_off(dev);
936 phy_stop(lp->phydev);
938 /* clear everything */
939 bf537mac_shutdown(dev);
941 /* free the rx/tx buffers */
947 static int __init bf537mac_probe(struct net_device *dev)
949 struct bf537mac_local *lp = netdev_priv(dev);
953 /* Grab the MAC address in the MAC */
954 *(__le32 *) (&(dev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
955 *(__le16 *) (&(dev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
958 /*todo: how to proble? which is revision_register */
959 bfin_write_EMAC_ADDRLO(0x12345678);
960 if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
961 pr_debug("can't detect bf537 mac!\n");
966 /* set the GPIO pins to Ethernet mode */
967 retval = setup_pin_mux(1);
971 /*Is it valid? (Did bootloader initialize it?) */
972 if (!is_valid_ether_addr(dev->dev_addr)) {
973 /* Grab the MAC from the board somehow - this is done in the
974 arch/blackfin/mach-bf537/boards/eth_mac.c */
975 bfin_get_ether_addr(dev->dev_addr);
978 /* If still not valid, get a random one */
979 if (!is_valid_ether_addr(dev->dev_addr)) {
980 random_ether_addr(dev->dev_addr);
983 setup_mac_addr(dev->dev_addr);
985 /* MDIO bus initial */
986 lp->mii_bus.priv = dev;
987 lp->mii_bus.read = mdiobus_read;
988 lp->mii_bus.write = mdiobus_write;
989 lp->mii_bus.reset = mdiobus_reset;
990 lp->mii_bus.name = "bfin_mac_mdio";
992 lp->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
993 for (i = 0; i < PHY_MAX_ADDR; ++i)
994 lp->mii_bus.irq[i] = PHY_POLL;
996 mdiobus_register(&lp->mii_bus);
998 retval = mii_probe(dev);
1002 /* Fill in the fields of the device structure with ethernet values. */
1005 dev->open = bf537mac_open;
1006 dev->stop = bf537mac_close;
1007 dev->hard_start_xmit = bf537mac_hard_start_xmit;
1008 dev->set_mac_address = bf537mac_set_mac_address;
1009 dev->tx_timeout = bf537mac_timeout;
1010 dev->set_multicast_list = bf537mac_set_multicast_list;
1011 #ifdef CONFIG_NET_POLL_CONTROLLER
1012 dev->poll_controller = bf537mac_poll;
1015 spin_lock_init(&lp->lock);
1017 /* now, enable interrupts */
1018 /* register irq handler */
1020 (IRQ_MAC_RX, bf537mac_interrupt, IRQF_DISABLED | IRQF_SHARED,
1021 "BFIN537_MAC_RX", dev)) {
1022 printk(KERN_WARNING DRV_NAME
1023 ": Unable to attach BlackFin MAC RX interrupt\n");
1028 retval = register_netdev(dev);
1030 /* now, print out the card info, in a short format.. */
1031 printk(KERN_INFO "%s: Version %s, %s\n",
1032 DRV_NAME, DRV_VERSION, DRV_DESC);
1039 static int bfin_mac_probe(struct platform_device *pdev)
1041 struct net_device *ndev;
1043 ndev = alloc_etherdev(sizeof(struct bf537mac_local));
1045 printk(KERN_WARNING DRV_NAME ": could not allocate device\n");
1049 SET_NETDEV_DEV(ndev, &pdev->dev);
1051 platform_set_drvdata(pdev, ndev);
1053 if (bf537mac_probe(ndev) != 0) {
1054 platform_set_drvdata(pdev, NULL);
1056 printk(KERN_WARNING DRV_NAME ": not found\n");
1063 static int bfin_mac_remove(struct platform_device *pdev)
1065 struct net_device *ndev = platform_get_drvdata(pdev);
1067 platform_set_drvdata(pdev, NULL);
1069 unregister_netdev(ndev);
1071 free_irq(IRQ_MAC_RX, ndev);
1081 static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg)
1083 struct net_device *net_dev = platform_get_drvdata(pdev);
1085 if (netif_running(net_dev))
1086 bf537mac_close(net_dev);
1091 static int bfin_mac_resume(struct platform_device *pdev)
1093 struct net_device *net_dev = platform_get_drvdata(pdev);
1095 if (netif_running(net_dev))
1096 bf537mac_open(net_dev);
1101 #define bfin_mac_suspend NULL
1102 #define bfin_mac_resume NULL
1103 #endif /* CONFIG_PM */
1105 static struct platform_driver bfin_mac_driver = {
1106 .probe = bfin_mac_probe,
1107 .remove = bfin_mac_remove,
1108 .resume = bfin_mac_resume,
1109 .suspend = bfin_mac_suspend,
1115 static int __init bfin_mac_init(void)
1117 return platform_driver_register(&bfin_mac_driver);
1120 module_init(bfin_mac_init);
1122 static void __exit bfin_mac_cleanup(void)
1124 platform_driver_unregister(&bfin_mac_driver);
1127 module_exit(bfin_mac_cleanup);