2 * Copyright (C) 2006, 2007 OpenWrt.org
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/moduleparam.h>
23 #include <linux/sched.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/errno.h>
27 #include <linux/types.h>
28 #include <linux/delay.h>
29 #include <linux/version.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/ethtool.h>
34 #include <linux/skbuff.h>
35 #include <linux/mii.h>
36 #include <linux/phy.h>
37 #include <linux/platform_device.h>
38 #include <linux/dma-mapping.h>
41 MODULE_AUTHOR("Eugene Konev");
42 MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
43 MODULE_LICENSE("GPL");
45 static int disable_napi
;
46 static int debug_level
= 8;
47 static int dumb_switch
;
49 module_param(disable_napi
, int, 0644);
50 /* Next 2 are only used in cpmac_probe, so it's pointless to change them */
51 module_param(debug_level
, int, 0444);
52 module_param(dumb_switch
, int, 0444);
54 MODULE_PARM_DESC(disable_napi
, "Disable NAPI polling");
55 MODULE_PARM_DESC(debug_level
, "Number of NETIF_MSG bits to enable");
56 MODULE_PARM_DESC(dumb_switch
, "Assume switch is not connected to MDIO bus");
58 /* stolen from net/ieee80211.h */
60 #define MAC_FMT "%02x:%02x:%02x:%02x:%02x:%02x"
61 #define MAC_ARG(x) ((u8*)(x))[0], ((u8*)(x))[1], ((u8*)(x))[2], \
62 ((u8*)(x))[3], ((u8*)(x))[4], ((u8*)(x))[5]
64 /* frame size + 802.1q tag */
65 #define CPMAC_SKB_SIZE (ETH_FRAME_LEN + 4)
66 #define CPMAC_TX_RING_SIZE 8
68 /* Ethernet registers */
69 #define CPMAC_TX_CONTROL 0x0004
70 #define CPMAC_TX_TEARDOWN 0x0008
71 #define CPMAC_RX_CONTROL 0x0014
72 #define CPMAC_RX_TEARDOWN 0x0018
73 #define CPMAC_MBP 0x0100
74 # define MBP_RXPASSCRC 0x40000000
75 # define MBP_RXQOS 0x20000000
76 # define MBP_RXNOCHAIN 0x10000000
77 # define MBP_RXCMF 0x01000000
78 # define MBP_RXSHORT 0x00800000
79 # define MBP_RXCEF 0x00400000
80 # define MBP_RXPROMISC 0x00200000
81 # define MBP_PROMISCCHAN(channel) (((channel) & 0x7) << 16)
82 # define MBP_RXBCAST 0x00002000
83 # define MBP_BCASTCHAN(channel) (((channel) & 0x7) << 8)
84 # define MBP_RXMCAST 0x00000020
85 # define MBP_MCASTCHAN(channel) ((channel) & 0x7)
86 #define CPMAC_UNICAST_ENABLE 0x0104
87 #define CPMAC_UNICAST_CLEAR 0x0108
88 #define CPMAC_MAX_LENGTH 0x010c
89 #define CPMAC_BUFFER_OFFSET 0x0110
90 #define CPMAC_MAC_CONTROL 0x0160
91 # define MAC_TXPTYPE 0x00000200
92 # define MAC_TXPACE 0x00000040
93 # define MAC_MII 0x00000020
94 # define MAC_TXFLOW 0x00000010
95 # define MAC_RXFLOW 0x00000008
96 # define MAC_MTEST 0x00000004
97 # define MAC_LOOPBACK 0x00000002
98 # define MAC_FDX 0x00000001
99 #define CPMAC_MAC_STATUS 0x0164
100 # define MAC_STATUS_QOS 0x00000004
101 # define MAC_STATUS_RXFLOW 0x00000002
102 # define MAC_STATUS_TXFLOW 0x00000001
103 #define CPMAC_TX_INT_ENABLE 0x0178
104 #define CPMAC_TX_INT_CLEAR 0x017c
105 #define CPMAC_MAC_INT_VECTOR 0x0180
106 # define MAC_INT_STATUS 0x00080000
107 # define MAC_INT_HOST 0x00040000
108 # define MAC_INT_RX 0x00020000
109 # define MAC_INT_TX 0x00010000
110 #define CPMAC_MAC_EOI_VECTOR 0x0184
111 #define CPMAC_RX_INT_ENABLE 0x0198
112 #define CPMAC_RX_INT_CLEAR 0x019c
113 #define CPMAC_MAC_INT_ENABLE 0x01a8
114 #define CPMAC_MAC_INT_CLEAR 0x01ac
115 #define CPMAC_MAC_ADDR_LO(channel) (0x01b0 + (channel) * 4)
116 #define CPMAC_MAC_ADDR_MID 0x01d0
117 #define CPMAC_MAC_ADDR_HI 0x01d4
118 #define CPMAC_MAC_HASH_LO 0x01d8
119 #define CPMAC_MAC_HASH_HI 0x01dc
120 #define CPMAC_TX_PTR(channel) (0x0600 + (channel) * 4)
121 #define CPMAC_RX_PTR(channel) (0x0620 + (channel) * 4)
122 #define CPMAC_TX_ACK(channel) (0x0640 + (channel) * 4)
123 #define CPMAC_RX_ACK(channel) (0x0660 + (channel) * 4)
124 #define CPMAC_REG_END 0x0680
127 * TODO: use some of them to fill stats in cpmac_stats()
129 #define CPMAC_STATS_RX_GOOD 0x0200
130 #define CPMAC_STATS_RX_BCAST 0x0204
131 #define CPMAC_STATS_RX_MCAST 0x0208
132 #define CPMAC_STATS_RX_PAUSE 0x020c
133 #define CPMAC_STATS_RX_CRC 0x0210
134 #define CPMAC_STATS_RX_ALIGN 0x0214
135 #define CPMAC_STATS_RX_OVER 0x0218
136 #define CPMAC_STATS_RX_JABBER 0x021c
137 #define CPMAC_STATS_RX_UNDER 0x0220
138 #define CPMAC_STATS_RX_FRAG 0x0224
139 #define CPMAC_STATS_RX_FILTER 0x0228
140 #define CPMAC_STATS_RX_QOSFILTER 0x022c
141 #define CPMAC_STATS_RX_OCTETS 0x0230
143 #define CPMAC_STATS_TX_GOOD 0x0234
144 #define CPMAC_STATS_TX_BCAST 0x0238
145 #define CPMAC_STATS_TX_MCAST 0x023c
146 #define CPMAC_STATS_TX_PAUSE 0x0240
147 #define CPMAC_STATS_TX_DEFER 0x0244
148 #define CPMAC_STATS_TX_COLLISION 0x0248
149 #define CPMAC_STATS_TX_SINGLECOLL 0x024c
150 #define CPMAC_STATS_TX_MULTICOLL 0x0250
151 #define CPMAC_STATS_TX_EXCESSCOLL 0x0254
152 #define CPMAC_STATS_TX_LATECOLL 0x0258
153 #define CPMAC_STATS_TX_UNDERRUN 0x025c
154 #define CPMAC_STATS_TX_CARRIERSENSE 0x0260
155 #define CPMAC_STATS_TX_OCTETS 0x0264
157 #define cpmac_read(base, reg) (readl((void __iomem *)(base) + (reg)))
158 #define cpmac_write(base, reg, val) (writel(val, (void __iomem *)(base) + \
162 #define CPMAC_MDIO_VERSION 0x0000
163 #define CPMAC_MDIO_CONTROL 0x0004
164 # define MDIOC_IDLE 0x80000000
165 # define MDIOC_ENABLE 0x40000000
166 # define MDIOC_PREAMBLE 0x00100000
167 # define MDIOC_FAULT 0x00080000
168 # define MDIOC_FAULTDETECT 0x00040000
169 # define MDIOC_INTTEST 0x00020000
170 # define MDIOC_CLKDIV(div) ((div) & 0xff)
171 #define CPMAC_MDIO_ALIVE 0x0008
172 #define CPMAC_MDIO_LINK 0x000c
173 #define CPMAC_MDIO_ACCESS(channel) (0x0080 + (channel) * 8)
174 # define MDIO_BUSY 0x80000000
175 # define MDIO_WRITE 0x40000000
176 # define MDIO_REG(reg) (((reg) & 0x1f) << 21)
177 # define MDIO_PHY(phy) (((phy) & 0x1f) << 16)
178 # define MDIO_DATA(data) ((data) & 0xffff)
179 #define CPMAC_MDIO_PHYSEL(channel) (0x0084 + (channel) * 8)
180 # define PHYSEL_LINKSEL 0x00000040
181 # define PHYSEL_LINKINT 0x00000020
190 #define CPMAC_SOP 0x8000
191 #define CPMAC_EOP 0x4000
192 #define CPMAC_OWN 0x2000
193 #define CPMAC_EOQ 0x1000
195 struct cpmac_desc
*next
;
197 dma_addr_t data_mapping
;
202 struct cpmac_desc
*rx_head
;
203 int tx_head
, tx_tail
;
205 struct cpmac_desc
*desc_ring
;
208 struct mii_bus
*mii_bus
;
209 struct phy_device
*phy
;
210 char phy_name
[BUS_ID_SIZE
];
211 int oldlink
, oldspeed
, oldduplex
;
213 struct platform_device
*pdev
;
216 static irqreturn_t
cpmac_irq(int, void *);
217 static void cpmac_reset(struct net_device
*dev
);
218 static void cpmac_hw_init(struct net_device
*dev
);
219 static int cpmac_stop(struct net_device
*dev
);
220 static int cpmac_open(struct net_device
*dev
);
222 static void cpmac_dump_regs(struct net_device
*dev
)
225 struct cpmac_priv
*priv
= netdev_priv(dev
);
226 for (i
= 0; i
< CPMAC_REG_END
; i
+= 4) {
230 printk(KERN_DEBUG
"%s: reg[%p]:", dev
->name
,
233 printk(" %08x", cpmac_read(priv
->regs
, i
));
238 static void cpmac_dump_desc(struct net_device
*dev
, struct cpmac_desc
*desc
)
241 printk(KERN_DEBUG
"%s: desc[%p]:", dev
->name
, desc
);
242 for (i
= 0; i
< sizeof(*desc
) / 4; i
++)
243 printk(" %08x", ((u32
*)desc
)[i
]);
247 static void cpmac_dump_skb(struct net_device
*dev
, struct sk_buff
*skb
)
250 printk(KERN_DEBUG
"%s: skb 0x%p, len=%d\n", dev
->name
, skb
, skb
->len
);
251 for (i
= 0; i
< skb
->len
; i
++) {
255 printk(KERN_DEBUG
"%s: data[%p]:", dev
->name
,
258 printk(" %02x", ((u8
*)skb
->data
)[i
]);
263 static int cpmac_mdio_read(struct mii_bus
*bus
, int phy_id
, int reg
)
267 while (cpmac_read(bus
->priv
, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY
)
269 cpmac_write(bus
->priv
, CPMAC_MDIO_ACCESS(0), MDIO_BUSY
| MDIO_REG(reg
) |
271 while ((val
= cpmac_read(bus
->priv
, CPMAC_MDIO_ACCESS(0))) & MDIO_BUSY
)
273 return MDIO_DATA(val
);
276 static int cpmac_mdio_write(struct mii_bus
*bus
, int phy_id
,
279 while (cpmac_read(bus
->priv
, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY
)
281 cpmac_write(bus
->priv
, CPMAC_MDIO_ACCESS(0), MDIO_BUSY
| MDIO_WRITE
|
282 MDIO_REG(reg
) | MDIO_PHY(phy_id
) | MDIO_DATA(val
));
286 static int cpmac_mdio_reset(struct mii_bus
*bus
)
288 ar7_device_reset(AR7_RESET_BIT_MDIO
);
289 cpmac_write(bus
->priv
, CPMAC_MDIO_CONTROL
, MDIOC_ENABLE
|
290 MDIOC_CLKDIV(ar7_cpmac_freq() / 2200000 - 1));
294 static int mii_irqs
[PHY_MAX_ADDR
] = { PHY_POLL
, };
296 static struct mii_bus cpmac_mii
= {
298 .read
= cpmac_mdio_read
,
299 .write
= cpmac_mdio_write
,
300 .reset
= cpmac_mdio_reset
,
304 static int cpmac_config(struct net_device
*dev
, struct ifmap
*map
)
306 if (dev
->flags
& IFF_UP
)
309 /* Don't allow changing the I/O address */
310 if (map
->base_addr
!= dev
->base_addr
)
313 /* ignore other fields */
317 static void cpmac_set_multicast_list(struct net_device
*dev
)
319 struct dev_mc_list
*iter
;
322 u32 mbp
, bit
, hash
[2] = { 0, };
323 struct cpmac_priv
*priv
= netdev_priv(dev
);
325 mbp
= cpmac_read(priv
->regs
, CPMAC_MBP
);
326 if (dev
->flags
& IFF_PROMISC
) {
327 cpmac_write(priv
->regs
, CPMAC_MBP
, (mbp
& ~MBP_PROMISCCHAN(0)) |
330 cpmac_write(priv
->regs
, CPMAC_MBP
, mbp
& ~MBP_RXPROMISC
);
331 if (dev
->flags
& IFF_ALLMULTI
) {
332 /* enable all multicast mode */
333 cpmac_write(priv
->regs
, CPMAC_MAC_HASH_LO
, 0xffffffff);
334 cpmac_write(priv
->regs
, CPMAC_MAC_HASH_HI
, 0xffffffff);
337 * cpmac uses some strange mac address hashing
340 for (i
= 0, iter
= dev
->mc_list
; i
< dev
->mc_count
;
341 i
++, iter
= iter
->next
) {
343 tmp
= iter
->dmi_addr
[0];
344 bit
^= (tmp
>> 2) ^ (tmp
<< 4);
345 tmp
= iter
->dmi_addr
[1];
346 bit
^= (tmp
>> 4) ^ (tmp
<< 2);
347 tmp
= iter
->dmi_addr
[2];
348 bit
^= (tmp
>> 6) ^ tmp
;
349 tmp
= iter
->dmi_addr
[3];
350 bit
^= (tmp
>> 2) ^ (tmp
<< 4);
351 tmp
= iter
->dmi_addr
[4];
352 bit
^= (tmp
>> 4) ^ (tmp
<< 2);
353 tmp
= iter
->dmi_addr
[5];
354 bit
^= (tmp
>> 6) ^ tmp
;
356 hash
[bit
/ 32] |= 1 << (bit
% 32);
359 cpmac_write(priv
->regs
, CPMAC_MAC_HASH_LO
, hash
[0]);
360 cpmac_write(priv
->regs
, CPMAC_MAC_HASH_HI
, hash
[1]);
365 static struct sk_buff
*cpmac_rx_one(struct net_device
*dev
,
366 struct cpmac_priv
*priv
,
367 struct cpmac_desc
*desc
)
370 struct sk_buff
*skb
, *result
= NULL
;
372 if (unlikely(netif_msg_hw(priv
)))
373 cpmac_dump_desc(dev
, desc
);
374 cpmac_write(priv
->regs
, CPMAC_RX_ACK(0), (u32
)desc
->mapping
);
375 if (unlikely(!desc
->datalen
)) {
376 if (netif_msg_rx_err(priv
) && net_ratelimit())
377 printk(KERN_WARNING
"%s: rx: spurious interrupt\n",
382 skb
= netdev_alloc_skb(dev
, CPMAC_SKB_SIZE
);
383 spin_lock_irqsave(&priv
->lock
, flags
);
386 skb_put(desc
->skb
, desc
->datalen
);
387 desc
->skb
->protocol
= eth_type_trans(desc
->skb
, dev
);
388 desc
->skb
->ip_summed
= CHECKSUM_NONE
;
389 dev
->stats
.rx_packets
++;
390 dev
->stats
.rx_bytes
+= desc
->datalen
;
392 dma_unmap_single(&dev
->dev
, desc
->data_mapping
, CPMAC_SKB_SIZE
,
395 desc
->data_mapping
= dma_map_single(&dev
->dev
, skb
->data
,
398 desc
->hw_data
= (u32
)desc
->data_mapping
;
399 if (unlikely(netif_msg_pktdata(priv
))) {
400 printk(KERN_DEBUG
"%s: received packet:\n", dev
->name
);
401 cpmac_dump_skb(dev
, result
);
404 if (netif_msg_rx_err(priv
) && net_ratelimit())
406 "%s: low on skbs, dropping packet\n", dev
->name
);
407 dev
->stats
.rx_dropped
++;
409 spin_unlock_irqrestore(&priv
->lock
, flags
);
411 desc
->buflen
= CPMAC_SKB_SIZE
;
412 desc
->dataflags
= CPMAC_OWN
;
417 static void cpmac_rx(struct net_device
*dev
)
420 struct cpmac_desc
*desc
;
421 struct cpmac_priv
*priv
= netdev_priv(dev
);
423 spin_lock(&priv
->lock
);
424 if (unlikely(!priv
->rx_head
)) {
425 spin_unlock(&priv
->lock
);
429 desc
= priv
->rx_head
;
431 while ((desc
->dataflags
& CPMAC_OWN
) == 0) {
432 skb
= cpmac_rx_one(dev
, priv
, desc
);
438 priv
->rx_head
= desc
;
439 cpmac_write(priv
->regs
, CPMAC_RX_PTR(0), (u32
)desc
->mapping
);
440 spin_unlock(&priv
->lock
);
443 static int cpmac_poll(struct net_device
*dev
, int *budget
)
446 struct cpmac_desc
*desc
;
447 int received
= 0, quota
= min(dev
->quota
, *budget
);
448 struct cpmac_priv
*priv
= netdev_priv(dev
);
450 if (unlikely(!priv
->rx_head
)) {
451 if (netif_msg_rx_err(priv
) && net_ratelimit())
452 printk(KERN_WARNING
"%s: rx: polling, but no queue\n",
454 netif_rx_complete(dev
);
458 desc
= priv
->rx_head
;
460 while ((received
< quota
) && ((desc
->dataflags
& CPMAC_OWN
) == 0)) {
461 skb
= cpmac_rx_one(dev
, priv
, desc
);
463 netif_receive_skb(skb
);
469 priv
->rx_head
= desc
;
471 dev
->quota
-= received
;
472 if (unlikely(netif_msg_rx_status(priv
)))
473 printk(KERN_DEBUG
"%s: poll processed %d packets\n", dev
->name
,
475 if (desc
->dataflags
& CPMAC_OWN
) {
476 netif_rx_complete(dev
);
477 cpmac_write(priv
->regs
, CPMAC_RX_PTR(0), (u32
)desc
->mapping
);
478 cpmac_write(priv
->regs
, CPMAC_RX_INT_ENABLE
, 1);
485 static int cpmac_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
489 struct cpmac_desc
*desc
;
490 struct cpmac_priv
*priv
= netdev_priv(dev
);
492 if (unlikely(skb_padto(skb
, ETH_ZLEN
))) {
493 if (netif_msg_tx_err(priv
) && net_ratelimit())
494 printk(KERN_WARNING
"%s: tx: padding failed, dropping\n",
496 spin_lock_irqsave(&priv
->lock
, flags
);
497 dev
->stats
.tx_dropped
++;
498 spin_unlock_irqrestore(&priv
->lock
, flags
);
502 len
= max(skb
->len
, ETH_ZLEN
);
503 spin_lock_irqsave(&priv
->lock
, flags
);
504 channel
= priv
->tx_tail
++;
505 priv
->tx_tail
%= CPMAC_TX_RING_SIZE
;
506 if (priv
->tx_tail
== priv
->tx_head
)
507 netif_stop_queue(dev
);
509 desc
= &priv
->desc_ring
[channel
];
510 if (desc
->dataflags
& CPMAC_OWN
) {
511 if (netif_msg_tx_err(priv
) && net_ratelimit())
512 printk(KERN_WARNING
"%s: tx dma ring full, dropping\n",
514 dev
->stats
.tx_dropped
++;
515 spin_unlock_irqrestore(&priv
->lock
, flags
);
516 dev_kfree_skb_any(skb
);
520 dev
->trans_start
= jiffies
;
521 spin_unlock_irqrestore(&priv
->lock
, flags
);
522 desc
->dataflags
= CPMAC_SOP
| CPMAC_EOP
| CPMAC_OWN
;
524 desc
->data_mapping
= dma_map_single(&dev
->dev
, skb
->data
, len
,
526 desc
->hw_data
= (u32
)desc
->data_mapping
;
529 if (unlikely(netif_msg_tx_queued(priv
)))
530 printk(KERN_DEBUG
"%s: sending 0x%p, len=%d\n", dev
->name
, skb
,
532 if (unlikely(netif_msg_hw(priv
)))
533 cpmac_dump_desc(dev
, desc
);
534 if (unlikely(netif_msg_pktdata(priv
)))
535 cpmac_dump_skb(dev
, skb
);
536 cpmac_write(priv
->regs
, CPMAC_TX_PTR(channel
), (u32
)desc
->mapping
);
541 static void cpmac_end_xmit(struct net_device
*dev
, int channel
)
543 struct cpmac_desc
*desc
;
544 struct cpmac_priv
*priv
= netdev_priv(dev
);
546 spin_lock(&priv
->lock
);
547 desc
= &priv
->desc_ring
[channel
];
548 cpmac_write(priv
->regs
, CPMAC_TX_ACK(channel
), (u32
)desc
->mapping
);
549 if (likely(desc
->skb
)) {
550 dev
->stats
.tx_packets
++;
551 dev
->stats
.tx_bytes
+= desc
->skb
->len
;
552 dma_unmap_single(&dev
->dev
, desc
->data_mapping
, desc
->skb
->len
,
555 if (unlikely(netif_msg_tx_done(priv
)))
556 printk(KERN_DEBUG
"%s: sent 0x%p, len=%d\n", dev
->name
,
557 desc
->skb
, desc
->skb
->len
);
559 dev_kfree_skb_irq(desc
->skb
);
560 if (netif_queue_stopped(dev
))
561 netif_wake_queue(dev
);
563 if (netif_msg_tx_err(priv
) && net_ratelimit())
565 "%s: end_xmit: spurious interrupt\n", dev
->name
);
566 spin_unlock(&priv
->lock
);
569 static void cpmac_reset(struct net_device
*dev
)
572 struct cpmac_priv
*priv
= netdev_priv(dev
);
573 struct plat_cpmac_data
*pdata
= priv
->pdev
->dev
.platform_data
;
575 ar7_device_reset(pdata
->reset_bit
);
576 cpmac_write(priv
->regs
, CPMAC_RX_CONTROL
,
577 cpmac_read(priv
->regs
, CPMAC_RX_CONTROL
) & ~1);
578 cpmac_write(priv
->regs
, CPMAC_TX_CONTROL
,
579 cpmac_read(priv
->regs
, CPMAC_TX_CONTROL
) & ~1);
580 for (i
= 0; i
< 8; i
++) {
581 cpmac_write(priv
->regs
, CPMAC_TX_PTR(i
), 0);
582 cpmac_write(priv
->regs
, CPMAC_RX_PTR(i
), 0);
584 cpmac_write(priv
->regs
, CPMAC_MAC_CONTROL
,
585 cpmac_read(priv
->regs
, CPMAC_MAC_CONTROL
) & ~MAC_MII
);
588 static inline void cpmac_free_rx_ring(struct net_device
*dev
)
590 struct cpmac_desc
*desc
;
592 struct cpmac_priv
*priv
= netdev_priv(dev
);
594 if (unlikely(!priv
->rx_head
))
597 desc
= priv
->rx_head
;
599 for (i
= 0; i
< priv
->ring_size
; i
++) {
600 desc
->buflen
= CPMAC_SKB_SIZE
;
601 if ((desc
->dataflags
& CPMAC_OWN
) == 0) {
602 if (netif_msg_rx_err(priv
) && net_ratelimit())
603 printk(KERN_WARNING
"%s: packet dropped\n",
605 if (unlikely(netif_msg_hw(priv
)))
606 cpmac_dump_desc(dev
, desc
);
607 desc
->dataflags
= CPMAC_OWN
;
608 dev
->stats
.rx_dropped
++;
614 static irqreturn_t
cpmac_irq(int irq
, void *dev_id
)
616 struct net_device
*dev
= dev_id
;
617 struct cpmac_priv
*priv
;
623 priv
= netdev_priv(dev
);
625 status
= cpmac_read(priv
->regs
, CPMAC_MAC_INT_VECTOR
);
627 if (unlikely(netif_msg_intr(priv
)))
628 printk(KERN_DEBUG
"%s: interrupt status: 0x%08x\n", dev
->name
,
631 if (status
& MAC_INT_TX
)
632 cpmac_end_xmit(dev
, (status
& 7));
634 if (status
& MAC_INT_RX
) {
638 cpmac_write(priv
->regs
, CPMAC_RX_INT_CLEAR
, 1);
639 netif_rx_schedule(dev
);
643 cpmac_write(priv
->regs
, CPMAC_MAC_EOI_VECTOR
, 0);
645 if (unlikely(status
& (MAC_INT_HOST
| MAC_INT_STATUS
))) {
646 if (netif_msg_drv(priv
) && net_ratelimit())
647 printk(KERN_ERR
"%s: hw error, resetting...\n",
649 if (unlikely(netif_msg_hw(priv
)))
650 cpmac_dump_regs(dev
);
651 spin_lock(&priv
->lock
);
654 cpmac_free_rx_ring(dev
);
656 spin_unlock(&priv
->lock
);
662 static void cpmac_tx_timeout(struct net_device
*dev
)
664 struct cpmac_priv
*priv
= netdev_priv(dev
);
665 struct cpmac_desc
*desc
;
667 dev
->stats
.tx_errors
++;
668 desc
= &priv
->desc_ring
[priv
->tx_head
++];
670 if (netif_msg_tx_err(priv
) && net_ratelimit())
671 printk(KERN_WARNING
"%s: transmit timeout\n", dev
->name
);
673 dev_kfree_skb_any(desc
->skb
);
674 netif_wake_queue(dev
);
677 static int cpmac_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
679 struct cpmac_priv
*priv
= netdev_priv(dev
);
680 if (!(netif_running(dev
)))
684 if ((cmd
== SIOCGMIIPHY
) || (cmd
== SIOCGMIIREG
) ||
685 (cmd
== SIOCSMIIREG
))
686 return phy_mii_ioctl(priv
->phy
, if_mii(ifr
), cmd
);
691 static int cpmac_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
693 struct cpmac_priv
*priv
= netdev_priv(dev
);
696 return phy_ethtool_gset(priv
->phy
, cmd
);
701 static int cpmac_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
703 struct cpmac_priv
*priv
= netdev_priv(dev
);
705 if (!capable(CAP_NET_ADMIN
))
709 return phy_ethtool_sset(priv
->phy
, cmd
);
714 static void cpmac_get_ringparam(struct net_device
*dev
, struct ethtool_ringparam
* ring
)
716 struct cpmac_priv
*priv
= netdev_priv(dev
);
718 ring
->rx_max_pending
= 1024;
719 ring
->rx_mini_max_pending
= 1;
720 ring
->rx_jumbo_max_pending
= 1;
721 ring
->tx_max_pending
= 1;
723 ring
->rx_pending
= priv
->ring_size
;
724 ring
->rx_mini_pending
= 1;
725 ring
->rx_jumbo_pending
= 1;
726 ring
->tx_pending
= 1;
729 static int cpmac_set_ringparam(struct net_device
*dev
, struct ethtool_ringparam
* ring
)
731 struct cpmac_priv
*priv
= netdev_priv(dev
);
733 if (dev
->flags
&& IFF_UP
)
735 priv
->ring_size
= ring
->rx_pending
;
739 static void cpmac_get_drvinfo(struct net_device
*dev
,
740 struct ethtool_drvinfo
*info
)
742 strcpy(info
->driver
, "cpmac");
743 strcpy(info
->version
, "0.0.3");
744 info
->fw_version
[0] = '\0';
745 sprintf(info
->bus_info
, "%s", "cpmac");
746 info
->regdump_len
= 0;
749 static const struct ethtool_ops cpmac_ethtool_ops
= {
750 .get_settings
= cpmac_get_settings
,
751 .set_settings
= cpmac_set_settings
,
752 .get_drvinfo
= cpmac_get_drvinfo
,
753 .get_link
= ethtool_op_get_link
,
754 .get_ringparam
= cpmac_get_ringparam
,
755 .set_ringparam
= cpmac_set_ringparam
,
758 static void cpmac_adjust_link(struct net_device
*dev
)
760 struct cpmac_priv
*priv
= netdev_priv(dev
);
764 spin_lock_irqsave(&priv
->lock
, flags
);
765 if (priv
->phy
->link
) {
766 if (priv
->phy
->duplex
!= priv
->oldduplex
) {
768 priv
->oldduplex
= priv
->phy
->duplex
;
771 if (priv
->phy
->speed
!= priv
->oldspeed
) {
773 priv
->oldspeed
= priv
->phy
->speed
;
776 if (!priv
->oldlink
) {
781 } else if (priv
->oldlink
) {
785 priv
->oldduplex
= -1;
788 if (new_state
&& netif_msg_link(priv
) && net_ratelimit())
789 phy_print_status(priv
->phy
);
791 spin_unlock_irqrestore(&priv
->lock
, flags
);
794 static void cpmac_hw_init(struct net_device
*dev
)
797 struct cpmac_priv
*priv
= netdev_priv(dev
);
799 for (i
= 0; i
< 8; i
++) {
800 cpmac_write(priv
->regs
, CPMAC_TX_PTR(i
), 0);
801 cpmac_write(priv
->regs
, CPMAC_RX_PTR(i
), 0);
803 cpmac_write(priv
->regs
, CPMAC_RX_PTR(0), priv
->rx_head
->mapping
);
805 cpmac_write(priv
->regs
, CPMAC_MBP
, MBP_RXSHORT
| MBP_RXBCAST
|
807 cpmac_write(priv
->regs
, CPMAC_UNICAST_ENABLE
, 1);
808 cpmac_write(priv
->regs
, CPMAC_UNICAST_CLEAR
, 0xfe);
809 cpmac_write(priv
->regs
, CPMAC_BUFFER_OFFSET
, 0);
810 for (i
= 0; i
< 8; i
++)
811 cpmac_write(priv
->regs
, CPMAC_MAC_ADDR_LO(i
), dev
->dev_addr
[5]);
812 cpmac_write(priv
->regs
, CPMAC_MAC_ADDR_MID
, dev
->dev_addr
[4]);
813 cpmac_write(priv
->regs
, CPMAC_MAC_ADDR_HI
, dev
->dev_addr
[0] |
814 (dev
->dev_addr
[1] << 8) | (dev
->dev_addr
[2] << 16) |
815 (dev
->dev_addr
[3] << 24));
816 cpmac_write(priv
->regs
, CPMAC_MAX_LENGTH
, CPMAC_SKB_SIZE
);
817 cpmac_write(priv
->regs
, CPMAC_RX_INT_CLEAR
, 0xff);
818 cpmac_write(priv
->regs
, CPMAC_TX_INT_CLEAR
, 0xff);
819 cpmac_write(priv
->regs
, CPMAC_MAC_INT_CLEAR
, 0xff);
820 cpmac_write(priv
->regs
, CPMAC_RX_INT_ENABLE
, 1);
821 cpmac_write(priv
->regs
, CPMAC_TX_INT_ENABLE
, 0xff);
822 cpmac_write(priv
->regs
, CPMAC_MAC_INT_ENABLE
, 3);
824 cpmac_write(priv
->regs
, CPMAC_RX_CONTROL
,
825 cpmac_read(priv
->regs
, CPMAC_RX_CONTROL
) | 1);
826 cpmac_write(priv
->regs
, CPMAC_TX_CONTROL
,
827 cpmac_read(priv
->regs
, CPMAC_TX_CONTROL
) | 1);
828 cpmac_write(priv
->regs
, CPMAC_MAC_CONTROL
,
829 cpmac_read(priv
->regs
, CPMAC_MAC_CONTROL
) | MAC_MII
|
832 priv
->phy
->state
= PHY_CHANGELINK
;
833 phy_start(priv
->phy
);
836 static int cpmac_open(struct net_device
*dev
)
839 struct cpmac_priv
*priv
= netdev_priv(dev
);
840 struct resource
*mem
;
841 struct cpmac_desc
*desc
;
844 priv
->phy
= phy_connect(dev
, priv
->phy_name
, &cpmac_adjust_link
,
845 0, PHY_INTERFACE_MODE_MII
);
846 if (IS_ERR(priv
->phy
)) {
847 if (netif_msg_drv(priv
))
848 printk(KERN_ERR
"%s: Could not attach to PHY\n",
850 return PTR_ERR(priv
->phy
);
853 mem
= platform_get_resource_byname(priv
->pdev
, IORESOURCE_MEM
, "regs");
854 if (!request_mem_region(mem
->start
, mem
->end
- mem
->start
, dev
->name
)) {
855 if (netif_msg_drv(priv
))
856 printk(KERN_ERR
"%s: failed to request registers\n",
862 priv
->regs
= ioremap(mem
->start
, mem
->end
- mem
->start
);
864 if (netif_msg_drv(priv
))
865 printk(KERN_ERR
"%s: failed to remap registers\n",
871 priv
->rx_head
= NULL
;
872 size
= priv
->ring_size
+ CPMAC_TX_RING_SIZE
;
873 priv
->desc_ring
= dma_alloc_coherent(&dev
->dev
,
874 sizeof(struct cpmac_desc
) * size
,
877 if (!priv
->desc_ring
) {
882 priv
->rx_head
= &priv
->desc_ring
[CPMAC_TX_RING_SIZE
];
883 for (i
= 0; i
< size
; i
++)
884 priv
->desc_ring
[i
].mapping
= priv
->dma_ring
+ sizeof(*desc
) * i
;
886 for (i
= 0, desc
= &priv
->rx_head
[i
]; i
< priv
->ring_size
; i
++, desc
++) {
887 skb
= netdev_alloc_skb(dev
, CPMAC_SKB_SIZE
);
888 if (unlikely(!skb
)) {
894 desc
->data_mapping
= dma_map_single(&dev
->dev
, skb
->data
,
897 desc
->hw_data
= (u32
)desc
->data_mapping
;
898 desc
->buflen
= CPMAC_SKB_SIZE
;
899 desc
->dataflags
= CPMAC_OWN
;
900 desc
->next
= &priv
->rx_head
[(i
+ 1) % priv
->ring_size
];
901 desc
->hw_next
= (u32
)desc
->next
->mapping
;
904 if ((res
= request_irq(dev
->irq
, cpmac_irq
, IRQF_SHARED
,
906 if (netif_msg_drv(priv
))
907 printk(KERN_ERR
"%s: failed to obtain irq\n",
915 netif_start_queue(dev
);
920 for (i
= 0; i
< priv
->ring_size
; i
++) {
921 if (priv
->rx_head
[i
].skb
) {
922 kfree_skb(priv
->rx_head
[i
].skb
);
923 dma_unmap_single(&dev
->dev
,
924 priv
->rx_head
[i
].data_mapping
,
930 kfree(priv
->desc_ring
);
934 release_mem_region(mem
->start
, mem
->end
- mem
->start
);
937 phy_disconnect(priv
->phy
);
942 static int cpmac_stop(struct net_device
*dev
)
945 struct cpmac_priv
*priv
= netdev_priv(dev
);
946 struct resource
*mem
;
948 netif_stop_queue(dev
);
951 phy_disconnect(priv
->phy
);
956 for (i
= 0; i
< 8; i
++)
957 cpmac_write(priv
->regs
, CPMAC_TX_PTR(i
), 0);
958 cpmac_write(priv
->regs
, CPMAC_RX_PTR(0), 0);
959 cpmac_write(priv
->regs
, CPMAC_MBP
, 0);
961 free_irq(dev
->irq
, dev
);
963 mem
= platform_get_resource_byname(priv
->pdev
, IORESOURCE_MEM
, "regs");
964 release_mem_region(mem
->start
, mem
->end
- mem
->start
);
965 priv
->rx_head
= &priv
->desc_ring
[CPMAC_TX_RING_SIZE
];
966 for (i
= 0; i
< priv
->ring_size
; i
++) {
967 if (priv
->rx_head
[i
].skb
) {
968 kfree_skb(priv
->rx_head
[i
].skb
);
969 dma_unmap_single(&dev
->dev
,
970 priv
->rx_head
[i
].data_mapping
,
976 dma_free_coherent(&dev
->dev
, sizeof(struct cpmac_desc
) *
977 (CPMAC_TX_RING_SIZE
+ priv
->ring_size
),
978 priv
->desc_ring
, priv
->dma_ring
);
982 static int external_switch
;
984 static int __devinit
cpmac_probe(struct platform_device
*pdev
)
987 struct resource
*mem
;
988 struct cpmac_priv
*priv
;
989 struct net_device
*dev
;
990 struct plat_cpmac_data
*pdata
;
992 pdata
= pdev
->dev
.platform_data
;
994 for (phy_id
= 0; phy_id
< PHY_MAX_ADDR
; phy_id
++) {
995 if (!(pdata
->phy_mask
& (1 << phy_id
)))
997 if (!cpmac_mii
.phy_map
[phy_id
])
1002 if (phy_id
== PHY_MAX_ADDR
) {
1003 if (external_switch
|| dumb_switch
)
1006 printk(KERN_ERR
"cpmac: no PHY present\n");
1011 dev
= alloc_etherdev(sizeof(struct cpmac_priv
));
1014 printk(KERN_ERR
"cpmac: Unable to allocate net_device\n");
1018 platform_set_drvdata(pdev
, dev
);
1019 priv
= netdev_priv(dev
);
1022 mem
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "regs");
1028 dev
->irq
= platform_get_irq_byname(pdev
, "irq");
1030 dev
->open
= cpmac_open
;
1031 dev
->stop
= cpmac_stop
;
1032 dev
->set_config
= cpmac_config
;
1033 dev
->hard_start_xmit
= cpmac_start_xmit
;
1034 dev
->do_ioctl
= cpmac_ioctl
;
1035 dev
->set_multicast_list
= cpmac_set_multicast_list
;
1036 dev
->tx_timeout
= cpmac_tx_timeout
;
1037 dev
->ethtool_ops
= &cpmac_ethtool_ops
;
1038 if (!disable_napi
) {
1039 dev
->poll
= cpmac_poll
;
1043 spin_lock_init(&priv
->lock
);
1044 priv
->msg_enable
= netif_msg_init(debug_level
, 0xff);
1045 priv
->ring_size
= 64;
1046 memcpy(dev
->dev_addr
, pdata
->dev_addr
, sizeof(dev
->dev_addr
));
1048 snprintf(priv
->phy_name
, BUS_ID_SIZE
, PHY_ID_FMT
,
1049 cpmac_mii
.id
, phy_id
);
1050 /* cpmac_write(cpmac_mii.priv, CPMAC_MDIO_PHYSEL(0), PHYSEL_LINKSEL
1051 | PHYSEL_LINKINT | phy_id);*/
1053 snprintf(priv
->phy_name
, BUS_ID_SIZE
, "fixed@%d:%d", 100, 1);
1055 if ((rc
= register_netdev(dev
))) {
1056 printk(KERN_ERR
"cpmac: error %i registering device %s\n", rc
,
1061 if (netif_msg_probe(priv
)) {
1063 "cpmac: device %s (regs: %p, irq: %d, phy: %s, mac: "
1064 MAC_FMT
")\n", dev
->name
, (void *)mem
->start
, dev
->irq
,
1065 priv
->phy_name
, MAC_ARG(dev
->dev_addr
));
1074 static int __devexit
cpmac_remove(struct platform_device
*pdev
)
1076 struct net_device
*dev
= platform_get_drvdata(pdev
);
1077 unregister_netdev(dev
);
1082 static struct platform_driver cpmac_driver
= {
1083 .driver
.name
= "cpmac",
1084 .probe
= cpmac_probe
,
1085 .remove
= __devexit_p(cpmac_remove
),
1088 int __devinit
cpmac_init(void)
1093 cpmac_mii
.priv
= ioremap(AR7_REGS_MDIO
, 256);
1095 if (!cpmac_mii
.priv
) {
1096 printk(KERN_ERR
"Can't ioremap mdio registers\n");
1100 #warning FIXME: unhardcode gpio&reset bits
1101 ar7_gpio_disable(26);
1102 ar7_gpio_disable(27);
1103 ar7_device_reset(AR7_RESET_BIT_CPMAC_LO
);
1104 ar7_device_reset(AR7_RESET_BIT_CPMAC_HI
);
1105 ar7_device_reset(AR7_RESET_BIT_EPHY
);
1107 cpmac_mii
.reset(&cpmac_mii
);
1109 for (i
= 0; i
< 300000; i
++)
1110 if ((mask
= cpmac_read(cpmac_mii
.priv
, CPMAC_MDIO_ALIVE
)))
1116 if (mask
& (mask
- 1)) {
1117 external_switch
= 1;
1121 cpmac_mii
.phy_mask
= ~(mask
| 0x80000000);
1123 res
= mdiobus_register(&cpmac_mii
);
1127 res
= platform_driver_register(&cpmac_driver
);
1134 mdiobus_unregister(&cpmac_mii
);
1137 iounmap(cpmac_mii
.priv
);
1142 void __devexit
cpmac_exit(void)
1144 platform_driver_unregister(&cpmac_driver
);
1145 mdiobus_unregister(&cpmac_mii
);
1146 iounmap(cpmac_mii
.priv
);
1149 module_init(cpmac_init
);
1150 module_exit(cpmac_exit
);