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 MODULE_AUTHOR("Eugene Konev <ejka@imfi.kspu.ru>");
45 MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
46 MODULE_LICENSE("GPL");
47 MODULE_ALIAS("platform:cpmac");
49 static int debug_level = 8;
50 static int dumb_switch;
52 /* Next 2 are only used in cpmac_probe, so it's pointless to change them */
53 module_param(debug_level, int, 0444);
54 module_param(dumb_switch, int, 0444);
56 MODULE_PARM_DESC(debug_level, "Number of NETIF_MSG bits to enable");
57 MODULE_PARM_DESC(dumb_switch, "Assume switch is not connected to MDIO bus");
59 #define CPMAC_VERSION "0.5.2"
60 /* frame size + 802.1q tag + FCS size */
61 #define CPMAC_SKB_SIZE (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN)
62 #define CPMAC_QUEUES 8
64 /* Ethernet registers */
65 #define CPMAC_TX_CONTROL 0x0004
66 #define CPMAC_TX_TEARDOWN 0x0008
67 #define CPMAC_RX_CONTROL 0x0014
68 #define CPMAC_RX_TEARDOWN 0x0018
69 #define CPMAC_MBP 0x0100
70 #define MBP_RXPASSCRC 0x40000000
71 #define MBP_RXQOS 0x20000000
72 #define MBP_RXNOCHAIN 0x10000000
73 #define MBP_RXCMF 0x01000000
74 #define MBP_RXSHORT 0x00800000
75 #define MBP_RXCEF 0x00400000
76 #define MBP_RXPROMISC 0x00200000
77 #define MBP_PROMISCCHAN(channel) (((channel) & 0x7) << 16)
78 #define MBP_RXBCAST 0x00002000
79 #define MBP_BCASTCHAN(channel) (((channel) & 0x7) << 8)
80 #define MBP_RXMCAST 0x00000020
81 #define MBP_MCASTCHAN(channel) ((channel) & 0x7)
82 #define CPMAC_UNICAST_ENABLE 0x0104
83 #define CPMAC_UNICAST_CLEAR 0x0108
84 #define CPMAC_MAX_LENGTH 0x010c
85 #define CPMAC_BUFFER_OFFSET 0x0110
86 #define CPMAC_MAC_CONTROL 0x0160
87 #define MAC_TXPTYPE 0x00000200
88 #define MAC_TXPACE 0x00000040
89 #define MAC_MII 0x00000020
90 #define MAC_TXFLOW 0x00000010
91 #define MAC_RXFLOW 0x00000008
92 #define MAC_MTEST 0x00000004
93 #define MAC_LOOPBACK 0x00000002
94 #define MAC_FDX 0x00000001
95 #define CPMAC_MAC_STATUS 0x0164
96 #define MAC_STATUS_QOS 0x00000004
97 #define MAC_STATUS_RXFLOW 0x00000002
98 #define MAC_STATUS_TXFLOW 0x00000001
99 #define CPMAC_TX_INT_ENABLE 0x0178
100 #define CPMAC_TX_INT_CLEAR 0x017c
101 #define CPMAC_MAC_INT_VECTOR 0x0180
102 #define MAC_INT_STATUS 0x00080000
103 #define MAC_INT_HOST 0x00040000
104 #define MAC_INT_RX 0x00020000
105 #define MAC_INT_TX 0x00010000
106 #define CPMAC_MAC_EOI_VECTOR 0x0184
107 #define CPMAC_RX_INT_ENABLE 0x0198
108 #define CPMAC_RX_INT_CLEAR 0x019c
109 #define CPMAC_MAC_INT_ENABLE 0x01a8
110 #define CPMAC_MAC_INT_CLEAR 0x01ac
111 #define CPMAC_MAC_ADDR_LO(channel) (0x01b0 + (channel) * 4)
112 #define CPMAC_MAC_ADDR_MID 0x01d0
113 #define CPMAC_MAC_ADDR_HI 0x01d4
114 #define CPMAC_MAC_HASH_LO 0x01d8
115 #define CPMAC_MAC_HASH_HI 0x01dc
116 #define CPMAC_TX_PTR(channel) (0x0600 + (channel) * 4)
117 #define CPMAC_RX_PTR(channel) (0x0620 + (channel) * 4)
118 #define CPMAC_TX_ACK(channel) (0x0640 + (channel) * 4)
119 #define CPMAC_RX_ACK(channel) (0x0660 + (channel) * 4)
120 #define CPMAC_REG_END 0x0680
123 * TODO: use some of them to fill stats in cpmac_stats()
125 #define CPMAC_STATS_RX_GOOD 0x0200
126 #define CPMAC_STATS_RX_BCAST 0x0204
127 #define CPMAC_STATS_RX_MCAST 0x0208
128 #define CPMAC_STATS_RX_PAUSE 0x020c
129 #define CPMAC_STATS_RX_CRC 0x0210
130 #define CPMAC_STATS_RX_ALIGN 0x0214
131 #define CPMAC_STATS_RX_OVER 0x0218
132 #define CPMAC_STATS_RX_JABBER 0x021c
133 #define CPMAC_STATS_RX_UNDER 0x0220
134 #define CPMAC_STATS_RX_FRAG 0x0224
135 #define CPMAC_STATS_RX_FILTER 0x0228
136 #define CPMAC_STATS_RX_QOSFILTER 0x022c
137 #define CPMAC_STATS_RX_OCTETS 0x0230
139 #define CPMAC_STATS_TX_GOOD 0x0234
140 #define CPMAC_STATS_TX_BCAST 0x0238
141 #define CPMAC_STATS_TX_MCAST 0x023c
142 #define CPMAC_STATS_TX_PAUSE 0x0240
143 #define CPMAC_STATS_TX_DEFER 0x0244
144 #define CPMAC_STATS_TX_COLLISION 0x0248
145 #define CPMAC_STATS_TX_SINGLECOLL 0x024c
146 #define CPMAC_STATS_TX_MULTICOLL 0x0250
147 #define CPMAC_STATS_TX_EXCESSCOLL 0x0254
148 #define CPMAC_STATS_TX_LATECOLL 0x0258
149 #define CPMAC_STATS_TX_UNDERRUN 0x025c
150 #define CPMAC_STATS_TX_CARRIERSENSE 0x0260
151 #define CPMAC_STATS_TX_OCTETS 0x0264
153 #define cpmac_read(base, reg) (readl((void __iomem *)(base) + (reg)))
154 #define cpmac_write(base, reg, val) (writel(val, (void __iomem *)(base) + \
158 #define CPMAC_MDIO_VERSION 0x0000
159 #define CPMAC_MDIO_CONTROL 0x0004
160 #define MDIOC_IDLE 0x80000000
161 #define MDIOC_ENABLE 0x40000000
162 #define MDIOC_PREAMBLE 0x00100000
163 #define MDIOC_FAULT 0x00080000
164 #define MDIOC_FAULTDETECT 0x00040000
165 #define MDIOC_INTTEST 0x00020000
166 #define MDIOC_CLKDIV(div) ((div) & 0xff)
167 #define CPMAC_MDIO_ALIVE 0x0008
168 #define CPMAC_MDIO_LINK 0x000c
169 #define CPMAC_MDIO_ACCESS(channel) (0x0080 + (channel) * 8)
170 #define MDIO_BUSY 0x80000000
171 #define MDIO_WRITE 0x40000000
172 #define MDIO_REG(reg) (((reg) & 0x1f) << 21)
173 #define MDIO_PHY(phy) (((phy) & 0x1f) << 16)
174 #define MDIO_DATA(data) ((data) & 0xffff)
175 #define CPMAC_MDIO_PHYSEL(channel) (0x0084 + (channel) * 8)
176 #define PHYSEL_LINKSEL 0x00000040
177 #define PHYSEL_LINKINT 0x00000020
186 #define CPMAC_SOP 0x8000
187 #define CPMAC_EOP 0x4000
188 #define CPMAC_OWN 0x2000
189 #define CPMAC_EOQ 0x1000
191 struct cpmac_desc *next;
192 struct cpmac_desc *prev;
194 dma_addr_t data_mapping;
200 struct cpmac_desc *rx_head;
202 struct cpmac_desc *desc_ring;
205 struct mii_bus *mii_bus;
206 struct phy_device *phy;
207 char phy_name[MII_BUS_ID_SIZE + 3];
208 int oldlink, oldspeed, oldduplex;
210 struct net_device *dev;
211 struct work_struct reset_work;
212 struct platform_device *pdev;
213 struct napi_struct napi;
214 atomic_t reset_pending;
217 static irqreturn_t cpmac_irq(int, void *);
218 static void cpmac_hw_start(struct net_device *dev);
219 static void cpmac_hw_stop(struct net_device *dev);
220 static int cpmac_stop(struct net_device *dev);
221 static int cpmac_open(struct net_device *dev);
223 static void cpmac_dump_regs(struct net_device *dev)
226 struct cpmac_priv *priv = netdev_priv(dev);
228 for (i = 0; i < CPMAC_REG_END; i += 4) {
232 netdev_dbg(dev, "reg[%p]:", priv->regs + i);
234 pr_debug(" %08x", cpmac_read(priv->regs, i));
239 static void cpmac_dump_desc(struct net_device *dev, struct cpmac_desc *desc)
243 netdev_dbg(dev, "desc[%p]:", desc);
244 for (i = 0; i < sizeof(*desc) / 4; i++)
245 pr_debug(" %08x", ((u32 *)desc)[i]);
249 static void cpmac_dump_all_desc(struct net_device *dev)
251 struct cpmac_priv *priv = netdev_priv(dev);
252 struct cpmac_desc *dump = priv->rx_head;
255 cpmac_dump_desc(dev, dump);
257 } while (dump != priv->rx_head);
260 static void cpmac_dump_skb(struct net_device *dev, struct sk_buff *skb)
264 netdev_dbg(dev, "skb 0x%p, len=%d\n", skb, skb->len);
265 for (i = 0; i < skb->len; i++) {
269 netdev_dbg(dev, "data[%p]:", skb->data + i);
271 pr_debug(" %02x", ((u8 *)skb->data)[i]);
276 static int cpmac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
280 while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
282 cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_REG(reg) |
284 while ((val = cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0))) & MDIO_BUSY)
286 return MDIO_DATA(val);
289 static int cpmac_mdio_write(struct mii_bus *bus, int phy_id,
292 while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
294 cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_WRITE |
295 MDIO_REG(reg) | MDIO_PHY(phy_id) | MDIO_DATA(val));
299 static int cpmac_mdio_reset(struct mii_bus *bus)
301 struct clk *cpmac_clk;
303 cpmac_clk = clk_get(&bus->dev, "cpmac");
304 if (IS_ERR(cpmac_clk)) {
305 pr_err("unable to get cpmac clock\n");
308 ar7_device_reset(AR7_RESET_BIT_MDIO);
309 cpmac_write(bus->priv, CPMAC_MDIO_CONTROL, MDIOC_ENABLE |
310 MDIOC_CLKDIV(clk_get_rate(cpmac_clk) / 2200000 - 1));
314 static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, };
316 static struct mii_bus *cpmac_mii;
318 static void cpmac_set_multicast_list(struct net_device *dev)
320 struct netdev_hw_addr *ha;
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);
336 /* cpmac uses some strange mac address hashing
339 netdev_for_each_mc_addr(ha, dev) {
342 bit ^= (tmp >> 2) ^ (tmp << 4);
344 bit ^= (tmp >> 4) ^ (tmp << 2);
346 bit ^= (tmp >> 6) ^ tmp;
348 bit ^= (tmp >> 2) ^ (tmp << 4);
350 bit ^= (tmp >> 4) ^ (tmp << 2);
352 bit ^= (tmp >> 6) ^ tmp;
354 hash[bit / 32] |= 1 << (bit % 32);
357 cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, hash[0]);
358 cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, hash[1]);
363 static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
364 struct cpmac_desc *desc)
366 struct sk_buff *skb, *result = NULL;
368 if (unlikely(netif_msg_hw(priv)))
369 cpmac_dump_desc(priv->dev, desc);
370 cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
371 if (unlikely(!desc->datalen)) {
372 if (netif_msg_rx_err(priv) && net_ratelimit())
373 netdev_warn(priv->dev, "rx: spurious interrupt\n");
378 skb = netdev_alloc_skb_ip_align(priv->dev, CPMAC_SKB_SIZE);
380 skb_put(desc->skb, desc->datalen);
381 desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
382 skb_checksum_none_assert(desc->skb);
383 priv->dev->stats.rx_packets++;
384 priv->dev->stats.rx_bytes += desc->datalen;
386 dma_unmap_single(&priv->dev->dev, desc->data_mapping,
387 CPMAC_SKB_SIZE, DMA_FROM_DEVICE);
389 desc->data_mapping = dma_map_single(&priv->dev->dev, skb->data,
392 desc->hw_data = (u32)desc->data_mapping;
393 if (unlikely(netif_msg_pktdata(priv))) {
394 netdev_dbg(priv->dev, "received packet:\n");
395 cpmac_dump_skb(priv->dev, result);
398 if (netif_msg_rx_err(priv) && net_ratelimit())
399 netdev_warn(priv->dev,
400 "low on skbs, dropping packet\n");
402 priv->dev->stats.rx_dropped++;
405 desc->buflen = CPMAC_SKB_SIZE;
406 desc->dataflags = CPMAC_OWN;
411 static int cpmac_poll(struct napi_struct *napi, int budget)
414 struct cpmac_desc *desc, *restart;
415 struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
416 int received = 0, processed = 0;
418 spin_lock(&priv->rx_lock);
419 if (unlikely(!priv->rx_head)) {
420 if (netif_msg_rx_err(priv) && net_ratelimit())
421 netdev_warn(priv->dev, "rx: polling, but no queue\n");
423 spin_unlock(&priv->rx_lock);
428 desc = priv->rx_head;
430 while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
433 if ((desc->dataflags & CPMAC_EOQ) != 0) {
434 /* The last update to eoq->hw_next didn't happen
435 * soon enough, and the receiver stopped here.
436 * Remember this descriptor so we can restart
437 * the receiver after freeing some space.
439 if (unlikely(restart)) {
440 if (netif_msg_rx_err(priv))
441 netdev_err(priv->dev, "poll found a"
442 " duplicate EOQ: %p and %p\n",
447 restart = desc->next;
450 skb = cpmac_rx_one(priv, desc);
452 netif_receive_skb(skb);
458 if (desc != priv->rx_head) {
459 /* We freed some buffers, but not the whole ring,
460 * add what we did free to the rx list
462 desc->prev->hw_next = (u32)0;
463 priv->rx_head->prev->hw_next = priv->rx_head->mapping;
466 /* Optimization: If we did not actually process an EOQ (perhaps because
467 * of quota limits), check to see if the tail of the queue has EOQ set.
468 * We should immediately restart in that case so that the receiver can
469 * restart and run in parallel with more packet processing.
470 * This lets us handle slightly larger bursts before running
471 * out of ring space (assuming dev->weight < ring_size)
475 (priv->rx_head->prev->dataflags & (CPMAC_OWN|CPMAC_EOQ))
477 (priv->rx_head->dataflags & CPMAC_OWN) != 0) {
478 /* reset EOQ so the poll loop (above) doesn't try to
479 * restart this when it eventually gets to this descriptor.
481 priv->rx_head->prev->dataflags &= ~CPMAC_EOQ;
482 restart = priv->rx_head;
486 priv->dev->stats.rx_errors++;
487 priv->dev->stats.rx_fifo_errors++;
488 if (netif_msg_rx_err(priv) && net_ratelimit())
489 netdev_warn(priv->dev, "rx dma ring overrun\n");
491 if (unlikely((restart->dataflags & CPMAC_OWN) == 0)) {
492 if (netif_msg_drv(priv))
493 netdev_err(priv->dev, "cpmac_poll is trying "
494 "to restart rx from a descriptor "
495 "that's not free: %p\n", restart);
499 cpmac_write(priv->regs, CPMAC_RX_PTR(0), restart->mapping);
502 priv->rx_head = desc;
503 spin_unlock(&priv->rx_lock);
504 if (unlikely(netif_msg_rx_status(priv)))
505 netdev_dbg(priv->dev, "poll processed %d packets\n", received);
507 if (processed == 0) {
508 /* we ran out of packets to read,
509 * revert to interrupt-driven mode
512 cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
519 /* Something went horribly wrong.
520 * Reset hardware to try to recover rather than wedging.
522 if (netif_msg_drv(priv)) {
523 netdev_err(priv->dev, "cpmac_poll is confused. "
524 "Resetting hardware\n");
525 cpmac_dump_all_desc(priv->dev);
526 netdev_dbg(priv->dev, "RX_PTR(0)=0x%08x RX_ACK(0)=0x%08x\n",
527 cpmac_read(priv->regs, CPMAC_RX_PTR(0)),
528 cpmac_read(priv->regs, CPMAC_RX_ACK(0)));
531 spin_unlock(&priv->rx_lock);
533 netif_tx_stop_all_queues(priv->dev);
534 napi_disable(&priv->napi);
536 atomic_inc(&priv->reset_pending);
537 cpmac_hw_stop(priv->dev);
538 if (!schedule_work(&priv->reset_work))
539 atomic_dec(&priv->reset_pending);
544 static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
547 struct cpmac_desc *desc;
548 struct cpmac_priv *priv = netdev_priv(dev);
550 if (unlikely(atomic_read(&priv->reset_pending)))
551 return NETDEV_TX_BUSY;
553 if (unlikely(skb_padto(skb, ETH_ZLEN)))
556 len = max(skb->len, ETH_ZLEN);
557 queue = skb_get_queue_mapping(skb);
558 netif_stop_subqueue(dev, queue);
560 desc = &priv->desc_ring[queue];
561 if (unlikely(desc->dataflags & CPMAC_OWN)) {
562 if (netif_msg_tx_err(priv) && net_ratelimit())
563 netdev_warn(dev, "tx dma ring full\n");
565 return NETDEV_TX_BUSY;
568 spin_lock(&priv->lock);
569 spin_unlock(&priv->lock);
570 desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
572 desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
574 desc->hw_data = (u32)desc->data_mapping;
577 if (unlikely(netif_msg_tx_queued(priv)))
578 netdev_dbg(dev, "sending 0x%p, len=%d\n", skb, skb->len);
579 if (unlikely(netif_msg_hw(priv)))
580 cpmac_dump_desc(dev, desc);
581 if (unlikely(netif_msg_pktdata(priv)))
582 cpmac_dump_skb(dev, skb);
583 cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
588 static void cpmac_end_xmit(struct net_device *dev, int queue)
590 struct cpmac_desc *desc;
591 struct cpmac_priv *priv = netdev_priv(dev);
593 desc = &priv->desc_ring[queue];
594 cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
595 if (likely(desc->skb)) {
596 spin_lock(&priv->lock);
597 dev->stats.tx_packets++;
598 dev->stats.tx_bytes += desc->skb->len;
599 spin_unlock(&priv->lock);
600 dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
603 if (unlikely(netif_msg_tx_done(priv)))
604 netdev_dbg(dev, "sent 0x%p, len=%d\n",
605 desc->skb, desc->skb->len);
607 dev_kfree_skb_irq(desc->skb);
609 if (__netif_subqueue_stopped(dev, queue))
610 netif_wake_subqueue(dev, queue);
612 if (netif_msg_tx_err(priv) && net_ratelimit())
613 netdev_warn(dev, "end_xmit: spurious interrupt\n");
614 if (__netif_subqueue_stopped(dev, queue))
615 netif_wake_subqueue(dev, queue);
619 static void cpmac_hw_stop(struct net_device *dev)
622 struct cpmac_priv *priv = netdev_priv(dev);
623 struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
625 ar7_device_reset(pdata->reset_bit);
626 cpmac_write(priv->regs, CPMAC_RX_CONTROL,
627 cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
628 cpmac_write(priv->regs, CPMAC_TX_CONTROL,
629 cpmac_read(priv->regs, CPMAC_TX_CONTROL) & ~1);
630 for (i = 0; i < 8; i++) {
631 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
632 cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
634 cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
635 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
636 cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
637 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
638 cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
639 cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
642 static void cpmac_hw_start(struct net_device *dev)
645 struct cpmac_priv *priv = netdev_priv(dev);
646 struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
648 ar7_device_reset(pdata->reset_bit);
649 for (i = 0; i < 8; i++) {
650 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
651 cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
653 cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
655 cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
657 cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
658 for (i = 0; i < 8; i++)
659 cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
660 cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
661 cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
662 (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
663 (dev->dev_addr[3] << 24));
664 cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
665 cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
666 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
667 cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
668 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
669 cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
670 cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
671 cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
672 cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
674 cpmac_write(priv->regs, CPMAC_RX_CONTROL,
675 cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
676 cpmac_write(priv->regs, CPMAC_TX_CONTROL,
677 cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
678 cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
679 cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
683 static void cpmac_clear_rx(struct net_device *dev)
685 struct cpmac_priv *priv = netdev_priv(dev);
686 struct cpmac_desc *desc;
689 if (unlikely(!priv->rx_head))
691 desc = priv->rx_head;
692 for (i = 0; i < priv->ring_size; i++) {
693 if ((desc->dataflags & CPMAC_OWN) == 0) {
694 if (netif_msg_rx_err(priv) && net_ratelimit())
695 netdev_warn(dev, "packet dropped\n");
696 if (unlikely(netif_msg_hw(priv)))
697 cpmac_dump_desc(dev, desc);
698 desc->dataflags = CPMAC_OWN;
699 dev->stats.rx_dropped++;
701 desc->hw_next = desc->next->mapping;
704 priv->rx_head->prev->hw_next = 0;
707 static void cpmac_clear_tx(struct net_device *dev)
709 struct cpmac_priv *priv = netdev_priv(dev);
712 if (unlikely(!priv->desc_ring))
714 for (i = 0; i < CPMAC_QUEUES; i++) {
715 priv->desc_ring[i].dataflags = 0;
716 if (priv->desc_ring[i].skb) {
717 dev_kfree_skb_any(priv->desc_ring[i].skb);
718 priv->desc_ring[i].skb = NULL;
723 static void cpmac_hw_error(struct work_struct *work)
725 struct cpmac_priv *priv =
726 container_of(work, struct cpmac_priv, reset_work);
728 spin_lock(&priv->rx_lock);
729 cpmac_clear_rx(priv->dev);
730 spin_unlock(&priv->rx_lock);
731 cpmac_clear_tx(priv->dev);
732 cpmac_hw_start(priv->dev);
734 atomic_dec(&priv->reset_pending);
736 netif_tx_wake_all_queues(priv->dev);
737 cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
740 static void cpmac_check_status(struct net_device *dev)
742 struct cpmac_priv *priv = netdev_priv(dev);
744 u32 macstatus = cpmac_read(priv->regs, CPMAC_MAC_STATUS);
745 int rx_channel = (macstatus >> 8) & 7;
746 int rx_code = (macstatus >> 12) & 15;
747 int tx_channel = (macstatus >> 16) & 7;
748 int tx_code = (macstatus >> 20) & 15;
750 if (rx_code || tx_code) {
751 if (netif_msg_drv(priv) && net_ratelimit()) {
752 /* Can't find any documentation on what these
753 * error codes actually are. So just log them and hope..
756 netdev_warn(dev, "host error %d on rx "
757 "channel %d (macstatus %08x), resetting\n",
758 rx_code, rx_channel, macstatus);
760 netdev_warn(dev, "host error %d on tx "
761 "channel %d (macstatus %08x), resetting\n",
762 tx_code, tx_channel, macstatus);
765 netif_tx_stop_all_queues(dev);
767 if (schedule_work(&priv->reset_work))
768 atomic_inc(&priv->reset_pending);
769 if (unlikely(netif_msg_hw(priv)))
770 cpmac_dump_regs(dev);
772 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
775 static irqreturn_t cpmac_irq(int irq, void *dev_id)
777 struct net_device *dev = dev_id;
778 struct cpmac_priv *priv;
782 priv = netdev_priv(dev);
784 status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
786 if (unlikely(netif_msg_intr(priv)))
787 netdev_dbg(dev, "interrupt status: 0x%08x\n", status);
789 if (status & MAC_INT_TX)
790 cpmac_end_xmit(dev, (status & 7));
792 if (status & MAC_INT_RX) {
793 queue = (status >> 8) & 7;
794 if (napi_schedule_prep(&priv->napi)) {
795 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
796 __napi_schedule(&priv->napi);
800 cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
802 if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
803 cpmac_check_status(dev);
808 static void cpmac_tx_timeout(struct net_device *dev)
810 struct cpmac_priv *priv = netdev_priv(dev);
812 spin_lock(&priv->lock);
813 dev->stats.tx_errors++;
814 spin_unlock(&priv->lock);
815 if (netif_msg_tx_err(priv) && net_ratelimit())
816 netdev_warn(dev, "transmit timeout\n");
818 atomic_inc(&priv->reset_pending);
822 atomic_dec(&priv->reset_pending);
824 netif_tx_wake_all_queues(priv->dev);
827 static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
829 struct cpmac_priv *priv = netdev_priv(dev);
831 if (!(netif_running(dev)))
836 return phy_mii_ioctl(priv->phy, ifr, cmd);
839 static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
841 struct cpmac_priv *priv = netdev_priv(dev);
844 return phy_ethtool_gset(priv->phy, cmd);
849 static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
851 struct cpmac_priv *priv = netdev_priv(dev);
853 if (!capable(CAP_NET_ADMIN))
857 return phy_ethtool_sset(priv->phy, cmd);
862 static void cpmac_get_ringparam(struct net_device *dev,
863 struct ethtool_ringparam *ring)
865 struct cpmac_priv *priv = netdev_priv(dev);
867 ring->rx_max_pending = 1024;
868 ring->rx_mini_max_pending = 1;
869 ring->rx_jumbo_max_pending = 1;
870 ring->tx_max_pending = 1;
872 ring->rx_pending = priv->ring_size;
873 ring->rx_mini_pending = 1;
874 ring->rx_jumbo_pending = 1;
875 ring->tx_pending = 1;
878 static int cpmac_set_ringparam(struct net_device *dev,
879 struct ethtool_ringparam *ring)
881 struct cpmac_priv *priv = netdev_priv(dev);
883 if (netif_running(dev))
885 priv->ring_size = ring->rx_pending;
889 static void cpmac_get_drvinfo(struct net_device *dev,
890 struct ethtool_drvinfo *info)
892 strlcpy(info->driver, "cpmac", sizeof(info->driver));
893 strlcpy(info->version, CPMAC_VERSION, sizeof(info->version));
894 snprintf(info->bus_info, sizeof(info->bus_info), "%s", "cpmac");
895 info->regdump_len = 0;
898 static const struct ethtool_ops cpmac_ethtool_ops = {
899 .get_settings = cpmac_get_settings,
900 .set_settings = cpmac_set_settings,
901 .get_drvinfo = cpmac_get_drvinfo,
902 .get_link = ethtool_op_get_link,
903 .get_ringparam = cpmac_get_ringparam,
904 .set_ringparam = cpmac_set_ringparam,
907 static void cpmac_adjust_link(struct net_device *dev)
909 struct cpmac_priv *priv = netdev_priv(dev);
912 spin_lock(&priv->lock);
913 if (priv->phy->link) {
914 netif_tx_start_all_queues(dev);
915 if (priv->phy->duplex != priv->oldduplex) {
917 priv->oldduplex = priv->phy->duplex;
920 if (priv->phy->speed != priv->oldspeed) {
922 priv->oldspeed = priv->phy->speed;
925 if (!priv->oldlink) {
929 } else if (priv->oldlink) {
933 priv->oldduplex = -1;
936 if (new_state && netif_msg_link(priv) && net_ratelimit())
937 phy_print_status(priv->phy);
939 spin_unlock(&priv->lock);
942 static int cpmac_open(struct net_device *dev)
945 struct cpmac_priv *priv = netdev_priv(dev);
946 struct resource *mem;
947 struct cpmac_desc *desc;
950 mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
951 if (!request_mem_region(mem->start, resource_size(mem), dev->name)) {
952 if (netif_msg_drv(priv))
953 netdev_err(dev, "failed to request registers\n");
959 priv->regs = ioremap(mem->start, resource_size(mem));
961 if (netif_msg_drv(priv))
962 netdev_err(dev, "failed to remap registers\n");
968 size = priv->ring_size + CPMAC_QUEUES;
969 priv->desc_ring = dma_alloc_coherent(&dev->dev,
970 sizeof(struct cpmac_desc) * size,
973 if (!priv->desc_ring) {
978 for (i = 0; i < size; i++)
979 priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
981 priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
982 for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
983 skb = netdev_alloc_skb_ip_align(dev, CPMAC_SKB_SIZE);
984 if (unlikely(!skb)) {
989 desc->data_mapping = dma_map_single(&dev->dev, skb->data,
992 desc->hw_data = (u32)desc->data_mapping;
993 desc->buflen = CPMAC_SKB_SIZE;
994 desc->dataflags = CPMAC_OWN;
995 desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
996 desc->next->prev = desc;
997 desc->hw_next = (u32)desc->next->mapping;
1000 priv->rx_head->prev->hw_next = (u32)0;
1002 res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED, dev->name, dev);
1004 if (netif_msg_drv(priv))
1005 netdev_err(dev, "failed to obtain irq\n");
1010 atomic_set(&priv->reset_pending, 0);
1011 INIT_WORK(&priv->reset_work, cpmac_hw_error);
1012 cpmac_hw_start(dev);
1014 napi_enable(&priv->napi);
1015 priv->phy->state = PHY_CHANGELINK;
1016 phy_start(priv->phy);
1022 for (i = 0; i < priv->ring_size; i++) {
1023 if (priv->rx_head[i].skb) {
1024 dma_unmap_single(&dev->dev,
1025 priv->rx_head[i].data_mapping,
1028 kfree_skb(priv->rx_head[i].skb);
1032 kfree(priv->desc_ring);
1033 iounmap(priv->regs);
1036 release_mem_region(mem->start, resource_size(mem));
1042 static int cpmac_stop(struct net_device *dev)
1045 struct cpmac_priv *priv = netdev_priv(dev);
1046 struct resource *mem;
1048 netif_tx_stop_all_queues(dev);
1050 cancel_work_sync(&priv->reset_work);
1051 napi_disable(&priv->napi);
1052 phy_stop(priv->phy);
1056 for (i = 0; i < 8; i++)
1057 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
1058 cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
1059 cpmac_write(priv->regs, CPMAC_MBP, 0);
1061 free_irq(dev->irq, dev);
1062 iounmap(priv->regs);
1063 mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
1064 release_mem_region(mem->start, resource_size(mem));
1065 priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
1066 for (i = 0; i < priv->ring_size; i++) {
1067 if (priv->rx_head[i].skb) {
1068 dma_unmap_single(&dev->dev,
1069 priv->rx_head[i].data_mapping,
1072 kfree_skb(priv->rx_head[i].skb);
1076 dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
1077 (CPMAC_QUEUES + priv->ring_size),
1078 priv->desc_ring, priv->dma_ring);
1082 static const struct net_device_ops cpmac_netdev_ops = {
1083 .ndo_open = cpmac_open,
1084 .ndo_stop = cpmac_stop,
1085 .ndo_start_xmit = cpmac_start_xmit,
1086 .ndo_tx_timeout = cpmac_tx_timeout,
1087 .ndo_set_rx_mode = cpmac_set_multicast_list,
1088 .ndo_do_ioctl = cpmac_ioctl,
1089 .ndo_change_mtu = eth_change_mtu,
1090 .ndo_validate_addr = eth_validate_addr,
1091 .ndo_set_mac_address = eth_mac_addr,
1094 static int external_switch;
1096 static int cpmac_probe(struct platform_device *pdev)
1099 char mdio_bus_id[MII_BUS_ID_SIZE];
1100 struct resource *mem;
1101 struct cpmac_priv *priv;
1102 struct net_device *dev;
1103 struct plat_cpmac_data *pdata;
1105 pdata = dev_get_platdata(&pdev->dev);
1107 if (external_switch || dumb_switch) {
1108 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
1111 for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
1112 if (!(pdata->phy_mask & (1 << phy_id)))
1114 if (!cpmac_mii->phy_map[phy_id])
1116 strncpy(mdio_bus_id, cpmac_mii->id, MII_BUS_ID_SIZE);
1121 if (phy_id == PHY_MAX_ADDR) {
1122 dev_err(&pdev->dev, "no PHY present, falling back "
1123 "to switch on MDIO bus 0\n");
1124 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
1128 dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
1132 platform_set_drvdata(pdev, dev);
1133 priv = netdev_priv(dev);
1136 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
1142 dev->irq = platform_get_irq_byname(pdev, "irq");
1144 dev->netdev_ops = &cpmac_netdev_ops;
1145 dev->ethtool_ops = &cpmac_ethtool_ops;
1147 netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
1149 spin_lock_init(&priv->lock);
1150 spin_lock_init(&priv->rx_lock);
1152 priv->ring_size = 64;
1153 priv->msg_enable = netif_msg_init(debug_level, 0xff);
1154 memcpy(dev->dev_addr, pdata->dev_addr, sizeof(pdata->dev_addr));
1156 snprintf(priv->phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
1157 mdio_bus_id, phy_id);
1159 priv->phy = phy_connect(dev, priv->phy_name, cpmac_adjust_link,
1160 PHY_INTERFACE_MODE_MII);
1162 if (IS_ERR(priv->phy)) {
1163 if (netif_msg_drv(priv))
1164 dev_err(&pdev->dev, "Could not attach to PHY\n");
1166 rc = PTR_ERR(priv->phy);
1170 rc = register_netdev(dev);
1172 dev_err(&pdev->dev, "Could not register net device\n");
1176 if (netif_msg_probe(priv)) {
1177 dev_info(&pdev->dev, "regs: %p, irq: %d, phy: %s, "
1178 "mac: %pM\n", (void *)mem->start, dev->irq,
1179 priv->phy_name, dev->dev_addr);
1188 static int cpmac_remove(struct platform_device *pdev)
1190 struct net_device *dev = platform_get_drvdata(pdev);
1192 unregister_netdev(dev);
1197 static struct platform_driver cpmac_driver = {
1200 .owner = THIS_MODULE,
1202 .probe = cpmac_probe,
1203 .remove = cpmac_remove,
1206 int cpmac_init(void)
1211 cpmac_mii = mdiobus_alloc();
1212 if (cpmac_mii == NULL)
1215 cpmac_mii->name = "cpmac-mii";
1216 cpmac_mii->read = cpmac_mdio_read;
1217 cpmac_mii->write = cpmac_mdio_write;
1218 cpmac_mii->reset = cpmac_mdio_reset;
1219 cpmac_mii->irq = mii_irqs;
1221 cpmac_mii->priv = ioremap(AR7_REGS_MDIO, 256);
1223 if (!cpmac_mii->priv) {
1224 pr_err("Can't ioremap mdio registers\n");
1229 #warning FIXME: unhardcode gpio&reset bits
1230 ar7_gpio_disable(26);
1231 ar7_gpio_disable(27);
1232 ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
1233 ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
1234 ar7_device_reset(AR7_RESET_BIT_EPHY);
1236 cpmac_mii->reset(cpmac_mii);
1238 for (i = 0; i < 300; i++) {
1239 mask = cpmac_read(cpmac_mii->priv, CPMAC_MDIO_ALIVE);
1247 if (mask & (mask - 1)) {
1248 external_switch = 1;
1252 cpmac_mii->phy_mask = ~(mask | 0x80000000);
1253 snprintf(cpmac_mii->id, MII_BUS_ID_SIZE, "cpmac-1");
1255 res = mdiobus_register(cpmac_mii);
1259 res = platform_driver_register(&cpmac_driver);
1266 mdiobus_unregister(cpmac_mii);
1269 iounmap(cpmac_mii->priv);
1272 mdiobus_free(cpmac_mii);
1277 void cpmac_exit(void)
1279 platform_driver_unregister(&cpmac_driver);
1280 mdiobus_unregister(cpmac_mii);
1281 iounmap(cpmac_mii->priv);
1282 mdiobus_free(cpmac_mii);
1285 module_init(cpmac_init);
1286 module_exit(cpmac_exit);