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>
42 MODULE_AUTHOR("Eugene Konev <ejka@imfi.kspu.ru>");
43 MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
44 MODULE_LICENSE("GPL");
45 MODULE_ALIAS("platform:cpmac");
47 static int debug_level = 8;
48 static int dumb_switch;
50 /* Next 2 are only used in cpmac_probe, so it's pointless to change them */
51 module_param(debug_level, int, 0444);
52 module_param(dumb_switch, int, 0444);
54 MODULE_PARM_DESC(debug_level, "Number of NETIF_MSG bits to enable");
55 MODULE_PARM_DESC(dumb_switch, "Assume switch is not connected to MDIO bus");
57 #define CPMAC_VERSION "0.5.0"
58 /* frame size + 802.1q tag */
59 #define CPMAC_SKB_SIZE (ETH_FRAME_LEN + 4)
60 #define CPMAC_QUEUES 8
62 /* Ethernet registers */
63 #define CPMAC_TX_CONTROL 0x0004
64 #define CPMAC_TX_TEARDOWN 0x0008
65 #define CPMAC_RX_CONTROL 0x0014
66 #define CPMAC_RX_TEARDOWN 0x0018
67 #define CPMAC_MBP 0x0100
68 # define MBP_RXPASSCRC 0x40000000
69 # define MBP_RXQOS 0x20000000
70 # define MBP_RXNOCHAIN 0x10000000
71 # define MBP_RXCMF 0x01000000
72 # define MBP_RXSHORT 0x00800000
73 # define MBP_RXCEF 0x00400000
74 # define MBP_RXPROMISC 0x00200000
75 # define MBP_PROMISCCHAN(channel) (((channel) & 0x7) << 16)
76 # define MBP_RXBCAST 0x00002000
77 # define MBP_BCASTCHAN(channel) (((channel) & 0x7) << 8)
78 # define MBP_RXMCAST 0x00000020
79 # define MBP_MCASTCHAN(channel) ((channel) & 0x7)
80 #define CPMAC_UNICAST_ENABLE 0x0104
81 #define CPMAC_UNICAST_CLEAR 0x0108
82 #define CPMAC_MAX_LENGTH 0x010c
83 #define CPMAC_BUFFER_OFFSET 0x0110
84 #define CPMAC_MAC_CONTROL 0x0160
85 # define MAC_TXPTYPE 0x00000200
86 # define MAC_TXPACE 0x00000040
87 # define MAC_MII 0x00000020
88 # define MAC_TXFLOW 0x00000010
89 # define MAC_RXFLOW 0x00000008
90 # define MAC_MTEST 0x00000004
91 # define MAC_LOOPBACK 0x00000002
92 # define MAC_FDX 0x00000001
93 #define CPMAC_MAC_STATUS 0x0164
94 # define MAC_STATUS_QOS 0x00000004
95 # define MAC_STATUS_RXFLOW 0x00000002
96 # define MAC_STATUS_TXFLOW 0x00000001
97 #define CPMAC_TX_INT_ENABLE 0x0178
98 #define CPMAC_TX_INT_CLEAR 0x017c
99 #define CPMAC_MAC_INT_VECTOR 0x0180
100 # define MAC_INT_STATUS 0x00080000
101 # define MAC_INT_HOST 0x00040000
102 # define MAC_INT_RX 0x00020000
103 # define MAC_INT_TX 0x00010000
104 #define CPMAC_MAC_EOI_VECTOR 0x0184
105 #define CPMAC_RX_INT_ENABLE 0x0198
106 #define CPMAC_RX_INT_CLEAR 0x019c
107 #define CPMAC_MAC_INT_ENABLE 0x01a8
108 #define CPMAC_MAC_INT_CLEAR 0x01ac
109 #define CPMAC_MAC_ADDR_LO(channel) (0x01b0 + (channel) * 4)
110 #define CPMAC_MAC_ADDR_MID 0x01d0
111 #define CPMAC_MAC_ADDR_HI 0x01d4
112 #define CPMAC_MAC_HASH_LO 0x01d8
113 #define CPMAC_MAC_HASH_HI 0x01dc
114 #define CPMAC_TX_PTR(channel) (0x0600 + (channel) * 4)
115 #define CPMAC_RX_PTR(channel) (0x0620 + (channel) * 4)
116 #define CPMAC_TX_ACK(channel) (0x0640 + (channel) * 4)
117 #define CPMAC_RX_ACK(channel) (0x0660 + (channel) * 4)
118 #define CPMAC_REG_END 0x0680
121 * TODO: use some of them to fill stats in cpmac_stats()
123 #define CPMAC_STATS_RX_GOOD 0x0200
124 #define CPMAC_STATS_RX_BCAST 0x0204
125 #define CPMAC_STATS_RX_MCAST 0x0208
126 #define CPMAC_STATS_RX_PAUSE 0x020c
127 #define CPMAC_STATS_RX_CRC 0x0210
128 #define CPMAC_STATS_RX_ALIGN 0x0214
129 #define CPMAC_STATS_RX_OVER 0x0218
130 #define CPMAC_STATS_RX_JABBER 0x021c
131 #define CPMAC_STATS_RX_UNDER 0x0220
132 #define CPMAC_STATS_RX_FRAG 0x0224
133 #define CPMAC_STATS_RX_FILTER 0x0228
134 #define CPMAC_STATS_RX_QOSFILTER 0x022c
135 #define CPMAC_STATS_RX_OCTETS 0x0230
137 #define CPMAC_STATS_TX_GOOD 0x0234
138 #define CPMAC_STATS_TX_BCAST 0x0238
139 #define CPMAC_STATS_TX_MCAST 0x023c
140 #define CPMAC_STATS_TX_PAUSE 0x0240
141 #define CPMAC_STATS_TX_DEFER 0x0244
142 #define CPMAC_STATS_TX_COLLISION 0x0248
143 #define CPMAC_STATS_TX_SINGLECOLL 0x024c
144 #define CPMAC_STATS_TX_MULTICOLL 0x0250
145 #define CPMAC_STATS_TX_EXCESSCOLL 0x0254
146 #define CPMAC_STATS_TX_LATECOLL 0x0258
147 #define CPMAC_STATS_TX_UNDERRUN 0x025c
148 #define CPMAC_STATS_TX_CARRIERSENSE 0x0260
149 #define CPMAC_STATS_TX_OCTETS 0x0264
151 #define cpmac_read(base, reg) (readl((void __iomem *)(base) + (reg)))
152 #define cpmac_write(base, reg, val) (writel(val, (void __iomem *)(base) + \
156 #define CPMAC_MDIO_VERSION 0x0000
157 #define CPMAC_MDIO_CONTROL 0x0004
158 # define MDIOC_IDLE 0x80000000
159 # define MDIOC_ENABLE 0x40000000
160 # define MDIOC_PREAMBLE 0x00100000
161 # define MDIOC_FAULT 0x00080000
162 # define MDIOC_FAULTDETECT 0x00040000
163 # define MDIOC_INTTEST 0x00020000
164 # define MDIOC_CLKDIV(div) ((div) & 0xff)
165 #define CPMAC_MDIO_ALIVE 0x0008
166 #define CPMAC_MDIO_LINK 0x000c
167 #define CPMAC_MDIO_ACCESS(channel) (0x0080 + (channel) * 8)
168 # define MDIO_BUSY 0x80000000
169 # define MDIO_WRITE 0x40000000
170 # define MDIO_REG(reg) (((reg) & 0x1f) << 21)
171 # define MDIO_PHY(phy) (((phy) & 0x1f) << 16)
172 # define MDIO_DATA(data) ((data) & 0xffff)
173 #define CPMAC_MDIO_PHYSEL(channel) (0x0084 + (channel) * 8)
174 # define PHYSEL_LINKSEL 0x00000040
175 # define PHYSEL_LINKINT 0x00000020
184 #define CPMAC_SOP 0x8000
185 #define CPMAC_EOP 0x4000
186 #define CPMAC_OWN 0x2000
187 #define CPMAC_EOQ 0x1000
189 struct cpmac_desc *next;
191 dma_addr_t data_mapping;
197 struct cpmac_desc *rx_head;
199 struct cpmac_desc *desc_ring;
202 struct mii_bus *mii_bus;
203 struct phy_device *phy;
204 char phy_name[BUS_ID_SIZE];
205 int oldlink, oldspeed, oldduplex;
207 struct net_device *dev;
208 struct work_struct reset_work;
209 struct platform_device *pdev;
210 struct napi_struct napi;
213 static irqreturn_t cpmac_irq(int, void *);
214 static void cpmac_hw_start(struct net_device *dev);
215 static void cpmac_hw_stop(struct net_device *dev);
216 static int cpmac_stop(struct net_device *dev);
217 static int cpmac_open(struct net_device *dev);
219 static void cpmac_dump_regs(struct net_device *dev)
222 struct cpmac_priv *priv = netdev_priv(dev);
223 for (i = 0; i < CPMAC_REG_END; i += 4) {
227 printk(KERN_DEBUG "%s: reg[%p]:", dev->name,
230 printk(" %08x", cpmac_read(priv->regs, i));
235 static void cpmac_dump_desc(struct net_device *dev, struct cpmac_desc *desc)
238 printk(KERN_DEBUG "%s: desc[%p]:", dev->name, desc);
239 for (i = 0; i < sizeof(*desc) / 4; i++)
240 printk(" %08x", ((u32 *)desc)[i]);
244 static void cpmac_dump_skb(struct net_device *dev, struct sk_buff *skb)
247 printk(KERN_DEBUG "%s: skb 0x%p, len=%d\n", dev->name, skb, skb->len);
248 for (i = 0; i < skb->len; i++) {
252 printk(KERN_DEBUG "%s: data[%p]:", dev->name,
255 printk(" %02x", ((u8 *)skb->data)[i]);
260 static int cpmac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
264 while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
266 cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_REG(reg) |
268 while ((val = cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0))) & MDIO_BUSY)
270 return MDIO_DATA(val);
273 static int cpmac_mdio_write(struct mii_bus *bus, int phy_id,
276 while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
278 cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_WRITE |
279 MDIO_REG(reg) | MDIO_PHY(phy_id) | MDIO_DATA(val));
283 static int cpmac_mdio_reset(struct mii_bus *bus)
285 ar7_device_reset(AR7_RESET_BIT_MDIO);
286 cpmac_write(bus->priv, CPMAC_MDIO_CONTROL, MDIOC_ENABLE |
287 MDIOC_CLKDIV(ar7_cpmac_freq() / 2200000 - 1));
291 static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, };
293 static struct mii_bus cpmac_mii = {
295 .read = cpmac_mdio_read,
296 .write = cpmac_mdio_write,
297 .reset = cpmac_mdio_reset,
301 static int cpmac_config(struct net_device *dev, struct ifmap *map)
303 if (dev->flags & IFF_UP)
306 /* Don't allow changing the I/O address */
307 if (map->base_addr != dev->base_addr)
310 /* ignore other fields */
314 static void cpmac_set_multicast_list(struct net_device *dev)
316 struct dev_mc_list *iter;
319 u32 mbp, bit, hash[2] = { 0, };
320 struct cpmac_priv *priv = netdev_priv(dev);
322 mbp = cpmac_read(priv->regs, CPMAC_MBP);
323 if (dev->flags & IFF_PROMISC) {
324 cpmac_write(priv->regs, CPMAC_MBP, (mbp & ~MBP_PROMISCCHAN(0)) |
327 cpmac_write(priv->regs, CPMAC_MBP, mbp & ~MBP_RXPROMISC);
328 if (dev->flags & IFF_ALLMULTI) {
329 /* enable all multicast mode */
330 cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, 0xffffffff);
331 cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, 0xffffffff);
334 * cpmac uses some strange mac address hashing
337 for (i = 0, iter = dev->mc_list; i < dev->mc_count;
338 i++, iter = iter->next) {
340 tmp = iter->dmi_addr[0];
341 bit ^= (tmp >> 2) ^ (tmp << 4);
342 tmp = iter->dmi_addr[1];
343 bit ^= (tmp >> 4) ^ (tmp << 2);
344 tmp = iter->dmi_addr[2];
345 bit ^= (tmp >> 6) ^ tmp;
346 tmp = iter->dmi_addr[3];
347 bit ^= (tmp >> 2) ^ (tmp << 4);
348 tmp = iter->dmi_addr[4];
349 bit ^= (tmp >> 4) ^ (tmp << 2);
350 tmp = iter->dmi_addr[5];
351 bit ^= (tmp >> 6) ^ tmp;
353 hash[bit / 32] |= 1 << (bit % 32);
356 cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, hash[0]);
357 cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, hash[1]);
362 static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
363 struct cpmac_desc *desc)
365 struct sk_buff *skb, *result = NULL;
367 if (unlikely(netif_msg_hw(priv)))
368 cpmac_dump_desc(priv->dev, desc);
369 cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
370 if (unlikely(!desc->datalen)) {
371 if (netif_msg_rx_err(priv) && net_ratelimit())
372 printk(KERN_WARNING "%s: rx: spurious interrupt\n",
377 skb = netdev_alloc_skb(priv->dev, CPMAC_SKB_SIZE);
380 skb_put(desc->skb, desc->datalen);
381 desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
382 desc->skb->ip_summed = CHECKSUM_NONE;
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 printk(KERN_DEBUG "%s: received packet:\n",
396 cpmac_dump_skb(priv->dev, result);
399 if (netif_msg_rx_err(priv) && net_ratelimit())
401 "%s: low on skbs, dropping packet\n",
403 priv->dev->stats.rx_dropped++;
406 desc->buflen = CPMAC_SKB_SIZE;
407 desc->dataflags = CPMAC_OWN;
412 static int cpmac_poll(struct napi_struct *napi, int budget)
415 struct cpmac_desc *desc;
417 struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
419 spin_lock(&priv->rx_lock);
420 if (unlikely(!priv->rx_head)) {
421 if (netif_msg_rx_err(priv) && net_ratelimit())
422 printk(KERN_WARNING "%s: rx: polling, but no queue\n",
424 netif_rx_complete(priv->dev, napi);
428 desc = priv->rx_head;
429 while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
430 skb = cpmac_rx_one(priv, desc);
432 netif_receive_skb(skb);
438 priv->rx_head = desc;
439 spin_unlock(&priv->rx_lock);
440 if (unlikely(netif_msg_rx_status(priv)))
441 printk(KERN_DEBUG "%s: poll processed %d packets\n",
442 priv->dev->name, received);
443 if (desc->dataflags & CPMAC_OWN) {
444 netif_rx_complete(priv->dev, napi);
445 cpmac_write(priv->regs, CPMAC_RX_PTR(0), (u32)desc->mapping);
446 cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
453 static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
456 struct cpmac_desc *desc;
457 struct cpmac_priv *priv = netdev_priv(dev);
459 if (unlikely(skb_padto(skb, ETH_ZLEN)))
462 len = max(skb->len, ETH_ZLEN);
463 queue = skb_get_queue_mapping(skb);
464 #ifdef CONFIG_NETDEVICES_MULTIQUEUE
465 netif_stop_subqueue(dev, queue);
467 netif_stop_queue(dev);
470 desc = &priv->desc_ring[queue];
471 if (unlikely(desc->dataflags & CPMAC_OWN)) {
472 if (netif_msg_tx_err(priv) && net_ratelimit())
473 printk(KERN_WARNING "%s: tx dma ring full\n",
475 return NETDEV_TX_BUSY;
478 spin_lock(&priv->lock);
479 dev->trans_start = jiffies;
480 spin_unlock(&priv->lock);
481 desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
483 desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
485 desc->hw_data = (u32)desc->data_mapping;
488 if (unlikely(netif_msg_tx_queued(priv)))
489 printk(KERN_DEBUG "%s: sending 0x%p, len=%d\n", dev->name, skb,
491 if (unlikely(netif_msg_hw(priv)))
492 cpmac_dump_desc(dev, desc);
493 if (unlikely(netif_msg_pktdata(priv)))
494 cpmac_dump_skb(dev, skb);
495 cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
500 static void cpmac_end_xmit(struct net_device *dev, int queue)
502 struct cpmac_desc *desc;
503 struct cpmac_priv *priv = netdev_priv(dev);
505 desc = &priv->desc_ring[queue];
506 cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
507 if (likely(desc->skb)) {
508 spin_lock(&priv->lock);
509 dev->stats.tx_packets++;
510 dev->stats.tx_bytes += desc->skb->len;
511 spin_unlock(&priv->lock);
512 dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
515 if (unlikely(netif_msg_tx_done(priv)))
516 printk(KERN_DEBUG "%s: sent 0x%p, len=%d\n", dev->name,
517 desc->skb, desc->skb->len);
519 dev_kfree_skb_irq(desc->skb);
521 #ifdef CONFIG_NETDEVICES_MULTIQUEUE
522 if (netif_subqueue_stopped(dev, queue))
523 netif_wake_subqueue(dev, queue);
525 if (netif_queue_stopped(dev))
526 netif_wake_queue(dev);
529 if (netif_msg_tx_err(priv) && net_ratelimit())
531 "%s: end_xmit: spurious interrupt\n", dev->name);
532 #ifdef CONFIG_NETDEVICES_MULTIQUEUE
533 if (netif_subqueue_stopped(dev, queue))
534 netif_wake_subqueue(dev, queue);
536 if (netif_queue_stopped(dev))
537 netif_wake_queue(dev);
542 static void cpmac_hw_stop(struct net_device *dev)
545 struct cpmac_priv *priv = netdev_priv(dev);
546 struct plat_cpmac_data *pdata = priv->pdev->dev.platform_data;
548 ar7_device_reset(pdata->reset_bit);
549 cpmac_write(priv->regs, CPMAC_RX_CONTROL,
550 cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
551 cpmac_write(priv->regs, CPMAC_TX_CONTROL,
552 cpmac_read(priv->regs, CPMAC_TX_CONTROL) & ~1);
553 for (i = 0; i < 8; i++) {
554 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
555 cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
557 cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
558 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
559 cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
560 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
561 cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
562 cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
565 static void cpmac_hw_start(struct net_device *dev)
568 struct cpmac_priv *priv = netdev_priv(dev);
569 struct plat_cpmac_data *pdata = priv->pdev->dev.platform_data;
571 ar7_device_reset(pdata->reset_bit);
572 for (i = 0; i < 8; i++) {
573 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
574 cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
576 cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
578 cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
580 cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
581 for (i = 0; i < 8; i++)
582 cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
583 cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
584 cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
585 (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
586 (dev->dev_addr[3] << 24));
587 cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
588 cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
589 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
590 cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
591 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
592 cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
593 cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
594 cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
595 cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
597 cpmac_write(priv->regs, CPMAC_RX_CONTROL,
598 cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
599 cpmac_write(priv->regs, CPMAC_TX_CONTROL,
600 cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
601 cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
602 cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
606 static void cpmac_clear_rx(struct net_device *dev)
608 struct cpmac_priv *priv = netdev_priv(dev);
609 struct cpmac_desc *desc;
611 if (unlikely(!priv->rx_head))
613 desc = priv->rx_head;
614 for (i = 0; i < priv->ring_size; i++) {
615 if ((desc->dataflags & CPMAC_OWN) == 0) {
616 if (netif_msg_rx_err(priv) && net_ratelimit())
617 printk(KERN_WARNING "%s: packet dropped\n",
619 if (unlikely(netif_msg_hw(priv)))
620 cpmac_dump_desc(dev, desc);
621 desc->dataflags = CPMAC_OWN;
622 dev->stats.rx_dropped++;
628 static void cpmac_clear_tx(struct net_device *dev)
630 struct cpmac_priv *priv = netdev_priv(dev);
632 if (unlikely(!priv->desc_ring))
634 for (i = 0; i < CPMAC_QUEUES; i++) {
635 priv->desc_ring[i].dataflags = 0;
636 if (priv->desc_ring[i].skb) {
637 dev_kfree_skb_any(priv->desc_ring[i].skb);
638 if (netif_subqueue_stopped(dev, i))
639 netif_wake_subqueue(dev, i);
644 static void cpmac_hw_error(struct work_struct *work)
646 struct cpmac_priv *priv =
647 container_of(work, struct cpmac_priv, reset_work);
649 spin_lock(&priv->rx_lock);
650 cpmac_clear_rx(priv->dev);
651 spin_unlock(&priv->rx_lock);
652 cpmac_clear_tx(priv->dev);
653 cpmac_hw_start(priv->dev);
654 napi_enable(&priv->napi);
655 netif_start_queue(priv->dev);
658 static irqreturn_t cpmac_irq(int irq, void *dev_id)
660 struct net_device *dev = dev_id;
661 struct cpmac_priv *priv;
665 priv = netdev_priv(dev);
667 status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
669 if (unlikely(netif_msg_intr(priv)))
670 printk(KERN_DEBUG "%s: interrupt status: 0x%08x\n", dev->name,
673 if (status & MAC_INT_TX)
674 cpmac_end_xmit(dev, (status & 7));
676 if (status & MAC_INT_RX) {
677 queue = (status >> 8) & 7;
678 if (netif_rx_schedule_prep(dev, &priv->napi)) {
679 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
680 __netif_rx_schedule(dev, &priv->napi);
684 cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
686 if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS))) {
687 if (netif_msg_drv(priv) && net_ratelimit())
688 printk(KERN_ERR "%s: hw error, resetting...\n",
690 netif_stop_queue(dev);
691 napi_disable(&priv->napi);
693 schedule_work(&priv->reset_work);
694 if (unlikely(netif_msg_hw(priv)))
695 cpmac_dump_regs(dev);
701 static void cpmac_tx_timeout(struct net_device *dev)
703 struct cpmac_priv *priv = netdev_priv(dev);
706 spin_lock(&priv->lock);
707 dev->stats.tx_errors++;
708 spin_unlock(&priv->lock);
709 if (netif_msg_tx_err(priv) && net_ratelimit())
710 printk(KERN_WARNING "%s: transmit timeout\n", dev->name);
712 * FIXME: waking up random queue is not the best thing to
713 * do... on the other hand why we got here at all?
715 #ifdef CONFIG_NETDEVICES_MULTIQUEUE
716 for (i = 0; i < CPMAC_QUEUES; i++)
717 if (priv->desc_ring[i].skb) {
718 priv->desc_ring[i].dataflags = 0;
719 dev_kfree_skb_any(priv->desc_ring[i].skb);
720 netif_wake_subqueue(dev, i);
724 priv->desc_ring[0].dataflags = 0;
725 if (priv->desc_ring[0].skb)
726 dev_kfree_skb_any(priv->desc_ring[0].skb);
727 netif_wake_queue(dev);
731 static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
733 struct cpmac_priv *priv = netdev_priv(dev);
734 if (!(netif_running(dev)))
738 if ((cmd == SIOCGMIIPHY) || (cmd == SIOCGMIIREG) ||
739 (cmd == SIOCSMIIREG))
740 return phy_mii_ioctl(priv->phy, if_mii(ifr), cmd);
745 static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
747 struct cpmac_priv *priv = netdev_priv(dev);
750 return phy_ethtool_gset(priv->phy, cmd);
755 static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
757 struct cpmac_priv *priv = netdev_priv(dev);
759 if (!capable(CAP_NET_ADMIN))
763 return phy_ethtool_sset(priv->phy, cmd);
768 static void cpmac_get_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
770 struct cpmac_priv *priv = netdev_priv(dev);
772 ring->rx_max_pending = 1024;
773 ring->rx_mini_max_pending = 1;
774 ring->rx_jumbo_max_pending = 1;
775 ring->tx_max_pending = 1;
777 ring->rx_pending = priv->ring_size;
778 ring->rx_mini_pending = 1;
779 ring->rx_jumbo_pending = 1;
780 ring->tx_pending = 1;
783 static int cpmac_set_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
785 struct cpmac_priv *priv = netdev_priv(dev);
787 if (netif_running(dev))
789 priv->ring_size = ring->rx_pending;
793 static void cpmac_get_drvinfo(struct net_device *dev,
794 struct ethtool_drvinfo *info)
796 strcpy(info->driver, "cpmac");
797 strcpy(info->version, CPMAC_VERSION);
798 info->fw_version[0] = '\0';
799 sprintf(info->bus_info, "%s", "cpmac");
800 info->regdump_len = 0;
803 static const struct ethtool_ops cpmac_ethtool_ops = {
804 .get_settings = cpmac_get_settings,
805 .set_settings = cpmac_set_settings,
806 .get_drvinfo = cpmac_get_drvinfo,
807 .get_link = ethtool_op_get_link,
808 .get_ringparam = cpmac_get_ringparam,
809 .set_ringparam = cpmac_set_ringparam,
812 static void cpmac_adjust_link(struct net_device *dev)
814 struct cpmac_priv *priv = netdev_priv(dev);
817 spin_lock(&priv->lock);
818 if (priv->phy->link) {
819 netif_start_queue(dev);
820 if (priv->phy->duplex != priv->oldduplex) {
822 priv->oldduplex = priv->phy->duplex;
825 if (priv->phy->speed != priv->oldspeed) {
827 priv->oldspeed = priv->phy->speed;
830 if (!priv->oldlink) {
835 } else if (priv->oldlink) {
836 netif_stop_queue(dev);
840 priv->oldduplex = -1;
843 if (new_state && netif_msg_link(priv) && net_ratelimit())
844 phy_print_status(priv->phy);
846 spin_unlock(&priv->lock);
849 static int cpmac_open(struct net_device *dev)
852 struct cpmac_priv *priv = netdev_priv(dev);
853 struct resource *mem;
854 struct cpmac_desc *desc;
857 mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
858 if (!request_mem_region(mem->start, mem->end - mem->start, dev->name)) {
859 if (netif_msg_drv(priv))
860 printk(KERN_ERR "%s: failed to request registers\n",
866 priv->regs = ioremap(mem->start, mem->end - mem->start);
868 if (netif_msg_drv(priv))
869 printk(KERN_ERR "%s: failed to remap registers\n",
875 size = priv->ring_size + CPMAC_QUEUES;
876 priv->desc_ring = dma_alloc_coherent(&dev->dev,
877 sizeof(struct cpmac_desc) * size,
880 if (!priv->desc_ring) {
885 for (i = 0; i < size; i++)
886 priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
888 priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
889 for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
890 skb = netdev_alloc_skb(dev, CPMAC_SKB_SIZE);
891 if (unlikely(!skb)) {
897 desc->data_mapping = dma_map_single(&dev->dev, skb->data,
900 desc->hw_data = (u32)desc->data_mapping;
901 desc->buflen = CPMAC_SKB_SIZE;
902 desc->dataflags = CPMAC_OWN;
903 desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
904 desc->hw_next = (u32)desc->next->mapping;
907 if ((res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED,
909 if (netif_msg_drv(priv))
910 printk(KERN_ERR "%s: failed to obtain irq\n",
915 INIT_WORK(&priv->reset_work, cpmac_hw_error);
918 napi_enable(&priv->napi);
919 priv->phy->state = PHY_CHANGELINK;
920 phy_start(priv->phy);
926 for (i = 0; i < priv->ring_size; i++) {
927 if (priv->rx_head[i].skb) {
928 dma_unmap_single(&dev->dev,
929 priv->rx_head[i].data_mapping,
932 kfree_skb(priv->rx_head[i].skb);
936 kfree(priv->desc_ring);
940 release_mem_region(mem->start, mem->end - mem->start);
946 static int cpmac_stop(struct net_device *dev)
949 struct cpmac_priv *priv = netdev_priv(dev);
950 struct resource *mem;
952 netif_stop_queue(dev);
954 cancel_work_sync(&priv->reset_work);
955 napi_disable(&priv->napi);
960 for (i = 0; i < 8; i++)
961 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
962 cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
963 cpmac_write(priv->regs, CPMAC_MBP, 0);
965 free_irq(dev->irq, dev);
967 mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
968 release_mem_region(mem->start, mem->end - mem->start);
969 priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
970 for (i = 0; i < priv->ring_size; i++) {
971 if (priv->rx_head[i].skb) {
972 dma_unmap_single(&dev->dev,
973 priv->rx_head[i].data_mapping,
976 kfree_skb(priv->rx_head[i].skb);
980 dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
981 (CPMAC_QUEUES + priv->ring_size),
982 priv->desc_ring, priv->dma_ring);
986 static int external_switch;
988 static int __devinit cpmac_probe(struct platform_device *pdev)
991 char *mdio_bus_id = "0";
992 struct resource *mem;
993 struct cpmac_priv *priv;
994 struct net_device *dev;
995 struct plat_cpmac_data *pdata;
996 DECLARE_MAC_BUF(mac);
998 pdata = pdev->dev.platform_data;
1000 for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
1001 if (!(pdata->phy_mask & (1 << phy_id)))
1003 if (!cpmac_mii.phy_map[phy_id])
1008 if (phy_id == PHY_MAX_ADDR) {
1009 if (external_switch || dumb_switch) {
1010 struct fixed_phy_status status = {};
1013 * FIXME: this should be in the platform code!
1014 * Since there is not platform code at all (that is,
1015 * no mainline users of that driver), place it here
1022 fixed_phy_add(PHY_POLL, phy_id, &status);
1024 printk(KERN_ERR "cpmac: no PHY present\n");
1029 dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
1032 printk(KERN_ERR "cpmac: Unable to allocate net_device\n");
1036 platform_set_drvdata(pdev, dev);
1037 priv = netdev_priv(dev);
1040 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
1046 dev->irq = platform_get_irq_byname(pdev, "irq");
1048 dev->open = cpmac_open;
1049 dev->stop = cpmac_stop;
1050 dev->set_config = cpmac_config;
1051 dev->hard_start_xmit = cpmac_start_xmit;
1052 dev->do_ioctl = cpmac_ioctl;
1053 dev->set_multicast_list = cpmac_set_multicast_list;
1054 dev->tx_timeout = cpmac_tx_timeout;
1055 dev->ethtool_ops = &cpmac_ethtool_ops;
1056 dev->features |= NETIF_F_MULTI_QUEUE;
1058 netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
1060 spin_lock_init(&priv->lock);
1061 spin_lock_init(&priv->rx_lock);
1063 priv->ring_size = 64;
1064 priv->msg_enable = netif_msg_init(debug_level, 0xff);
1065 memcpy(dev->dev_addr, pdata->dev_addr, sizeof(dev->dev_addr));
1067 snprintf(priv->phy_name, BUS_ID_SIZE, PHY_ID_FMT, mdio_bus_id, phy_id);
1069 priv->phy = phy_connect(dev, priv->phy_name, &cpmac_adjust_link, 0,
1070 PHY_INTERFACE_MODE_MII);
1071 if (IS_ERR(priv->phy)) {
1072 if (netif_msg_drv(priv))
1073 printk(KERN_ERR "%s: Could not attach to PHY\n",
1075 return PTR_ERR(priv->phy);
1078 if ((rc = register_netdev(dev))) {
1079 printk(KERN_ERR "cpmac: error %i registering device %s\n", rc,
1084 if (netif_msg_probe(priv)) {
1086 "cpmac: device %s (regs: %p, irq: %d, phy: %s, "
1087 "mac: %s)\n", dev->name, (void *)mem->start, dev->irq,
1088 priv->phy_name, print_mac(mac, dev->dev_addr));
1097 static int __devexit cpmac_remove(struct platform_device *pdev)
1099 struct net_device *dev = platform_get_drvdata(pdev);
1100 unregister_netdev(dev);
1105 static struct platform_driver cpmac_driver = {
1106 .driver.name = "cpmac",
1107 .driver.owner = THIS_MODULE,
1108 .probe = cpmac_probe,
1109 .remove = __devexit_p(cpmac_remove),
1112 int __devinit cpmac_init(void)
1117 cpmac_mii.priv = ioremap(AR7_REGS_MDIO, 256);
1119 if (!cpmac_mii.priv) {
1120 printk(KERN_ERR "Can't ioremap mdio registers\n");
1124 #warning FIXME: unhardcode gpio&reset bits
1125 ar7_gpio_disable(26);
1126 ar7_gpio_disable(27);
1127 ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
1128 ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
1129 ar7_device_reset(AR7_RESET_BIT_EPHY);
1131 cpmac_mii.reset(&cpmac_mii);
1133 for (i = 0; i < 300000; i++)
1134 if ((mask = cpmac_read(cpmac_mii.priv, CPMAC_MDIO_ALIVE)))
1140 if (mask & (mask - 1)) {
1141 external_switch = 1;
1145 cpmac_mii.phy_mask = ~(mask | 0x80000000);
1146 snprintf(cpmac_mii.id, MII_BUS_ID_SIZE, "0");
1148 res = mdiobus_register(&cpmac_mii);
1152 res = platform_driver_register(&cpmac_driver);
1159 mdiobus_unregister(&cpmac_mii);
1162 iounmap(cpmac_mii.priv);
1167 void __devexit cpmac_exit(void)
1169 platform_driver_unregister(&cpmac_driver);
1170 mdiobus_unregister(&cpmac_mii);
1171 iounmap(cpmac_mii.priv);
1174 module_init(cpmac_init);
1175 module_exit(cpmac_exit);