4 * Copyright (C) 2006, 2007 OpenWrt.org
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/moduleparam.h>
25 #include <linux/sched.h>
26 #include <linux/kernel.h> /* printk() */
27 #include <linux/slab.h>
28 #include <linux/errno.h>
29 #include <linux/types.h>
30 #include <linux/delay.h>
31 #include <linux/version.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/ethtool.h>
36 #include <linux/skbuff.h>
37 #include <linux/mii.h>
38 #include <linux/phy.h>
39 #include <linux/platform_device.h>
40 #include <asm/ar7/ar7.h>
43 MODULE_AUTHOR("Eugene Konev");
44 MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
45 MODULE_LICENSE("GPL");
47 static int rx_ring_size = 64;
48 static int disable_napi = 0;
49 module_param(rx_ring_size, int, 64);
50 module_param(disable_napi, int, 0);
51 MODULE_PARM_DESC(rx_ring_size, "Size of rx ring (in skbs)");
52 MODULE_PARM_DESC(disable_napi, "Disable NAPI polling");
54 /* Register definitions */
55 struct cpmac_control_regs {
56 volatile u32 revision;
58 volatile u32 teardown;
60 } __attribute__ ((packed));
62 struct cpmac_int_regs {
63 volatile u32 stat_raw;
64 volatile u32 stat_masked;
67 } __attribute__ ((packed));
74 volatile u32 crc_error;
75 volatile u32 align_error;
76 volatile u32 oversized;
78 volatile u32 undersized;
79 volatile u32 fragment;
80 volatile u32 filtered;
81 volatile u32 qos_filtered;
83 } __attribute__ ((packed));
86 struct cpmac_control_regs tx_ctrl;
87 struct cpmac_control_regs rx_ctrl;
88 volatile u32 unused1[56];
91 #define MBP_RXPASSCRC 0x40000000
92 #define MBP_RXQOS 0x20000000
93 #define MBP_RXNOCHAIN 0x10000000
94 #define MBP_RXCMF 0x01000000
95 #define MBP_RXSHORT 0x00800000
96 #define MBP_RXCEF 0x00400000
97 #define MBP_RXPROMISC 0x00200000
98 #define MBP_PROMISCCHAN(chan) (((chan) & 0x7) << 16)
99 #define MBP_RXBCAST 0x00002000
100 #define MBP_BCASTCHAN(chan) (((chan) & 0x7) << 8)
101 #define MBP_RXMCAST 0x00000020
102 #define MBP_MCASTCHAN(chan) ((chan) & 0x7)
103 volatile u32 unicast_enable;
104 volatile u32 unicast_clear;
105 volatile u32 max_len;
106 volatile u32 buffer_offset;
107 volatile u32 filter_flow_threshold;
108 volatile u32 unused2[2];
109 volatile u32 flow_thre[8];
110 volatile u32 free_buffer[8];
111 volatile u32 mac_control;
112 #define MAC_TXPTYPE 0x00000200
113 #define MAC_TXPACE 0x00000040
114 #define MAC_MII 0x00000020
115 #define MAC_TXFLOW 0x00000010
116 #define MAC_RXFLOW 0x00000008
117 #define MAC_MTEST 0x00000004
118 #define MAC_LOOPBACK 0x00000002
119 #define MAC_FDX 0x00000001
120 volatile u32 mac_status;
121 #define MACST_QOS 0x4
122 #define MACST_RXFLOW 0x2
123 #define MACST_TXFLOW 0x1
124 volatile u32 emc_control;
125 volatile u32 unused3;
126 struct cpmac_int_regs tx_int;
127 volatile u32 mac_int_vector;
128 /* Int Status bits */
129 #define INTST_STATUS 0x80000
130 #define INTST_HOST 0x40000
131 #define INTST_RX 0x20000
132 #define INTST_TX 0x10000
133 volatile u32 mac_eoi_vector;
134 volatile u32 unused4[2];
135 struct cpmac_int_regs rx_int;
136 volatile u32 mac_int_stat_raw;
137 volatile u32 mac_int_stat_masked;
138 volatile u32 mac_int_enable;
139 volatile u32 mac_int_clear;
140 volatile u32 mac_addr_low[8];
141 volatile u32 mac_addr_mid;
142 volatile u32 mac_addr_high;
143 volatile u32 mac_hash_low;
144 volatile u32 mac_hash_high;
145 volatile u32 boff_test;
146 volatile u32 pac_test;
147 volatile u32 rx_pause;
148 volatile u32 tx_pause;
149 volatile u32 unused5[2];
150 struct cpmac_stats rx_stats;
151 struct cpmac_stats tx_stats;
152 volatile u32 unused6[232];
153 volatile u32 tx_ptr[8];
154 volatile u32 rx_ptr[8];
155 volatile u32 tx_ack[8];
156 volatile u32 rx_ack[8];
158 } __attribute__ ((packed));
160 struct cpmac_mdio_regs {
161 volatile u32 version;
162 volatile u32 control;
163 #define MDIOC_IDLE 0x80000000
164 #define MDIOC_ENABLE 0x40000000
165 #define MDIOC_PREAMBLE 0x00100000
166 #define MDIOC_FAULT 0x00080000
167 #define MDIOC_FAULTDETECT 0x00040000
168 #define MDIOC_INTTEST 0x00020000
169 #define MDIOC_CLKDIV(div) ((div) & 0xff)
172 struct cpmac_int_regs link_int;
173 struct cpmac_int_regs user_int;
176 #define MDIO_BUSY 0x80000000
177 #define MDIO_WRITE 0x40000000
178 #define MDIO_REG(reg) (((reg) & 0x1f) << 21)
179 #define MDIO_PHY(phy) (((phy) & 0x1f) << 16)
180 #define MDIO_DATA(data) ((data) & 0xffff)
182 } __attribute__ ((packed));
193 #define CPMAC_SOP 0x8000
194 #define CPMAC_EOP 0x4000
195 #define CPMAC_OWN 0x2000
196 #define CPMAC_EOQ 0x1000
198 struct cpmac_desc *next;
199 } __attribute__ ((packed));
202 struct net_device_stats stats;
204 struct sk_buff *skb_pool;
206 struct cpmac_desc *rx_head;
207 int tx_head, tx_tail;
208 struct cpmac_desc *desc_ring;
209 struct cpmac_regs *regs;
210 struct mii_bus *mii_bus;
211 struct phy_device *phy;
212 char phy_name[BUS_ID_SIZE];
213 struct plat_cpmac_data *config;
214 int oldlink, oldspeed, oldduplex;
216 struct net_device *dev;
217 struct work_struct alloc_work;
220 static irqreturn_t cpmac_irq(int, void *);
221 static void cpmac_reset(struct net_device *dev);
222 static void cpmac_hw_init(struct net_device *dev);
223 static int cpmac_stop(struct net_device *dev);
224 static int cpmac_open(struct net_device *dev);
226 #define CPMAC_LOW_THRESH 32
227 #define CPMAC_ALLOC_SIZE 64
228 #define CPMAC_SKB_SIZE 1518
229 #define CPMAC_TX_RING_SIZE 8
232 static void cpmac_dump_regs(u32 *base, int count)
235 for (i = 0; i < (count + 3) / 4; i++) {
236 if (i % 4 == 0) printk("\nCPMAC[0x%04x]:", i * 4);
237 printk(" 0x%08x", *(base + i));
243 static int cpmac_mdio_read(struct mii_bus *bus, int phy_id, int regnum)
245 struct cpmac_mdio_regs *regs = (struct cpmac_mdio_regs *)bus->priv;
248 while ((val = regs->access) & MDIO_BUSY);
249 regs->access = MDIO_BUSY | MDIO_REG(regnum & 0x1f) |
250 MDIO_PHY(phy_id & 0x1f);
251 while ((val = regs->access) & MDIO_BUSY);
256 static int cpmac_mdio_write(struct mii_bus *bus, int phy_id, int regnum, u16 val)
258 struct cpmac_mdio_regs *regs = (struct cpmac_mdio_regs *)bus->priv;
261 while ((tmp = regs->access) & MDIO_BUSY);
262 regs->access = MDIO_BUSY | MDIO_WRITE |
263 MDIO_REG(regnum & 0x1f) | MDIO_PHY(phy_id & 0x1f) |
269 static int cpmac_mdio_reset(struct mii_bus *bus)
271 struct cpmac_mdio_regs *regs = (struct cpmac_mdio_regs *)bus->priv;
273 ar7_device_reset(AR7_RESET_BIT_MDIO);
274 regs->control = MDIOC_ENABLE |
275 MDIOC_CLKDIV(ar7_cpmac_freq() / 2200000 - 1);
280 static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, };
282 static struct mii_bus cpmac_mii = {
284 .read = cpmac_mdio_read,
285 .write = cpmac_mdio_write,
286 .reset = cpmac_mdio_reset,
290 static int cpmac_config(struct net_device *dev, struct ifmap *map)
292 if (dev->flags & IFF_UP)
295 /* Don't allow changing the I/O address */
296 if (map->base_addr != dev->base_addr)
299 /* ignore other fields */
303 static int cpmac_set_mac_address(struct net_device *dev, void *addr)
305 struct sockaddr *sa = addr;
307 if (dev->flags & IFF_UP)
310 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
315 static void cpmac_set_multicast_list(struct net_device *dev)
317 struct dev_mc_list *iter;
320 int hashlo = 0, hashhi = 0;
321 struct cpmac_priv *priv = netdev_priv(dev);
323 if(dev->flags & IFF_PROMISC) {
324 priv->regs->mbp &= ~MBP_PROMISCCHAN(0); /* promisc channel 0 */
325 priv->regs->mbp |= MBP_RXPROMISC;
327 priv->regs->mbp &= ~MBP_RXPROMISC;
328 if(dev->flags & IFF_ALLMULTI) {
329 /* enable all multicast mode */
330 priv->regs->mac_hash_low = 0xffffffff;
331 priv->regs->mac_hash_high = 0xffffffff;
333 for(i = 0, iter = dev->mc_list; i < dev->mc_count;
334 i++, iter = iter->next) {
336 tmp = iter->dmi_addr[0];
337 hash ^= (tmp >> 2) ^ (tmp << 4);
338 tmp = iter->dmi_addr[1];
339 hash ^= (tmp >> 4) ^ (tmp << 2);
340 tmp = iter->dmi_addr[2];
341 hash ^= (tmp >> 6) ^ tmp;
342 tmp = iter->dmi_addr[4];
343 hash ^= (tmp >> 2) ^ (tmp << 4);
344 tmp = iter->dmi_addr[5];
345 hash ^= (tmp >> 4) ^ (tmp << 2);
346 tmp = iter->dmi_addr[6];
347 hash ^= (tmp >> 6) ^ tmp;
352 hashhi |= 1<<(hash - 32);
356 priv->regs->mac_hash_low = hashlo;
357 priv->regs->mac_hash_high = hashhi;
362 static struct sk_buff *cpmac_get_skb(struct net_device *dev)
365 struct cpmac_priv *priv = netdev_priv(dev);
367 skb = priv->skb_pool;
369 priv->skb_pool = skb->next;
371 skb = dev_alloc_skb(CPMAC_SKB_SIZE + 2);
375 skb->dev = priv->dev;
379 if (likely(priv->free_skbs))
382 if (priv->free_skbs < CPMAC_LOW_THRESH)
383 schedule_work(&priv->alloc_work);
388 static inline struct sk_buff *cpmac_rx_one(struct net_device *dev,
389 struct cpmac_priv *priv,
390 struct cpmac_desc *desc)
394 struct sk_buff *skb, *result = NULL;
396 priv->regs->rx_ack[0] = virt_to_phys(desc);
397 if (unlikely(!desc->datalen)) {
398 if (printk_ratelimit())
399 printk(KERN_WARNING "%s: rx: spurious interrupt\n",
401 priv->stats.rx_errors++;
405 spin_lock_irqsave(&priv->lock, flags);
406 skb = cpmac_get_skb(dev);
408 data = (char *)phys_to_virt(desc->hw_data);
409 dma_cache_inv((u32)data, desc->datalen);
410 skb_put(desc->skb, desc->datalen);
411 desc->skb->protocol = eth_type_trans(desc->skb, dev);
412 desc->skb->ip_summed = CHECKSUM_NONE;
413 priv->stats.rx_packets++;
414 priv->stats.rx_bytes += desc->datalen;
419 if (printk_ratelimit())
420 printk("%s: low on skbs, dropping packet\n",
423 priv->stats.rx_dropped++;
425 spin_unlock_irqrestore(&priv->lock, flags);
427 desc->hw_data = virt_to_phys(desc->skb->data);
428 desc->buflen = CPMAC_SKB_SIZE;
429 desc->dataflags = CPMAC_OWN;
430 dma_cache_wback((u32)desc, 16);
435 static void cpmac_rx(struct net_device *dev)
438 struct cpmac_desc *desc;
439 struct cpmac_priv *priv = netdev_priv(dev);
441 spin_lock(&priv->lock);
442 if (unlikely(!priv->rx_head)) {
443 spin_unlock(&priv->lock);
447 desc = priv->rx_head;
448 dma_cache_inv((u32)desc, 16);
450 while ((desc->dataflags & CPMAC_OWN) == 0) {
451 skb = cpmac_rx_one(dev, priv, desc);
456 dma_cache_inv((u32)desc, 16);
459 priv->rx_head = desc;
460 priv->regs->rx_ptr[0] = virt_to_phys(desc);
461 spin_unlock(&priv->lock);
464 static int cpmac_poll(struct net_device *dev, int *budget)
467 struct cpmac_desc *desc;
468 int received = 0, quota = min(dev->quota, *budget);
469 struct cpmac_priv *priv = netdev_priv(dev);
471 if (unlikely(!priv->rx_head)) {
472 if (printk_ratelimit())
473 printk(KERN_WARNING "%s: rx: polling, but no queue\n",
475 netif_rx_complete(dev);
479 desc = priv->rx_head;
480 dma_cache_inv((u32)desc, 16);
482 while ((received < quota) && ((desc->dataflags & CPMAC_OWN) == 0)) {
483 skb = cpmac_rx_one(dev, priv, desc);
485 netif_receive_skb(skb);
489 priv->rx_head = desc;
490 dma_cache_inv((u32)desc, 16);
494 dev->quota -= received;
496 printk("%s: processed %d packets\n", dev->name, received);
498 if (desc->dataflags & CPMAC_OWN) {
499 priv->regs->rx_ptr[0] = virt_to_phys(desc);
500 netif_rx_complete(dev);
501 priv->regs->rx_int.enable = 0x1;
502 priv->regs->rx_int.clear = 0xfe;
509 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
511 cpmac_alloc_skbs(struct work_struct *work)
513 struct cpmac_priv *priv = container_of(work, struct cpmac_priv,
517 cpmac_alloc_skbs(void *data)
519 struct net_device *dev = (struct net_device*)data;
520 struct cpmac_priv *priv = netdev_priv(dev);
524 struct sk_buff *skb, *skbs = NULL;
526 for (i = 0; i < CPMAC_ALLOC_SIZE; i++) {
527 skb = alloc_skb(CPMAC_SKB_SIZE + 2, GFP_KERNEL);
532 skb->dev = priv->dev;
538 spin_lock_irqsave(&priv->lock, flags);
539 for (skb = priv->skb_pool; skb && skb->next; skb = skb->next);
541 priv->skb_pool = skbs;
545 priv->free_skbs += num_skbs;
546 spin_unlock_irqrestore(&priv->lock, flags);
548 printk("%s: allocated %d skbs\n", priv->dev->name, num_skbs);
553 static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
557 struct cpmac_desc *desc;
558 struct cpmac_priv *priv = netdev_priv(dev);
561 if (unlikely(len < ETH_ZLEN)) {
562 if (unlikely(skb_padto(skb, ETH_ZLEN))) {
563 if (printk_ratelimit())
564 printk(KERN_NOTICE "%s: padding failed, dropping\n",
566 spin_lock_irqsave(&priv->lock, flags);
567 priv->stats.tx_dropped++;
568 spin_unlock_irqrestore(&priv->lock, flags);
573 spin_lock_irqsave(&priv->lock, flags);
574 chan = priv->tx_tail++;
576 if (priv->tx_tail == priv->tx_head)
577 netif_stop_queue(dev);
579 desc = &priv->desc_ring[chan];
580 dma_cache_inv((u32)desc, 16);
581 if (desc->dataflags & CPMAC_OWN) {
582 printk(KERN_NOTICE "%s: tx dma ring full, dropping\n", dev->name);
583 priv->stats.tx_dropped++;
584 spin_unlock_irqrestore(&priv->lock, flags);
588 dev->trans_start = jiffies;
589 desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
591 desc->hw_data = virt_to_phys(skb->data);
592 dma_cache_wback((u32)skb->data, len);
596 dma_cache_wback((u32)desc, 16);
597 priv->regs->tx_ptr[chan] = virt_to_phys(desc);
598 spin_unlock_irqrestore(&priv->lock, flags);
603 static void cpmac_end_xmit(struct net_device *dev, int channel)
605 struct cpmac_desc *desc;
606 struct cpmac_priv *priv = netdev_priv(dev);
608 spin_lock(&priv->lock);
609 desc = &priv->desc_ring[channel];
610 priv->regs->tx_ack[channel] = virt_to_phys(desc);
611 if (likely(desc->skb)) {
612 priv->stats.tx_packets++;
613 priv->stats.tx_bytes += desc->skb->len;
614 dev_kfree_skb_irq(desc->skb);
615 if (netif_queue_stopped(dev))
616 netif_wake_queue(dev);
618 if (printk_ratelimit())
619 printk(KERN_NOTICE "%s: end_xmit: spurious interrupt\n",
622 spin_unlock(&priv->lock);
625 static void cpmac_reset(struct net_device *dev)
628 struct cpmac_priv *priv = netdev_priv(dev);
630 ar7_device_reset(priv->config->reset_bit);
631 priv->regs->rx_ctrl.control &= ~1;
632 priv->regs->tx_ctrl.control &= ~1;
633 for (i = 0; i < 8; i++) {
634 priv->regs->tx_ptr[i] = 0;
635 priv->regs->rx_ptr[i] = 0;
637 priv->regs->mac_control &= ~MAC_MII; /* disable mii */
640 static inline void cpmac_free_rx_ring(struct net_device *dev)
642 struct cpmac_desc *desc;
644 struct cpmac_priv *priv = netdev_priv(dev);
646 if (unlikely(!priv->rx_head))
649 desc = priv->rx_head;
650 dma_cache_inv((u32)desc, 16);
652 for (i = 0; i < rx_ring_size; i++) {
653 desc->buflen = CPMAC_SKB_SIZE;
654 if ((desc->dataflags & CPMAC_OWN) == 0) {
655 desc->dataflags = CPMAC_OWN;
656 priv->stats.rx_dropped++;
658 dma_cache_wback((u32)desc, 16);
660 dma_cache_inv((u32)desc, 16);
664 static irqreturn_t cpmac_irq(int irq, void *dev_id)
666 struct net_device *dev = (struct net_device *)dev_id;
667 struct cpmac_priv *priv = netdev_priv(dev);
673 status = priv->regs->mac_int_vector;
675 if (status & INTST_TX) {
676 cpmac_end_xmit(dev, (status & 7));
679 if (status & INTST_RX) {
683 priv->regs->rx_int.enable = 0;
684 priv->regs->rx_int.clear = 0xff;
685 netif_rx_schedule(dev);
689 priv->regs->mac_eoi_vector = 0;
691 if (unlikely(status & (INTST_HOST | INTST_STATUS))) {
692 printk(KERN_ERR "%s: hw error, resetting...\n", dev->name);
693 spin_lock(&priv->lock);
696 cpmac_free_rx_ring(dev);
698 spin_unlock(&priv->lock);
704 static void cpmac_tx_timeout(struct net_device *dev)
706 struct cpmac_priv *priv = netdev_priv(dev);
707 struct cpmac_desc *desc;
709 priv->stats.tx_errors++;
710 desc = &priv->desc_ring[priv->tx_head++];
712 printk("%s: transmit timeout\n", dev->name);
714 dev_kfree_skb(desc->skb);
715 netif_wake_queue(dev);
718 static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
720 struct cpmac_priv *priv = netdev_priv(dev);
721 if (!(netif_running(dev)))
725 if ((cmd == SIOCGMIIPHY) || (cmd == SIOCGMIIREG) ||
726 (cmd == SIOCSMIIREG))
727 return phy_mii_ioctl(priv->phy, if_mii(ifr), cmd);
732 static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
734 struct cpmac_priv *priv = netdev_priv(dev);
737 return phy_ethtool_gset(priv->phy, cmd);
742 static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
744 struct cpmac_priv *priv = netdev_priv(dev);
746 if (!capable(CAP_NET_ADMIN))
750 return phy_ethtool_sset(priv->phy, cmd);
755 static void cpmac_get_drvinfo(struct net_device *dev,
756 struct ethtool_drvinfo *info)
758 strcpy(info->driver, "cpmac");
759 strcpy(info->version, "0.0.3");
760 info->fw_version[0] = '\0';
761 sprintf(info->bus_info, "%s", "cpmac");
762 info->regdump_len = 0;
765 static const struct ethtool_ops cpmac_ethtool_ops = {
766 .get_settings = cpmac_get_settings,
767 .set_settings = cpmac_set_settings,
768 .get_drvinfo = cpmac_get_drvinfo,
769 .get_link = ethtool_op_get_link,
772 static struct net_device_stats *cpmac_stats(struct net_device *dev)
774 struct cpmac_priv *priv = netdev_priv(dev);
776 if (netif_device_present(dev))
782 static int cpmac_change_mtu(struct net_device *dev, int mtu)
785 struct cpmac_priv *priv = netdev_priv(dev);
786 spinlock_t *lock = &priv->lock;
788 if ((mtu < 68) || (mtu > 1500))
791 spin_lock_irqsave(lock, flags);
793 spin_unlock_irqrestore(lock, flags);
798 static void cpmac_adjust_link(struct net_device *dev)
800 struct cpmac_priv *priv = netdev_priv(dev);
804 spin_lock_irqsave(&priv->lock, flags);
805 if (priv->phy->link) {
806 if (priv->phy->duplex != priv->oldduplex) {
808 priv->oldduplex = priv->phy->duplex;
811 if (priv->phy->speed != priv->oldspeed) {
813 priv->oldspeed = priv->phy->speed;
816 if (!priv->oldlink) {
821 } else if (priv->oldlink) {
825 priv->oldduplex = -1;
829 phy_print_status(priv->phy);
831 spin_unlock_irqrestore(&priv->lock, flags);
834 static void cpmac_hw_init(struct net_device *dev)
837 struct cpmac_priv *priv = netdev_priv(dev);
839 for (i = 0; i < 8; i++)
840 priv->regs->tx_ptr[i] = 0;
841 priv->regs->rx_ptr[0] = virt_to_phys(priv->rx_head);
843 priv->regs->mbp = MBP_RXSHORT | MBP_RXBCAST | MBP_RXMCAST;
844 priv->regs->unicast_enable = 0x1;
845 priv->regs->unicast_clear = 0xfe;
846 priv->regs->buffer_offset = 0;
847 for (i = 0; i < 8; i++)
848 priv->regs->mac_addr_low[i] = dev->dev_addr[5];
849 priv->regs->mac_addr_mid = dev->dev_addr[4];
850 priv->regs->mac_addr_high = dev->dev_addr[0] | (dev->dev_addr[1] << 8)
851 | (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
852 priv->regs->max_len = CPMAC_SKB_SIZE;
853 priv->regs->rx_int.enable = 0x1;
854 priv->regs->rx_int.clear = 0xfe;
855 priv->regs->tx_int.enable = 0xff;
856 priv->regs->tx_int.clear = 0;
857 priv->regs->mac_int_enable = 3;
858 priv->regs->mac_int_clear = 0xfc;
860 priv->regs->rx_ctrl.control |= 1;
861 priv->regs->tx_ctrl.control |= 1;
862 priv->regs->mac_control |= MAC_MII | MAC_FDX;
864 priv->phy->state = PHY_CHANGELINK;
865 phy_start(priv->phy);
868 static int cpmac_open(struct net_device *dev)
871 struct cpmac_priv *priv = netdev_priv(dev);
872 struct cpmac_desc *desc;
875 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
876 priv->phy = phy_connect(dev, priv->phy_name, &cpmac_adjust_link,
877 0, PHY_INTERFACE_MODE_MII);
879 priv->phy = phy_connect(dev, priv->phy_name, &cpmac_adjust_link, 0);
881 if (IS_ERR(priv->phy)) {
882 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
883 return PTR_ERR(priv->phy);
886 if (!request_mem_region(dev->mem_start, dev->mem_end -
887 dev->mem_start, dev->name)) {
888 printk("%s: failed to request registers\n",
894 priv->regs = ioremap_nocache(dev->mem_start, dev->mem_end -
897 printk("%s: failed to remap registers\n", dev->name);
902 priv->rx_head = NULL;
903 size = sizeof(struct cpmac_desc) * (rx_ring_size +
905 priv->desc_ring = (struct cpmac_desc *)kmalloc(size, GFP_KERNEL);
906 if (!priv->desc_ring) {
911 memset((char *)priv->desc_ring, 0, size);
913 priv->skb_pool = NULL;
915 priv->rx_head = &priv->desc_ring[CPMAC_TX_RING_SIZE];
917 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
918 INIT_WORK(&priv->alloc_work, cpmac_alloc_skbs);
920 INIT_WORK(&priv->alloc_work, cpmac_alloc_skbs, dev);
922 schedule_work(&priv->alloc_work);
923 flush_scheduled_work();
925 for (i = 0; i < rx_ring_size; i++) {
926 desc = &priv->rx_head[i];
927 skb = cpmac_get_skb(dev);
933 desc->hw_data = virt_to_phys(skb->data);
934 desc->buflen = CPMAC_SKB_SIZE;
935 desc->dataflags = CPMAC_OWN;
936 desc->next = &priv->rx_head[(i + 1) % rx_ring_size];
937 desc->hw_next = virt_to_phys(desc->next);
938 dma_cache_wback((u32)desc, 16);
941 if((res = request_irq(dev->irq, cpmac_irq, SA_INTERRUPT,
943 printk("%s: failed to obtain irq\n", dev->name);
950 netif_start_queue(dev);
955 for (i = 0; i < rx_ring_size; i++)
956 if (priv->rx_head[i].skb)
957 kfree_skb(priv->rx_head[i].skb);
959 kfree(priv->desc_ring);
961 for (skb = priv->skb_pool; skb; skb = priv->skb_pool) {
962 priv->skb_pool = skb->next;
969 release_mem_region(dev->mem_start, dev->mem_end -
973 phy_disconnect(priv->phy);
978 static int cpmac_stop(struct net_device *dev)
982 struct cpmac_priv *priv = netdev_priv(dev);
984 netif_stop_queue(dev);
987 phy_disconnect(priv->phy);
992 for (i = 0; i < 8; i++) {
993 priv->regs->rx_ptr[i] = 0;
994 priv->regs->tx_ptr[i] = 0;
998 free_irq(dev->irq, dev);
999 release_mem_region(dev->mem_start, dev->mem_end -
1002 cancel_delayed_work(&priv->alloc_work);
1003 flush_scheduled_work();
1005 priv->rx_head = &priv->desc_ring[CPMAC_TX_RING_SIZE];
1006 for (i = 0; i < rx_ring_size; i++)
1007 if (priv->rx_head[i].skb)
1008 kfree_skb(priv->rx_head[i].skb);
1010 kfree(priv->desc_ring);
1012 for (skb = priv->skb_pool; skb; skb = priv->skb_pool) {
1013 priv->skb_pool = skb->next;
1020 static int external_switch = 0;
1022 static int __devinit cpmac_probe(struct platform_device *pdev)
1025 struct resource *res;
1026 struct cpmac_priv *priv;
1027 struct net_device *dev;
1028 struct plat_cpmac_data *pdata;
1030 if (strcmp(pdev->name, "cpmac") != 0)
1033 pdata = pdev->dev.platform_data;
1035 for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
1036 if (!(pdata->phy_mask & (1 << phy_id)))
1038 if (!cpmac_mii.phy_map[phy_id])
1043 if (phy_id == PHY_MAX_ADDR) {
1044 if (external_switch) {
1047 printk("cpmac: no PHY present\n");
1052 dev = alloc_etherdev(sizeof(struct cpmac_priv));
1055 printk(KERN_ERR "cpmac: Unable to allocate net_device structure!\n");
1059 SET_MODULE_OWNER(dev);
1060 platform_set_drvdata(pdev, dev);
1061 priv = netdev_priv(dev);
1063 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
1069 dev->mem_start = res->start;
1070 dev->mem_end = res->end;
1071 dev->irq = platform_get_irq_byname(pdev, "irq");
1074 dev->open = cpmac_open;
1075 dev->stop = cpmac_stop;
1076 dev->set_config = cpmac_config;
1077 dev->hard_start_xmit = cpmac_start_xmit;
1078 dev->do_ioctl = cpmac_ioctl;
1079 dev->get_stats = cpmac_stats;
1080 dev->change_mtu = cpmac_change_mtu;
1081 dev->set_mac_address = cpmac_set_mac_address;
1082 dev->set_multicast_list = cpmac_set_multicast_list;
1083 dev->tx_timeout = cpmac_tx_timeout;
1084 dev->ethtool_ops = &cpmac_ethtool_ops;
1085 if (!disable_napi) {
1086 dev->poll = cpmac_poll;
1087 dev->weight = min(rx_ring_size, 64);
1090 memset(priv, 0, sizeof(struct cpmac_priv));
1091 spin_lock_init(&priv->lock);
1092 priv->msg_enable = netif_msg_init(NETIF_MSG_WOL, 0x3fff);
1093 priv->config = pdata;
1095 memcpy(dev->dev_addr, priv->config->dev_addr, sizeof(dev->dev_addr));
1097 snprintf(priv->phy_name, BUS_ID_SIZE, PHY_ID_FMT,
1098 cpmac_mii.id, phy_id);
1100 snprintf(priv->phy_name, BUS_ID_SIZE, "fixed@%d:%d", 100, 1);
1103 if ((rc = register_netdev(dev))) {
1104 printk("cpmac: error %i registering device %s\n",
1109 printk("cpmac: device %s (regs: %p, irq: %d, phy: %s, mac: ",
1110 dev->name, (u32 *)dev->mem_start, dev->irq,
1112 for (i = 0; i < 6; i++) {
1113 printk("%02x", dev->dev_addr[i]);
1114 if (i < 5) printk(":");
1125 static int __devexit cpmac_remove(struct platform_device *pdev)
1127 struct net_device *dev = platform_get_drvdata(pdev);
1128 unregister_netdev(dev);
1133 static struct platform_driver cpmac_driver = {
1134 .driver.name = "cpmac",
1135 .probe = cpmac_probe,
1136 .remove = cpmac_remove,
1139 int __devinit cpmac_init(void)
1143 cpmac_mii.priv = (struct cpmac_mdio_regs *)
1144 ioremap_nocache(AR7_REGS_MDIO, sizeof(struct cpmac_mdio_regs));
1146 if (!cpmac_mii.priv) {
1147 printk("Can't ioremap mdio registers\n");
1151 #warning FIXME: unhardcode gpio&reset bits
1152 ar7_gpio_disable(26);
1153 ar7_gpio_disable(27);
1154 ar7_device_reset(17);
1155 ar7_device_reset(21);
1156 ar7_device_reset(26);
1158 cpmac_mii.reset(&cpmac_mii);
1160 for (i = 0; i < 300000; i++) {
1161 mask = ((struct cpmac_mdio_regs *)cpmac_mii.priv)->alive;
1167 if (mask & (mask - 1)) {
1168 external_switch = 1;
1172 cpmac_mii.phy_mask = ~(mask | 0x80000000);
1174 res = mdiobus_register(&cpmac_mii);
1178 res = platform_driver_register(&cpmac_driver);
1185 mdiobus_unregister(&cpmac_mii);
1188 iounmap(cpmac_mii.priv);
1193 void __devexit cpmac_exit(void)
1195 platform_driver_unregister(&cpmac_driver);
1196 mdiobus_unregister(&cpmac_mii);
1199 module_init(cpmac_init);
1200 module_exit(cpmac_exit);