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>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/ethtool.h>
35 #include <linux/skbuff.h>
36 #include <linux/mii.h>
37 #include <linux/phy.h>
38 #include <linux/platform_device.h>
39 #include <asm/ar7/ar7.h>
42 MODULE_AUTHOR("Eugene Konev");
43 MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
44 MODULE_LICENSE("GPL");
46 /* Register definitions */
47 struct cpmac_control_regs
{
48 volatile u32 revision
;
50 volatile u32 teardown
;
54 struct cpmac_int_regs
{
55 volatile u32 stat_raw
;
56 volatile u32 stat_masked
;
66 volatile u32 crc_error
;
67 volatile u32 align_error
;
68 volatile u32 oversized
;
70 volatile u32 undersized
;
71 volatile u32 fragment
;
72 volatile u32 filtered
;
73 volatile u32 qos_filtered
;
78 struct cpmac_control_regs tx_ctrl
;
79 struct cpmac_control_regs rx_ctrl
;
80 volatile u32 unused1
[56];
83 #define MBP_RXPASSCRC 0x40000000
84 #define MBP_RXQOS 0x20000000
85 #define MBP_RXNOCHAIN 0x10000000
86 #define MBP_RXCMF 0x01000000
87 #define MBP_RXSHORT 0x00800000
88 #define MBP_RXCEF 0x00400000
89 #define MBP_RXPROMISC 0x00200000
90 #define MBP_PROMISCCHAN(chan) (((chan) & 0x7) << 16)
91 #define MBP_RXBCAST 0x00002000
92 #define MBP_BCASTCHAN(chan) (((chan) & 0x7) << 8)
93 #define MBP_RXMCAST 0x00000020
94 #define MBP_MCASTCHAN(chan) ((chan) & 0x7)
95 volatile u32 unicast_enable
;
96 volatile u32 unicast_clear
;
98 volatile u32 buffer_offset
;
99 volatile u32 filter_flow_threshold
;
100 volatile u32 unused2
[2];
101 volatile u32 flow_thre
[8];
102 volatile u32 free_buffer
[8];
103 volatile u32 mac_control
;
104 #define MAC_TXPTYPE 0x00000200
105 #define MAC_TXPACE 0x00000040
106 #define MAC_MII 0x00000020
107 #define MAC_TXFLOW 0x00000010
108 #define MAC_RXFLOW 0x00000008
109 #define MAC_MTEST 0x00000004
110 #define MAC_LOOPBACK 0x00000002
111 #define MAC_FDX 0x00000001
112 volatile u32 mac_status
;
113 #define MACST_QOS 0x4
114 #define MACST_RXFLOW 0x2
115 #define MACST_TXFLOW 0x1
116 volatile u32 emc_control
;
117 volatile u32 unused3
;
118 struct cpmac_int_regs tx_int
;
119 volatile u32 mac_int_vector
;
120 /* Int Status bits */
121 #define INTST_STATUS 0x80000
122 #define INTST_HOST 0x40000
123 #define INTST_RX 0x20000
124 #define INTST_TX 0x10000
125 volatile u32 mac_eoi_vector
;
126 volatile u32 unused4
[2];
127 struct cpmac_int_regs rx_int
;
128 volatile u32 mac_int_stat_raw
;
129 volatile u32 mac_int_stat_masked
;
130 volatile u32 mac_int_enable
;
131 volatile u32 mac_int_clear
;
132 volatile u32 mac_addr_low
[8];
133 volatile u32 mac_addr_mid
;
134 volatile u32 mac_addr_high
;
135 volatile u32 mac_hash_low
;
136 volatile u32 mac_hash_high
;
137 volatile u32 boff_test
;
138 volatile u32 pac_test
;
139 volatile u32 rx_pause
;
140 volatile u32 tx_pause
;
141 volatile u32 unused5
[2];
142 struct cpmac_stats rx_stats
;
143 struct cpmac_stats tx_stats
;
144 volatile u32 unused6
[232];
145 volatile u32 tx_ptr
[8];
146 volatile u32 rx_ptr
[8];
147 volatile u32 tx_ack
[8];
148 volatile u32 rx_ack
[8];
152 struct cpmac_mdio_regs
{
153 volatile u32 version
;
154 volatile u32 control
;
155 #define MDIOC_IDLE 0x80000000
156 #define MDIOC_ENABLE 0x40000000
157 #define MDIOC_PREAMBLE 0x00100000
158 #define MDIOC_FAULT 0x00080000
159 #define MDIOC_FAULTDETECT 0x00040000
160 #define MDIOC_INTTEST 0x00020000
161 #define MDIOC_CLKDIV(div) ((div) & 0xff)
164 struct cpmac_int_regs link_int
;
165 struct cpmac_int_regs user_int
;
168 #define MDIO_BUSY 0x80000000
169 #define MDIO_WRITE 0x40000000
170 #define MDIO_REG(reg) (((reg) & 0x1f) << 21)
171 #define MDIO_PHY(phy) (((phy) & 0x1f) << 16)
172 #define MDIO_DATA(data) ((data) & 0xffff)
185 #define CPMAC_SOP 0x8000
186 #define CPMAC_EOP 0x4000
187 #define CPMAC_OWN 0x2000
188 #define CPMAC_EOQ 0x1000
191 struct cpmac_desc
*next
;
195 struct net_device_stats stats
;
197 int free_tx_channels
;
198 struct cpmac_desc
*tx_pool
;
199 struct cpmac_desc
*rx_channels
[8];
200 struct cpmac_desc
*tx_channels
[8];
201 struct cpmac_regs
*regs
;
202 struct mii_bus
*mii_bus
;
203 struct phy_device
*phy
;
204 char phy_name
[BUS_ID_SIZE
];
207 struct plat_cpmac_data
*config
;
208 int oldlink
, oldspeed
, oldduplex
;
212 static irqreturn_t
cpmac_irq(int, void *);
213 void cpmac_exit(void);
216 static void cpmac_dump_regs(u32
*base
, int count
)
219 for (i
= 0; i
< (count
+ 3) / 4; i
++) {
220 if (i
% 4 == 0) printk("\nCPMAC[0x%04x]:", i
* 4);
221 printk(" 0x%08x", *(base
+ i
));
227 static int cpmac_mdio_read(struct mii_bus
*bus
, int phy_id
, int regnum
)
229 struct cpmac_mdio_regs
*regs
= (struct cpmac_mdio_regs
*)bus
->priv
;
232 while ((val
= regs
->access
) & MDIO_BUSY
);
233 regs
->access
= MDIO_BUSY
| MDIO_REG(regnum
& 0x1f) |
234 MDIO_PHY(phy_id
& 0x1f);
235 while ((val
= regs
->access
) & MDIO_BUSY
);
240 static int cpmac_mdio_write(struct mii_bus
*bus
, int phy_id
, int regnum
, u16 val
)
242 struct cpmac_mdio_regs
*regs
= (struct cpmac_mdio_regs
*)bus
->priv
;
245 while ((tmp
= regs
->access
) & MDIO_BUSY
);
246 regs
->access
= MDIO_BUSY
| MDIO_WRITE
|
247 MDIO_REG(regnum
& 0x1f) | MDIO_PHY(phy_id
& 0x1f) |
253 static int cpmac_mdio_reset(struct mii_bus
*bus
)
255 struct cpmac_mdio_regs
*regs
= (struct cpmac_mdio_regs
*)bus
->priv
;
257 ar7_device_reset(AR7_RESET_BIT_MDIO
);
258 regs
->control
= MDIOC_ENABLE
|
259 MDIOC_CLKDIV(ar7_cpmac_freq() / 2200000 - 1);
264 static int mii_irqs
[PHY_MAX_ADDR
] = { PHY_POLL
, };
266 struct mii_bus cpmac_mii
= {
268 .read
= cpmac_mdio_read
,
269 .write
= cpmac_mdio_write
,
270 .reset
= cpmac_mdio_reset
,
274 int cpmac_config(struct net_device
*dev
, struct ifmap
*map
)
276 if (dev
->flags
& IFF_UP
)
279 /* Don't allow changing the I/O address */
280 if (map
->base_addr
!= dev
->base_addr
)
283 /* ignore other fields */
287 int cpmac_set_mac_address(struct net_device
*dev
, void *addr
)
289 struct sockaddr
*sa
= addr
;
291 if (dev
->flags
& IFF_UP
)
294 memcpy(dev
->dev_addr
, sa
->sa_data
, dev
->addr_len
);
299 void cpmac_set_multicast_list(struct net_device
*dev
)
301 struct dev_mc_list
*iter
;
304 int hashlo
= 0, hashhi
= 0;
305 struct cpmac_priv
*priv
= netdev_priv(dev
);
307 if(dev
->flags
& IFF_PROMISC
) {
308 priv
->regs
->mbp
&= ~MBP_PROMISCCHAN(0); /* promisc channel 0 */
309 priv
->regs
->mbp
|= MBP_RXPROMISC
;
311 priv
->regs
->mbp
&= ~MBP_RXPROMISC
;
312 if(dev
->flags
& IFF_ALLMULTI
) {
313 /* enable all multicast mode */
314 priv
->regs
->mac_hash_low
= 0xffffffff;
315 priv
->regs
->mac_hash_high
= 0xffffffff;
317 for(i
= 0, iter
= dev
->mc_list
; i
< dev
->mc_count
;
318 i
++, iter
= iter
->next
) {
320 tmp
= iter
->dmi_addr
[0];
321 hash
^= (tmp
>> 2) ^ (tmp
<< 4);
322 tmp
= iter
->dmi_addr
[1];
323 hash
^= (tmp
>> 4) ^ (tmp
<< 2);
324 tmp
= iter
->dmi_addr
[2];
325 hash
^= (tmp
>> 6) ^ tmp
;
326 tmp
= iter
->dmi_addr
[4];
327 hash
^= (tmp
>> 2) ^ (tmp
<< 4);
328 tmp
= iter
->dmi_addr
[5];
329 hash
^= (tmp
>> 4) ^ (tmp
<< 2);
330 tmp
= iter
->dmi_addr
[6];
331 hash
^= (tmp
>> 6) ^ tmp
;
336 hashhi
|= 1<<(hash
- 32);
340 priv
->regs
->mac_hash_low
= hashlo
;
341 priv
->regs
->mac_hash_high
= hashhi
;
346 static void cpmac_rx(struct net_device
*dev
, int channel
)
348 struct cpmac_desc
*pkt
;
351 struct cpmac_priv
*priv
= netdev_priv(dev
);
353 spin_lock(&priv
->lock
);
354 pkt
= priv
->rx_channels
[channel
];
356 if (printk_ratelimit())
357 printk(KERN_NOTICE
"%s: rx: spurious interrupt\n",
359 priv
->stats
.rx_errors
++;
363 priv
->regs
->rx_ack
[channel
] = virt_to_phys(pkt
);
364 dma_cache_inv((u32
)pkt
, 16);
366 if (printk_ratelimit())
367 printk(KERN_NOTICE
"%s: rx: spurious interrupt\n",
369 priv
->stats
.rx_errors
++;
372 skb
= dev_alloc_skb(1536);
374 if (printk_ratelimit())
375 printk(KERN_NOTICE
"%s: rx: low on mem - packet dropped\n",
377 priv
->stats
.rx_dropped
++;
379 data
= (char *)phys_to_virt(pkt
->hw_data
);
380 dma_cache_inv((u32
)data
, pkt
->datalen
);
381 skb_put(pkt
->skb
, pkt
->datalen
);
382 pkt
->skb
->protocol
= eth_type_trans(pkt
->skb
, dev
);
383 pkt
->skb
->ip_summed
= CHECKSUM_NONE
;
384 priv
->stats
.rx_packets
++;
385 priv
->stats
.rx_bytes
+= pkt
->datalen
;
390 pkt
->hw_data
= virt_to_phys(skb
->data
);
392 spin_unlock(&priv
->lock
);
393 pkt
->buflen
= 1500 + ETH_HLEN
+ 4;
395 pkt
->dataflags
= CPMAC_OWN
;
396 dma_cache_wback_inv((u32
)pkt
, 16);
397 priv
->regs
->rx_ptr
[channel
] = virt_to_phys(pkt
);
400 struct cpmac_desc
*cpmac_get_desc(struct net_device
*dev
)
402 struct cpmac_desc
*pkt
;
403 struct cpmac_priv
*priv
= netdev_priv(dev
);
405 priv
->tx_pool
= pkt
->next
;
407 if (priv
->tx_pool
== NULL
)
408 netif_stop_queue(dev
);
412 void cpmac_release_desc(struct net_device
*dev
, struct cpmac_desc
*pkt
)
414 struct cpmac_priv
*priv
= netdev_priv(dev
);
415 struct cpmac_desc
*p
;
417 while (p
->next
) p
= p
->next
;
418 p
->next
= priv
->tx_pool
;
422 int cpmac_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
426 skb_frag_t
*this_frag
;
428 struct cpmac_desc
*head
, *tail
, *curr
;
429 struct cpmac_priv
*priv
= netdev_priv(dev
);
431 BUG_ON(priv
->free_tx_channels
< 1);
433 if (len
< ETH_ZLEN
) {
434 if (skb_padto(skb
, ETH_ZLEN
)) {
435 if (printk_ratelimit())
436 printk(KERN_NOTICE
"%s: padding failed, dropping\n",
438 spin_lock_irqsave(&priv
->lock
, flags
);
439 priv
->stats
.tx_dropped
++;
440 spin_unlock_irqrestore(&priv
->lock
, flags
);
445 spin_lock_irqsave(&priv
->lock
, flags
);
446 dev
->trans_start
= jiffies
;
447 for (i
= 0; i
< 8; i
++)
448 if (!priv
->tx_channels
[i
])
453 head
= cpmac_get_desc(dev
);
454 priv
->tx_channels
[i
] = head
;
455 head
->jiffies
= dev
->trans_start
;
456 if (!(--priv
->free_tx_channels
))
457 netif_stop_queue(dev
);
458 spin_unlock_irqrestore(&priv
->lock
, flags
);
460 head
->dataflags
= CPMAC_SOP
| CPMAC_OWN
;
462 head
->hw_data
= virt_to_phys(skb
->data
);
463 dma_cache_wback_inv((u32
)skb
->data
, len
);
467 for (frag
= 0; frag
< skb_shinfo(skb
)->nr_frags
; frag
++) {
468 dma_cache_wback_inv((u32
)tail
, 16);
469 this_frag
= &skb_shinfo(skb
)->frags
[frag
];
470 curr
= cpmac_get_desc(dev
);
471 data
= page_address(this_frag
->page
) +
472 this_frag
->page_offset
;
473 curr
->hw_data
= virt_to_phys(data
);
474 curr
->buflen
= this_frag
->size
;
475 curr
->datalen
= this_frag
->size
;
476 curr
->dataflags
= CPMAC_OWN
;
477 dma_cache_wback_inv((u32
)data
, len
);
478 tail
->hw_next
= virt_to_phys(curr
);
483 tail
->dataflags
|= CPMAC_EOP
;
484 dma_cache_wback_inv((u32
)tail
, 16);
485 priv
->regs
->tx_ptr
[i
] = virt_to_phys(head
);
489 void cpmac_end_xmit(struct net_device
*dev
, int channel
)
491 struct cpmac_desc
*pkt
;
492 struct cpmac_priv
*priv
= netdev_priv(dev
);
494 spin_lock(&priv
->lock
);
495 pkt
= priv
->tx_channels
[channel
];
496 priv
->tx_channels
[channel
] = NULL
;
497 priv
->free_tx_channels
++;
498 priv
->regs
->tx_ack
[channel
] = virt_to_phys(pkt
);
500 priv
->stats
.tx_packets
++;
501 priv
->stats
.tx_bytes
+= pkt
->skb
->len
;
502 dev_kfree_skb_irq(pkt
->skb
);
503 cpmac_release_desc(dev
, pkt
);
504 if (netif_queue_stopped(dev
))
505 netif_wake_queue(dev
);
507 if (printk_ratelimit())
508 printk(KERN_NOTICE
"%s: end_xmit: spurious interrupt\n",
511 spin_unlock(&priv
->lock
);
514 static irqreturn_t
cpmac_irq(int irq
, void *dev_id
)
516 struct net_device
*dev
= (struct net_device
*)dev_id
;
517 struct cpmac_priv
*priv
= netdev_priv(dev
);
523 status
= priv
->regs
->mac_int_vector
;
525 if (status
& INTST_TX
) {
526 cpmac_end_xmit(dev
, (status
& 7));
529 if (status
& INTST_RX
) {
530 cpmac_rx(dev
, (status
>> 8) & 7);
533 if (status
& INTST_HOST
) { /* host interrupt ??? */
534 printk("%s: host int, something bad happened...\n", dev
->name
);
535 printk("%s: mac status: 0x%08x\n", dev
->name
,
536 priv
->regs
->mac_status
);
539 if (status
& INTST_STATUS
) { /* status interrupt ??? */
540 printk("%s: status int, what are we gonna do?\n", dev
->name
);
543 priv
->regs
->mac_eoi_vector
= 0;
547 void cpmac_tx_timeout(struct net_device
*dev
)
550 struct cpmac_priv
*priv
= netdev_priv(dev
);
551 struct cpmac_desc
*pkt
= NULL
, *tmp
;
553 priv
->stats
.tx_errors
++;
554 for (i
= 0; i
< 8; i
++) {
555 tmp
= priv
->tx_channels
[i
];
556 if (tmp
&& (!pkt
|| (pkt
->jiffies
> tmp
->jiffies
)))
560 printk("Transmit timeout at %ld, latency %ld\n", jiffies
,
561 jiffies
- pkt
->jiffies
);
562 for (i
= 0; i
< 8; i
++)
563 if (priv
->tx_channels
[i
] == pkt
)
564 priv
->tx_channels
[i
] = NULL
;
565 dev_kfree_skb(pkt
->skb
);
566 cpmac_release_desc(dev
, pkt
);
567 priv
->free_tx_channels
++;
568 netif_wake_queue(dev
);
572 int cpmac_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
574 struct cpmac_priv
*priv
= netdev_priv(dev
);
575 if (!(netif_running(dev
)))
579 return phy_mii_ioctl(priv
->phy
, if_mii(ifr
), cmd
);
582 static int cpmac_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
584 struct cpmac_priv
*priv
= netdev_priv(dev
);
587 return phy_ethtool_gset(priv
->phy
, cmd
);
592 static int cpmac_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
594 struct cpmac_priv
*priv
= netdev_priv(dev
);
596 if (!capable(CAP_NET_ADMIN
))
600 return phy_ethtool_sset(priv
->phy
, cmd
);
605 static void cpmac_get_drvinfo(struct net_device
*dev
,
606 struct ethtool_drvinfo
*info
)
608 strcpy(info
->driver
, "cpmac");
609 strcpy(info
->version
, "0.0.3");
610 info
->fw_version
[0] = '\0';
611 sprintf(info
->bus_info
, "%s", "cpmac");
612 info
->regdump_len
= 0;
615 static const struct ethtool_ops cpmac_ethtool_ops
= {
616 .get_settings
= cpmac_get_settings
,
617 .set_settings
= cpmac_set_settings
,
618 .get_drvinfo
= cpmac_get_drvinfo
,
619 .get_link
= ethtool_op_get_link
,
622 static struct net_device_stats
*cpmac_stats(struct net_device
*dev
)
624 struct cpmac_priv
*priv
= netdev_priv(dev
);
626 if (netif_device_present(dev
))
632 static int cpmac_change_mtu(struct net_device
*dev
, int mtu
)
635 struct cpmac_priv
*priv
= netdev_priv(dev
);
636 spinlock_t
*lock
= &priv
->lock
;
638 if ((mtu
< 68) || (mtu
> 1500))
641 spin_lock_irqsave(lock
, flags
);
643 spin_unlock_irqrestore(lock
, flags
);
648 static void cpmac_reset(struct net_device
*dev
)
651 struct cpmac_priv
*priv
= netdev_priv(dev
);
653 ar7_device_reset(priv
->config
->reset_bit
);
654 priv
->regs
->rx_ctrl
.control
&= ~1;
655 priv
->regs
->tx_ctrl
.control
&= ~1;
656 for (i
= 0; i
< 8; i
++) {
657 priv
->regs
->tx_ptr
[i
] = 0;
658 priv
->regs
->rx_ptr
[i
] = 0;
660 priv
->regs
->mac_control
&= ~MAC_MII
; /* disable mii */
663 static void cpmac_adjust_link(struct net_device
*dev
)
665 struct cpmac_priv
*priv
= netdev_priv(dev
);
669 spin_lock_irqsave(&priv
->lock
, flags
);
670 if (priv
->phy
->link
) {
671 if (priv
->phy
->duplex
!= priv
->oldduplex
) {
673 priv
->oldduplex
= priv
->phy
->duplex
;
676 if (priv
->phy
->speed
!= priv
->oldspeed
) {
678 priv
->oldspeed
= priv
->phy
->speed
;
681 if (!priv
->oldlink
) {
686 } else if (priv
->oldlink
) {
690 priv
->oldduplex
= -1;
694 phy_print_status(priv
->phy
);
696 spin_unlock_irqrestore(&priv
->lock
, flags
);
699 int cpmac_open(struct net_device
*dev
)
702 struct cpmac_priv
*priv
= netdev_priv(dev
);
703 struct cpmac_desc
*pkt
;
706 /* priv->phy = phy_connect(dev, priv->phy_name, &cpmac_adjust_link,
707 0, PHY_INTERFACE_MODE_MII);*/
708 priv
->phy
= phy_connect(dev
, priv
->phy_name
, &cpmac_adjust_link
, 0);
709 if (IS_ERR(priv
->phy
)) {
710 printk(KERN_ERR
"%s: Could not attach to PHY\n", dev
->name
);
711 return PTR_ERR(priv
->phy
);
714 if (!request_mem_region(dev
->mem_start
, dev
->mem_end
-
715 dev
->mem_start
, dev
->name
)) {
716 printk("%s: failed to request registers\n",
722 priv
->regs
= ioremap_nocache(dev
->mem_start
, dev
->mem_end
-
725 printk("%s: failed to remap registers\n", dev
->name
);
730 priv
->order
= get_order(4096);
731 priv
->pages
= __get_dma_pages(GFP_KERNEL
, priv
->order
);
736 memset((char *)priv
->pages
, 0, 4096);
738 priv
->tx_pool
= NULL
;
740 for (i
= 0; i
< 4096 / sizeof(struct cpmac_desc
); i
++) {
741 pkt
= (struct cpmac_desc
*)
742 (priv
->pages
+ i
* sizeof(struct cpmac_desc
));
743 memset(pkt
, sizeof(struct cpmac_desc
), 0);
745 skb
= alloc_skb(1500 + ETH_HLEN
+ 6, GFP_KERNEL
);
747 for(j
= 0; j
< i
- 1; j
++)
748 kfree_skb(priv
->rx_channels
[j
]->skb
);
749 free_pages(priv
->pages
, priv
->order
);
756 pkt
->hw_data
= virt_to_phys(skb
->data
);
757 pkt
->buflen
= 1500 + ETH_HLEN
+ 4;
758 pkt
->dataflags
= CPMAC_OWN
;
759 dma_cache_wback_inv((u32
)pkt
, 16);
760 priv
->rx_channels
[i
] = pkt
;
761 priv
->tx_channels
[i
] = NULL
;
763 pkt
->next
= priv
->tx_pool
;
769 priv
->free_tx_channels
= 8;
771 for (i
= 0; i
< 8; i
++) {
772 priv
->regs
->tx_ptr
[i
] = 0;
773 priv
->regs
->rx_ptr
[i
] = virt_to_phys(priv
->rx_channels
[i
]);
776 priv
->regs
->mbp
= MBP_RXNOCHAIN
| MBP_RXSHORT
| MBP_RXBCAST
|
778 priv
->regs
->unicast_enable
= 0xff;
779 priv
->regs
->unicast_clear
= 0;
780 priv
->regs
->buffer_offset
= 0;
781 for (i
= 0; i
< 8; i
++)
782 priv
->regs
->mac_addr_low
[i
] = dev
->dev_addr
[5];
783 priv
->regs
->mac_addr_mid
= dev
->dev_addr
[4];
784 priv
->regs
->mac_addr_high
= dev
->dev_addr
[0] | (dev
->dev_addr
[1] << 8)
785 | (dev
->dev_addr
[2] << 16) | (dev
->dev_addr
[3] << 24);
786 priv
->regs
->max_len
= 1536;
787 priv
->regs
->rx_int
.enable
= 0xff;
788 priv
->regs
->rx_int
.clear
= 0;
789 priv
->regs
->tx_int
.enable
= 0xff;
790 priv
->regs
->tx_int
.clear
= 0;
791 priv
->regs
->mac_int_enable
= 3;
792 priv
->regs
->mac_int_clear
= 0xfc;
794 if((res
= request_irq(dev
->irq
, cpmac_irq
, SA_INTERRUPT
,
796 printk("%s: failed to obtain irq\n", dev
->name
);
800 priv
->regs
->rx_ctrl
.control
|= 1;
801 priv
->regs
->tx_ctrl
.control
|= 1;
802 priv
->regs
->mac_control
|= MAC_MII
| MAC_FDX
;
804 priv
->phy
->state
= PHY_CHANGELINK
;
805 phy_start(priv
->phy
);
807 netif_start_queue(dev
);
812 for(i
= 0; i
< 8; i
++)
813 if (priv
->rx_channels
[i
]->skb
)
814 kfree_skb(priv
->rx_channels
[i
]->skb
);
815 free_pages(priv
->pages
, priv
->order
);
821 release_mem_region(dev
->mem_start
, dev
->mem_end
-
825 phy_disconnect(priv
->phy
);
830 int cpmac_stop(struct net_device
*dev
)
833 struct cpmac_priv
*priv
= netdev_priv(dev
);
835 netif_stop_queue(dev
);
838 phy_disconnect(priv
->phy
);
843 for (i
= 0; i
< 8; i
++) {
844 priv
->regs
->rx_ptr
[i
] = 0;
845 priv
->regs
->tx_ptr
[i
] = 0;
849 free_irq(dev
->irq
, dev
);
850 release_mem_region(dev
->mem_start
, dev
->mem_end
-
853 for(i
= 0; i
< 8; i
++)
854 if (priv
->rx_channels
[i
]->skb
)
855 kfree_skb(priv
->rx_channels
[i
]->skb
);
857 free_pages(priv
->pages
, priv
->order
);
862 static int external_switch
= 0;
864 int __devinit
cpmac_probe(struct platform_device
*pdev
)
867 struct resource
*res
;
868 struct cpmac_priv
*priv
;
869 struct net_device
*dev
;
870 struct plat_cpmac_data
*pdata
;
872 if (strcmp(pdev
->name
, "cpmac") != 0)
875 pdata
= pdev
->dev
.platform_data
;
877 for (phy_id
= 0; phy_id
< PHY_MAX_ADDR
; phy_id
++) {
878 if (!(pdata
->phy_mask
& (1 << phy_id
)))
880 if (!cpmac_mii
.phy_map
[phy_id
])
885 if (phy_id
== PHY_MAX_ADDR
) {
886 if (external_switch
) {
889 printk("cpmac: no PHY present\n");
894 dev
= alloc_etherdev(sizeof(struct cpmac_priv
));
897 printk(KERN_ERR
"cpmac: Unable to allocate net_device structure!\n");
901 SET_MODULE_OWNER(dev
);
902 platform_set_drvdata(pdev
, dev
);
903 priv
= netdev_priv(dev
);
905 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "regs");
911 dev
->mem_start
= res
->start
;
912 dev
->mem_end
= res
->end
;
913 dev
->irq
= platform_get_irq_byname(pdev
, "irq");
916 dev
->open
= cpmac_open
;
917 dev
->stop
= cpmac_stop
;
918 dev
->set_config
= cpmac_config
;
919 dev
->hard_start_xmit
= cpmac_start_xmit
;
920 dev
->do_ioctl
= cpmac_ioctl
;
921 dev
->get_stats
= cpmac_stats
;
922 dev
->change_mtu
= cpmac_change_mtu
;
923 dev
->set_mac_address
= cpmac_set_mac_address
;
924 dev
->set_multicast_list
= cpmac_set_multicast_list
;
925 dev
->tx_timeout
= cpmac_tx_timeout
;
926 dev
->ethtool_ops
= &cpmac_ethtool_ops
;
928 memset(priv
, 0, sizeof(struct cpmac_priv
));
929 spin_lock_init(&priv
->lock
);
930 priv
->msg_enable
= netif_msg_init(NETIF_MSG_WOL
, 0x3fff);
931 priv
->config
= pdata
;
932 memcpy(dev
->dev_addr
, priv
->config
->dev_addr
, sizeof(dev
->dev_addr
));
934 snprintf(priv
->phy_name
, BUS_ID_SIZE
, PHY_ID_FMT
,
935 cpmac_mii
.id
, phy_id
);
937 snprintf(priv
->phy_name
, BUS_ID_SIZE
, "fixed@%d:%d", 100, 1);
940 if ((rc
= register_netdev(dev
))) {
941 printk("cpmac: error %i registering device %s\n",
946 printk("cpmac: device %s (regs: %p, irq: %d, phy: %s, mac: ",
947 dev
->name
, (u32
*)dev
->mem_start
, dev
->irq
,
949 for (i
= 0; i
< 6; i
++) {
950 printk("%02x", dev
->dev_addr
[i
]);
951 if (i
< 5) printk(":");
962 static int __devexit
cpmac_remove(struct platform_device
*pdev
)
964 struct net_device
*dev
= platform_get_drvdata(pdev
);
965 unregister_netdev(dev
);
970 static struct platform_driver cpmac_driver
= {
971 .driver
.name
= "cpmac",
972 .probe
= cpmac_probe
,
973 .remove
= cpmac_remove
,
976 int __devinit
cpmac_init(void)
980 cpmac_mii
.priv
= (struct cpmac_mdio_regs
*)
981 ioremap_nocache(AR7_REGS_MDIO
, sizeof(struct cpmac_mdio_regs
));
983 if (!cpmac_mii
.priv
) {
984 printk("Can't ioremap mdio registers\n");
988 #warning FIXME: unhardcode gpio&reset bits
989 ar7_gpio_disable(26);
990 ar7_gpio_disable(27);
991 /* ar7_device_reset(17);
992 ar7_device_reset(21);
993 ar7_device_reset(26);*/
995 cpmac_mii
.reset(&cpmac_mii
);
997 for (i
= 0; i
< 300000; i
++) {
998 mask
= ((struct cpmac_mdio_regs
*)cpmac_mii
.priv
)->alive
;
1004 if (mask
& (mask
- 1)) {
1005 external_switch
= 1;
1009 cpmac_mii
.phy_mask
= ~(mask
| 0x80000000);
1011 res
= mdiobus_register(&cpmac_mii
);
1015 res
= platform_driver_register(&cpmac_driver
);
1022 mdiobus_unregister(&cpmac_mii
);
1025 iounmap(cpmac_mii
.priv
);
1030 void __devexit
cpmac_exit(void)
1032 platform_driver_unregister(&cpmac_driver
);
1033 mdiobus_unregister(&cpmac_mii
);
1036 module_init(cpmac_init
);
1037 module_exit(cpmac_exit
);