2 * Copyright (C) 2006, 2007 Eugene Konev
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/phy_fixed.h>
38 #include <linux/platform_device.h>
39 #include <linux/dma-mapping.h>
41 #include <asm/atomic.h>
43 MODULE_AUTHOR("Eugene Konev <ejka@imfi.kspu.ru>");
44 MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
45 MODULE_LICENSE("GPL");
46 MODULE_ALIAS("platform:cpmac");
48 static int debug_level = 8;
49 static int dumb_switch;
51 /* Next 2 are only used in cpmac_probe, so it's pointless to change them */
52 module_param(debug_level, int, 0444);
53 module_param(dumb_switch, int, 0444);
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 #define CPMAC_VERSION "0.5.0"
59 /* frame size + 802.1q tag */
60 #define CPMAC_SKB_SIZE (ETH_FRAME_LEN + 4)
61 #define CPMAC_QUEUES 8
63 /* Ethernet registers */
64 #define CPMAC_TX_CONTROL 0x0004
65 #define CPMAC_TX_TEARDOWN 0x0008
66 #define CPMAC_RX_CONTROL 0x0014
67 #define CPMAC_RX_TEARDOWN 0x0018
68 #define CPMAC_MBP 0x0100
69 # define MBP_RXPASSCRC 0x40000000
70 # define MBP_RXQOS 0x20000000
71 # define MBP_RXNOCHAIN 0x10000000
72 # define MBP_RXCMF 0x01000000
73 # define MBP_RXSHORT 0x00800000
74 # define MBP_RXCEF 0x00400000
75 # define MBP_RXPROMISC 0x00200000
76 # define MBP_PROMISCCHAN(channel) (((channel) & 0x7) << 16)
77 # define MBP_RXBCAST 0x00002000
78 # define MBP_BCASTCHAN(channel) (((channel) & 0x7) << 8)
79 # define MBP_RXMCAST 0x00000020
80 # define MBP_MCASTCHAN(channel) ((channel) & 0x7)
81 #define CPMAC_UNICAST_ENABLE 0x0104
82 #define CPMAC_UNICAST_CLEAR 0x0108
83 #define CPMAC_MAX_LENGTH 0x010c
84 #define CPMAC_BUFFER_OFFSET 0x0110
85 #define CPMAC_MAC_CONTROL 0x0160
86 # define MAC_TXPTYPE 0x00000200
87 # define MAC_TXPACE 0x00000040
88 # define MAC_MII 0x00000020
89 # define MAC_TXFLOW 0x00000010
90 # define MAC_RXFLOW 0x00000008
91 # define MAC_MTEST 0x00000004
92 # define MAC_LOOPBACK 0x00000002
93 # define MAC_FDX 0x00000001
94 #define CPMAC_MAC_STATUS 0x0164
95 # define MAC_STATUS_QOS 0x00000004
96 # define MAC_STATUS_RXFLOW 0x00000002
97 # define MAC_STATUS_TXFLOW 0x00000001
98 #define CPMAC_TX_INT_ENABLE 0x0178
99 #define CPMAC_TX_INT_CLEAR 0x017c
100 #define CPMAC_MAC_INT_VECTOR 0x0180
101 # define MAC_INT_STATUS 0x00080000
102 # define MAC_INT_HOST 0x00040000
103 # define MAC_INT_RX 0x00020000
104 # define MAC_INT_TX 0x00010000
105 #define CPMAC_MAC_EOI_VECTOR 0x0184
106 #define CPMAC_RX_INT_ENABLE 0x0198
107 #define CPMAC_RX_INT_CLEAR 0x019c
108 #define CPMAC_MAC_INT_ENABLE 0x01a8
109 #define CPMAC_MAC_INT_CLEAR 0x01ac
110 #define CPMAC_MAC_ADDR_LO(channel) (0x01b0 + (channel) * 4)
111 #define CPMAC_MAC_ADDR_MID 0x01d0
112 #define CPMAC_MAC_ADDR_HI 0x01d4
113 #define CPMAC_MAC_HASH_LO 0x01d8
114 #define CPMAC_MAC_HASH_HI 0x01dc
115 #define CPMAC_TX_PTR(channel) (0x0600 + (channel) * 4)
116 #define CPMAC_RX_PTR(channel) (0x0620 + (channel) * 4)
117 #define CPMAC_TX_ACK(channel) (0x0640 + (channel) * 4)
118 #define CPMAC_RX_ACK(channel) (0x0660 + (channel) * 4)
119 #define CPMAC_REG_END 0x0680
122 * TODO: use some of them to fill stats in cpmac_stats()
124 #define CPMAC_STATS_RX_GOOD 0x0200
125 #define CPMAC_STATS_RX_BCAST 0x0204
126 #define CPMAC_STATS_RX_MCAST 0x0208
127 #define CPMAC_STATS_RX_PAUSE 0x020c
128 #define CPMAC_STATS_RX_CRC 0x0210
129 #define CPMAC_STATS_RX_ALIGN 0x0214
130 #define CPMAC_STATS_RX_OVER 0x0218
131 #define CPMAC_STATS_RX_JABBER 0x021c
132 #define CPMAC_STATS_RX_UNDER 0x0220
133 #define CPMAC_STATS_RX_FRAG 0x0224
134 #define CPMAC_STATS_RX_FILTER 0x0228
135 #define CPMAC_STATS_RX_QOSFILTER 0x022c
136 #define CPMAC_STATS_RX_OCTETS 0x0230
138 #define CPMAC_STATS_TX_GOOD 0x0234
139 #define CPMAC_STATS_TX_BCAST 0x0238
140 #define CPMAC_STATS_TX_MCAST 0x023c
141 #define CPMAC_STATS_TX_PAUSE 0x0240
142 #define CPMAC_STATS_TX_DEFER 0x0244
143 #define CPMAC_STATS_TX_COLLISION 0x0248
144 #define CPMAC_STATS_TX_SINGLECOLL 0x024c
145 #define CPMAC_STATS_TX_MULTICOLL 0x0250
146 #define CPMAC_STATS_TX_EXCESSCOLL 0x0254
147 #define CPMAC_STATS_TX_LATECOLL 0x0258
148 #define CPMAC_STATS_TX_UNDERRUN 0x025c
149 #define CPMAC_STATS_TX_CARRIERSENSE 0x0260
150 #define CPMAC_STATS_TX_OCTETS 0x0264
152 #define cpmac_read(base, reg) (readl((void __iomem *)(base) + (reg)))
153 #define cpmac_write(base, reg, val) (writel(val, (void __iomem *)(base) + \
157 #define CPMAC_MDIO_VERSION 0x0000
158 #define CPMAC_MDIO_CONTROL 0x0004
159 # define MDIOC_IDLE 0x80000000
160 # define MDIOC_ENABLE 0x40000000
161 # define MDIOC_PREAMBLE 0x00100000
162 # define MDIOC_FAULT 0x00080000
163 # define MDIOC_FAULTDETECT 0x00040000
164 # define MDIOC_INTTEST 0x00020000
165 # define MDIOC_CLKDIV(div) ((div) & 0xff)
166 #define CPMAC_MDIO_ALIVE 0x0008
167 #define CPMAC_MDIO_LINK 0x000c
168 #define CPMAC_MDIO_ACCESS(channel) (0x0080 + (channel) * 8)
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)
174 #define CPMAC_MDIO_PHYSEL(channel) (0x0084 + (channel) * 8)
175 # define PHYSEL_LINKSEL 0x00000040
176 # define PHYSEL_LINKINT 0x00000020
185 #define CPMAC_SOP 0x8000
186 #define CPMAC_EOP 0x4000
187 #define CPMAC_OWN 0x2000
188 #define CPMAC_EOQ 0x1000
190 struct cpmac_desc *next;
191 struct cpmac_desc *prev;
193 dma_addr_t data_mapping;
199 struct cpmac_desc *rx_head;
201 struct cpmac_desc *desc_ring;
204 struct mii_bus *mii_bus;
205 struct phy_device *phy;
206 char phy_name[BUS_ID_SIZE];
207 int oldlink, oldspeed, oldduplex;
209 struct net_device *dev;
210 struct work_struct reset_work;
211 struct platform_device *pdev;
212 struct napi_struct napi;
213 atomic_t reset_pending;
216 static irqreturn_t cpmac_irq(int, void *);
217 static void cpmac_hw_start(struct net_device *dev);
218 static void cpmac_hw_stop(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_all_desc(struct net_device *dev)
249 struct cpmac_priv *priv = netdev_priv(dev);
250 struct cpmac_desc *dump = priv->rx_head;
252 cpmac_dump_desc(dev, dump);
254 } while (dump != priv->rx_head);
257 static void cpmac_dump_skb(struct net_device *dev, struct sk_buff *skb)
260 printk(KERN_DEBUG "%s: skb 0x%p, len=%d\n", dev->name, skb, skb->len);
261 for (i = 0; i < skb->len; i++) {
265 printk(KERN_DEBUG "%s: data[%p]:", dev->name,
268 printk(" %02x", ((u8 *)skb->data)[i]);
273 static int cpmac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
277 while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
279 cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_REG(reg) |
281 while ((val = cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0))) & MDIO_BUSY)
283 return MDIO_DATA(val);
286 static int cpmac_mdio_write(struct mii_bus *bus, int phy_id,
289 while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
291 cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_WRITE |
292 MDIO_REG(reg) | MDIO_PHY(phy_id) | MDIO_DATA(val));
296 static int cpmac_mdio_reset(struct mii_bus *bus)
298 ar7_device_reset(AR7_RESET_BIT_MDIO);
299 cpmac_write(bus->priv, CPMAC_MDIO_CONTROL, MDIOC_ENABLE |
300 MDIOC_CLKDIV(ar7_cpmac_freq() / 2200000 - 1));
304 static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, };
306 static struct mii_bus cpmac_mii = {
308 .read = cpmac_mdio_read,
309 .write = cpmac_mdio_write,
310 .reset = cpmac_mdio_reset,
314 static int cpmac_config(struct net_device *dev, struct ifmap *map)
316 if (dev->flags & IFF_UP)
319 /* Don't allow changing the I/O address */
320 if (map->base_addr != dev->base_addr)
323 /* ignore other fields */
327 static void cpmac_set_multicast_list(struct net_device *dev)
329 struct dev_mc_list *iter;
332 u32 mbp, bit, hash[2] = { 0, };
333 struct cpmac_priv *priv = netdev_priv(dev);
335 mbp = cpmac_read(priv->regs, CPMAC_MBP);
336 if (dev->flags & IFF_PROMISC) {
337 cpmac_write(priv->regs, CPMAC_MBP, (mbp & ~MBP_PROMISCCHAN(0)) |
340 cpmac_write(priv->regs, CPMAC_MBP, mbp & ~MBP_RXPROMISC);
341 if (dev->flags & IFF_ALLMULTI) {
342 /* enable all multicast mode */
343 cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, 0xffffffff);
344 cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, 0xffffffff);
347 * cpmac uses some strange mac address hashing
350 for (i = 0, iter = dev->mc_list; i < dev->mc_count;
351 i++, iter = iter->next) {
353 tmp = iter->dmi_addr[0];
354 bit ^= (tmp >> 2) ^ (tmp << 4);
355 tmp = iter->dmi_addr[1];
356 bit ^= (tmp >> 4) ^ (tmp << 2);
357 tmp = iter->dmi_addr[2];
358 bit ^= (tmp >> 6) ^ tmp;
359 tmp = iter->dmi_addr[3];
360 bit ^= (tmp >> 2) ^ (tmp << 4);
361 tmp = iter->dmi_addr[4];
362 bit ^= (tmp >> 4) ^ (tmp << 2);
363 tmp = iter->dmi_addr[5];
364 bit ^= (tmp >> 6) ^ tmp;
366 hash[bit / 32] |= 1 << (bit % 32);
369 cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, hash[0]);
370 cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, hash[1]);
375 static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
376 struct cpmac_desc *desc)
378 struct sk_buff *skb, *result = NULL;
380 if (unlikely(netif_msg_hw(priv)))
381 cpmac_dump_desc(priv->dev, desc);
382 cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
383 if (unlikely(!desc->datalen)) {
384 if (netif_msg_rx_err(priv) && net_ratelimit())
385 printk(KERN_WARNING "%s: rx: spurious interrupt\n",
390 skb = netdev_alloc_skb(priv->dev, CPMAC_SKB_SIZE);
393 skb_put(desc->skb, desc->datalen);
394 desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
395 desc->skb->ip_summed = CHECKSUM_NONE;
396 priv->dev->stats.rx_packets++;
397 priv->dev->stats.rx_bytes += desc->datalen;
399 dma_unmap_single(&priv->dev->dev, desc->data_mapping,
400 CPMAC_SKB_SIZE, DMA_FROM_DEVICE);
402 desc->data_mapping = dma_map_single(&priv->dev->dev, skb->data,
405 desc->hw_data = (u32)desc->data_mapping;
406 if (unlikely(netif_msg_pktdata(priv))) {
407 printk(KERN_DEBUG "%s: received packet:\n",
409 cpmac_dump_skb(priv->dev, result);
412 if (netif_msg_rx_err(priv) && net_ratelimit())
414 "%s: low on skbs, dropping packet\n",
416 priv->dev->stats.rx_dropped++;
419 desc->buflen = CPMAC_SKB_SIZE;
420 desc->dataflags = CPMAC_OWN;
425 static int cpmac_poll(struct napi_struct *napi, int budget)
428 struct cpmac_desc *desc, *restart;
429 struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
430 int received = 0, processed = 0;
432 spin_lock(&priv->rx_lock);
433 if (unlikely(!priv->rx_head)) {
434 if (netif_msg_rx_err(priv) && net_ratelimit())
435 printk(KERN_WARNING "%s: rx: polling, but no queue\n",
437 spin_unlock(&priv->rx_lock);
438 netif_rx_complete(priv->dev, napi);
442 desc = priv->rx_head;
444 while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
447 if ((desc->dataflags & CPMAC_EOQ) != 0) {
448 /* The last update to eoq->hw_next didn't happen
449 * soon enough, and the receiver stopped here.
450 *Remember this descriptor so we can restart
451 * the receiver after freeing some space.
453 if (unlikely(restart)) {
454 if (netif_msg_rx_err(priv))
455 printk(KERN_ERR "%s: poll found a"
456 " duplicate EOQ: %p and %p\n",
457 priv->dev->name, restart, desc);
461 restart = desc->next;
464 skb = cpmac_rx_one(priv, desc);
466 netif_receive_skb(skb);
472 if (desc != priv->rx_head) {
473 /* We freed some buffers, but not the whole ring,
474 * add what we did free to the rx list */
475 desc->prev->hw_next = (u32)0;
476 priv->rx_head->prev->hw_next = priv->rx_head->mapping;
479 /* Optimization: If we did not actually process an EOQ (perhaps because
480 * of quota limits), check to see if the tail of the queue has EOQ set.
481 * We should immediately restart in that case so that the receiver can
482 * restart and run in parallel with more packet processing.
483 * This lets us handle slightly larger bursts before running
484 * out of ring space (assuming dev->weight < ring_size) */
487 (priv->rx_head->prev->dataflags & (CPMAC_OWN|CPMAC_EOQ))
489 (priv->rx_head->dataflags & CPMAC_OWN) != 0) {
490 /* reset EOQ so the poll loop (above) doesn't try to
491 * restart this when it eventually gets to this descriptor.
493 priv->rx_head->prev->dataflags &= ~CPMAC_EOQ;
494 restart = priv->rx_head;
498 priv->dev->stats.rx_errors++;
499 priv->dev->stats.rx_fifo_errors++;
500 if (netif_msg_rx_err(priv) && net_ratelimit())
501 printk(KERN_WARNING "%s: rx dma ring overrun\n",
504 if (unlikely((restart->dataflags & CPMAC_OWN) == 0)) {
505 if (netif_msg_drv(priv))
506 printk(KERN_ERR "%s: cpmac_poll is trying to "
507 "restart rx from a descriptor that's "
509 priv->dev->name, restart);
513 cpmac_write(priv->regs, CPMAC_RX_PTR(0), restart->mapping);
516 priv->rx_head = desc;
517 spin_unlock(&priv->rx_lock);
518 if (unlikely(netif_msg_rx_status(priv)))
519 printk(KERN_DEBUG "%s: poll processed %d packets\n",
520 priv->dev->name, received);
521 if (processed == 0) {
522 /* we ran out of packets to read,
523 * revert to interrupt-driven mode */
524 netif_rx_complete(priv->dev, napi);
525 cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
532 /* Something went horribly wrong.
533 * Reset hardware to try to recover rather than wedging. */
535 if (netif_msg_drv(priv)) {
536 printk(KERN_ERR "%s: cpmac_poll is confused. "
537 "Resetting hardware\n", priv->dev->name);
538 cpmac_dump_all_desc(priv->dev);
539 printk(KERN_DEBUG "%s: RX_PTR(0)=0x%08x RX_ACK(0)=0x%08x\n",
541 cpmac_read(priv->regs, CPMAC_RX_PTR(0)),
542 cpmac_read(priv->regs, CPMAC_RX_ACK(0)));
545 spin_unlock(&priv->rx_lock);
546 netif_rx_complete(priv->dev, napi);
547 netif_tx_stop_all_queues(priv->dev);
548 napi_disable(&priv->napi);
550 atomic_inc(&priv->reset_pending);
551 cpmac_hw_stop(priv->dev);
552 if (!schedule_work(&priv->reset_work))
553 atomic_dec(&priv->reset_pending);
558 static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
561 struct cpmac_desc *desc;
562 struct cpmac_priv *priv = netdev_priv(dev);
564 if (unlikely(atomic_read(&priv->reset_pending)))
565 return NETDEV_TX_BUSY;
567 if (unlikely(skb_padto(skb, ETH_ZLEN)))
570 len = max(skb->len, ETH_ZLEN);
571 queue = skb_get_queue_mapping(skb);
572 netif_stop_subqueue(dev, queue);
574 desc = &priv->desc_ring[queue];
575 if (unlikely(desc->dataflags & CPMAC_OWN)) {
576 if (netif_msg_tx_err(priv) && net_ratelimit())
577 printk(KERN_WARNING "%s: tx dma ring full\n",
579 return NETDEV_TX_BUSY;
582 spin_lock(&priv->lock);
583 dev->trans_start = jiffies;
584 spin_unlock(&priv->lock);
585 desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
587 desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
589 desc->hw_data = (u32)desc->data_mapping;
592 if (unlikely(netif_msg_tx_queued(priv)))
593 printk(KERN_DEBUG "%s: sending 0x%p, len=%d\n", dev->name, skb,
595 if (unlikely(netif_msg_hw(priv)))
596 cpmac_dump_desc(dev, desc);
597 if (unlikely(netif_msg_pktdata(priv)))
598 cpmac_dump_skb(dev, skb);
599 cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
604 static void cpmac_end_xmit(struct net_device *dev, int queue)
606 struct cpmac_desc *desc;
607 struct cpmac_priv *priv = netdev_priv(dev);
609 desc = &priv->desc_ring[queue];
610 cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
611 if (likely(desc->skb)) {
612 spin_lock(&priv->lock);
613 dev->stats.tx_packets++;
614 dev->stats.tx_bytes += desc->skb->len;
615 spin_unlock(&priv->lock);
616 dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
619 if (unlikely(netif_msg_tx_done(priv)))
620 printk(KERN_DEBUG "%s: sent 0x%p, len=%d\n", dev->name,
621 desc->skb, desc->skb->len);
623 dev_kfree_skb_irq(desc->skb);
625 if (netif_subqueue_stopped(dev, queue))
626 netif_wake_subqueue(dev, queue);
628 if (netif_msg_tx_err(priv) && net_ratelimit())
630 "%s: end_xmit: spurious interrupt\n", dev->name);
631 if (netif_subqueue_stopped(dev, queue))
632 netif_wake_subqueue(dev, queue);
636 static void cpmac_hw_stop(struct net_device *dev)
639 struct cpmac_priv *priv = netdev_priv(dev);
640 struct plat_cpmac_data *pdata = priv->pdev->dev.platform_data;
642 ar7_device_reset(pdata->reset_bit);
643 cpmac_write(priv->regs, CPMAC_RX_CONTROL,
644 cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
645 cpmac_write(priv->regs, CPMAC_TX_CONTROL,
646 cpmac_read(priv->regs, CPMAC_TX_CONTROL) & ~1);
647 for (i = 0; i < 8; i++) {
648 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
649 cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
651 cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
652 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
653 cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
654 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
655 cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
656 cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
659 static void cpmac_hw_start(struct net_device *dev)
662 struct cpmac_priv *priv = netdev_priv(dev);
663 struct plat_cpmac_data *pdata = priv->pdev->dev.platform_data;
665 ar7_device_reset(pdata->reset_bit);
666 for (i = 0; i < 8; i++) {
667 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
668 cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
670 cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
672 cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
674 cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
675 for (i = 0; i < 8; i++)
676 cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
677 cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
678 cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
679 (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
680 (dev->dev_addr[3] << 24));
681 cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
682 cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
683 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
684 cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
685 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
686 cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
687 cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
688 cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
689 cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
691 cpmac_write(priv->regs, CPMAC_RX_CONTROL,
692 cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
693 cpmac_write(priv->regs, CPMAC_TX_CONTROL,
694 cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
695 cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
696 cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
700 static void cpmac_clear_rx(struct net_device *dev)
702 struct cpmac_priv *priv = netdev_priv(dev);
703 struct cpmac_desc *desc;
705 if (unlikely(!priv->rx_head))
707 desc = priv->rx_head;
708 for (i = 0; i < priv->ring_size; i++) {
709 if ((desc->dataflags & CPMAC_OWN) == 0) {
710 if (netif_msg_rx_err(priv) && net_ratelimit())
711 printk(KERN_WARNING "%s: packet dropped\n",
713 if (unlikely(netif_msg_hw(priv)))
714 cpmac_dump_desc(dev, desc);
715 desc->dataflags = CPMAC_OWN;
716 dev->stats.rx_dropped++;
718 desc->hw_next = desc->next->mapping;
721 priv->rx_head->prev->hw_next = 0;
724 static void cpmac_clear_tx(struct net_device *dev)
726 struct cpmac_priv *priv = netdev_priv(dev);
728 if (unlikely(!priv->desc_ring))
730 for (i = 0; i < CPMAC_QUEUES; i++) {
731 priv->desc_ring[i].dataflags = 0;
732 if (priv->desc_ring[i].skb) {
733 dev_kfree_skb_any(priv->desc_ring[i].skb);
734 priv->desc_ring[i].skb = NULL;
739 static void cpmac_hw_error(struct work_struct *work)
742 struct cpmac_priv *priv =
743 container_of(work, struct cpmac_priv, reset_work);
745 spin_lock(&priv->rx_lock);
746 cpmac_clear_rx(priv->dev);
747 spin_unlock(&priv->rx_lock);
748 cpmac_clear_tx(priv->dev);
749 cpmac_hw_start(priv->dev);
751 atomic_dec(&priv->reset_pending);
753 netif_tx_wake_all_queues(priv->dev);
754 cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
757 static void cpmac_check_status(struct net_device *dev)
759 struct cpmac_priv *priv = netdev_priv(dev);
761 u32 macstatus = cpmac_read(priv->regs, CPMAC_MAC_STATUS);
762 int rx_channel = (macstatus >> 8) & 7;
763 int rx_code = (macstatus >> 12) & 15;
764 int tx_channel = (macstatus >> 16) & 7;
765 int tx_code = (macstatus >> 20) & 15;
767 if (rx_code || tx_code) {
768 if (netif_msg_drv(priv) && net_ratelimit()) {
769 /* Can't find any documentation on what these
770 *error codes actually are. So just log them and hope..
773 printk(KERN_WARNING "%s: host error %d on rx "
774 "channel %d (macstatus %08x), resetting\n",
775 dev->name, rx_code, rx_channel, macstatus);
777 printk(KERN_WARNING "%s: host error %d on tx "
778 "channel %d (macstatus %08x), resetting\n",
779 dev->name, tx_code, tx_channel, macstatus);
782 netif_tx_stop_all_queues(dev);
784 if (schedule_work(&priv->reset_work))
785 atomic_inc(&priv->reset_pending);
786 if (unlikely(netif_msg_hw(priv)))
787 cpmac_dump_regs(dev);
789 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
792 static irqreturn_t cpmac_irq(int irq, void *dev_id)
794 struct net_device *dev = dev_id;
795 struct cpmac_priv *priv;
799 priv = netdev_priv(dev);
801 status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
803 if (unlikely(netif_msg_intr(priv)))
804 printk(KERN_DEBUG "%s: interrupt status: 0x%08x\n", dev->name,
807 if (status & MAC_INT_TX)
808 cpmac_end_xmit(dev, (status & 7));
810 if (status & MAC_INT_RX) {
811 queue = (status >> 8) & 7;
812 if (netif_rx_schedule_prep(dev, &priv->napi)) {
813 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
814 __netif_rx_schedule(dev, &priv->napi);
818 cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
820 if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
821 cpmac_check_status(dev);
826 static void cpmac_tx_timeout(struct net_device *dev)
829 struct cpmac_priv *priv = netdev_priv(dev);
831 spin_lock(&priv->lock);
832 dev->stats.tx_errors++;
833 spin_unlock(&priv->lock);
834 if (netif_msg_tx_err(priv) && net_ratelimit())
835 printk(KERN_WARNING "%s: transmit timeout\n", dev->name);
837 atomic_inc(&priv->reset_pending);
841 atomic_dec(&priv->reset_pending);
843 netif_tx_wake_all_queues(priv->dev);
846 static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
848 struct cpmac_priv *priv = netdev_priv(dev);
849 if (!(netif_running(dev)))
853 if ((cmd == SIOCGMIIPHY) || (cmd == SIOCGMIIREG) ||
854 (cmd == SIOCSMIIREG))
855 return phy_mii_ioctl(priv->phy, if_mii(ifr), cmd);
860 static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
862 struct cpmac_priv *priv = netdev_priv(dev);
865 return phy_ethtool_gset(priv->phy, cmd);
870 static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
872 struct cpmac_priv *priv = netdev_priv(dev);
874 if (!capable(CAP_NET_ADMIN))
878 return phy_ethtool_sset(priv->phy, cmd);
883 static void cpmac_get_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
885 struct cpmac_priv *priv = netdev_priv(dev);
887 ring->rx_max_pending = 1024;
888 ring->rx_mini_max_pending = 1;
889 ring->rx_jumbo_max_pending = 1;
890 ring->tx_max_pending = 1;
892 ring->rx_pending = priv->ring_size;
893 ring->rx_mini_pending = 1;
894 ring->rx_jumbo_pending = 1;
895 ring->tx_pending = 1;
898 static int cpmac_set_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
900 struct cpmac_priv *priv = netdev_priv(dev);
902 if (netif_running(dev))
904 priv->ring_size = ring->rx_pending;
908 static void cpmac_get_drvinfo(struct net_device *dev,
909 struct ethtool_drvinfo *info)
911 strcpy(info->driver, "cpmac");
912 strcpy(info->version, CPMAC_VERSION);
913 info->fw_version[0] = '\0';
914 sprintf(info->bus_info, "%s", "cpmac");
915 info->regdump_len = 0;
918 static const struct ethtool_ops cpmac_ethtool_ops = {
919 .get_settings = cpmac_get_settings,
920 .set_settings = cpmac_set_settings,
921 .get_drvinfo = cpmac_get_drvinfo,
922 .get_link = ethtool_op_get_link,
923 .get_ringparam = cpmac_get_ringparam,
924 .set_ringparam = cpmac_set_ringparam,
927 static void cpmac_adjust_link(struct net_device *dev)
929 struct cpmac_priv *priv = netdev_priv(dev);
932 spin_lock(&priv->lock);
933 if (priv->phy->link) {
934 netif_tx_start_all_queues(dev);
935 if (priv->phy->duplex != priv->oldduplex) {
937 priv->oldduplex = priv->phy->duplex;
940 if (priv->phy->speed != priv->oldspeed) {
942 priv->oldspeed = priv->phy->speed;
945 if (!priv->oldlink) {
949 } else if (priv->oldlink) {
953 priv->oldduplex = -1;
956 if (new_state && netif_msg_link(priv) && net_ratelimit())
957 phy_print_status(priv->phy);
959 spin_unlock(&priv->lock);
962 static int cpmac_open(struct net_device *dev)
965 struct cpmac_priv *priv = netdev_priv(dev);
966 struct resource *mem;
967 struct cpmac_desc *desc;
970 mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
971 if (!request_mem_region(mem->start, mem->end - mem->start, dev->name)) {
972 if (netif_msg_drv(priv))
973 printk(KERN_ERR "%s: failed to request registers\n",
979 priv->regs = ioremap(mem->start, mem->end - mem->start);
981 if (netif_msg_drv(priv))
982 printk(KERN_ERR "%s: failed to remap registers\n",
988 size = priv->ring_size + CPMAC_QUEUES;
989 priv->desc_ring = dma_alloc_coherent(&dev->dev,
990 sizeof(struct cpmac_desc) * size,
993 if (!priv->desc_ring) {
998 for (i = 0; i < size; i++)
999 priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
1001 priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
1002 for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
1003 skb = netdev_alloc_skb(dev, CPMAC_SKB_SIZE);
1004 if (unlikely(!skb)) {
1008 skb_reserve(skb, 2);
1010 desc->data_mapping = dma_map_single(&dev->dev, skb->data,
1013 desc->hw_data = (u32)desc->data_mapping;
1014 desc->buflen = CPMAC_SKB_SIZE;
1015 desc->dataflags = CPMAC_OWN;
1016 desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
1017 desc->next->prev = desc;
1018 desc->hw_next = (u32)desc->next->mapping;
1021 priv->rx_head->prev->hw_next = (u32)0;
1023 if ((res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED,
1025 if (netif_msg_drv(priv))
1026 printk(KERN_ERR "%s: failed to obtain irq\n",
1031 atomic_set(&priv->reset_pending, 0);
1032 INIT_WORK(&priv->reset_work, cpmac_hw_error);
1033 cpmac_hw_start(dev);
1035 napi_enable(&priv->napi);
1036 priv->phy->state = PHY_CHANGELINK;
1037 phy_start(priv->phy);
1043 for (i = 0; i < priv->ring_size; i++) {
1044 if (priv->rx_head[i].skb) {
1045 dma_unmap_single(&dev->dev,
1046 priv->rx_head[i].data_mapping,
1049 kfree_skb(priv->rx_head[i].skb);
1053 kfree(priv->desc_ring);
1054 iounmap(priv->regs);
1057 release_mem_region(mem->start, mem->end - mem->start);
1063 static int cpmac_stop(struct net_device *dev)
1066 struct cpmac_priv *priv = netdev_priv(dev);
1067 struct resource *mem;
1069 netif_tx_stop_all_queues(dev);
1071 cancel_work_sync(&priv->reset_work);
1072 napi_disable(&priv->napi);
1073 phy_stop(priv->phy);
1077 for (i = 0; i < 8; i++)
1078 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
1079 cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
1080 cpmac_write(priv->regs, CPMAC_MBP, 0);
1082 free_irq(dev->irq, dev);
1083 iounmap(priv->regs);
1084 mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
1085 release_mem_region(mem->start, mem->end - mem->start);
1086 priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
1087 for (i = 0; i < priv->ring_size; i++) {
1088 if (priv->rx_head[i].skb) {
1089 dma_unmap_single(&dev->dev,
1090 priv->rx_head[i].data_mapping,
1093 kfree_skb(priv->rx_head[i].skb);
1097 dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
1098 (CPMAC_QUEUES + priv->ring_size),
1099 priv->desc_ring, priv->dma_ring);
1103 static int external_switch;
1105 static int __devinit cpmac_probe(struct platform_device *pdev)
1108 char *mdio_bus_id = "0";
1109 struct resource *mem;
1110 struct cpmac_priv *priv;
1111 struct net_device *dev;
1112 struct plat_cpmac_data *pdata;
1113 DECLARE_MAC_BUF(mac);
1115 pdata = pdev->dev.platform_data;
1117 for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
1118 if (!(pdata->phy_mask & (1 << phy_id)))
1120 if (!cpmac_mii.phy_map[phy_id])
1125 if (phy_id == PHY_MAX_ADDR) {
1126 if (external_switch || dumb_switch) {
1127 mdio_bus_id = 0; /* fixed phys bus */
1130 dev_err(&pdev->dev, "no PHY present\n");
1135 dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
1138 printk(KERN_ERR "cpmac: Unable to allocate net_device\n");
1142 platform_set_drvdata(pdev, dev);
1143 priv = netdev_priv(dev);
1146 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
1152 dev->irq = platform_get_irq_byname(pdev, "irq");
1154 dev->open = cpmac_open;
1155 dev->stop = cpmac_stop;
1156 dev->set_config = cpmac_config;
1157 dev->hard_start_xmit = cpmac_start_xmit;
1158 dev->do_ioctl = cpmac_ioctl;
1159 dev->set_multicast_list = cpmac_set_multicast_list;
1160 dev->tx_timeout = cpmac_tx_timeout;
1161 dev->ethtool_ops = &cpmac_ethtool_ops;
1163 netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
1165 spin_lock_init(&priv->lock);
1166 spin_lock_init(&priv->rx_lock);
1168 priv->ring_size = 64;
1169 priv->msg_enable = netif_msg_init(debug_level, 0xff);
1170 memcpy(dev->dev_addr, pdata->dev_addr, sizeof(dev->dev_addr));
1172 priv->phy = phy_connect(dev, cpmac_mii.phy_map[phy_id]->dev.bus_id,
1173 &cpmac_adjust_link, 0, PHY_INTERFACE_MODE_MII);
1174 if (IS_ERR(priv->phy)) {
1175 if (netif_msg_drv(priv))
1176 printk(KERN_ERR "%s: Could not attach to PHY\n",
1178 return PTR_ERR(priv->phy);
1181 if ((rc = register_netdev(dev))) {
1182 printk(KERN_ERR "cpmac: error %i registering device %s\n", rc,
1187 if (netif_msg_probe(priv)) {
1189 "cpmac: device %s (regs: %p, irq: %d, phy: %s, "
1190 "mac: %s)\n", dev->name, (void *)mem->start, dev->irq,
1191 priv->phy_name, print_mac(mac, dev->dev_addr));
1200 static int __devexit cpmac_remove(struct platform_device *pdev)
1202 struct net_device *dev = platform_get_drvdata(pdev);
1203 unregister_netdev(dev);
1208 static struct platform_driver cpmac_driver = {
1209 .driver.name = "cpmac",
1210 .driver.owner = THIS_MODULE,
1211 .probe = cpmac_probe,
1212 .remove = __devexit_p(cpmac_remove),
1215 int __devinit cpmac_init(void)
1220 cpmac_mii.priv = ioremap(AR7_REGS_MDIO, 256);
1222 if (!cpmac_mii.priv) {
1223 printk(KERN_ERR "Can't ioremap mdio registers\n");
1227 #warning FIXME: unhardcode gpio&reset bits
1228 ar7_gpio_disable(26);
1229 ar7_gpio_disable(27);
1230 ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
1231 ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
1232 ar7_device_reset(AR7_RESET_BIT_EPHY);
1234 cpmac_mii.reset(&cpmac_mii);
1236 for (i = 0; i < 300000; i++)
1237 if ((mask = cpmac_read(cpmac_mii.priv, CPMAC_MDIO_ALIVE)))
1243 if (mask & (mask - 1)) {
1244 external_switch = 1;
1248 cpmac_mii.phy_mask = ~(mask | 0x80000000);
1249 snprintf(cpmac_mii.id, MII_BUS_ID_SIZE, "0");
1251 res = mdiobus_register(&cpmac_mii);
1255 res = platform_driver_register(&cpmac_driver);
1262 mdiobus_unregister(&cpmac_mii);
1265 iounmap(cpmac_mii.priv);
1270 void __devexit cpmac_exit(void)
1272 platform_driver_unregister(&cpmac_driver);
1273 mdiobus_unregister(&cpmac_mii);
1274 iounmap(cpmac_mii.priv);
1277 module_init(cpmac_init);
1278 module_exit(cpmac_exit);