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/interrupt.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>
30 #include <linux/netdevice.h>
31 #include <linux/if_vlan.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>
40 #include <linux/clk.h>
41 #include <linux/gpio.h>
42 #include <linux/atomic.h>
44 #include <asm/mach-ar7/ar7.h>
46 MODULE_AUTHOR("Eugene Konev <ejka@imfi.kspu.ru>");
47 MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
48 MODULE_LICENSE("GPL");
49 MODULE_ALIAS("platform:cpmac");
51 static int debug_level = 8;
52 static int dumb_switch;
54 /* Next 2 are only used in cpmac_probe, so it's pointless to change them */
55 module_param(debug_level, int, 0444);
56 module_param(dumb_switch, int, 0444);
58 MODULE_PARM_DESC(debug_level, "Number of NETIF_MSG bits to enable");
59 MODULE_PARM_DESC(dumb_switch, "Assume switch is not connected to MDIO bus");
61 #define CPMAC_VERSION "0.5.2"
62 /* frame size + 802.1q tag + FCS size */
63 #define CPMAC_SKB_SIZE (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN)
64 #define CPMAC_QUEUES 8
66 /* Ethernet registers */
67 #define CPMAC_TX_CONTROL 0x0004
68 #define CPMAC_TX_TEARDOWN 0x0008
69 #define CPMAC_RX_CONTROL 0x0014
70 #define CPMAC_RX_TEARDOWN 0x0018
71 #define CPMAC_MBP 0x0100
72 #define MBP_RXPASSCRC 0x40000000
73 #define MBP_RXQOS 0x20000000
74 #define MBP_RXNOCHAIN 0x10000000
75 #define MBP_RXCMF 0x01000000
76 #define MBP_RXSHORT 0x00800000
77 #define MBP_RXCEF 0x00400000
78 #define MBP_RXPROMISC 0x00200000
79 #define MBP_PROMISCCHAN(channel) (((channel) & 0x7) << 16)
80 #define MBP_RXBCAST 0x00002000
81 #define MBP_BCASTCHAN(channel) (((channel) & 0x7) << 8)
82 #define MBP_RXMCAST 0x00000020
83 #define MBP_MCASTCHAN(channel) ((channel) & 0x7)
84 #define CPMAC_UNICAST_ENABLE 0x0104
85 #define CPMAC_UNICAST_CLEAR 0x0108
86 #define CPMAC_MAX_LENGTH 0x010c
87 #define CPMAC_BUFFER_OFFSET 0x0110
88 #define CPMAC_MAC_CONTROL 0x0160
89 #define MAC_TXPTYPE 0x00000200
90 #define MAC_TXPACE 0x00000040
91 #define MAC_MII 0x00000020
92 #define MAC_TXFLOW 0x00000010
93 #define MAC_RXFLOW 0x00000008
94 #define MAC_MTEST 0x00000004
95 #define MAC_LOOPBACK 0x00000002
96 #define MAC_FDX 0x00000001
97 #define CPMAC_MAC_STATUS 0x0164
98 #define MAC_STATUS_QOS 0x00000004
99 #define MAC_STATUS_RXFLOW 0x00000002
100 #define MAC_STATUS_TXFLOW 0x00000001
101 #define CPMAC_TX_INT_ENABLE 0x0178
102 #define CPMAC_TX_INT_CLEAR 0x017c
103 #define CPMAC_MAC_INT_VECTOR 0x0180
104 #define MAC_INT_STATUS 0x00080000
105 #define MAC_INT_HOST 0x00040000
106 #define MAC_INT_RX 0x00020000
107 #define MAC_INT_TX 0x00010000
108 #define CPMAC_MAC_EOI_VECTOR 0x0184
109 #define CPMAC_RX_INT_ENABLE 0x0198
110 #define CPMAC_RX_INT_CLEAR 0x019c
111 #define CPMAC_MAC_INT_ENABLE 0x01a8
112 #define CPMAC_MAC_INT_CLEAR 0x01ac
113 #define CPMAC_MAC_ADDR_LO(channel) (0x01b0 + (channel) * 4)
114 #define CPMAC_MAC_ADDR_MID 0x01d0
115 #define CPMAC_MAC_ADDR_HI 0x01d4
116 #define CPMAC_MAC_HASH_LO 0x01d8
117 #define CPMAC_MAC_HASH_HI 0x01dc
118 #define CPMAC_TX_PTR(channel) (0x0600 + (channel) * 4)
119 #define CPMAC_RX_PTR(channel) (0x0620 + (channel) * 4)
120 #define CPMAC_TX_ACK(channel) (0x0640 + (channel) * 4)
121 #define CPMAC_RX_ACK(channel) (0x0660 + (channel) * 4)
122 #define CPMAC_REG_END 0x0680
125 * TODO: use some of them to fill stats in cpmac_stats()
127 #define CPMAC_STATS_RX_GOOD 0x0200
128 #define CPMAC_STATS_RX_BCAST 0x0204
129 #define CPMAC_STATS_RX_MCAST 0x0208
130 #define CPMAC_STATS_RX_PAUSE 0x020c
131 #define CPMAC_STATS_RX_CRC 0x0210
132 #define CPMAC_STATS_RX_ALIGN 0x0214
133 #define CPMAC_STATS_RX_OVER 0x0218
134 #define CPMAC_STATS_RX_JABBER 0x021c
135 #define CPMAC_STATS_RX_UNDER 0x0220
136 #define CPMAC_STATS_RX_FRAG 0x0224
137 #define CPMAC_STATS_RX_FILTER 0x0228
138 #define CPMAC_STATS_RX_QOSFILTER 0x022c
139 #define CPMAC_STATS_RX_OCTETS 0x0230
141 #define CPMAC_STATS_TX_GOOD 0x0234
142 #define CPMAC_STATS_TX_BCAST 0x0238
143 #define CPMAC_STATS_TX_MCAST 0x023c
144 #define CPMAC_STATS_TX_PAUSE 0x0240
145 #define CPMAC_STATS_TX_DEFER 0x0244
146 #define CPMAC_STATS_TX_COLLISION 0x0248
147 #define CPMAC_STATS_TX_SINGLECOLL 0x024c
148 #define CPMAC_STATS_TX_MULTICOLL 0x0250
149 #define CPMAC_STATS_TX_EXCESSCOLL 0x0254
150 #define CPMAC_STATS_TX_LATECOLL 0x0258
151 #define CPMAC_STATS_TX_UNDERRUN 0x025c
152 #define CPMAC_STATS_TX_CARRIERSENSE 0x0260
153 #define CPMAC_STATS_TX_OCTETS 0x0264
155 #define cpmac_read(base, reg) (readl((void __iomem *)(base) + (reg)))
156 #define cpmac_write(base, reg, val) (writel(val, (void __iomem *)(base) + \
160 #define CPMAC_MDIO_VERSION 0x0000
161 #define CPMAC_MDIO_CONTROL 0x0004
162 #define MDIOC_IDLE 0x80000000
163 #define MDIOC_ENABLE 0x40000000
164 #define MDIOC_PREAMBLE 0x00100000
165 #define MDIOC_FAULT 0x00080000
166 #define MDIOC_FAULTDETECT 0x00040000
167 #define MDIOC_INTTEST 0x00020000
168 #define MDIOC_CLKDIV(div) ((div) & 0xff)
169 #define CPMAC_MDIO_ALIVE 0x0008
170 #define CPMAC_MDIO_LINK 0x000c
171 #define CPMAC_MDIO_ACCESS(channel) (0x0080 + (channel) * 8)
172 #define MDIO_BUSY 0x80000000
173 #define MDIO_WRITE 0x40000000
174 #define MDIO_REG(reg) (((reg) & 0x1f) << 21)
175 #define MDIO_PHY(phy) (((phy) & 0x1f) << 16)
176 #define MDIO_DATA(data) ((data) & 0xffff)
177 #define CPMAC_MDIO_PHYSEL(channel) (0x0084 + (channel) * 8)
178 #define PHYSEL_LINKSEL 0x00000040
179 #define PHYSEL_LINKINT 0x00000020
188 #define CPMAC_SOP 0x8000
189 #define CPMAC_EOP 0x4000
190 #define CPMAC_OWN 0x2000
191 #define CPMAC_EOQ 0x1000
193 struct cpmac_desc *next;
194 struct cpmac_desc *prev;
196 dma_addr_t data_mapping;
202 struct cpmac_desc *rx_head;
204 struct cpmac_desc *desc_ring;
207 struct mii_bus *mii_bus;
208 struct phy_device *phy;
209 char phy_name[MII_BUS_ID_SIZE + 3];
210 int oldlink, oldspeed, oldduplex;
212 struct net_device *dev;
213 struct work_struct reset_work;
214 struct platform_device *pdev;
215 struct napi_struct napi;
216 atomic_t reset_pending;
219 static irqreturn_t cpmac_irq(int, void *);
220 static void cpmac_hw_start(struct net_device *dev);
221 static void cpmac_hw_stop(struct net_device *dev);
222 static int cpmac_stop(struct net_device *dev);
223 static int cpmac_open(struct net_device *dev);
225 static void cpmac_dump_regs(struct net_device *dev)
228 struct cpmac_priv *priv = netdev_priv(dev);
230 for (i = 0; i < CPMAC_REG_END; i += 4) {
234 printk("%s: reg[%p]:", dev->name, priv->regs + i);
236 printk(" %08x", cpmac_read(priv->regs, i));
241 static void cpmac_dump_desc(struct net_device *dev, struct cpmac_desc *desc)
245 printk("%s: desc[%p]:", dev->name, desc);
246 for (i = 0; i < sizeof(*desc) / 4; i++)
247 printk(" %08x", ((u32 *)desc)[i]);
251 static void cpmac_dump_all_desc(struct net_device *dev)
253 struct cpmac_priv *priv = netdev_priv(dev);
254 struct cpmac_desc *dump = priv->rx_head;
257 cpmac_dump_desc(dev, dump);
259 } while (dump != priv->rx_head);
262 static void cpmac_dump_skb(struct net_device *dev, struct sk_buff *skb)
266 printk("%s: skb 0x%p, len=%d\n", dev->name, skb, skb->len);
267 for (i = 0; i < skb->len; i++) {
271 printk("%s: data[%p]:", dev->name, skb->data + i);
273 printk(" %02x", ((u8 *)skb->data)[i]);
278 static int cpmac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
282 while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
284 cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_REG(reg) |
286 while ((val = cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0))) & MDIO_BUSY)
289 return MDIO_DATA(val);
292 static int cpmac_mdio_write(struct mii_bus *bus, int phy_id,
295 while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
297 cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_WRITE |
298 MDIO_REG(reg) | MDIO_PHY(phy_id) | MDIO_DATA(val));
303 static int cpmac_mdio_reset(struct mii_bus *bus)
305 struct clk *cpmac_clk;
307 cpmac_clk = clk_get(&bus->dev, "cpmac");
308 if (IS_ERR(cpmac_clk)) {
309 pr_err("unable to get cpmac clock\n");
312 ar7_device_reset(AR7_RESET_BIT_MDIO);
313 cpmac_write(bus->priv, CPMAC_MDIO_CONTROL, MDIOC_ENABLE |
314 MDIOC_CLKDIV(clk_get_rate(cpmac_clk) / 2200000 - 1));
319 static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, };
321 static struct mii_bus *cpmac_mii;
323 static void cpmac_set_multicast_list(struct net_device *dev)
325 struct netdev_hw_addr *ha;
327 u32 mbp, bit, hash[2] = { 0, };
328 struct cpmac_priv *priv = netdev_priv(dev);
330 mbp = cpmac_read(priv->regs, CPMAC_MBP);
331 if (dev->flags & IFF_PROMISC) {
332 cpmac_write(priv->regs, CPMAC_MBP, (mbp & ~MBP_PROMISCCHAN(0)) |
335 cpmac_write(priv->regs, CPMAC_MBP, mbp & ~MBP_RXPROMISC);
336 if (dev->flags & IFF_ALLMULTI) {
337 /* enable all multicast mode */
338 cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, 0xffffffff);
339 cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, 0xffffffff);
341 /* cpmac uses some strange mac address hashing
344 netdev_for_each_mc_addr(ha, dev) {
347 bit ^= (tmp >> 2) ^ (tmp << 4);
349 bit ^= (tmp >> 4) ^ (tmp << 2);
351 bit ^= (tmp >> 6) ^ tmp;
353 bit ^= (tmp >> 2) ^ (tmp << 4);
355 bit ^= (tmp >> 4) ^ (tmp << 2);
357 bit ^= (tmp >> 6) ^ tmp;
359 hash[bit / 32] |= 1 << (bit % 32);
362 cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, hash[0]);
363 cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, hash[1]);
368 static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
369 struct cpmac_desc *desc)
371 struct sk_buff *skb, *result = NULL;
373 if (unlikely(netif_msg_hw(priv)))
374 cpmac_dump_desc(priv->dev, desc);
375 cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
376 if (unlikely(!desc->datalen)) {
377 if (netif_msg_rx_err(priv) && net_ratelimit())
378 netdev_warn(priv->dev, "rx: spurious interrupt\n");
383 skb = netdev_alloc_skb_ip_align(priv->dev, CPMAC_SKB_SIZE);
385 skb_put(desc->skb, desc->datalen);
386 desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
387 skb_checksum_none_assert(desc->skb);
388 priv->dev->stats.rx_packets++;
389 priv->dev->stats.rx_bytes += desc->datalen;
391 dma_unmap_single(&priv->dev->dev, desc->data_mapping,
392 CPMAC_SKB_SIZE, DMA_FROM_DEVICE);
394 desc->data_mapping = dma_map_single(&priv->dev->dev, skb->data,
397 desc->hw_data = (u32)desc->data_mapping;
398 if (unlikely(netif_msg_pktdata(priv))) {
399 netdev_dbg(priv->dev, "received packet:\n");
400 cpmac_dump_skb(priv->dev, result);
403 if (netif_msg_rx_err(priv) && net_ratelimit())
404 netdev_warn(priv->dev,
405 "low on skbs, dropping packet\n");
407 priv->dev->stats.rx_dropped++;
410 desc->buflen = CPMAC_SKB_SIZE;
411 desc->dataflags = CPMAC_OWN;
416 static int cpmac_poll(struct napi_struct *napi, int budget)
419 struct cpmac_desc *desc, *restart;
420 struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
421 int received = 0, processed = 0;
423 spin_lock(&priv->rx_lock);
424 if (unlikely(!priv->rx_head)) {
425 if (netif_msg_rx_err(priv) && net_ratelimit())
426 netdev_warn(priv->dev, "rx: polling, but no queue\n");
428 spin_unlock(&priv->rx_lock);
433 desc = priv->rx_head;
435 while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
438 if ((desc->dataflags & CPMAC_EOQ) != 0) {
439 /* The last update to eoq->hw_next didn't happen
440 * soon enough, and the receiver stopped here.
441 * Remember this descriptor so we can restart
442 * the receiver after freeing some space.
444 if (unlikely(restart)) {
445 if (netif_msg_rx_err(priv))
446 netdev_err(priv->dev, "poll found a"
447 " duplicate EOQ: %p and %p\n",
452 restart = desc->next;
455 skb = cpmac_rx_one(priv, desc);
457 netif_receive_skb(skb);
463 if (desc != priv->rx_head) {
464 /* We freed some buffers, but not the whole ring,
465 * add what we did free to the rx list
467 desc->prev->hw_next = (u32)0;
468 priv->rx_head->prev->hw_next = priv->rx_head->mapping;
471 /* Optimization: If we did not actually process an EOQ (perhaps because
472 * of quota limits), check to see if the tail of the queue has EOQ set.
473 * We should immediately restart in that case so that the receiver can
474 * restart and run in parallel with more packet processing.
475 * This lets us handle slightly larger bursts before running
476 * out of ring space (assuming dev->weight < ring_size)
480 (priv->rx_head->prev->dataflags & (CPMAC_OWN|CPMAC_EOQ))
482 (priv->rx_head->dataflags & CPMAC_OWN) != 0) {
483 /* reset EOQ so the poll loop (above) doesn't try to
484 * restart this when it eventually gets to this descriptor.
486 priv->rx_head->prev->dataflags &= ~CPMAC_EOQ;
487 restart = priv->rx_head;
491 priv->dev->stats.rx_errors++;
492 priv->dev->stats.rx_fifo_errors++;
493 if (netif_msg_rx_err(priv) && net_ratelimit())
494 netdev_warn(priv->dev, "rx dma ring overrun\n");
496 if (unlikely((restart->dataflags & CPMAC_OWN) == 0)) {
497 if (netif_msg_drv(priv))
498 netdev_err(priv->dev, "cpmac_poll is trying "
499 "to restart rx from a descriptor "
500 "that's not free: %p\n", restart);
504 cpmac_write(priv->regs, CPMAC_RX_PTR(0), restart->mapping);
507 priv->rx_head = desc;
508 spin_unlock(&priv->rx_lock);
509 if (unlikely(netif_msg_rx_status(priv)))
510 netdev_dbg(priv->dev, "poll processed %d packets\n", received);
512 if (processed == 0) {
513 /* we ran out of packets to read,
514 * revert to interrupt-driven mode
517 cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
524 /* Something went horribly wrong.
525 * Reset hardware to try to recover rather than wedging.
527 if (netif_msg_drv(priv)) {
528 netdev_err(priv->dev, "cpmac_poll is confused. "
529 "Resetting hardware\n");
530 cpmac_dump_all_desc(priv->dev);
531 netdev_dbg(priv->dev, "RX_PTR(0)=0x%08x RX_ACK(0)=0x%08x\n",
532 cpmac_read(priv->regs, CPMAC_RX_PTR(0)),
533 cpmac_read(priv->regs, CPMAC_RX_ACK(0)));
536 spin_unlock(&priv->rx_lock);
538 netif_tx_stop_all_queues(priv->dev);
539 napi_disable(&priv->napi);
541 atomic_inc(&priv->reset_pending);
542 cpmac_hw_stop(priv->dev);
543 if (!schedule_work(&priv->reset_work))
544 atomic_dec(&priv->reset_pending);
550 static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
554 struct cpmac_desc *desc;
555 struct cpmac_priv *priv = netdev_priv(dev);
557 if (unlikely(atomic_read(&priv->reset_pending)))
558 return NETDEV_TX_BUSY;
560 if (unlikely(skb_padto(skb, ETH_ZLEN)))
563 len = max_t(unsigned int, skb->len, ETH_ZLEN);
564 queue = skb_get_queue_mapping(skb);
565 netif_stop_subqueue(dev, queue);
567 desc = &priv->desc_ring[queue];
568 if (unlikely(desc->dataflags & CPMAC_OWN)) {
569 if (netif_msg_tx_err(priv) && net_ratelimit())
570 netdev_warn(dev, "tx dma ring full\n");
572 return NETDEV_TX_BUSY;
575 spin_lock(&priv->lock);
576 spin_unlock(&priv->lock);
577 desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
579 desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
581 desc->hw_data = (u32)desc->data_mapping;
584 if (unlikely(netif_msg_tx_queued(priv)))
585 netdev_dbg(dev, "sending 0x%p, len=%d\n", skb, skb->len);
586 if (unlikely(netif_msg_hw(priv)))
587 cpmac_dump_desc(dev, desc);
588 if (unlikely(netif_msg_pktdata(priv)))
589 cpmac_dump_skb(dev, skb);
590 cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
595 static void cpmac_end_xmit(struct net_device *dev, int queue)
597 struct cpmac_desc *desc;
598 struct cpmac_priv *priv = netdev_priv(dev);
600 desc = &priv->desc_ring[queue];
601 cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
602 if (likely(desc->skb)) {
603 spin_lock(&priv->lock);
604 dev->stats.tx_packets++;
605 dev->stats.tx_bytes += desc->skb->len;
606 spin_unlock(&priv->lock);
607 dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
610 if (unlikely(netif_msg_tx_done(priv)))
611 netdev_dbg(dev, "sent 0x%p, len=%d\n",
612 desc->skb, desc->skb->len);
614 dev_kfree_skb_irq(desc->skb);
616 if (__netif_subqueue_stopped(dev, queue))
617 netif_wake_subqueue(dev, queue);
619 if (netif_msg_tx_err(priv) && net_ratelimit())
620 netdev_warn(dev, "end_xmit: spurious interrupt\n");
621 if (__netif_subqueue_stopped(dev, queue))
622 netif_wake_subqueue(dev, queue);
626 static void cpmac_hw_stop(struct net_device *dev)
629 struct cpmac_priv *priv = netdev_priv(dev);
630 struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
632 ar7_device_reset(pdata->reset_bit);
633 cpmac_write(priv->regs, CPMAC_RX_CONTROL,
634 cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
635 cpmac_write(priv->regs, CPMAC_TX_CONTROL,
636 cpmac_read(priv->regs, CPMAC_TX_CONTROL) & ~1);
637 for (i = 0; i < 8; i++) {
638 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
639 cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
641 cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
642 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
643 cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
644 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
645 cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
646 cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
649 static void cpmac_hw_start(struct net_device *dev)
652 struct cpmac_priv *priv = netdev_priv(dev);
653 struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
655 ar7_device_reset(pdata->reset_bit);
656 for (i = 0; i < 8; i++) {
657 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
658 cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
660 cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
662 cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
664 cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
665 for (i = 0; i < 8; i++)
666 cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
667 cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
668 cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
669 (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
670 (dev->dev_addr[3] << 24));
671 cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
672 cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
673 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
674 cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
675 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
676 cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
677 cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
678 cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
679 cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
681 cpmac_write(priv->regs, CPMAC_RX_CONTROL,
682 cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
683 cpmac_write(priv->regs, CPMAC_TX_CONTROL,
684 cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
685 cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
686 cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
690 static void cpmac_clear_rx(struct net_device *dev)
692 struct cpmac_priv *priv = netdev_priv(dev);
693 struct cpmac_desc *desc;
696 if (unlikely(!priv->rx_head))
698 desc = priv->rx_head;
699 for (i = 0; i < priv->ring_size; i++) {
700 if ((desc->dataflags & CPMAC_OWN) == 0) {
701 if (netif_msg_rx_err(priv) && net_ratelimit())
702 netdev_warn(dev, "packet dropped\n");
703 if (unlikely(netif_msg_hw(priv)))
704 cpmac_dump_desc(dev, desc);
705 desc->dataflags = CPMAC_OWN;
706 dev->stats.rx_dropped++;
708 desc->hw_next = desc->next->mapping;
711 priv->rx_head->prev->hw_next = 0;
714 static void cpmac_clear_tx(struct net_device *dev)
716 struct cpmac_priv *priv = netdev_priv(dev);
719 if (unlikely(!priv->desc_ring))
721 for (i = 0; i < CPMAC_QUEUES; i++) {
722 priv->desc_ring[i].dataflags = 0;
723 if (priv->desc_ring[i].skb) {
724 dev_kfree_skb_any(priv->desc_ring[i].skb);
725 priv->desc_ring[i].skb = NULL;
730 static void cpmac_hw_error(struct work_struct *work)
732 struct cpmac_priv *priv =
733 container_of(work, struct cpmac_priv, reset_work);
735 spin_lock(&priv->rx_lock);
736 cpmac_clear_rx(priv->dev);
737 spin_unlock(&priv->rx_lock);
738 cpmac_clear_tx(priv->dev);
739 cpmac_hw_start(priv->dev);
741 atomic_dec(&priv->reset_pending);
743 netif_tx_wake_all_queues(priv->dev);
744 cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
747 static void cpmac_check_status(struct net_device *dev)
749 struct cpmac_priv *priv = netdev_priv(dev);
751 u32 macstatus = cpmac_read(priv->regs, CPMAC_MAC_STATUS);
752 int rx_channel = (macstatus >> 8) & 7;
753 int rx_code = (macstatus >> 12) & 15;
754 int tx_channel = (macstatus >> 16) & 7;
755 int tx_code = (macstatus >> 20) & 15;
757 if (rx_code || tx_code) {
758 if (netif_msg_drv(priv) && net_ratelimit()) {
759 /* Can't find any documentation on what these
760 * error codes actually are. So just log them and hope..
763 netdev_warn(dev, "host error %d on rx "
764 "channel %d (macstatus %08x), resetting\n",
765 rx_code, rx_channel, macstatus);
767 netdev_warn(dev, "host error %d on tx "
768 "channel %d (macstatus %08x), resetting\n",
769 tx_code, tx_channel, macstatus);
772 netif_tx_stop_all_queues(dev);
774 if (schedule_work(&priv->reset_work))
775 atomic_inc(&priv->reset_pending);
776 if (unlikely(netif_msg_hw(priv)))
777 cpmac_dump_regs(dev);
779 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
782 static irqreturn_t cpmac_irq(int irq, void *dev_id)
784 struct net_device *dev = dev_id;
785 struct cpmac_priv *priv;
789 priv = netdev_priv(dev);
791 status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
793 if (unlikely(netif_msg_intr(priv)))
794 netdev_dbg(dev, "interrupt status: 0x%08x\n", status);
796 if (status & MAC_INT_TX)
797 cpmac_end_xmit(dev, (status & 7));
799 if (status & MAC_INT_RX) {
800 queue = (status >> 8) & 7;
801 if (napi_schedule_prep(&priv->napi)) {
802 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
803 __napi_schedule(&priv->napi);
807 cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
809 if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
810 cpmac_check_status(dev);
815 static void cpmac_tx_timeout(struct net_device *dev)
817 struct cpmac_priv *priv = netdev_priv(dev);
819 spin_lock(&priv->lock);
820 dev->stats.tx_errors++;
821 spin_unlock(&priv->lock);
822 if (netif_msg_tx_err(priv) && net_ratelimit())
823 netdev_warn(dev, "transmit timeout\n");
825 atomic_inc(&priv->reset_pending);
829 atomic_dec(&priv->reset_pending);
831 netif_tx_wake_all_queues(priv->dev);
834 static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
836 struct cpmac_priv *priv = netdev_priv(dev);
838 if (!(netif_running(dev)))
843 return phy_mii_ioctl(priv->phy, ifr, cmd);
846 static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
848 struct cpmac_priv *priv = netdev_priv(dev);
851 return phy_ethtool_gset(priv->phy, cmd);
856 static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
858 struct cpmac_priv *priv = netdev_priv(dev);
860 if (!capable(CAP_NET_ADMIN))
864 return phy_ethtool_sset(priv->phy, cmd);
869 static void cpmac_get_ringparam(struct net_device *dev,
870 struct ethtool_ringparam *ring)
872 struct cpmac_priv *priv = netdev_priv(dev);
874 ring->rx_max_pending = 1024;
875 ring->rx_mini_max_pending = 1;
876 ring->rx_jumbo_max_pending = 1;
877 ring->tx_max_pending = 1;
879 ring->rx_pending = priv->ring_size;
880 ring->rx_mini_pending = 1;
881 ring->rx_jumbo_pending = 1;
882 ring->tx_pending = 1;
885 static int cpmac_set_ringparam(struct net_device *dev,
886 struct ethtool_ringparam *ring)
888 struct cpmac_priv *priv = netdev_priv(dev);
890 if (netif_running(dev))
892 priv->ring_size = ring->rx_pending;
897 static void cpmac_get_drvinfo(struct net_device *dev,
898 struct ethtool_drvinfo *info)
900 strlcpy(info->driver, "cpmac", sizeof(info->driver));
901 strlcpy(info->version, CPMAC_VERSION, sizeof(info->version));
902 snprintf(info->bus_info, sizeof(info->bus_info), "%s", "cpmac");
905 static const struct ethtool_ops cpmac_ethtool_ops = {
906 .get_settings = cpmac_get_settings,
907 .set_settings = cpmac_set_settings,
908 .get_drvinfo = cpmac_get_drvinfo,
909 .get_link = ethtool_op_get_link,
910 .get_ringparam = cpmac_get_ringparam,
911 .set_ringparam = cpmac_set_ringparam,
914 static void cpmac_adjust_link(struct net_device *dev)
916 struct cpmac_priv *priv = netdev_priv(dev);
919 spin_lock(&priv->lock);
920 if (priv->phy->link) {
921 netif_tx_start_all_queues(dev);
922 if (priv->phy->duplex != priv->oldduplex) {
924 priv->oldduplex = priv->phy->duplex;
927 if (priv->phy->speed != priv->oldspeed) {
929 priv->oldspeed = priv->phy->speed;
932 if (!priv->oldlink) {
936 } else if (priv->oldlink) {
940 priv->oldduplex = -1;
943 if (new_state && netif_msg_link(priv) && net_ratelimit())
944 phy_print_status(priv->phy);
946 spin_unlock(&priv->lock);
949 static int cpmac_open(struct net_device *dev)
952 struct cpmac_priv *priv = netdev_priv(dev);
953 struct resource *mem;
954 struct cpmac_desc *desc;
957 mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
958 if (!request_mem_region(mem->start, resource_size(mem), dev->name)) {
959 if (netif_msg_drv(priv))
960 netdev_err(dev, "failed to request registers\n");
966 priv->regs = ioremap(mem->start, resource_size(mem));
968 if (netif_msg_drv(priv))
969 netdev_err(dev, "failed to remap registers\n");
975 size = priv->ring_size + CPMAC_QUEUES;
976 priv->desc_ring = dma_alloc_coherent(&dev->dev,
977 sizeof(struct cpmac_desc) * size,
980 if (!priv->desc_ring) {
985 for (i = 0; i < size; i++)
986 priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
988 priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
989 for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
990 skb = netdev_alloc_skb_ip_align(dev, CPMAC_SKB_SIZE);
991 if (unlikely(!skb)) {
996 desc->data_mapping = dma_map_single(&dev->dev, skb->data,
999 desc->hw_data = (u32)desc->data_mapping;
1000 desc->buflen = CPMAC_SKB_SIZE;
1001 desc->dataflags = CPMAC_OWN;
1002 desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
1003 desc->next->prev = desc;
1004 desc->hw_next = (u32)desc->next->mapping;
1007 priv->rx_head->prev->hw_next = (u32)0;
1009 res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED, dev->name, dev);
1011 if (netif_msg_drv(priv))
1012 netdev_err(dev, "failed to obtain irq\n");
1017 atomic_set(&priv->reset_pending, 0);
1018 INIT_WORK(&priv->reset_work, cpmac_hw_error);
1019 cpmac_hw_start(dev);
1021 napi_enable(&priv->napi);
1022 priv->phy->state = PHY_CHANGELINK;
1023 phy_start(priv->phy);
1029 for (i = 0; i < priv->ring_size; i++) {
1030 if (priv->rx_head[i].skb) {
1031 dma_unmap_single(&dev->dev,
1032 priv->rx_head[i].data_mapping,
1035 kfree_skb(priv->rx_head[i].skb);
1039 kfree(priv->desc_ring);
1040 iounmap(priv->regs);
1043 release_mem_region(mem->start, resource_size(mem));
1049 static int cpmac_stop(struct net_device *dev)
1052 struct cpmac_priv *priv = netdev_priv(dev);
1053 struct resource *mem;
1055 netif_tx_stop_all_queues(dev);
1057 cancel_work_sync(&priv->reset_work);
1058 napi_disable(&priv->napi);
1059 phy_stop(priv->phy);
1063 for (i = 0; i < 8; i++)
1064 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
1065 cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
1066 cpmac_write(priv->regs, CPMAC_MBP, 0);
1068 free_irq(dev->irq, dev);
1069 iounmap(priv->regs);
1070 mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
1071 release_mem_region(mem->start, resource_size(mem));
1072 priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
1073 for (i = 0; i < priv->ring_size; i++) {
1074 if (priv->rx_head[i].skb) {
1075 dma_unmap_single(&dev->dev,
1076 priv->rx_head[i].data_mapping,
1079 kfree_skb(priv->rx_head[i].skb);
1083 dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
1084 (CPMAC_QUEUES + priv->ring_size),
1085 priv->desc_ring, priv->dma_ring);
1090 static const struct net_device_ops cpmac_netdev_ops = {
1091 .ndo_open = cpmac_open,
1092 .ndo_stop = cpmac_stop,
1093 .ndo_start_xmit = cpmac_start_xmit,
1094 .ndo_tx_timeout = cpmac_tx_timeout,
1095 .ndo_set_rx_mode = cpmac_set_multicast_list,
1096 .ndo_do_ioctl = cpmac_ioctl,
1097 .ndo_change_mtu = eth_change_mtu,
1098 .ndo_validate_addr = eth_validate_addr,
1099 .ndo_set_mac_address = eth_mac_addr,
1102 static int external_switch;
1104 static int cpmac_probe(struct platform_device *pdev)
1107 char mdio_bus_id[MII_BUS_ID_SIZE];
1108 struct resource *mem;
1109 struct cpmac_priv *priv;
1110 struct net_device *dev;
1111 struct plat_cpmac_data *pdata;
1113 pdata = dev_get_platdata(&pdev->dev);
1115 if (external_switch || dumb_switch) {
1116 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
1119 for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
1120 if (!(pdata->phy_mask & (1 << phy_id)))
1122 if (!cpmac_mii->phy_map[phy_id])
1124 strncpy(mdio_bus_id, cpmac_mii->id, MII_BUS_ID_SIZE);
1129 if (phy_id == PHY_MAX_ADDR) {
1130 dev_err(&pdev->dev, "no PHY present, falling back "
1131 "to switch on MDIO bus 0\n");
1132 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
1135 mdio_bus_id[sizeof(mdio_bus_id) - 1] = '\0';
1137 dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
1141 platform_set_drvdata(pdev, dev);
1142 priv = netdev_priv(dev);
1145 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
1151 dev->irq = platform_get_irq_byname(pdev, "irq");
1153 dev->netdev_ops = &cpmac_netdev_ops;
1154 dev->ethtool_ops = &cpmac_ethtool_ops;
1156 netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
1158 spin_lock_init(&priv->lock);
1159 spin_lock_init(&priv->rx_lock);
1161 priv->ring_size = 64;
1162 priv->msg_enable = netif_msg_init(debug_level, 0xff);
1163 memcpy(dev->dev_addr, pdata->dev_addr, sizeof(pdata->dev_addr));
1165 snprintf(priv->phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
1166 mdio_bus_id, phy_id);
1168 priv->phy = phy_connect(dev, priv->phy_name, cpmac_adjust_link,
1169 PHY_INTERFACE_MODE_MII);
1171 if (IS_ERR(priv->phy)) {
1172 if (netif_msg_drv(priv))
1173 dev_err(&pdev->dev, "Could not attach to PHY\n");
1175 rc = PTR_ERR(priv->phy);
1179 rc = register_netdev(dev);
1181 dev_err(&pdev->dev, "Could not register net device\n");
1185 if (netif_msg_probe(priv)) {
1186 dev_info(&pdev->dev, "regs: %p, irq: %d, phy: %s, "
1187 "mac: %pM\n", (void *)mem->start, dev->irq,
1188 priv->phy_name, dev->dev_addr);
1199 static int cpmac_remove(struct platform_device *pdev)
1201 struct net_device *dev = platform_get_drvdata(pdev);
1203 unregister_netdev(dev);
1209 static struct platform_driver cpmac_driver = {
1213 .probe = cpmac_probe,
1214 .remove = cpmac_remove,
1217 int cpmac_init(void)
1222 cpmac_mii = mdiobus_alloc();
1223 if (cpmac_mii == NULL)
1226 cpmac_mii->name = "cpmac-mii";
1227 cpmac_mii->read = cpmac_mdio_read;
1228 cpmac_mii->write = cpmac_mdio_write;
1229 cpmac_mii->reset = cpmac_mdio_reset;
1230 cpmac_mii->irq = mii_irqs;
1232 cpmac_mii->priv = ioremap(AR7_REGS_MDIO, 256);
1234 if (!cpmac_mii->priv) {
1235 pr_err("Can't ioremap mdio registers\n");
1240 /* FIXME: unhardcode gpio&reset bits */
1241 ar7_gpio_disable(26);
1242 ar7_gpio_disable(27);
1243 ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
1244 ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
1245 ar7_device_reset(AR7_RESET_BIT_EPHY);
1247 cpmac_mii->reset(cpmac_mii);
1249 for (i = 0; i < 300; i++) {
1250 mask = cpmac_read(cpmac_mii->priv, CPMAC_MDIO_ALIVE);
1258 if (mask & (mask - 1)) {
1259 external_switch = 1;
1263 cpmac_mii->phy_mask = ~(mask | 0x80000000);
1264 snprintf(cpmac_mii->id, MII_BUS_ID_SIZE, "cpmac-1");
1266 res = mdiobus_register(cpmac_mii);
1270 res = platform_driver_register(&cpmac_driver);
1277 mdiobus_unregister(cpmac_mii);
1280 iounmap(cpmac_mii->priv);
1283 mdiobus_free(cpmac_mii);
1288 void cpmac_exit(void)
1290 platform_driver_unregister(&cpmac_driver);
1291 mdiobus_unregister(cpmac_mii);
1292 iounmap(cpmac_mii->priv);
1293 mdiobus_free(cpmac_mii);
1296 module_init(cpmac_init);
1297 module_exit(cpmac_exit);