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
);
227 #define CPMAC_LOW_THRESH 32
228 #define CPMAC_ALLOC_SIZE 64
229 #define CPMAC_SKB_SIZE 1518
230 #define CPMAC_TX_RING_SIZE 8
233 static void cpmac_dump_regs(u32
*base
, int count
)
236 for (i
= 0; i
< (count
+ 3) / 4; i
++) {
237 if (i
% 4 == 0) printk("\nCPMAC[0x%04x]:", i
* 4);
238 printk(" 0x%08x", *(base
+ i
));
243 static const char *cpmac_dump_buf(const uint8_t * buf
, unsigned size
)
245 static char buffer
[3 * 25 + 1];
246 char *p
= &buffer
[0];
250 p
+= sprintf(p
, " %02x", *buf
++);
256 static int cpmac_mdio_read(struct mii_bus
*bus
, int phy_id
, int regnum
)
258 struct cpmac_mdio_regs
*regs
= (struct cpmac_mdio_regs
*)bus
->priv
;
261 while ((val
= regs
->access
) & MDIO_BUSY
);
262 regs
->access
= MDIO_BUSY
| MDIO_REG(regnum
& 0x1f) |
263 MDIO_PHY(phy_id
& 0x1f);
264 while ((val
= regs
->access
) & MDIO_BUSY
);
269 static int cpmac_mdio_write(struct mii_bus
*bus
, int phy_id
, int regnum
, u16 val
)
271 struct cpmac_mdio_regs
*regs
= (struct cpmac_mdio_regs
*)bus
->priv
;
274 while ((tmp
= regs
->access
) & MDIO_BUSY
);
275 regs
->access
= MDIO_BUSY
| MDIO_WRITE
|
276 MDIO_REG(regnum
& 0x1f) | MDIO_PHY(phy_id
& 0x1f) |
282 static int cpmac_mdio_reset(struct mii_bus
*bus
)
284 struct cpmac_mdio_regs
*regs
= (struct cpmac_mdio_regs
*)bus
->priv
;
286 ar7_device_reset(AR7_RESET_BIT_MDIO
);
287 regs
->control
= MDIOC_ENABLE
|
288 MDIOC_CLKDIV(ar7_cpmac_freq() / 2200000 - 1);
293 static int mii_irqs
[PHY_MAX_ADDR
] = { PHY_POLL
, };
295 static struct mii_bus cpmac_mii
= {
297 .read
= cpmac_mdio_read
,
298 .write
= cpmac_mdio_write
,
299 .reset
= cpmac_mdio_reset
,
303 static int cpmac_config(struct net_device
*dev
, struct ifmap
*map
)
305 if (dev
->flags
& IFF_UP
)
308 /* Don't allow changing the I/O address */
309 if (map
->base_addr
!= dev
->base_addr
)
312 /* ignore other fields */
316 static int cpmac_set_mac_address(struct net_device
*dev
, void *addr
)
318 struct sockaddr
*sa
= addr
;
320 if (dev
->flags
& IFF_UP
)
323 memcpy(dev
->dev_addr
, sa
->sa_data
, dev
->addr_len
);
328 static void cpmac_set_multicast_list(struct net_device
*dev
)
330 struct dev_mc_list
*iter
;
333 int hashlo
= 0, hashhi
= 0;
334 struct cpmac_priv
*priv
= netdev_priv(dev
);
336 if(dev
->flags
& IFF_PROMISC
) {
337 priv
->regs
->mbp
&= ~MBP_PROMISCCHAN(0); /* promisc channel 0 */
338 priv
->regs
->mbp
|= MBP_RXPROMISC
;
340 priv
->regs
->mbp
&= ~MBP_RXPROMISC
;
341 if(dev
->flags
& IFF_ALLMULTI
) {
342 /* enable all multicast mode */
343 priv
->regs
->mac_hash_low
= 0xffffffff;
344 priv
->regs
->mac_hash_high
= 0xffffffff;
346 for(i
= 0, iter
= dev
->mc_list
; i
< dev
->mc_count
;
347 i
++, iter
= iter
->next
) {
349 tmp
= iter
->dmi_addr
[0];
350 hash
^= (tmp
>> 2) ^ (tmp
<< 4);
351 tmp
= iter
->dmi_addr
[1];
352 hash
^= (tmp
>> 4) ^ (tmp
<< 2);
353 tmp
= iter
->dmi_addr
[2];
354 hash
^= (tmp
>> 6) ^ tmp
;
355 tmp
= iter
->dmi_addr
[4];
356 hash
^= (tmp
>> 2) ^ (tmp
<< 4);
357 tmp
= iter
->dmi_addr
[5];
358 hash
^= (tmp
>> 4) ^ (tmp
<< 2);
359 tmp
= iter
->dmi_addr
[6];
360 hash
^= (tmp
>> 6) ^ tmp
;
365 hashhi
|= 1<<(hash
- 32);
369 priv
->regs
->mac_hash_low
= hashlo
;
370 priv
->regs
->mac_hash_high
= hashhi
;
375 static struct sk_buff
*cpmac_get_skb(struct net_device
*dev
)
378 struct cpmac_priv
*priv
= netdev_priv(dev
);
380 skb
= priv
->skb_pool
;
382 priv
->skb_pool
= skb
->next
;
384 skb
= dev_alloc_skb(CPMAC_SKB_SIZE
+ 2);
388 skb
->dev
= priv
->dev
;
392 if (likely(priv
->free_skbs
))
395 if (priv
->free_skbs
< CPMAC_LOW_THRESH
)
396 schedule_work(&priv
->alloc_work
);
401 static inline struct sk_buff
*cpmac_rx_one(struct net_device
*dev
,
402 struct cpmac_priv
*priv
,
403 struct cpmac_desc
*desc
)
407 struct sk_buff
*skb
, *result
= NULL
;
409 priv
->regs
->rx_ack
[0] = virt_to_phys(desc
);
410 if (unlikely(!desc
->datalen
)) {
411 if (printk_ratelimit())
412 printk(KERN_WARNING
"%s: rx: spurious interrupt\n",
414 priv
->stats
.rx_errors
++;
418 spin_lock_irqsave(&priv
->lock
, flags
);
419 skb
= cpmac_get_skb(dev
);
421 data
= (char *)phys_to_virt(desc
->hw_data
);
422 dma_cache_inv((u32
)data
, desc
->datalen
);
423 skb_put(desc
->skb
, desc
->datalen
);
424 desc
->skb
->protocol
= eth_type_trans(desc
->skb
, dev
);
425 desc
->skb
->ip_summed
= CHECKSUM_NONE
;
426 priv
->stats
.rx_packets
++;
427 priv
->stats
.rx_bytes
+= desc
->datalen
;
432 if (printk_ratelimit())
433 printk("%s: low on skbs, dropping packet\n",
436 priv
->stats
.rx_dropped
++;
438 spin_unlock_irqrestore(&priv
->lock
, flags
);
440 desc
->hw_data
= virt_to_phys(desc
->skb
->data
);
441 desc
->buflen
= CPMAC_SKB_SIZE
;
442 desc
->dataflags
= CPMAC_OWN
;
443 dma_cache_wback((u32
)desc
, 16);
448 static void cpmac_rx(struct net_device
*dev
)
451 struct cpmac_desc
*desc
;
452 struct cpmac_priv
*priv
= netdev_priv(dev
);
454 spin_lock(&priv
->lock
);
455 if (unlikely(!priv
->rx_head
)) {
456 spin_unlock(&priv
->lock
);
460 desc
= priv
->rx_head
;
461 dma_cache_inv((u32
)desc
, 16);
463 printk(KERN_DEBUG
"%s: len=%d, %s\n", __func__
, pkt
->datalen
,
464 cpmac_dump_buf(data
, pkt
->datalen
));
467 while ((desc
->dataflags
& CPMAC_OWN
) == 0) {
468 skb
= cpmac_rx_one(dev
, priv
, desc
);
473 dma_cache_inv((u32
)desc
, 16);
476 priv
->rx_head
= desc
;
477 priv
->regs
->rx_ptr
[0] = virt_to_phys(desc
);
478 spin_unlock(&priv
->lock
);
481 static int cpmac_poll(struct net_device
*dev
, int *budget
)
484 struct cpmac_desc
*desc
;
485 int received
= 0, quota
= min(dev
->quota
, *budget
);
486 struct cpmac_priv
*priv
= netdev_priv(dev
);
488 if (unlikely(!priv
->rx_head
)) {
489 if (printk_ratelimit())
490 printk(KERN_WARNING
"%s: rx: polling, but no queue\n",
492 netif_rx_complete(dev
);
496 desc
= priv
->rx_head
;
497 dma_cache_inv((u32
)desc
, 16);
499 while ((received
< quota
) && ((desc
->dataflags
& CPMAC_OWN
) == 0)) {
500 skb
= cpmac_rx_one(dev
, priv
, desc
);
502 netif_receive_skb(skb
);
506 priv
->rx_head
= desc
;
507 dma_cache_inv((u32
)desc
, 16);
511 dev
->quota
-= received
;
513 printk("%s: processed %d packets\n", dev
->name
, received
);
515 if (desc
->dataflags
& CPMAC_OWN
) {
516 priv
->regs
->rx_ptr
[0] = virt_to_phys(desc
);
517 netif_rx_complete(dev
);
518 priv
->regs
->rx_int
.enable
= 0x1;
519 priv
->regs
->rx_int
.clear
= 0xfe;
526 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
528 cpmac_alloc_skbs(struct work_struct
*work
)
530 struct cpmac_priv
*priv
= container_of(work
, struct cpmac_priv
,
534 cpmac_alloc_skbs(void *data
)
536 struct net_device
*dev
= (struct net_device
*)data
;
537 struct cpmac_priv
*priv
= netdev_priv(dev
);
541 struct sk_buff
*skb
, *skbs
= NULL
;
543 for (i
= 0; i
< CPMAC_ALLOC_SIZE
; i
++) {
544 skb
= alloc_skb(CPMAC_SKB_SIZE
+ 2, GFP_KERNEL
);
549 skb
->dev
= priv
->dev
;
555 spin_lock_irqsave(&priv
->lock
, flags
);
556 for (skb
= priv
->skb_pool
; skb
&& skb
->next
; skb
= skb
->next
);
558 priv
->skb_pool
= skbs
;
562 priv
->free_skbs
+= num_skbs
;
563 spin_unlock_irqrestore(&priv
->lock
, flags
);
565 printk("%s: allocated %d skbs\n", priv
->dev
->name
, num_skbs
);
570 static int cpmac_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
574 struct cpmac_desc
*desc
;
575 struct cpmac_priv
*priv
= netdev_priv(dev
);
579 printk(KERN_DEBUG
"%s: len=%d\n", __func__
, len
); //cpmac_dump_buf(const uint8_t * buf, unsigned size)
581 if (unlikely(len
< ETH_ZLEN
)) {
582 if (unlikely(skb_padto(skb
, ETH_ZLEN
))) {
583 if (printk_ratelimit())
584 printk(KERN_NOTICE
"%s: padding failed, dropping\n",
586 spin_lock_irqsave(&priv
->lock
, flags
);
587 priv
->stats
.tx_dropped
++;
588 spin_unlock_irqrestore(&priv
->lock
, flags
);
593 spin_lock_irqsave(&priv
->lock
, flags
);
594 chan
= priv
->tx_tail
++;
596 if (priv
->tx_tail
== priv
->tx_head
)
597 netif_stop_queue(dev
);
599 desc
= &priv
->desc_ring
[chan
];
600 dma_cache_inv((u32
)desc
, 16);
601 if (desc
->dataflags
& CPMAC_OWN
) {
602 printk(KERN_NOTICE
"%s: tx dma ring full, dropping\n", dev
->name
);
603 priv
->stats
.tx_dropped
++;
604 spin_unlock_irqrestore(&priv
->lock
, flags
);
608 dev
->trans_start
= jiffies
;
609 desc
->dataflags
= CPMAC_SOP
| CPMAC_EOP
| CPMAC_OWN
;
611 desc
->hw_data
= virt_to_phys(skb
->data
);
612 dma_cache_wback((u32
)skb
->data
, len
);
616 dma_cache_wback((u32
)desc
, 16);
617 priv
->regs
->tx_ptr
[chan
] = virt_to_phys(desc
);
618 spin_unlock_irqrestore(&priv
->lock
, flags
);
623 static void cpmac_end_xmit(struct net_device
*dev
, int channel
)
625 struct cpmac_desc
*desc
;
626 struct cpmac_priv
*priv
= netdev_priv(dev
);
628 spin_lock(&priv
->lock
);
629 desc
= &priv
->desc_ring
[channel
];
630 priv
->regs
->tx_ack
[channel
] = virt_to_phys(desc
);
631 if (likely(desc
->skb
)) {
632 priv
->stats
.tx_packets
++;
633 priv
->stats
.tx_bytes
+= desc
->skb
->len
;
634 dev_kfree_skb_irq(desc
->skb
);
635 if (netif_queue_stopped(dev
))
636 netif_wake_queue(dev
);
638 if (printk_ratelimit())
639 printk(KERN_NOTICE
"%s: end_xmit: spurious interrupt\n",
642 spin_unlock(&priv
->lock
);
645 static void cpmac_reset(struct net_device
*dev
)
648 struct cpmac_priv
*priv
= netdev_priv(dev
);
650 ar7_device_reset(priv
->config
->reset_bit
);
651 priv
->regs
->rx_ctrl
.control
&= ~1;
652 priv
->regs
->tx_ctrl
.control
&= ~1;
653 for (i
= 0; i
< 8; i
++) {
654 priv
->regs
->tx_ptr
[i
] = 0;
655 priv
->regs
->rx_ptr
[i
] = 0;
657 priv
->regs
->mac_control
&= ~MAC_MII
; /* disable mii */
660 static inline void cpmac_free_rx_ring(struct net_device
*dev
)
662 struct cpmac_desc
*desc
;
664 struct cpmac_priv
*priv
= netdev_priv(dev
);
666 if (unlikely(!priv
->rx_head
))
669 desc
= priv
->rx_head
;
670 dma_cache_inv((u32
)desc
, 16);
672 for (i
= 0; i
< rx_ring_size
; i
++) {
673 desc
->buflen
= CPMAC_SKB_SIZE
;
674 if ((desc
->dataflags
& CPMAC_OWN
) == 0) {
675 desc
->dataflags
= CPMAC_OWN
;
676 priv
->stats
.rx_dropped
++;
678 dma_cache_wback((u32
)desc
, 16);
680 dma_cache_inv((u32
)desc
, 16);
684 static irqreturn_t
cpmac_irq(int irq
, void *dev_id
)
686 struct net_device
*dev
= (struct net_device
*)dev_id
;
687 struct cpmac_priv
*priv
= netdev_priv(dev
);
693 status
= priv
->regs
->mac_int_vector
;
695 if (status
& INTST_TX
) {
696 cpmac_end_xmit(dev
, (status
& 7));
699 if (status
& INTST_RX
) {
703 priv
->regs
->rx_int
.enable
= 0;
704 priv
->regs
->rx_int
.clear
= 0xff;
705 netif_rx_schedule(dev
);
709 priv
->regs
->mac_eoi_vector
= 0;
711 if (unlikely(status
& (INTST_HOST
| INTST_STATUS
))) {
712 if (printk_ratelimit()) {
713 printk(KERN_ERR
"%s: hw error, resetting...\n", dev
->name
);
715 spin_lock(&priv
->lock
);
718 cpmac_free_rx_ring(dev
);
720 spin_unlock(&priv
->lock
);
726 static void cpmac_tx_timeout(struct net_device
*dev
)
728 struct cpmac_priv
*priv
= netdev_priv(dev
);
729 struct cpmac_desc
*desc
;
731 priv
->stats
.tx_errors
++;
732 desc
= &priv
->desc_ring
[priv
->tx_head
++];
734 printk("%s: transmit timeout\n", dev
->name
);
736 dev_kfree_skb(desc
->skb
);
737 netif_wake_queue(dev
);
740 static int cpmac_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
742 struct cpmac_priv
*priv
= netdev_priv(dev
);
743 if (!(netif_running(dev
)))
747 if ((cmd
== SIOCGMIIPHY
) || (cmd
== SIOCGMIIREG
) ||
748 (cmd
== SIOCSMIIREG
))
749 return phy_mii_ioctl(priv
->phy
, if_mii(ifr
), cmd
);
754 static int cpmac_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
756 struct cpmac_priv
*priv
= netdev_priv(dev
);
759 return phy_ethtool_gset(priv
->phy
, cmd
);
764 static int cpmac_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
766 struct cpmac_priv
*priv
= netdev_priv(dev
);
768 if (!capable(CAP_NET_ADMIN
))
772 return phy_ethtool_sset(priv
->phy
, cmd
);
777 static void cpmac_get_drvinfo(struct net_device
*dev
,
778 struct ethtool_drvinfo
*info
)
780 strcpy(info
->driver
, "cpmac");
781 strcpy(info
->version
, "0.0.3");
782 info
->fw_version
[0] = '\0';
783 sprintf(info
->bus_info
, "%s", "cpmac");
784 info
->regdump_len
= 0;
787 static const struct ethtool_ops cpmac_ethtool_ops
= {
788 .get_settings
= cpmac_get_settings
,
789 .set_settings
= cpmac_set_settings
,
790 .get_drvinfo
= cpmac_get_drvinfo
,
791 .get_link
= ethtool_op_get_link
,
794 static struct net_device_stats
*cpmac_stats(struct net_device
*dev
)
796 struct cpmac_priv
*priv
= netdev_priv(dev
);
798 if (netif_device_present(dev
))
804 static int cpmac_change_mtu(struct net_device
*dev
, int mtu
)
807 struct cpmac_priv
*priv
= netdev_priv(dev
);
808 spinlock_t
*lock
= &priv
->lock
;
810 if ((mtu
< 68) || (mtu
> 1500))
813 spin_lock_irqsave(lock
, flags
);
815 spin_unlock_irqrestore(lock
, flags
);
820 static void cpmac_adjust_link(struct net_device
*dev
)
822 struct cpmac_priv
*priv
= netdev_priv(dev
);
826 spin_lock_irqsave(&priv
->lock
, flags
);
827 if (priv
->phy
->link
) {
828 if (priv
->phy
->duplex
!= priv
->oldduplex
) {
830 priv
->oldduplex
= priv
->phy
->duplex
;
833 if (priv
->phy
->speed
!= priv
->oldspeed
) {
835 priv
->oldspeed
= priv
->phy
->speed
;
838 if (!priv
->oldlink
) {
843 } else if (priv
->oldlink
) {
847 priv
->oldduplex
= -1;
851 phy_print_status(priv
->phy
);
853 spin_unlock_irqrestore(&priv
->lock
, flags
);
856 static void cpmac_hw_init(struct net_device
*dev
)
859 struct cpmac_priv
*priv
= netdev_priv(dev
);
861 for (i
= 0; i
< 8; i
++)
862 priv
->regs
->tx_ptr
[i
] = 0;
863 priv
->regs
->rx_ptr
[0] = virt_to_phys(priv
->rx_head
);
865 priv
->regs
->mbp
= MBP_RXSHORT
| MBP_RXBCAST
| MBP_RXMCAST
;
866 priv
->regs
->unicast_enable
= 0x1;
867 priv
->regs
->unicast_clear
= 0xfe;
868 priv
->regs
->buffer_offset
= 0;
869 for (i
= 0; i
< 8; i
++)
870 priv
->regs
->mac_addr_low
[i
] = dev
->dev_addr
[5];
871 priv
->regs
->mac_addr_mid
= dev
->dev_addr
[4];
872 priv
->regs
->mac_addr_high
= dev
->dev_addr
[0] | (dev
->dev_addr
[1] << 8)
873 | (dev
->dev_addr
[2] << 16) | (dev
->dev_addr
[3] << 24);
874 priv
->regs
->max_len
= CPMAC_SKB_SIZE
;
875 priv
->regs
->rx_int
.enable
= 0x1;
876 priv
->regs
->rx_int
.clear
= 0xfe;
877 priv
->regs
->tx_int
.enable
= 0xff;
878 priv
->regs
->tx_int
.clear
= 0;
879 priv
->regs
->mac_int_enable
= 3;
880 priv
->regs
->mac_int_clear
= 0xfc;
882 priv
->regs
->rx_ctrl
.control
|= 1;
883 priv
->regs
->tx_ctrl
.control
|= 1;
884 priv
->regs
->mac_control
|= MAC_MII
| MAC_FDX
;
886 priv
->phy
->state
= PHY_CHANGELINK
;
887 phy_start(priv
->phy
);
890 static int cpmac_open(struct net_device
*dev
)
893 struct cpmac_priv
*priv
= netdev_priv(dev
);
894 struct cpmac_desc
*desc
;
897 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
898 priv
->phy
= phy_connect(dev
, priv
->phy_name
, &cpmac_adjust_link
,
899 0, PHY_INTERFACE_MODE_MII
);
901 priv
->phy
= phy_connect(dev
, priv
->phy_name
, &cpmac_adjust_link
, 0);
903 if (IS_ERR(priv
->phy
)) {
904 printk(KERN_ERR
"%s: Could not attach to PHY\n", dev
->name
);
905 return PTR_ERR(priv
->phy
);
908 if (!request_mem_region(dev
->mem_start
, dev
->mem_end
-
909 dev
->mem_start
, dev
->name
)) {
910 printk("%s: failed to request registers\n",
916 priv
->regs
= ioremap_nocache(dev
->mem_start
, dev
->mem_end
-
919 printk("%s: failed to remap registers\n", dev
->name
);
924 priv
->rx_head
= NULL
;
925 size
= sizeof(struct cpmac_desc
) * (rx_ring_size
+
927 priv
->desc_ring
= (struct cpmac_desc
*)kmalloc(size
, GFP_KERNEL
);
928 if (!priv
->desc_ring
) {
933 memset((char *)priv
->desc_ring
, 0, size
);
935 priv
->skb_pool
= NULL
;
937 priv
->rx_head
= &priv
->desc_ring
[CPMAC_TX_RING_SIZE
];
939 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
940 INIT_WORK(&priv
->alloc_work
, cpmac_alloc_skbs
);
942 INIT_WORK(&priv
->alloc_work
, cpmac_alloc_skbs
, dev
);
944 schedule_work(&priv
->alloc_work
);
945 flush_scheduled_work();
947 for (i
= 0; i
< rx_ring_size
; i
++) {
948 desc
= &priv
->rx_head
[i
];
949 skb
= cpmac_get_skb(dev
);
955 desc
->hw_data
= virt_to_phys(skb
->data
);
956 desc
->buflen
= CPMAC_SKB_SIZE
;
957 desc
->dataflags
= CPMAC_OWN
;
958 desc
->next
= &priv
->rx_head
[(i
+ 1) % rx_ring_size
];
959 desc
->hw_next
= virt_to_phys(desc
->next
);
960 dma_cache_wback((u32
)desc
, 16);
963 if((res
= request_irq(dev
->irq
, cpmac_irq
, SA_INTERRUPT
,
965 printk("%s: failed to obtain irq\n", dev
->name
);
972 netif_start_queue(dev
);
977 for (i
= 0; i
< rx_ring_size
; i
++)
978 if (priv
->rx_head
[i
].skb
)
979 kfree_skb(priv
->rx_head
[i
].skb
);
981 kfree(priv
->desc_ring
);
983 for (skb
= priv
->skb_pool
; skb
; skb
= priv
->skb_pool
) {
984 priv
->skb_pool
= skb
->next
;
991 release_mem_region(dev
->mem_start
, dev
->mem_end
-
995 phy_disconnect(priv
->phy
);
1000 static int cpmac_stop(struct net_device
*dev
)
1003 struct sk_buff
*skb
;
1004 struct cpmac_priv
*priv
= netdev_priv(dev
);
1006 netif_stop_queue(dev
);
1008 phy_stop(priv
->phy
);
1009 phy_disconnect(priv
->phy
);
1014 for (i
= 0; i
< 8; i
++) {
1015 priv
->regs
->rx_ptr
[i
] = 0;
1016 priv
->regs
->tx_ptr
[i
] = 0;
1017 priv
->regs
->mbp
= 0;
1020 free_irq(dev
->irq
, dev
);
1021 release_mem_region(dev
->mem_start
, dev
->mem_end
-
1024 cancel_delayed_work(&priv
->alloc_work
);
1025 flush_scheduled_work();
1027 priv
->rx_head
= &priv
->desc_ring
[CPMAC_TX_RING_SIZE
];
1028 for (i
= 0; i
< rx_ring_size
; i
++)
1029 if (priv
->rx_head
[i
].skb
)
1030 kfree_skb(priv
->rx_head
[i
].skb
);
1032 kfree(priv
->desc_ring
);
1034 for (skb
= priv
->skb_pool
; skb
; skb
= priv
->skb_pool
) {
1035 priv
->skb_pool
= skb
->next
;
1042 static int external_switch
= 0;
1044 static int __devinit
cpmac_probe(struct platform_device
*pdev
)
1047 struct resource
*res
;
1048 struct cpmac_priv
*priv
;
1049 struct net_device
*dev
;
1050 struct plat_cpmac_data
*pdata
;
1052 if (strcmp(pdev
->name
, "cpmac") != 0)
1055 pdata
= pdev
->dev
.platform_data
;
1057 for (phy_id
= 0; phy_id
< PHY_MAX_ADDR
; phy_id
++) {
1058 if (!(pdata
->phy_mask
& (1 << phy_id
)))
1060 if (!cpmac_mii
.phy_map
[phy_id
])
1065 if (phy_id
== PHY_MAX_ADDR
) {
1066 if (external_switch
) {
1069 printk("cpmac: no PHY present\n");
1074 dev
= alloc_etherdev(sizeof(struct cpmac_priv
));
1077 printk(KERN_ERR
"cpmac: Unable to allocate net_device structure!\n");
1081 SET_MODULE_OWNER(dev
);
1082 platform_set_drvdata(pdev
, dev
);
1083 priv
= netdev_priv(dev
);
1085 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "regs");
1091 dev
->mem_start
= res
->start
;
1092 dev
->mem_end
= res
->end
;
1093 dev
->irq
= platform_get_irq_byname(pdev
, "irq");
1096 dev
->open
= cpmac_open
;
1097 dev
->stop
= cpmac_stop
;
1098 dev
->set_config
= cpmac_config
;
1099 dev
->hard_start_xmit
= cpmac_start_xmit
;
1100 dev
->do_ioctl
= cpmac_ioctl
;
1101 dev
->get_stats
= cpmac_stats
;
1102 dev
->change_mtu
= cpmac_change_mtu
;
1103 dev
->set_mac_address
= cpmac_set_mac_address
;
1104 dev
->set_multicast_list
= cpmac_set_multicast_list
;
1105 dev
->tx_timeout
= cpmac_tx_timeout
;
1106 dev
->ethtool_ops
= &cpmac_ethtool_ops
;
1107 if (!disable_napi
) {
1108 dev
->poll
= cpmac_poll
;
1109 dev
->weight
= min(rx_ring_size
, 64);
1112 memset(priv
, 0, sizeof(struct cpmac_priv
));
1113 spin_lock_init(&priv
->lock
);
1114 priv
->msg_enable
= netif_msg_init(NETIF_MSG_WOL
, 0x3fff);
1115 priv
->config
= pdata
;
1117 memcpy(dev
->dev_addr
, priv
->config
->dev_addr
, sizeof(dev
->dev_addr
));
1119 snprintf(priv
->phy_name
, BUS_ID_SIZE
, PHY_ID_FMT
,
1120 cpmac_mii
.id
, phy_id
);
1122 snprintf(priv
->phy_name
, BUS_ID_SIZE
, "fixed@%d:%d", 100, 1);
1125 if ((rc
= register_netdev(dev
))) {
1126 printk("cpmac: error %i registering device %s\n",
1131 printk("cpmac: device %s (regs: %p, irq: %d, phy: %s, mac: ",
1132 dev
->name
, (u32
*)dev
->mem_start
, dev
->irq
,
1134 for (i
= 0; i
< 6; i
++) {
1135 printk("%02x", dev
->dev_addr
[i
]);
1136 if (i
< 5) printk(":");
1147 static int __devexit
cpmac_remove(struct platform_device
*pdev
)
1149 struct net_device
*dev
= platform_get_drvdata(pdev
);
1150 unregister_netdev(dev
);
1155 static struct platform_driver cpmac_driver
= {
1156 .driver
.name
= "cpmac",
1157 .probe
= cpmac_probe
,
1158 .remove
= cpmac_remove
,
1161 int __devinit
cpmac_init(void)
1165 cpmac_mii
.priv
= (struct cpmac_mdio_regs
*)
1166 ioremap_nocache(AR7_REGS_MDIO
, sizeof(struct cpmac_mdio_regs
));
1168 if (!cpmac_mii
.priv
) {
1169 printk("Can't ioremap mdio registers\n");
1173 #warning FIXME: unhardcode gpio&reset bits
1174 ar7_gpio_disable(26);
1175 ar7_gpio_disable(27);
1176 ar7_device_reset(17);
1177 ar7_device_reset(21);
1178 ar7_device_reset(26);
1180 cpmac_mii
.reset(&cpmac_mii
);
1182 for (i
= 0; i
< 300000; i
++) {
1183 mask
= ((struct cpmac_mdio_regs
*)cpmac_mii
.priv
)->alive
;
1189 if (mask
& (mask
- 1)) {
1190 external_switch
= 1;
1194 cpmac_mii
.phy_mask
= ~(mask
| 0x80000000);
1196 res
= mdiobus_register(&cpmac_mii
);
1200 res
= platform_driver_register(&cpmac_driver
);
1207 mdiobus_unregister(&cpmac_mii
);
1210 iounmap(cpmac_mii
.priv
);
1215 void __devexit
cpmac_exit(void)
1217 platform_driver_unregister(&cpmac_driver
);
1218 mdiobus_unregister(&cpmac_mii
);
1221 module_init(cpmac_init
);
1222 module_exit(cpmac_exit
);