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 /* Register definitions */
48 struct cpmac_control_regs {
49 volatile u32 revision;
51 volatile u32 teardown;
55 struct cpmac_int_regs {
56 volatile u32 stat_raw;
57 volatile u32 stat_masked;
67 volatile u32 crc_error;
68 volatile u32 align_error;
69 volatile u32 oversized;
71 volatile u32 undersized;
72 volatile u32 fragment;
73 volatile u32 filtered;
74 volatile u32 qos_filtered;
79 struct cpmac_control_regs tx_ctrl;
80 struct cpmac_control_regs rx_ctrl;
81 volatile u32 unused1[56];
84 #define MBP_RXPASSCRC 0x40000000
85 #define MBP_RXQOS 0x20000000
86 #define MBP_RXNOCHAIN 0x10000000
87 #define MBP_RXCMF 0x01000000
88 #define MBP_RXSHORT 0x00800000
89 #define MBP_RXCEF 0x00400000
90 #define MBP_RXPROMISC 0x00200000
91 #define MBP_PROMISCCHAN(chan) (((chan) & 0x7) << 16)
92 #define MBP_RXBCAST 0x00002000
93 #define MBP_BCASTCHAN(chan) (((chan) & 0x7) << 8)
94 #define MBP_RXMCAST 0x00000020
95 #define MBP_MCASTCHAN(chan) ((chan) & 0x7)
96 volatile u32 unicast_enable;
97 volatile u32 unicast_clear;
99 volatile u32 buffer_offset;
100 volatile u32 filter_flow_threshold;
101 volatile u32 unused2[2];
102 volatile u32 flow_thre[8];
103 volatile u32 free_buffer[8];
104 volatile u32 mac_control;
105 #define MAC_TXPTYPE 0x00000200
106 #define MAC_TXPACE 0x00000040
107 #define MAC_MII 0x00000020
108 #define MAC_TXFLOW 0x00000010
109 #define MAC_RXFLOW 0x00000008
110 #define MAC_MTEST 0x00000004
111 #define MAC_LOOPBACK 0x00000002
112 #define MAC_FDX 0x00000001
113 volatile u32 mac_status;
114 #define MACST_QOS 0x4
115 #define MACST_RXFLOW 0x2
116 #define MACST_TXFLOW 0x1
117 volatile u32 emc_control;
118 volatile u32 unused3;
119 struct cpmac_int_regs tx_int;
120 volatile u32 mac_int_vector;
121 /* Int Status bits */
122 #define INTST_STATUS 0x80000
123 #define INTST_HOST 0x40000
124 #define INTST_RX 0x20000
125 #define INTST_TX 0x10000
126 volatile u32 mac_eoi_vector;
127 volatile u32 unused4[2];
128 struct cpmac_int_regs rx_int;
129 volatile u32 mac_int_stat_raw;
130 volatile u32 mac_int_stat_masked;
131 volatile u32 mac_int_enable;
132 volatile u32 mac_int_clear;
133 volatile u32 mac_addr_low[8];
134 volatile u32 mac_addr_mid;
135 volatile u32 mac_addr_high;
136 volatile u32 mac_hash_low;
137 volatile u32 mac_hash_high;
138 volatile u32 boff_test;
139 volatile u32 pac_test;
140 volatile u32 rx_pause;
141 volatile u32 tx_pause;
142 volatile u32 unused5[2];
143 struct cpmac_stats rx_stats;
144 struct cpmac_stats tx_stats;
145 volatile u32 unused6[232];
146 volatile u32 tx_ptr[8];
147 volatile u32 rx_ptr[8];
148 volatile u32 tx_ack[8];
149 volatile u32 rx_ack[8];
153 struct cpmac_mdio_regs {
154 volatile u32 version;
155 volatile u32 control;
156 #define MDIOC_IDLE 0x80000000
157 #define MDIOC_ENABLE 0x40000000
158 #define MDIOC_PREAMBLE 0x00100000
159 #define MDIOC_FAULT 0x00080000
160 #define MDIOC_FAULTDETECT 0x00040000
161 #define MDIOC_INTTEST 0x00020000
162 #define MDIOC_CLKDIV(div) ((div) & 0xff)
165 struct cpmac_int_regs link_int;
166 struct cpmac_int_regs user_int;
169 #define MDIO_BUSY 0x80000000
170 #define MDIO_WRITE 0x40000000
171 #define MDIO_REG(reg) (((reg) & 0x1f) << 21)
172 #define MDIO_PHY(phy) (((phy) & 0x1f) << 16)
173 #define MDIO_DATA(data) ((data) & 0xffff)
186 #define CPMAC_SOP 0x8000
187 #define CPMAC_EOP 0x4000
188 #define CPMAC_OWN 0x2000
189 #define CPMAC_EOQ 0x1000
192 struct cpmac_desc *next;
196 struct net_device_stats stats;
198 struct sk_buff *skb_pool;
200 struct cpmac_desc *rx_head, *rx_tail;
201 int tx_head, tx_tail;
202 struct cpmac_desc *desc_ring;
203 struct cpmac_regs *regs;
204 struct mii_bus *mii_bus;
205 struct phy_device *phy;
206 char phy_name[BUS_ID_SIZE];
207 struct plat_cpmac_data *config;
208 int oldlink, oldspeed, oldduplex;
210 struct net_device *dev;
211 struct work_struct alloc_work;
212 struct work_struct reset_work;
215 static irqreturn_t cpmac_irq(int, void *);
216 static int cpmac_stop(struct net_device *dev);
217 static int cpmac_open(struct net_device *dev);
219 #define CPMAC_LOW_THRESH 8
220 #define CPMAC_ALLOC_SIZE 32
221 #define CPMAC_SKB_SIZE 1536
222 #define CPMAC_TX_RING_SIZE 8
223 #define CPMAC_RX_RING_SIZE 16
226 static void cpmac_dump_regs(u32 *base, int count)
229 for (i = 0; i < (count + 3) / 4; i++) {
230 if (i % 4 == 0) printk("\nCPMAC[0x%04x]:", i * 4);
231 printk(" 0x%08x", *(base + i));
237 static int cpmac_mdio_read(struct mii_bus *bus, int phy_id, int regnum)
239 struct cpmac_mdio_regs *regs = (struct cpmac_mdio_regs *)bus->priv;
242 while ((val = regs->access) & MDIO_BUSY);
243 regs->access = MDIO_BUSY | MDIO_REG(regnum & 0x1f) |
244 MDIO_PHY(phy_id & 0x1f);
245 while ((val = regs->access) & MDIO_BUSY);
250 static int cpmac_mdio_write(struct mii_bus *bus, int phy_id, int regnum, u16 val)
252 struct cpmac_mdio_regs *regs = (struct cpmac_mdio_regs *)bus->priv;
255 while ((tmp = regs->access) & MDIO_BUSY);
256 regs->access = MDIO_BUSY | MDIO_WRITE |
257 MDIO_REG(regnum & 0x1f) | MDIO_PHY(phy_id & 0x1f) |
263 static int cpmac_mdio_reset(struct mii_bus *bus)
265 struct cpmac_mdio_regs *regs = (struct cpmac_mdio_regs *)bus->priv;
267 ar7_device_reset(AR7_RESET_BIT_MDIO);
268 regs->control = MDIOC_ENABLE |
269 MDIOC_CLKDIV(ar7_cpmac_freq() / 2200000 - 1);
274 static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, };
276 static struct mii_bus cpmac_mii = {
278 .read = cpmac_mdio_read,
279 .write = cpmac_mdio_write,
280 .reset = cpmac_mdio_reset,
284 static int cpmac_config(struct net_device *dev, struct ifmap *map)
286 if (dev->flags & IFF_UP)
289 /* Don't allow changing the I/O address */
290 if (map->base_addr != dev->base_addr)
293 /* ignore other fields */
297 static int cpmac_set_mac_address(struct net_device *dev, void *addr)
299 struct sockaddr *sa = addr;
301 if (dev->flags & IFF_UP)
304 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
309 static void cpmac_set_multicast_list(struct net_device *dev)
311 struct dev_mc_list *iter;
314 int hashlo = 0, hashhi = 0;
315 struct cpmac_priv *priv = netdev_priv(dev);
317 if(dev->flags & IFF_PROMISC) {
318 priv->regs->mbp &= ~MBP_PROMISCCHAN(0); /* promisc channel 0 */
319 priv->regs->mbp |= MBP_RXPROMISC;
321 priv->regs->mbp &= ~MBP_RXPROMISC;
322 if(dev->flags & IFF_ALLMULTI) {
323 /* enable all multicast mode */
324 priv->regs->mac_hash_low = 0xffffffff;
325 priv->regs->mac_hash_high = 0xffffffff;
327 for(i = 0, iter = dev->mc_list; i < dev->mc_count;
328 i++, iter = iter->next) {
330 tmp = iter->dmi_addr[0];
331 hash ^= (tmp >> 2) ^ (tmp << 4);
332 tmp = iter->dmi_addr[1];
333 hash ^= (tmp >> 4) ^ (tmp << 2);
334 tmp = iter->dmi_addr[2];
335 hash ^= (tmp >> 6) ^ tmp;
336 tmp = iter->dmi_addr[4];
337 hash ^= (tmp >> 2) ^ (tmp << 4);
338 tmp = iter->dmi_addr[5];
339 hash ^= (tmp >> 4) ^ (tmp << 2);
340 tmp = iter->dmi_addr[6];
341 hash ^= (tmp >> 6) ^ tmp;
346 hashhi |= 1<<(hash - 32);
350 priv->regs->mac_hash_low = hashlo;
351 priv->regs->mac_hash_high = hashhi;
356 static struct sk_buff *cpmac_get_skb(struct net_device *dev)
359 struct cpmac_priv *priv = netdev_priv(dev);
361 skb = priv->skb_pool;
363 priv->skb_pool = skb->next;
365 if (likely(priv->free_skbs))
368 if (priv->free_skbs < CPMAC_LOW_THRESH)
369 schedule_work(&priv->alloc_work);
374 static void cpmac_rx(struct net_device *dev, int channel)
378 struct cpmac_desc *desc;
379 struct cpmac_desc *start;
380 struct cpmac_priv *priv = netdev_priv(dev);
382 spin_lock(&priv->lock);
383 if (unlikely(!priv->rx_head))
387 desc = priv->rx_tail->next;
388 dma_cache_inv((u32)desc, 16);
390 start = priv->rx_tail;
391 while((desc->dataflags & CPMAC_OWN) == 0) {
392 priv->regs->rx_ack[0] = virt_to_phys(desc);
393 if (unlikely(!desc->datalen)) {
394 if (printk_ratelimit())
395 printk(KERN_NOTICE "%s: rx: spurious interrupt\n",
397 priv->stats.rx_errors++;
401 skb = cpmac_get_skb(dev);
403 data = (char *)phys_to_virt(desc->hw_data);
404 dma_cache_inv((u32)data, desc->datalen);
405 skb_put(desc->skb, desc->datalen);
406 desc->skb->protocol = eth_type_trans(desc->skb, dev);
407 desc->skb->ip_summed = CHECKSUM_NONE;
408 priv->stats.rx_packets++;
409 priv->stats.rx_bytes += desc->datalen;
413 if (printk_ratelimit())
414 printk(KERN_NOTICE "%s: rx: no free skbs, dropping packet\n",
416 priv->regs->rx_ptr[0] = virt_to_phys(desc);
417 priv->stats.rx_dropped++;
419 desc->hw_data = virt_to_phys(desc->skb->data);
420 desc->buflen = CPMAC_SKB_SIZE;
421 desc->dataflags = CPMAC_OWN;
423 dma_cache_wback((u32)desc, 16);
425 priv->rx_tail->hw_next = virt_to_phys(desc);
426 priv->rx_tail->dataflags = CPMAC_OWN;
427 dma_cache_wback((u32)priv->rx_tail, 16);
429 priv->rx_tail = desc;
431 dma_cache_inv((u32)desc, 16);
433 printk("Somebody set up us the bomb!!\n");
438 priv->rx_head = desc;
439 priv->regs->rx_ptr[0] = virt_to_phys(desc);
440 spin_unlock(&priv->lock);
443 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
445 cpmac_alloc_skbs(struct work_struct *work)
447 struct cpmac_priv *priv = container_of(work, struct cpmac_priv,
451 cpmac_alloc_skbs(void *data)
453 struct net_device *dev = (struct net_device*)data;
454 struct cpmac_priv *priv = netdev_priv(dev);
458 struct sk_buff *skb, *skbs = NULL;
460 for (i = 0; i < CPMAC_ALLOC_SIZE; i++) {
461 skb = alloc_skb(CPMAC_SKB_SIZE + 2, GFP_KERNEL);
466 skb->dev = priv->dev;
472 spin_lock_irqsave(&priv->lock, flags);
473 for (skb = priv->skb_pool; skb && skb->next; skb = skb->next);
475 priv->skb_pool = skbs;
479 priv->free_skbs += num_skbs;
480 spin_unlock_irqrestore(&priv->lock, flags);
482 printk("%s: allocated %d skbs\n", priv->dev->name, num_skbs);
487 static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
491 struct cpmac_desc *desc;
492 struct cpmac_priv *priv = netdev_priv(dev);
495 if (unlikely(len < ETH_ZLEN)) {
496 if (unlikely(skb_padto(skb, ETH_ZLEN))) {
497 if (printk_ratelimit())
498 printk(KERN_NOTICE "%s: padding failed, dropping\n",
500 spin_lock_irqsave(&priv->lock, flags);
501 priv->stats.tx_dropped++;
502 spin_unlock_irqrestore(&priv->lock, flags);
507 spin_lock_irqsave(&priv->lock, flags);
508 chan = priv->tx_tail++;
510 if (priv->tx_tail == priv->tx_head)
511 netif_stop_queue(dev);
513 desc = &priv->desc_ring[chan];
514 dma_cache_inv((u32)desc, 16);
515 if (desc->dataflags & CPMAC_OWN) {
516 printk(KERN_NOTICE "%s: tx dma ring full, dropping\n", dev->name);
517 spin_lock_irqsave(&priv->lock, flags);
518 priv->stats.tx_dropped++;
519 spin_unlock_irqrestore(&priv->lock, flags);
523 dev->trans_start = jiffies;
524 desc->jiffies = dev->trans_start;
525 spin_unlock_irqrestore(&priv->lock, flags);
527 desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
529 desc->hw_data = virt_to_phys(skb->data);
530 dma_cache_wback((u32)skb->data, len);
534 dma_cache_wback((u32)desc, 16);
535 priv->regs->tx_ptr[chan] = virt_to_phys(desc);
539 static void cpmac_end_xmit(struct net_device *dev, int channel)
541 struct cpmac_desc *desc;
542 struct cpmac_priv *priv = netdev_priv(dev);
544 spin_lock(&priv->lock);
545 desc = &priv->desc_ring[channel];
546 priv->regs->tx_ack[channel] = virt_to_phys(desc);
547 if (likely(desc->skb)) {
548 priv->stats.tx_packets++;
549 priv->stats.tx_bytes += desc->skb->len;
550 dev_kfree_skb_irq(desc->skb);
551 if (priv->tx_head == channel) {
552 while ((desc->dataflags & CPMAC_OWN) == 0) {
555 if (priv->tx_head == priv->tx_tail)
557 desc = &priv->desc_ring[priv->tx_head];
560 if (netif_queue_stopped(dev))
561 netif_wake_queue(dev);
563 if (printk_ratelimit())
564 printk(KERN_NOTICE "%s: end_xmit: spurious interrupt\n",
567 spin_unlock(&priv->lock);
570 static void cpmac_reset(struct net_device *dev)
573 struct cpmac_priv *priv = netdev_priv(dev);
575 ar7_device_reset(priv->config->reset_bit);
576 priv->regs->rx_ctrl.control &= ~1;
577 priv->regs->tx_ctrl.control &= ~1;
578 for (i = 0; i < 8; i++) {
579 priv->regs->tx_ptr[i] = 0;
580 priv->regs->rx_ptr[i] = 0;
582 priv->regs->mac_control &= ~MAC_MII; /* disable mii */
585 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
587 cpmac_full_reset(struct work_struct *work)
589 struct cpmac_priv *priv = container_of(work, struct cpmac_priv,
591 struct net_device *dev = priv->dev;
594 cpmac_full_reset(void *data)
596 struct net_device *dev = (struct net_device*)data;
604 static irqreturn_t cpmac_irq(int irq, void *dev_id)
606 struct net_device *dev = (struct net_device *)dev_id;
607 struct cpmac_priv *priv = netdev_priv(dev);
613 status = priv->regs->mac_int_vector;
615 if (status & INTST_TX) {
616 cpmac_end_xmit(dev, (status & 7));
619 if (status & INTST_RX) {
620 cpmac_rx(dev, (status >> 8) & 7);
623 if (unlikely(status & INTST_HOST)) { /* host interrupt ??? */
624 printk("%s: host int, something bad happened - mac status: 0x%08x\n", dev->name, priv->regs->mac_status);
628 schedule_work(&priv->reset_work);
631 if (unlikely(status & INTST_STATUS)) { /* status interrupt ??? */
632 printk("%s: status int, what are we gonna do?\n", dev->name);
635 priv->regs->mac_eoi_vector = 0;
640 static void cpmac_tx_timeout(struct net_device *dev)
642 struct cpmac_priv *priv = netdev_priv(dev);
643 struct cpmac_desc *desc;
645 priv->stats.tx_errors++;
646 desc = &priv->desc_ring[priv->tx_head++];
648 printk("Transmit timeout at %ld, latency %ld\n", jiffies,
649 jiffies - desc->jiffies);
651 dev_kfree_skb(desc->skb);
652 netif_wake_queue(dev);
655 static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
657 struct cpmac_priv *priv = netdev_priv(dev);
658 if (!(netif_running(dev)))
662 return phy_mii_ioctl(priv->phy, if_mii(ifr), cmd);
665 static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
667 struct cpmac_priv *priv = netdev_priv(dev);
670 return phy_ethtool_gset(priv->phy, cmd);
675 static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
677 struct cpmac_priv *priv = netdev_priv(dev);
679 if (!capable(CAP_NET_ADMIN))
683 return phy_ethtool_sset(priv->phy, cmd);
688 static void cpmac_get_drvinfo(struct net_device *dev,
689 struct ethtool_drvinfo *info)
691 strcpy(info->driver, "cpmac");
692 strcpy(info->version, "0.0.3");
693 info->fw_version[0] = '\0';
694 sprintf(info->bus_info, "%s", "cpmac");
695 info->regdump_len = 0;
698 static const struct ethtool_ops cpmac_ethtool_ops = {
699 .get_settings = cpmac_get_settings,
700 .set_settings = cpmac_set_settings,
701 .get_drvinfo = cpmac_get_drvinfo,
702 .get_link = ethtool_op_get_link,
705 static struct net_device_stats *cpmac_stats(struct net_device *dev)
707 struct cpmac_priv *priv = netdev_priv(dev);
709 if (netif_device_present(dev))
715 static int cpmac_change_mtu(struct net_device *dev, int mtu)
718 struct cpmac_priv *priv = netdev_priv(dev);
719 spinlock_t *lock = &priv->lock;
721 if ((mtu < 68) || (mtu > 1500))
724 spin_lock_irqsave(lock, flags);
726 spin_unlock_irqrestore(lock, flags);
731 static void cpmac_adjust_link(struct net_device *dev)
733 struct cpmac_priv *priv = netdev_priv(dev);
737 spin_lock_irqsave(&priv->lock, flags);
738 if (priv->phy->link) {
739 if (priv->phy->duplex != priv->oldduplex) {
741 priv->oldduplex = priv->phy->duplex;
744 if (priv->phy->speed != priv->oldspeed) {
746 priv->oldspeed = priv->phy->speed;
749 if (!priv->oldlink) {
754 } else if (priv->oldlink) {
758 priv->oldduplex = -1;
762 phy_print_status(priv->phy);
764 spin_unlock_irqrestore(&priv->lock, flags);
767 static int cpmac_open(struct net_device *dev)
770 struct cpmac_priv *priv = netdev_priv(dev);
771 struct cpmac_desc *desc;
774 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
775 priv->phy = phy_connect(dev, priv->phy_name, &cpmac_adjust_link,
776 0, PHY_INTERFACE_MODE_MII);
778 priv->phy = phy_connect(dev, priv->phy_name, &cpmac_adjust_link, 0);
780 if (IS_ERR(priv->phy)) {
781 printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
782 return PTR_ERR(priv->phy);
785 if (!request_mem_region(dev->mem_start, dev->mem_end -
786 dev->mem_start, dev->name)) {
787 printk("%s: failed to request registers\n",
793 priv->regs = ioremap_nocache(dev->mem_start, dev->mem_end -
796 printk("%s: failed to remap registers\n", dev->name);
801 priv->rx_head = NULL;
802 size = sizeof(struct cpmac_desc) * (CPMAC_RX_RING_SIZE +
804 priv->desc_ring = (struct cpmac_desc *)kmalloc(size, GFP_KERNEL);
805 if (!priv->desc_ring) {
810 memset((char *)priv->desc_ring, 0, size);
812 priv->skb_pool = NULL;
814 priv->rx_head = &priv->desc_ring[CPMAC_TX_RING_SIZE];
816 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
817 INIT_WORK(&priv->alloc_work, cpmac_alloc_skbs);
818 INIT_WORK(&priv->reset_work, cpmac_full_reset);
820 INIT_WORK(&priv->alloc_work, cpmac_alloc_skbs, dev);
821 INIT_WORK(&priv->reset_work, cpmac_full_reset, dev);
823 schedule_work(&priv->alloc_work);
824 flush_scheduled_work();
826 for (i = 0; i < CPMAC_RX_RING_SIZE; i++) {
827 desc = &priv->rx_head[i];
828 skb = cpmac_get_skb(dev);
834 desc->hw_data = virt_to_phys(skb->data);
835 desc->buflen = CPMAC_SKB_SIZE;
836 desc->dataflags = CPMAC_OWN;
837 desc->next = &priv->rx_head[i + 1];
838 desc->hw_next = virt_to_phys(desc->next);
839 dma_cache_wback((u32)desc, 16);
841 priv->rx_tail = &priv->rx_head[CPMAC_RX_RING_SIZE - 1];
842 priv->rx_tail->next = priv->rx_head;
843 priv->rx_tail->hw_next = 0;
846 for (i = 0; i < 8; i++)
847 priv->regs->tx_ptr[i] = 0;
848 priv->regs->rx_ptr[0] = virt_to_phys(priv->rx_head);
850 priv->regs->mbp = MBP_RXSHORT | MBP_RXBCAST | MBP_RXMCAST;
851 priv->regs->unicast_enable = 0x1;
852 priv->regs->unicast_clear = 0xfe;
853 priv->regs->buffer_offset = 0;
854 for (i = 0; i < 8; i++)
855 priv->regs->mac_addr_low[i] = dev->dev_addr[5];
856 priv->regs->mac_addr_mid = dev->dev_addr[4];
857 priv->regs->mac_addr_high = dev->dev_addr[0] | (dev->dev_addr[1] << 8)
858 | (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
859 priv->regs->max_len = CPMAC_SKB_SIZE;
860 priv->regs->rx_int.enable = 0x1;
861 priv->regs->rx_int.clear = 0xfe;
862 priv->regs->tx_int.enable = 0xff;
863 priv->regs->tx_int.clear = 0;
864 priv->regs->mac_int_enable = 3;
865 priv->regs->mac_int_clear = 0xfc;
867 if((res = request_irq(dev->irq, cpmac_irq, SA_INTERRUPT,
869 printk("%s: failed to obtain irq\n", dev->name);
873 priv->regs->rx_ctrl.control |= 1;
874 priv->regs->tx_ctrl.control |= 1;
875 priv->regs->mac_control |= MAC_MII | MAC_FDX;
877 priv->phy->state = PHY_CHANGELINK;
878 phy_start(priv->phy);
880 netif_start_queue(dev);
885 for (i = 0; i < CPMAC_RX_RING_SIZE; i++)
886 if (priv->rx_head[i].skb)
887 kfree_skb(priv->rx_head[i].skb);
889 kfree(priv->desc_ring);
891 for (skb = priv->skb_pool; skb; skb = priv->skb_pool) {
892 priv->skb_pool = skb->next;
899 release_mem_region(dev->mem_start, dev->mem_end -
903 phy_disconnect(priv->phy);
908 static int cpmac_stop(struct net_device *dev)
912 struct cpmac_priv *priv = netdev_priv(dev);
914 netif_stop_queue(dev);
917 phy_disconnect(priv->phy);
922 for (i = 0; i < 8; i++) {
923 priv->regs->rx_ptr[i] = 0;
924 priv->regs->tx_ptr[i] = 0;
928 free_irq(dev->irq, dev);
929 release_mem_region(dev->mem_start, dev->mem_end -
932 cancel_delayed_work(&priv->alloc_work);
933 flush_scheduled_work();
935 priv->rx_head = &priv->desc_ring[CPMAC_TX_RING_SIZE];
936 for (i = 0; i < CPMAC_RX_RING_SIZE; i++)
937 if (priv->rx_head[i].skb)
938 kfree_skb(priv->rx_head[i].skb);
940 kfree(priv->desc_ring);
942 for (skb = priv->skb_pool; skb; skb = priv->skb_pool) {
943 priv->skb_pool = skb->next;
950 static int external_switch = 0;
952 static int __devinit cpmac_probe(struct platform_device *pdev)
955 struct resource *res;
956 struct cpmac_priv *priv;
957 struct net_device *dev;
958 struct plat_cpmac_data *pdata;
960 if (strcmp(pdev->name, "cpmac") != 0)
963 pdata = pdev->dev.platform_data;
965 for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
966 if (!(pdata->phy_mask & (1 << phy_id)))
968 if (!cpmac_mii.phy_map[phy_id])
973 if (phy_id == PHY_MAX_ADDR) {
974 if (external_switch) {
977 printk("cpmac: no PHY present\n");
982 dev = alloc_etherdev(sizeof(struct cpmac_priv));
985 printk(KERN_ERR "cpmac: Unable to allocate net_device structure!\n");
989 SET_MODULE_OWNER(dev);
990 platform_set_drvdata(pdev, dev);
991 priv = netdev_priv(dev);
993 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
999 dev->mem_start = res->start;
1000 dev->mem_end = res->end;
1001 dev->irq = platform_get_irq_byname(pdev, "irq");
1004 dev->open = cpmac_open;
1005 dev->stop = cpmac_stop;
1006 dev->set_config = cpmac_config;
1007 dev->hard_start_xmit = cpmac_start_xmit;
1008 dev->do_ioctl = cpmac_ioctl;
1009 dev->get_stats = cpmac_stats;
1010 dev->change_mtu = cpmac_change_mtu;
1011 dev->set_mac_address = cpmac_set_mac_address;
1012 dev->set_multicast_list = cpmac_set_multicast_list;
1013 dev->tx_timeout = cpmac_tx_timeout;
1014 dev->ethtool_ops = &cpmac_ethtool_ops;
1016 memset(priv, 0, sizeof(struct cpmac_priv));
1017 spin_lock_init(&priv->lock);
1018 priv->msg_enable = netif_msg_init(NETIF_MSG_WOL, 0x3fff);
1019 priv->config = pdata;
1021 memcpy(dev->dev_addr, priv->config->dev_addr, sizeof(dev->dev_addr));
1023 snprintf(priv->phy_name, BUS_ID_SIZE, PHY_ID_FMT,
1024 cpmac_mii.id, phy_id);
1026 snprintf(priv->phy_name, BUS_ID_SIZE, "fixed@%d:%d", 100, 1);
1029 if ((rc = register_netdev(dev))) {
1030 printk("cpmac: error %i registering device %s\n",
1035 printk("cpmac: device %s (regs: %p, irq: %d, phy: %s, mac: ",
1036 dev->name, (u32 *)dev->mem_start, dev->irq,
1038 for (i = 0; i < 6; i++) {
1039 printk("%02x", dev->dev_addr[i]);
1040 if (i < 5) printk(":");
1051 static int __devexit cpmac_remove(struct platform_device *pdev)
1053 struct net_device *dev = platform_get_drvdata(pdev);
1054 unregister_netdev(dev);
1059 static struct platform_driver cpmac_driver = {
1060 .driver.name = "cpmac",
1061 .probe = cpmac_probe,
1062 .remove = cpmac_remove,
1065 int __devinit cpmac_init(void)
1069 cpmac_mii.priv = (struct cpmac_mdio_regs *)
1070 ioremap_nocache(AR7_REGS_MDIO, sizeof(struct cpmac_mdio_regs));
1072 if (!cpmac_mii.priv) {
1073 printk("Can't ioremap mdio registers\n");
1077 #warning FIXME: unhardcode gpio&reset bits
1078 ar7_gpio_disable(26);
1079 ar7_gpio_disable(27);
1080 ar7_device_reset(17);
1081 ar7_device_reset(21);
1082 ar7_device_reset(26);
1084 cpmac_mii.reset(&cpmac_mii);
1086 for (i = 0; i < 300000; i++) {
1087 mask = ((struct cpmac_mdio_regs *)cpmac_mii.priv)->alive;
1093 if (mask & (mask - 1)) {
1094 external_switch = 1;
1098 cpmac_mii.phy_mask = ~(mask | 0x80000000);
1100 res = mdiobus_register(&cpmac_mii);
1104 res = platform_driver_register(&cpmac_driver);
1111 mdiobus_unregister(&cpmac_mii);
1114 iounmap(cpmac_mii.priv);
1119 void __devexit cpmac_exit(void)
1121 platform_driver_unregister(&cpmac_driver);
1122 mdiobus_unregister(&cpmac_mii);
1125 module_init(cpmac_init);
1126 module_exit(cpmac_exit);