2 * File: drivers/net/bfin_mac.c
5 * Bryan Wu <bryan.wu@analog.com>
8 * Luke Yang <luke.yang@analog.com>
14 * Copyright 2004-2006 Analog Devices Inc.
16 * Bugs: Enter bugs at http://blackfin.uclinux.org/
18 * This program is free software ; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation ; either version 2, or (at your option)
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program ; see the file COPYING.
30 * If not, write to the Free Software Foundation,
31 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include <linux/init.h>
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/slab.h>
39 #include <linux/delay.h>
40 #include <linux/timer.h>
41 #include <linux/errno.h>
42 #include <linux/irq.h>
44 #include <linux/ioport.h>
45 #include <linux/crc32.h>
46 #include <linux/device.h>
47 #include <linux/spinlock.h>
48 #include <linux/ethtool.h>
49 #include <linux/mii.h>
50 #include <linux/phy.h>
51 #include <linux/netdevice.h>
52 #include <linux/etherdevice.h>
53 #include <linux/skbuff.h>
54 #include <linux/platform_device.h>
57 #include <linux/dma-mapping.h>
59 #include <asm/blackfin.h>
60 #include <asm/cacheflush.h>
61 #include <asm/portmux.h>
65 #define DRV_NAME "bfin_mac"
66 #define DRV_VERSION "1.1"
67 #define DRV_AUTHOR "Bryan Wu, Luke Yang"
68 #define DRV_DESC "Blackfin BF53[67] on-chip Ethernet MAC driver"
70 MODULE_AUTHOR(DRV_AUTHOR);
71 MODULE_LICENSE("GPL");
72 MODULE_DESCRIPTION(DRV_DESC);
74 #if defined(CONFIG_BFIN_MAC_USE_L1)
75 # define bfin_mac_alloc(dma_handle, size) l1_data_sram_zalloc(size)
76 # define bfin_mac_free(dma_handle, ptr) l1_data_sram_free(ptr)
78 # define bfin_mac_alloc(dma_handle, size) \
79 dma_alloc_coherent(NULL, size, dma_handle, GFP_KERNEL)
80 # define bfin_mac_free(dma_handle, ptr) \
81 dma_free_coherent(NULL, sizeof(*ptr), ptr, dma_handle)
84 #define PKT_BUF_SZ 1580
86 #define MAX_TIMEOUT_CNT 500
88 /* pointers to maintain transmit list */
89 static struct net_dma_desc_tx *tx_list_head;
90 static struct net_dma_desc_tx *tx_list_tail;
91 static struct net_dma_desc_rx *rx_list_head;
92 static struct net_dma_desc_rx *rx_list_tail;
93 static struct net_dma_desc_rx *current_rx_ptr;
94 static struct net_dma_desc_tx *current_tx_ptr;
95 static struct net_dma_desc_tx *tx_desc;
96 static struct net_dma_desc_rx *rx_desc;
98 static void bf537mac_disable(void);
99 static void bf537mac_enable(void);
101 static void desc_list_free(void)
103 struct net_dma_desc_rx *r;
104 struct net_dma_desc_tx *t;
106 #if !defined(CONFIG_BFIN_MAC_USE_L1)
107 dma_addr_t dma_handle = 0;
112 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
115 dev_kfree_skb(t->skb);
121 bfin_mac_free(dma_handle, tx_desc);
126 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
129 dev_kfree_skb(r->skb);
135 bfin_mac_free(dma_handle, rx_desc);
139 static int desc_list_init(void)
142 struct sk_buff *new_skb;
143 #if !defined(CONFIG_BFIN_MAC_USE_L1)
145 * This dma_handle is useless in Blackfin dma_alloc_coherent().
146 * The real dma handler is the return value of dma_alloc_coherent().
148 dma_addr_t dma_handle;
151 tx_desc = bfin_mac_alloc(&dma_handle,
152 sizeof(struct net_dma_desc_tx) *
153 CONFIG_BFIN_TX_DESC_NUM);
157 rx_desc = bfin_mac_alloc(&dma_handle,
158 sizeof(struct net_dma_desc_rx) *
159 CONFIG_BFIN_RX_DESC_NUM);
164 tx_list_head = tx_list_tail = tx_desc;
166 for (i = 0; i < CONFIG_BFIN_TX_DESC_NUM; i++) {
167 struct net_dma_desc_tx *t = tx_desc + i;
168 struct dma_descriptor *a = &(t->desc_a);
169 struct dma_descriptor *b = &(t->desc_b);
173 * read from memory WNR = 0
174 * wordsize is 32 bits
175 * 6 half words is desc size
178 a->config = WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
179 a->start_addr = (unsigned long)t->packet;
181 a->next_dma_desc = b;
185 * write to memory WNR = 1
186 * wordsize is 32 bits
188 * 6 half words is desc size
191 b->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
192 b->start_addr = (unsigned long)(&(t->status));
196 tx_list_tail->desc_b.next_dma_desc = a;
197 tx_list_tail->next = t;
200 tx_list_tail->next = tx_list_head; /* tx_list is a circle */
201 tx_list_tail->desc_b.next_dma_desc = &(tx_list_head->desc_a);
202 current_tx_ptr = tx_list_head;
205 rx_list_head = rx_list_tail = rx_desc;
207 for (i = 0; i < CONFIG_BFIN_RX_DESC_NUM; i++) {
208 struct net_dma_desc_rx *r = rx_desc + i;
209 struct dma_descriptor *a = &(r->desc_a);
210 struct dma_descriptor *b = &(r->desc_b);
212 /* allocate a new skb for next time receive */
213 new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
215 printk(KERN_NOTICE DRV_NAME
216 ": init: low on mem - packet dropped\n");
219 skb_reserve(new_skb, 2);
224 * write to memory WNR = 1
225 * wordsize is 32 bits
227 * 6 half words is desc size
230 a->config = DMAEN | WNR | WDSIZE_32 | NDSIZE_6 | DMAFLOW_LARGE;
231 /* since RXDWA is enabled */
232 a->start_addr = (unsigned long)new_skb->data - 2;
234 a->next_dma_desc = b;
238 * write to memory WNR = 1
239 * wordsize is 32 bits
241 * 6 half words is desc size
244 b->config = DMAEN | WNR | WDSIZE_32 | DI_EN |
245 NDSIZE_6 | DMAFLOW_LARGE;
246 b->start_addr = (unsigned long)(&(r->status));
249 rx_list_tail->desc_b.next_dma_desc = a;
250 rx_list_tail->next = r;
253 rx_list_tail->next = rx_list_head; /* rx_list is a circle */
254 rx_list_tail->desc_b.next_dma_desc = &(rx_list_head->desc_a);
255 current_rx_ptr = rx_list_head;
261 printk(KERN_ERR DRV_NAME ": kmalloc failed\n");
266 /*---PHY CONTROL AND CONFIGURATION-----------------------------------------*/
268 /* Set FER regs to MUX in Ethernet pins */
269 static int setup_pin_mux(int action)
271 #if defined(CONFIG_BFIN_MAC_RMII)
272 u16 pin_req[] = P_RMII0;
274 u16 pin_req[] = P_MII0;
278 if (peripheral_request_list(pin_req, DRV_NAME)) {
279 printk(KERN_ERR DRV_NAME
280 ": Requesting Peripherals failed\n");
284 peripheral_free_list(pin_req);
292 /* Wait until the previous MDC/MDIO transaction has completed */
293 static void mdio_poll(void)
295 int timeout_cnt = MAX_TIMEOUT_CNT;
297 /* poll the STABUSY bit */
298 while ((bfin_read_EMAC_STAADD()) & STABUSY) {
300 if (timeout_cnt-- < 0) {
301 printk(KERN_ERR DRV_NAME
302 ": wait MDC/MDIO transaction to complete timeout\n");
308 /* Read an off-chip register in a PHY through the MDC/MDIO port */
309 static int mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
314 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
315 SET_REGAD((u16) regnum) |
320 return (int) bfin_read_EMAC_STADAT();
323 /* Write an off-chip register in a PHY through the MDC/MDIO port */
324 static int mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
329 bfin_write_EMAC_STADAT((u32) value);
332 bfin_write_EMAC_STAADD(SET_PHYAD((u16) phy_addr) |
333 SET_REGAD((u16) regnum) |
342 static int mdiobus_reset(struct mii_bus *bus)
347 static void bf537_adjust_link(struct net_device *dev)
349 struct bf537mac_local *lp = netdev_priv(dev);
350 struct phy_device *phydev = lp->phydev;
354 spin_lock_irqsave(&lp->lock, flags);
356 /* Now we make sure that we can be in full duplex mode.
357 * If not, we operate in half-duplex mode. */
358 if (phydev->duplex != lp->old_duplex) {
359 u32 opmode = bfin_read_EMAC_OPMODE();
367 bfin_write_EMAC_OPMODE(opmode);
368 lp->old_duplex = phydev->duplex;
371 if (phydev->speed != lp->old_speed) {
372 #if defined(CONFIG_BFIN_MAC_RMII)
373 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);
393 lp->old_speed = phydev->speed;
401 } else if (lp->old_link) {
409 u32 opmode = bfin_read_EMAC_OPMODE();
410 phy_print_status(phydev);
411 pr_debug("EMAC_OPMODE = 0x%08x\n", opmode);
414 spin_unlock_irqrestore(&lp->lock, flags);
417 static int mii_probe(struct net_device *dev)
419 struct bf537mac_local *lp = netdev_priv(dev);
420 struct phy_device *phydev = NULL;
421 unsigned short sysctl;
424 /* Enable PHY output early */
425 if (!(bfin_read_VR_CTL() & PHYCLKOE))
426 bfin_write_VR_CTL(bfin_read_VR_CTL() | PHYCLKOE);
429 sysctl = bfin_read_EMAC_SYSCTL();
430 sysctl |= SET_MDCDIV(24);
431 bfin_write_EMAC_SYSCTL(sysctl);
433 /* search for connect PHY device */
434 for (i = 0; i < PHY_MAX_ADDR; i++) {
435 struct phy_device *const tmp_phydev = lp->mii_bus.phy_map[i];
438 continue; /* no PHY here... */
441 break; /* found it */
444 /* now we are supposed to have a proper phydev, to attach to... */
446 printk(KERN_INFO "%s: Don't found any phy device at all\n",
451 #if defined(CONFIG_BFIN_MAC_RMII)
452 phydev = phy_connect(dev, phydev->dev.bus_id, &bf537_adjust_link, 0,
453 PHY_INTERFACE_MODE_RMII);
455 phydev = phy_connect(dev, phydev->dev.bus_id, &bf537_adjust_link, 0,
456 PHY_INTERFACE_MODE_MII);
459 if (IS_ERR(phydev)) {
460 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
461 return PTR_ERR(phydev);
464 /* mask with MAC supported features */
465 phydev->supported &= (SUPPORTED_10baseT_Half
466 | SUPPORTED_10baseT_Full
467 | SUPPORTED_100baseT_Half
468 | SUPPORTED_100baseT_Full
470 | SUPPORTED_Pause | SUPPORTED_Asym_Pause
474 phydev->advertising = phydev->supported;
481 printk(KERN_INFO "%s: attached PHY driver [%s] "
482 "(mii_bus:phy_addr=%s, irq=%d)\n",
483 DRV_NAME, phydev->drv->name, phydev->dev.bus_id, phydev->irq);
488 /**************************************************************************/
489 void setup_system_regs(struct net_device *dev)
491 unsigned short sysctl;
494 * Odd word alignment for Receive Frame DMA word
495 * Configure checksum support and rcve frame word alignment
497 sysctl = bfin_read_EMAC_SYSCTL();
498 #if defined(BFIN_MAC_CSUM_OFFLOAD)
499 sysctl |= RXDWA | RXCKS;
503 bfin_write_EMAC_SYSCTL(sysctl);
505 bfin_write_EMAC_MMC_CTL(RSTC | CROLL);
507 /* Initialize the TX DMA channel registers */
508 bfin_write_DMA2_X_COUNT(0);
509 bfin_write_DMA2_X_MODIFY(4);
510 bfin_write_DMA2_Y_COUNT(0);
511 bfin_write_DMA2_Y_MODIFY(0);
513 /* Initialize the RX DMA channel registers */
514 bfin_write_DMA1_X_COUNT(0);
515 bfin_write_DMA1_X_MODIFY(4);
516 bfin_write_DMA1_Y_COUNT(0);
517 bfin_write_DMA1_Y_MODIFY(0);
520 static void setup_mac_addr(u8 *mac_addr)
522 u32 addr_low = le32_to_cpu(*(__le32 *) & mac_addr[0]);
523 u16 addr_hi = le16_to_cpu(*(__le16 *) & mac_addr[4]);
525 /* this depends on a little-endian machine */
526 bfin_write_EMAC_ADDRLO(addr_low);
527 bfin_write_EMAC_ADDRHI(addr_hi);
530 static int bf537mac_set_mac_address(struct net_device *dev, void *p)
532 struct sockaddr *addr = p;
533 if (netif_running(dev))
535 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
536 setup_mac_addr(dev->dev_addr);
540 static void adjust_tx_list(void)
542 int timeout_cnt = MAX_TIMEOUT_CNT;
544 if (tx_list_head->status.status_word != 0
545 && current_tx_ptr != tx_list_head) {
546 goto adjust_head; /* released something, just return; */
550 * if nothing released, check wait condition
551 * current's next can not be the head,
552 * otherwise the dma will not stop as we want
554 if (current_tx_ptr->next->next == tx_list_head) {
555 while (tx_list_head->status.status_word == 0) {
557 if (tx_list_head->status.status_word != 0
558 || !(bfin_read_DMA2_IRQ_STATUS() & 0x08)) {
561 if (timeout_cnt-- < 0) {
562 printk(KERN_ERR DRV_NAME
563 ": wait for adjust tx list head timeout\n");
567 if (tx_list_head->status.status_word != 0) {
576 tx_list_head->desc_a.config &= ~DMAEN;
577 tx_list_head->status.status_word = 0;
578 if (tx_list_head->skb) {
579 dev_kfree_skb(tx_list_head->skb);
580 tx_list_head->skb = NULL;
582 printk(KERN_ERR DRV_NAME
583 ": no sk_buff in a transmitted frame!\n");
585 tx_list_head = tx_list_head->next;
586 } while (tx_list_head->status.status_word != 0
587 && current_tx_ptr != tx_list_head);
592 static int bf537mac_hard_start_xmit(struct sk_buff *skb,
593 struct net_device *dev)
595 struct bf537mac_local *lp = netdev_priv(dev);
598 current_tx_ptr->skb = skb;
601 * Is skb->data always 16-bit aligned?
602 * Do we need to memcpy((char *)(tail->packet + 2), skb->data, len)?
604 if ((((unsigned int)(skb->data)) & 0x02) == 2) {
605 /* move skb->data to current_tx_ptr payload */
606 data = (unsigned int)(skb->data) - 2;
607 *((unsigned short *)data) = (unsigned short)(skb->len);
608 current_tx_ptr->desc_a.start_addr = (unsigned long)data;
609 /* this is important! */
610 blackfin_dcache_flush_range(data, (data + (skb->len)) + 2);
613 *((unsigned short *)(current_tx_ptr->packet)) =
614 (unsigned short)(skb->len);
615 memcpy((char *)(current_tx_ptr->packet + 2), skb->data,
617 current_tx_ptr->desc_a.start_addr =
618 (unsigned long)current_tx_ptr->packet;
619 if (current_tx_ptr->status.status_word != 0)
620 current_tx_ptr->status.status_word = 0;
621 blackfin_dcache_flush_range((unsigned int)current_tx_ptr->
623 (unsigned int)(current_tx_ptr->
628 /* enable this packet's dma */
629 current_tx_ptr->desc_a.config |= DMAEN;
631 /* tx dma is running, just return */
632 if (bfin_read_DMA2_IRQ_STATUS() & 0x08)
635 /* tx dma is not running */
636 bfin_write_DMA2_NEXT_DESC_PTR(&(current_tx_ptr->desc_a));
637 /* dma enabled, read from memory, size is 6 */
638 bfin_write_DMA2_CONFIG(current_tx_ptr->desc_a.config);
639 /* Turn on the EMAC tx */
640 bfin_write_EMAC_OPMODE(bfin_read_EMAC_OPMODE() | TE);
644 current_tx_ptr = current_tx_ptr->next;
645 dev->trans_start = jiffies;
646 dev->stats.tx_packets++;
647 dev->stats.tx_bytes += (skb->len);
651 static void bf537mac_rx(struct net_device *dev)
653 struct sk_buff *skb, *new_skb;
654 struct bf537mac_local *lp = netdev_priv(dev);
657 /* allocate a new skb for next time receive */
658 skb = current_rx_ptr->skb;
659 new_skb = dev_alloc_skb(PKT_BUF_SZ + 2);
661 printk(KERN_NOTICE DRV_NAME
662 ": rx: low on mem - packet dropped\n");
663 dev->stats.rx_dropped++;
666 /* reserve 2 bytes for RXDWA padding */
667 skb_reserve(new_skb, 2);
668 current_rx_ptr->skb = new_skb;
669 current_rx_ptr->desc_a.start_addr = (unsigned long)new_skb->data - 2;
671 len = (unsigned short)((current_rx_ptr->status.status_word) & RX_FRLEN);
673 blackfin_dcache_invalidate_range((unsigned long)skb->head,
674 (unsigned long)skb->tail);
676 dev->last_rx = jiffies;
678 skb->protocol = eth_type_trans(skb, dev);
679 #if defined(BFIN_MAC_CSUM_OFFLOAD)
680 skb->csum = current_rx_ptr->status.ip_payload_csum;
681 skb->ip_summed = CHECKSUM_PARTIAL;
685 dev->stats.rx_packets++;
686 dev->stats.rx_bytes += len;
687 current_rx_ptr->status.status_word = 0x00000000;
688 current_rx_ptr = current_rx_ptr->next;
694 /* interrupt routine to handle rx and error signal */
695 static irqreturn_t bf537mac_interrupt(int irq, void *dev_id)
697 struct net_device *dev = dev_id;
701 if (current_rx_ptr->status.status_word == 0) {
702 /* no more new packet received */
704 if (current_rx_ptr->next->status.status_word != 0) {
705 current_rx_ptr = current_rx_ptr->next;
709 bfin_write_DMA1_IRQ_STATUS(bfin_read_DMA1_IRQ_STATUS() |
720 #ifdef CONFIG_NET_POLL_CONTROLLER
721 static void bf537mac_poll(struct net_device *dev)
723 disable_irq(IRQ_MAC_RX);
724 bf537mac_interrupt(IRQ_MAC_RX, dev);
725 enable_irq(IRQ_MAC_RX);
727 #endif /* CONFIG_NET_POLL_CONTROLLER */
729 static void bf537mac_disable(void)
733 opmode = bfin_read_EMAC_OPMODE();
736 /* Turn off the EMAC */
737 bfin_write_EMAC_OPMODE(opmode);
741 * Enable Interrupts, Receive, and Transmit
743 static void bf537mac_enable(void)
747 pr_debug("%s: %s\n", DRV_NAME, __FUNCTION__);
750 bfin_write_DMA1_NEXT_DESC_PTR(&(rx_list_head->desc_a));
751 bfin_write_DMA1_CONFIG(rx_list_head->desc_a.config);
756 /* We enable only RX here */
757 /* ASTP : Enable Automatic Pad Stripping
758 PR : Promiscuous Mode for test
759 PSF : Receive frames with total length less than 64 bytes.
760 FDMODE : Full Duplex Mode
761 LB : Internal Loopback for test
762 RE : Receiver Enable */
763 opmode = bfin_read_EMAC_OPMODE();
767 opmode |= DRO | DC | PSF;
770 #if defined(CONFIG_BFIN_MAC_RMII)
771 opmode |= RMII; /* For Now only 100MBit are supported */
772 #ifdef CONFIG_BF_REV_0_2
776 /* Turn on the EMAC rx */
777 bfin_write_EMAC_OPMODE(opmode);
780 /* Our watchdog timed out. Called by the networking layer */
781 static void bf537mac_timeout(struct net_device *dev)
783 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
788 tx_list_tail = tx_list_head->next;
792 /* We can accept TX packets again */
793 dev->trans_start = jiffies;
794 netif_wake_queue(dev);
798 * This routine will, depending on the values passed to it,
799 * either make it accept multicast packets, go into
800 * promiscuous mode (for TCPDUMP and cousins) or accept
801 * a select set of multicast packets
803 static void bf537mac_set_multicast_list(struct net_device *dev)
807 if (dev->flags & IFF_PROMISC) {
808 printk(KERN_INFO "%s: set to promisc mode\n", dev->name);
809 sysctl = bfin_read_EMAC_OPMODE();
811 bfin_write_EMAC_OPMODE(sysctl);
812 } else if (dev->flags & IFF_ALLMULTI || dev->mc_count) {
813 /* accept all multicast */
814 sysctl = bfin_read_EMAC_OPMODE();
816 bfin_write_EMAC_OPMODE(sysctl);
818 /* clear promisc or multicast mode */
819 sysctl = bfin_read_EMAC_OPMODE();
820 sysctl &= ~(RAF | PAM);
821 bfin_write_EMAC_OPMODE(sysctl);
826 * this puts the device in an inactive state
828 static void bf537mac_shutdown(struct net_device *dev)
830 /* Turn off the EMAC */
831 bfin_write_EMAC_OPMODE(0x00000000);
832 /* Turn off the EMAC RX DMA */
833 bfin_write_DMA1_CONFIG(0x0000);
834 bfin_write_DMA2_CONFIG(0x0000);
838 * Open and Initialize the interface
840 * Set up everything, reset the card, etc..
842 static int bf537mac_open(struct net_device *dev)
844 struct bf537mac_local *lp = netdev_priv(dev);
846 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
849 * Check that the address is valid. If its not, refuse
850 * to bring the device up. The user must specify an
851 * address using ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
853 if (!is_valid_ether_addr(dev->dev_addr)) {
854 printk(KERN_WARNING DRV_NAME ": no valid ethernet hw addr\n");
858 /* initial rx and tx list */
859 retval = desc_list_init();
864 phy_start(lp->phydev);
865 setup_system_regs(dev);
869 pr_debug("hardware init finished\n");
870 netif_start_queue(dev);
871 netif_carrier_on(dev);
878 * this makes the board clean up everything that it can
879 * and not talk to the outside world. Caused by
880 * an 'ifconfig ethX down'
882 static int bf537mac_close(struct net_device *dev)
884 struct bf537mac_local *lp = netdev_priv(dev);
885 pr_debug("%s: %s\n", dev->name, __FUNCTION__);
887 netif_stop_queue(dev);
888 netif_carrier_off(dev);
890 phy_stop(lp->phydev);
892 /* clear everything */
893 bf537mac_shutdown(dev);
895 /* free the rx/tx buffers */
901 static int __init bf537mac_probe(struct net_device *dev)
903 struct bf537mac_local *lp = netdev_priv(dev);
907 /* Grab the MAC address in the MAC */
908 *(__le32 *) (&(dev->dev_addr[0])) = cpu_to_le32(bfin_read_EMAC_ADDRLO());
909 *(__le16 *) (&(dev->dev_addr[4])) = cpu_to_le16((u16) bfin_read_EMAC_ADDRHI());
912 /*todo: how to proble? which is revision_register */
913 bfin_write_EMAC_ADDRLO(0x12345678);
914 if (bfin_read_EMAC_ADDRLO() != 0x12345678) {
915 pr_debug("can't detect bf537 mac!\n");
920 /* set the GPIO pins to Ethernet mode */
921 retval = setup_pin_mux(1);
925 /*Is it valid? (Did bootloader initialize it?) */
926 if (!is_valid_ether_addr(dev->dev_addr)) {
927 /* Grab the MAC from the board somehow - this is done in the
928 arch/blackfin/mach-bf537/boards/eth_mac.c */
929 get_bf537_ether_addr(dev->dev_addr);
932 /* If still not valid, get a random one */
933 if (!is_valid_ether_addr(dev->dev_addr)) {
934 random_ether_addr(dev->dev_addr);
937 setup_mac_addr(dev->dev_addr);
939 /* MDIO bus initial */
940 lp->mii_bus.priv = dev;
941 lp->mii_bus.read = mdiobus_read;
942 lp->mii_bus.write = mdiobus_write;
943 lp->mii_bus.reset = mdiobus_reset;
944 lp->mii_bus.name = "bfin_mac_mdio";
946 lp->mii_bus.irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
947 for (i = 0; i < PHY_MAX_ADDR; ++i)
948 lp->mii_bus.irq[i] = PHY_POLL;
950 mdiobus_register(&lp->mii_bus);
952 retval = mii_probe(dev);
956 /* Fill in the fields of the device structure with ethernet values. */
959 dev->open = bf537mac_open;
960 dev->stop = bf537mac_close;
961 dev->hard_start_xmit = bf537mac_hard_start_xmit;
962 dev->set_mac_address = bf537mac_set_mac_address;
963 dev->tx_timeout = bf537mac_timeout;
964 dev->set_multicast_list = bf537mac_set_multicast_list;
965 #ifdef CONFIG_NET_POLL_CONTROLLER
966 dev->poll_controller = bf537mac_poll;
969 spin_lock_init(&lp->lock);
971 /* now, enable interrupts */
972 /* register irq handler */
974 (IRQ_MAC_RX, bf537mac_interrupt, IRQF_DISABLED | IRQF_SHARED,
975 "BFIN537_MAC_RX", dev)) {
976 printk(KERN_WARNING DRV_NAME
977 ": Unable to attach BlackFin MAC RX interrupt\n");
982 retval = register_netdev(dev);
984 /* now, print out the card info, in a short format.. */
985 printk(KERN_INFO "%s: Version %s, %s\n",
986 DRV_NAME, DRV_VERSION, DRV_DESC);
993 static int bfin_mac_probe(struct platform_device *pdev)
995 struct net_device *ndev;
997 ndev = alloc_etherdev(sizeof(struct bf537mac_local));
999 printk(KERN_WARNING DRV_NAME ": could not allocate device\n");
1003 SET_NETDEV_DEV(ndev, &pdev->dev);
1005 platform_set_drvdata(pdev, ndev);
1007 if (bf537mac_probe(ndev) != 0) {
1008 platform_set_drvdata(pdev, NULL);
1010 printk(KERN_WARNING DRV_NAME ": not found\n");
1017 static int bfin_mac_remove(struct platform_device *pdev)
1019 struct net_device *ndev = platform_get_drvdata(pdev);
1021 platform_set_drvdata(pdev, NULL);
1023 unregister_netdev(ndev);
1025 free_irq(IRQ_MAC_RX, ndev);
1035 static int bfin_mac_suspend(struct platform_device *pdev, pm_message_t mesg)
1037 struct net_device *net_dev = platform_get_drvdata(pdev);
1039 if (netif_running(net_dev))
1040 bf537mac_close(net_dev);
1045 static int bfin_mac_resume(struct platform_device *pdev)
1047 struct net_device *net_dev = platform_get_drvdata(pdev);
1049 if (netif_running(net_dev))
1050 bf537mac_open(net_dev);
1055 #define bfin_mac_suspend NULL
1056 #define bfin_mac_resume NULL
1057 #endif /* CONFIG_PM */
1059 static struct platform_driver bfin_mac_driver = {
1060 .probe = bfin_mac_probe,
1061 .remove = bfin_mac_remove,
1062 .resume = bfin_mac_resume,
1063 .suspend = bfin_mac_suspend,
1069 static int __init bfin_mac_init(void)
1071 return platform_driver_register(&bfin_mac_driver);
1074 module_init(bfin_mac_init);
1076 static void __exit bfin_mac_cleanup(void)
1078 platform_driver_unregister(&bfin_mac_driver);
1081 module_exit(bfin_mac_cleanup);