blob: 1d8ef39f370fd9efdcc4a6596ced02b4feba4ddd [file] [log] [blame]
Matteo Croced95b39c2007-10-14 18:10:13 +02001/*
2 * Copyright (C) 2006, 2007 Eugene Konev
3 *
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.
8 *
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.
13 *
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
17 */
18
19#include <linux/module.h>
Alexey Dobriyan539d3ee2011-06-10 03:36:43 +000020#include <linux/interrupt.h>
Matteo Croced95b39c2007-10-14 18:10:13 +020021#include <linux/moduleparam.h>
22
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>
Matteo Croced95b39c2007-10-14 18:10:13 +020029
30#include <linux/netdevice.h>
Florian Fainelli30765d02010-03-07 00:55:26 +000031#include <linux/if_vlan.h>
Matteo Croced95b39c2007-10-14 18:10:13 +020032#include <linux/etherdevice.h>
33#include <linux/ethtool.h>
34#include <linux/skbuff.h>
35#include <linux/mii.h>
36#include <linux/phy.h>
Eugene Konevb88219f2007-10-24 10:42:03 +080037#include <linux/phy_fixed.h>
Matteo Croced95b39c2007-10-14 18:10:13 +020038#include <linux/platform_device.h>
39#include <linux/dma-mapping.h>
Florian Fainelli780019d2010-01-27 09:10:06 +010040#include <linux/clk.h>
Florian Fainelli559764d2010-08-08 10:09:39 +000041#include <linux/gpio.h>
Arun Sharma600634972011-07-26 16:09:06 -070042#include <linux/atomic.h>
Matteo Croced95b39c2007-10-14 18:10:13 +020043
44MODULE_AUTHOR("Eugene Konev <ejka@imfi.kspu.ru>");
45MODULE_DESCRIPTION("TI AR7 ethernet driver (CPMAC)");
46MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -070047MODULE_ALIAS("platform:cpmac");
Matteo Croced95b39c2007-10-14 18:10:13 +020048
49static int debug_level = 8;
50static int dumb_switch;
51
52/* Next 2 are only used in cpmac_probe, so it's pointless to change them */
53module_param(debug_level, int, 0444);
54module_param(dumb_switch, int, 0444);
55
56MODULE_PARM_DESC(debug_level, "Number of NETIF_MSG bits to enable");
57MODULE_PARM_DESC(dumb_switch, "Assume switch is not connected to MDIO bus");
58
Florian Fainelli25dc27d2010-03-07 00:55:50 +000059#define CPMAC_VERSION "0.5.2"
Florian Fainelli30765d02010-03-07 00:55:26 +000060/* frame size + 802.1q tag + FCS size */
61#define CPMAC_SKB_SIZE (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN)
Matteo Croced95b39c2007-10-14 18:10:13 +020062#define CPMAC_QUEUES 8
63
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
Varka Bhadramaf595152014-07-10 11:05:39 +053070#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)
Matteo Croced95b39c2007-10-14 18:10:13 +020082#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
Varka Bhadramaf595152014-07-10 11:05:39 +053087#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
Matteo Croced95b39c2007-10-14 18:10:13 +020095#define CPMAC_MAC_STATUS 0x0164
Varka Bhadramaf595152014-07-10 11:05:39 +053096#define MAC_STATUS_QOS 0x00000004
97#define MAC_STATUS_RXFLOW 0x00000002
98#define MAC_STATUS_TXFLOW 0x00000001
Matteo Croced95b39c2007-10-14 18:10:13 +020099#define CPMAC_TX_INT_ENABLE 0x0178
100#define CPMAC_TX_INT_CLEAR 0x017c
101#define CPMAC_MAC_INT_VECTOR 0x0180
Varka Bhadramaf595152014-07-10 11:05:39 +0530102#define MAC_INT_STATUS 0x00080000
103#define MAC_INT_HOST 0x00040000
104#define MAC_INT_RX 0x00020000
105#define MAC_INT_TX 0x00010000
Matteo Croced95b39c2007-10-14 18:10:13 +0200106#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
Florian Fainelli559764d2010-08-08 10:09:39 +0000111#define CPMAC_MAC_ADDR_LO(channel) (0x01b0 + (channel) * 4)
Matteo Croced95b39c2007-10-14 18:10:13 +0200112#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
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530121
122/* Rx/Tx statistics
Matteo Croced95b39c2007-10-14 18:10:13 +0200123 * TODO: use some of them to fill stats in cpmac_stats()
124 */
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
138
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
152
153#define cpmac_read(base, reg) (readl((void __iomem *)(base) + (reg)))
154#define cpmac_write(base, reg, val) (writel(val, (void __iomem *)(base) + \
155 (reg)))
156
157/* MDIO bus */
158#define CPMAC_MDIO_VERSION 0x0000
159#define CPMAC_MDIO_CONTROL 0x0004
Varka Bhadramaf595152014-07-10 11:05:39 +0530160#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)
Matteo Croced95b39c2007-10-14 18:10:13 +0200167#define CPMAC_MDIO_ALIVE 0x0008
168#define CPMAC_MDIO_LINK 0x000c
169#define CPMAC_MDIO_ACCESS(channel) (0x0080 + (channel) * 8)
Varka Bhadramaf595152014-07-10 11:05:39 +0530170#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)
Matteo Croced95b39c2007-10-14 18:10:13 +0200175#define CPMAC_MDIO_PHYSEL(channel) (0x0084 + (channel) * 8)
Varka Bhadramaf595152014-07-10 11:05:39 +0530176#define PHYSEL_LINKSEL 0x00000040
177#define PHYSEL_LINKINT 0x00000020
Matteo Croced95b39c2007-10-14 18:10:13 +0200178
179struct cpmac_desc {
180 u32 hw_next;
181 u32 hw_data;
182 u16 buflen;
183 u16 bufflags;
184 u16 datalen;
185 u16 dataflags;
186#define CPMAC_SOP 0x8000
187#define CPMAC_EOP 0x4000
188#define CPMAC_OWN 0x2000
189#define CPMAC_EOQ 0x1000
190 struct sk_buff *skb;
191 struct cpmac_desc *next;
Matteo Crocef917d582008-05-14 00:58:32 +0200192 struct cpmac_desc *prev;
Matteo Croced95b39c2007-10-14 18:10:13 +0200193 dma_addr_t mapping;
194 dma_addr_t data_mapping;
195};
196
197struct cpmac_priv {
198 spinlock_t lock;
199 spinlock_t rx_lock;
200 struct cpmac_desc *rx_head;
201 int ring_size;
202 struct cpmac_desc *desc_ring;
203 dma_addr_t dma_ring;
204 void __iomem *regs;
205 struct mii_bus *mii_bus;
206 struct phy_device *phy;
David S. Miller21a8cfe2009-05-26 21:10:22 -0700207 char phy_name[MII_BUS_ID_SIZE + 3];
Matteo Croced95b39c2007-10-14 18:10:13 +0200208 int oldlink, oldspeed, oldduplex;
209 u32 msg_enable;
210 struct net_device *dev;
211 struct work_struct reset_work;
212 struct platform_device *pdev;
Eugene Konev67d129d2007-10-24 10:42:02 +0800213 struct napi_struct napi;
Matteo Crocef917d582008-05-14 00:58:32 +0200214 atomic_t reset_pending;
Matteo Croced95b39c2007-10-14 18:10:13 +0200215};
216
217static irqreturn_t cpmac_irq(int, void *);
218static void cpmac_hw_start(struct net_device *dev);
219static void cpmac_hw_stop(struct net_device *dev);
220static int cpmac_stop(struct net_device *dev);
221static int cpmac_open(struct net_device *dev);
222
223static void cpmac_dump_regs(struct net_device *dev)
224{
225 int i;
226 struct cpmac_priv *priv = netdev_priv(dev);
227 for (i = 0; i < CPMAC_REG_END; i += 4) {
228 if (i % 16 == 0) {
229 if (i)
Florian Fainelli559764d2010-08-08 10:09:39 +0000230 pr_cont("\n");
Matteo Croced95b39c2007-10-14 18:10:13 +0200231 printk(KERN_DEBUG "%s: reg[%p]:", dev->name,
232 priv->regs + i);
233 }
234 printk(" %08x", cpmac_read(priv->regs, i));
235 }
236 printk("\n");
237}
238
239static void cpmac_dump_desc(struct net_device *dev, struct cpmac_desc *desc)
240{
241 int i;
242 printk(KERN_DEBUG "%s: desc[%p]:", dev->name, desc);
243 for (i = 0; i < sizeof(*desc) / 4; i++)
244 printk(" %08x", ((u32 *)desc)[i]);
245 printk("\n");
246}
247
Matteo Crocef917d582008-05-14 00:58:32 +0200248static void cpmac_dump_all_desc(struct net_device *dev)
249{
250 struct cpmac_priv *priv = netdev_priv(dev);
251 struct cpmac_desc *dump = priv->rx_head;
252 do {
253 cpmac_dump_desc(dev, dump);
254 dump = dump->next;
255 } while (dump != priv->rx_head);
256}
257
Matteo Croced95b39c2007-10-14 18:10:13 +0200258static void cpmac_dump_skb(struct net_device *dev, struct sk_buff *skb)
259{
260 int i;
261 printk(KERN_DEBUG "%s: skb 0x%p, len=%d\n", dev->name, skb, skb->len);
262 for (i = 0; i < skb->len; i++) {
263 if (i % 16 == 0) {
264 if (i)
Florian Fainelli559764d2010-08-08 10:09:39 +0000265 pr_cont("\n");
Matteo Croced95b39c2007-10-14 18:10:13 +0200266 printk(KERN_DEBUG "%s: data[%p]:", dev->name,
267 skb->data + i);
268 }
269 printk(" %02x", ((u8 *)skb->data)[i]);
270 }
271 printk("\n");
272}
273
274static int cpmac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
275{
276 u32 val;
277
278 while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
279 cpu_relax();
280 cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_REG(reg) |
281 MDIO_PHY(phy_id));
282 while ((val = cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0))) & MDIO_BUSY)
283 cpu_relax();
284 return MDIO_DATA(val);
285}
286
287static int cpmac_mdio_write(struct mii_bus *bus, int phy_id,
288 int reg, u16 val)
289{
290 while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
291 cpu_relax();
292 cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_WRITE |
293 MDIO_REG(reg) | MDIO_PHY(phy_id) | MDIO_DATA(val));
294 return 0;
295}
296
297static int cpmac_mdio_reset(struct mii_bus *bus)
298{
Florian Fainelli780019d2010-01-27 09:10:06 +0100299 struct clk *cpmac_clk;
300
301 cpmac_clk = clk_get(&bus->dev, "cpmac");
302 if (IS_ERR(cpmac_clk)) {
303 printk(KERN_ERR "unable to get cpmac clock\n");
304 return -1;
305 }
Matteo Croced95b39c2007-10-14 18:10:13 +0200306 ar7_device_reset(AR7_RESET_BIT_MDIO);
307 cpmac_write(bus->priv, CPMAC_MDIO_CONTROL, MDIOC_ENABLE |
Florian Fainelli780019d2010-01-27 09:10:06 +0100308 MDIOC_CLKDIV(clk_get_rate(cpmac_clk) / 2200000 - 1));
Matteo Croced95b39c2007-10-14 18:10:13 +0200309 return 0;
310}
311
312static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, };
313
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700314static struct mii_bus *cpmac_mii;
Matteo Croced95b39c2007-10-14 18:10:13 +0200315
Matteo Croced95b39c2007-10-14 18:10:13 +0200316static void cpmac_set_multicast_list(struct net_device *dev)
317{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000318 struct netdev_hw_addr *ha;
Matteo Croced95b39c2007-10-14 18:10:13 +0200319 u8 tmp;
320 u32 mbp, bit, hash[2] = { 0, };
321 struct cpmac_priv *priv = netdev_priv(dev);
322
323 mbp = cpmac_read(priv->regs, CPMAC_MBP);
324 if (dev->flags & IFF_PROMISC) {
325 cpmac_write(priv->regs, CPMAC_MBP, (mbp & ~MBP_PROMISCCHAN(0)) |
326 MBP_RXPROMISC);
327 } else {
328 cpmac_write(priv->regs, CPMAC_MBP, mbp & ~MBP_RXPROMISC);
329 if (dev->flags & IFF_ALLMULTI) {
330 /* enable all multicast mode */
331 cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, 0xffffffff);
332 cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, 0xffffffff);
333 } else {
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530334 /* cpmac uses some strange mac address hashing
Matteo Croced95b39c2007-10-14 18:10:13 +0200335 * (not crc32)
336 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000337 netdev_for_each_mc_addr(ha, dev) {
Matteo Croced95b39c2007-10-14 18:10:13 +0200338 bit = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000339 tmp = ha->addr[0];
Matteo Croced95b39c2007-10-14 18:10:13 +0200340 bit ^= (tmp >> 2) ^ (tmp << 4);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000341 tmp = ha->addr[1];
Matteo Croced95b39c2007-10-14 18:10:13 +0200342 bit ^= (tmp >> 4) ^ (tmp << 2);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000343 tmp = ha->addr[2];
Matteo Croced95b39c2007-10-14 18:10:13 +0200344 bit ^= (tmp >> 6) ^ tmp;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000345 tmp = ha->addr[3];
Matteo Croced95b39c2007-10-14 18:10:13 +0200346 bit ^= (tmp >> 2) ^ (tmp << 4);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000347 tmp = ha->addr[4];
Matteo Croced95b39c2007-10-14 18:10:13 +0200348 bit ^= (tmp >> 4) ^ (tmp << 2);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000349 tmp = ha->addr[5];
Matteo Croced95b39c2007-10-14 18:10:13 +0200350 bit ^= (tmp >> 6) ^ tmp;
351 bit &= 0x3f;
352 hash[bit / 32] |= 1 << (bit % 32);
353 }
354
355 cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, hash[0]);
356 cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, hash[1]);
357 }
358 }
359}
360
Eugene Konev67d129d2007-10-24 10:42:02 +0800361static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
Matteo Croced95b39c2007-10-14 18:10:13 +0200362 struct cpmac_desc *desc)
363{
364 struct sk_buff *skb, *result = NULL;
365
366 if (unlikely(netif_msg_hw(priv)))
Eugene Konev67d129d2007-10-24 10:42:02 +0800367 cpmac_dump_desc(priv->dev, desc);
Matteo Croced95b39c2007-10-14 18:10:13 +0200368 cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
369 if (unlikely(!desc->datalen)) {
370 if (netif_msg_rx_err(priv) && net_ratelimit())
371 printk(KERN_WARNING "%s: rx: spurious interrupt\n",
Eugene Konev67d129d2007-10-24 10:42:02 +0800372 priv->dev->name);
Matteo Croced95b39c2007-10-14 18:10:13 +0200373 return NULL;
374 }
375
Eric Dumazet89d71a62009-10-13 05:34:20 +0000376 skb = netdev_alloc_skb_ip_align(priv->dev, CPMAC_SKB_SIZE);
Matteo Croced95b39c2007-10-14 18:10:13 +0200377 if (likely(skb)) {
Matteo Croced95b39c2007-10-14 18:10:13 +0200378 skb_put(desc->skb, desc->datalen);
Eugene Konev67d129d2007-10-24 10:42:02 +0800379 desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700380 skb_checksum_none_assert(desc->skb);
Eugene Konev67d129d2007-10-24 10:42:02 +0800381 priv->dev->stats.rx_packets++;
382 priv->dev->stats.rx_bytes += desc->datalen;
Matteo Croced95b39c2007-10-14 18:10:13 +0200383 result = desc->skb;
Eugene Konev67d129d2007-10-24 10:42:02 +0800384 dma_unmap_single(&priv->dev->dev, desc->data_mapping,
385 CPMAC_SKB_SIZE, DMA_FROM_DEVICE);
Matteo Croced95b39c2007-10-14 18:10:13 +0200386 desc->skb = skb;
Eugene Konev67d129d2007-10-24 10:42:02 +0800387 desc->data_mapping = dma_map_single(&priv->dev->dev, skb->data,
Matteo Croced95b39c2007-10-14 18:10:13 +0200388 CPMAC_SKB_SIZE,
389 DMA_FROM_DEVICE);
390 desc->hw_data = (u32)desc->data_mapping;
391 if (unlikely(netif_msg_pktdata(priv))) {
Eugene Konev67d129d2007-10-24 10:42:02 +0800392 printk(KERN_DEBUG "%s: received packet:\n",
393 priv->dev->name);
394 cpmac_dump_skb(priv->dev, result);
Matteo Croced95b39c2007-10-14 18:10:13 +0200395 }
396 } else {
397 if (netif_msg_rx_err(priv) && net_ratelimit())
398 printk(KERN_WARNING
Eugene Konev67d129d2007-10-24 10:42:02 +0800399 "%s: low on skbs, dropping packet\n",
400 priv->dev->name);
401 priv->dev->stats.rx_dropped++;
Matteo Croced95b39c2007-10-14 18:10:13 +0200402 }
403
404 desc->buflen = CPMAC_SKB_SIZE;
405 desc->dataflags = CPMAC_OWN;
406
407 return result;
408}
409
Eugene Konev67d129d2007-10-24 10:42:02 +0800410static int cpmac_poll(struct napi_struct *napi, int budget)
Matteo Croced95b39c2007-10-14 18:10:13 +0200411{
412 struct sk_buff *skb;
Matteo Crocef917d582008-05-14 00:58:32 +0200413 struct cpmac_desc *desc, *restart;
Eugene Konev67d129d2007-10-24 10:42:02 +0800414 struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
Matteo Crocef917d582008-05-14 00:58:32 +0200415 int received = 0, processed = 0;
Matteo Croced95b39c2007-10-14 18:10:13 +0200416
417 spin_lock(&priv->rx_lock);
418 if (unlikely(!priv->rx_head)) {
419 if (netif_msg_rx_err(priv) && net_ratelimit())
420 printk(KERN_WARNING "%s: rx: polling, but no queue\n",
Eugene Konev67d129d2007-10-24 10:42:02 +0800421 priv->dev->name);
Matteo Crocef917d582008-05-14 00:58:32 +0200422 spin_unlock(&priv->rx_lock);
Ben Hutchings288379f2009-01-19 16:43:59 -0800423 napi_complete(napi);
Matteo Croced95b39c2007-10-14 18:10:13 +0200424 return 0;
425 }
426
427 desc = priv->rx_head;
Matteo Crocef917d582008-05-14 00:58:32 +0200428 restart = NULL;
Eugene Konev67d129d2007-10-24 10:42:02 +0800429 while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
Matteo Crocef917d582008-05-14 00:58:32 +0200430 processed++;
431
432 if ((desc->dataflags & CPMAC_EOQ) != 0) {
433 /* The last update to eoq->hw_next didn't happen
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530434 * soon enough, and the receiver stopped here.
435 * Remember this descriptor so we can restart
436 * the receiver after freeing some space.
437 */
Matteo Crocef917d582008-05-14 00:58:32 +0200438 if (unlikely(restart)) {
439 if (netif_msg_rx_err(priv))
440 printk(KERN_ERR "%s: poll found a"
441 " duplicate EOQ: %p and %p\n",
442 priv->dev->name, restart, desc);
443 goto fatal_error;
444 }
445
446 restart = desc->next;
447 }
448
Eugene Konev67d129d2007-10-24 10:42:02 +0800449 skb = cpmac_rx_one(priv, desc);
Matteo Croced95b39c2007-10-14 18:10:13 +0200450 if (likely(skb)) {
451 netif_receive_skb(skb);
452 received++;
453 }
454 desc = desc->next;
455 }
456
Matteo Crocef917d582008-05-14 00:58:32 +0200457 if (desc != priv->rx_head) {
458 /* We freed some buffers, but not the whole ring,
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530459 * add what we did free to the rx list
460 */
Matteo Crocef917d582008-05-14 00:58:32 +0200461 desc->prev->hw_next = (u32)0;
462 priv->rx_head->prev->hw_next = priv->rx_head->mapping;
463 }
464
465 /* Optimization: If we did not actually process an EOQ (perhaps because
466 * of quota limits), check to see if the tail of the queue has EOQ set.
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530467 * We should immediately restart in that case so that the receiver can
468 * restart and run in parallel with more packet processing.
469 * This lets us handle slightly larger bursts before running
470 * out of ring space (assuming dev->weight < ring_size)
471 */
Matteo Crocef917d582008-05-14 00:58:32 +0200472
473 if (!restart &&
474 (priv->rx_head->prev->dataflags & (CPMAC_OWN|CPMAC_EOQ))
475 == CPMAC_EOQ &&
476 (priv->rx_head->dataflags & CPMAC_OWN) != 0) {
477 /* reset EOQ so the poll loop (above) doesn't try to
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530478 * restart this when it eventually gets to this descriptor.
479 */
Matteo Crocef917d582008-05-14 00:58:32 +0200480 priv->rx_head->prev->dataflags &= ~CPMAC_EOQ;
481 restart = priv->rx_head;
482 }
483
484 if (restart) {
485 priv->dev->stats.rx_errors++;
486 priv->dev->stats.rx_fifo_errors++;
487 if (netif_msg_rx_err(priv) && net_ratelimit())
488 printk(KERN_WARNING "%s: rx dma ring overrun\n",
489 priv->dev->name);
490
491 if (unlikely((restart->dataflags & CPMAC_OWN) == 0)) {
492 if (netif_msg_drv(priv))
493 printk(KERN_ERR "%s: cpmac_poll is trying to "
494 "restart rx from a descriptor that's "
495 "not free: %p\n",
496 priv->dev->name, restart);
Julia Lawall9e1634a2010-08-05 10:28:31 +0000497 goto fatal_error;
Matteo Crocef917d582008-05-14 00:58:32 +0200498 }
499
500 cpmac_write(priv->regs, CPMAC_RX_PTR(0), restart->mapping);
501 }
502
Matteo Croced95b39c2007-10-14 18:10:13 +0200503 priv->rx_head = desc;
504 spin_unlock(&priv->rx_lock);
Matteo Croced95b39c2007-10-14 18:10:13 +0200505 if (unlikely(netif_msg_rx_status(priv)))
Eugene Konev67d129d2007-10-24 10:42:02 +0800506 printk(KERN_DEBUG "%s: poll processed %d packets\n",
507 priv->dev->name, received);
Matteo Crocef917d582008-05-14 00:58:32 +0200508 if (processed == 0) {
509 /* we ran out of packets to read,
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530510 * revert to interrupt-driven mode
511 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800512 napi_complete(napi);
Matteo Croced95b39c2007-10-14 18:10:13 +0200513 cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
514 return 0;
515 }
516
517 return 1;
Matteo Crocef917d582008-05-14 00:58:32 +0200518
519fatal_error:
520 /* Something went horribly wrong.
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530521 * Reset hardware to try to recover rather than wedging.
522 */
Matteo Crocef917d582008-05-14 00:58:32 +0200523 if (netif_msg_drv(priv)) {
524 printk(KERN_ERR "%s: cpmac_poll is confused. "
525 "Resetting hardware\n", priv->dev->name);
526 cpmac_dump_all_desc(priv->dev);
527 printk(KERN_DEBUG "%s: RX_PTR(0)=0x%08x RX_ACK(0)=0x%08x\n",
528 priv->dev->name,
529 cpmac_read(priv->regs, CPMAC_RX_PTR(0)),
530 cpmac_read(priv->regs, CPMAC_RX_ACK(0)));
531 }
532
533 spin_unlock(&priv->rx_lock);
Ben Hutchings288379f2009-01-19 16:43:59 -0800534 napi_complete(napi);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700535 netif_tx_stop_all_queues(priv->dev);
Matteo Crocef917d582008-05-14 00:58:32 +0200536 napi_disable(&priv->napi);
537
538 atomic_inc(&priv->reset_pending);
539 cpmac_hw_stop(priv->dev);
540 if (!schedule_work(&priv->reset_work))
541 atomic_dec(&priv->reset_pending);
542 return 0;
543
Matteo Croced95b39c2007-10-14 18:10:13 +0200544}
545
546static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
547{
548 int queue, len;
549 struct cpmac_desc *desc;
550 struct cpmac_priv *priv = netdev_priv(dev);
551
Matteo Crocef917d582008-05-14 00:58:32 +0200552 if (unlikely(atomic_read(&priv->reset_pending)))
553 return NETDEV_TX_BUSY;
554
Matteo Croce6cd043d2007-10-23 19:12:22 +0200555 if (unlikely(skb_padto(skb, ETH_ZLEN)))
556 return NETDEV_TX_OK;
Matteo Croced95b39c2007-10-14 18:10:13 +0200557
558 len = max(skb->len, ETH_ZLEN);
Matteo Croceba596a02008-01-12 19:05:23 +0100559 queue = skb_get_queue_mapping(skb);
Matteo Croced95b39c2007-10-14 18:10:13 +0200560 netif_stop_subqueue(dev, queue);
Matteo Croced95b39c2007-10-14 18:10:13 +0200561
562 desc = &priv->desc_ring[queue];
563 if (unlikely(desc->dataflags & CPMAC_OWN)) {
564 if (netif_msg_tx_err(priv) && net_ratelimit())
Matteo Croce6cd043d2007-10-23 19:12:22 +0200565 printk(KERN_WARNING "%s: tx dma ring full\n",
Matteo Croced95b39c2007-10-14 18:10:13 +0200566 dev->name);
Matteo Croce6cd043d2007-10-23 19:12:22 +0200567 return NETDEV_TX_BUSY;
Matteo Croced95b39c2007-10-14 18:10:13 +0200568 }
569
570 spin_lock(&priv->lock);
Matteo Croced95b39c2007-10-14 18:10:13 +0200571 spin_unlock(&priv->lock);
572 desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
573 desc->skb = skb;
574 desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
575 DMA_TO_DEVICE);
576 desc->hw_data = (u32)desc->data_mapping;
577 desc->datalen = len;
578 desc->buflen = len;
579 if (unlikely(netif_msg_tx_queued(priv)))
580 printk(KERN_DEBUG "%s: sending 0x%p, len=%d\n", dev->name, skb,
581 skb->len);
582 if (unlikely(netif_msg_hw(priv)))
583 cpmac_dump_desc(dev, desc);
584 if (unlikely(netif_msg_pktdata(priv)))
585 cpmac_dump_skb(dev, skb);
586 cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
587
Matteo Croce6cd043d2007-10-23 19:12:22 +0200588 return NETDEV_TX_OK;
Matteo Croced95b39c2007-10-14 18:10:13 +0200589}
590
591static void cpmac_end_xmit(struct net_device *dev, int queue)
592{
593 struct cpmac_desc *desc;
594 struct cpmac_priv *priv = netdev_priv(dev);
595
596 desc = &priv->desc_ring[queue];
597 cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
598 if (likely(desc->skb)) {
599 spin_lock(&priv->lock);
600 dev->stats.tx_packets++;
601 dev->stats.tx_bytes += desc->skb->len;
602 spin_unlock(&priv->lock);
603 dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
604 DMA_TO_DEVICE);
605
606 if (unlikely(netif_msg_tx_done(priv)))
607 printk(KERN_DEBUG "%s: sent 0x%p, len=%d\n", dev->name,
608 desc->skb, desc->skb->len);
609
610 dev_kfree_skb_irq(desc->skb);
611 desc->skb = NULL;
Stefan Weil0220ff72009-05-31 10:59:15 +0000612 if (__netif_subqueue_stopped(dev, queue))
Matteo Croced95b39c2007-10-14 18:10:13 +0200613 netif_wake_subqueue(dev, queue);
Matteo Croced95b39c2007-10-14 18:10:13 +0200614 } else {
615 if (netif_msg_tx_err(priv) && net_ratelimit())
616 printk(KERN_WARNING
617 "%s: end_xmit: spurious interrupt\n", dev->name);
Stefan Weil0220ff72009-05-31 10:59:15 +0000618 if (__netif_subqueue_stopped(dev, queue))
Matteo Croced95b39c2007-10-14 18:10:13 +0200619 netif_wake_subqueue(dev, queue);
Matteo Croced95b39c2007-10-14 18:10:13 +0200620 }
621}
622
623static void cpmac_hw_stop(struct net_device *dev)
624{
625 int i;
626 struct cpmac_priv *priv = netdev_priv(dev);
Jingoo Hana0ea2ac2013-08-30 14:05:02 +0900627 struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200628
629 ar7_device_reset(pdata->reset_bit);
630 cpmac_write(priv->regs, CPMAC_RX_CONTROL,
631 cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
632 cpmac_write(priv->regs, CPMAC_TX_CONTROL,
633 cpmac_read(priv->regs, CPMAC_TX_CONTROL) & ~1);
634 for (i = 0; i < 8; i++) {
635 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
636 cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
637 }
638 cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
639 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
640 cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
641 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
642 cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
643 cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
644}
645
646static void cpmac_hw_start(struct net_device *dev)
647{
648 int i;
649 struct cpmac_priv *priv = netdev_priv(dev);
Jingoo Hana0ea2ac2013-08-30 14:05:02 +0900650 struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200651
652 ar7_device_reset(pdata->reset_bit);
653 for (i = 0; i < 8; i++) {
654 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
655 cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
656 }
657 cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
658
659 cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
660 MBP_RXMCAST);
661 cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
662 for (i = 0; i < 8; i++)
663 cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
664 cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
665 cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
666 (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
667 (dev->dev_addr[3] << 24));
668 cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
669 cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
670 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
671 cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
672 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
673 cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
674 cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
675 cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
676 cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
677
678 cpmac_write(priv->regs, CPMAC_RX_CONTROL,
679 cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
680 cpmac_write(priv->regs, CPMAC_TX_CONTROL,
681 cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
682 cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
683 cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
684 MAC_FDX);
685}
686
687static void cpmac_clear_rx(struct net_device *dev)
688{
689 struct cpmac_priv *priv = netdev_priv(dev);
690 struct cpmac_desc *desc;
691 int i;
692 if (unlikely(!priv->rx_head))
693 return;
694 desc = priv->rx_head;
695 for (i = 0; i < priv->ring_size; i++) {
696 if ((desc->dataflags & CPMAC_OWN) == 0) {
697 if (netif_msg_rx_err(priv) && net_ratelimit())
698 printk(KERN_WARNING "%s: packet dropped\n",
699 dev->name);
700 if (unlikely(netif_msg_hw(priv)))
701 cpmac_dump_desc(dev, desc);
702 desc->dataflags = CPMAC_OWN;
703 dev->stats.rx_dropped++;
704 }
Matteo Crocef917d582008-05-14 00:58:32 +0200705 desc->hw_next = desc->next->mapping;
Matteo Croced95b39c2007-10-14 18:10:13 +0200706 desc = desc->next;
707 }
Matteo Crocef917d582008-05-14 00:58:32 +0200708 priv->rx_head->prev->hw_next = 0;
Matteo Croced95b39c2007-10-14 18:10:13 +0200709}
710
711static void cpmac_clear_tx(struct net_device *dev)
712{
713 struct cpmac_priv *priv = netdev_priv(dev);
714 int i;
715 if (unlikely(!priv->desc_ring))
716 return;
Matteo Croce6cd043d2007-10-23 19:12:22 +0200717 for (i = 0; i < CPMAC_QUEUES; i++) {
718 priv->desc_ring[i].dataflags = 0;
Matteo Croced95b39c2007-10-14 18:10:13 +0200719 if (priv->desc_ring[i].skb) {
720 dev_kfree_skb_any(priv->desc_ring[i].skb);
Matteo Crocef917d582008-05-14 00:58:32 +0200721 priv->desc_ring[i].skb = NULL;
Matteo Croced95b39c2007-10-14 18:10:13 +0200722 }
Matteo Croce6cd043d2007-10-23 19:12:22 +0200723 }
Matteo Croced95b39c2007-10-14 18:10:13 +0200724}
725
726static void cpmac_hw_error(struct work_struct *work)
727{
728 struct cpmac_priv *priv =
729 container_of(work, struct cpmac_priv, reset_work);
730
731 spin_lock(&priv->rx_lock);
732 cpmac_clear_rx(priv->dev);
733 spin_unlock(&priv->rx_lock);
734 cpmac_clear_tx(priv->dev);
735 cpmac_hw_start(priv->dev);
Matteo Crocef917d582008-05-14 00:58:32 +0200736 barrier();
737 atomic_dec(&priv->reset_pending);
738
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700739 netif_tx_wake_all_queues(priv->dev);
Matteo Crocef917d582008-05-14 00:58:32 +0200740 cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
741}
742
743static void cpmac_check_status(struct net_device *dev)
744{
745 struct cpmac_priv *priv = netdev_priv(dev);
746
747 u32 macstatus = cpmac_read(priv->regs, CPMAC_MAC_STATUS);
748 int rx_channel = (macstatus >> 8) & 7;
749 int rx_code = (macstatus >> 12) & 15;
750 int tx_channel = (macstatus >> 16) & 7;
751 int tx_code = (macstatus >> 20) & 15;
752
753 if (rx_code || tx_code) {
754 if (netif_msg_drv(priv) && net_ratelimit()) {
755 /* Can't find any documentation on what these
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530756 * error codes actually are. So just log them and hope..
Matteo Crocef917d582008-05-14 00:58:32 +0200757 */
758 if (rx_code)
759 printk(KERN_WARNING "%s: host error %d on rx "
760 "channel %d (macstatus %08x), resetting\n",
761 dev->name, rx_code, rx_channel, macstatus);
762 if (tx_code)
763 printk(KERN_WARNING "%s: host error %d on tx "
764 "channel %d (macstatus %08x), resetting\n",
765 dev->name, tx_code, tx_channel, macstatus);
766 }
767
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700768 netif_tx_stop_all_queues(dev);
Matteo Crocef917d582008-05-14 00:58:32 +0200769 cpmac_hw_stop(dev);
770 if (schedule_work(&priv->reset_work))
771 atomic_inc(&priv->reset_pending);
772 if (unlikely(netif_msg_hw(priv)))
773 cpmac_dump_regs(dev);
774 }
775 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
Matteo Croced95b39c2007-10-14 18:10:13 +0200776}
777
778static irqreturn_t cpmac_irq(int irq, void *dev_id)
779{
780 struct net_device *dev = dev_id;
781 struct cpmac_priv *priv;
782 int queue;
783 u32 status;
784
Matteo Croced95b39c2007-10-14 18:10:13 +0200785 priv = netdev_priv(dev);
786
787 status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
788
789 if (unlikely(netif_msg_intr(priv)))
790 printk(KERN_DEBUG "%s: interrupt status: 0x%08x\n", dev->name,
791 status);
792
793 if (status & MAC_INT_TX)
794 cpmac_end_xmit(dev, (status & 7));
795
796 if (status & MAC_INT_RX) {
797 queue = (status >> 8) & 7;
Ben Hutchings288379f2009-01-19 16:43:59 -0800798 if (napi_schedule_prep(&priv->napi)) {
Eugene Konev67d129d2007-10-24 10:42:02 +0800799 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
Ben Hutchings288379f2009-01-19 16:43:59 -0800800 __napi_schedule(&priv->napi);
Eugene Konev67d129d2007-10-24 10:42:02 +0800801 }
Matteo Croced95b39c2007-10-14 18:10:13 +0200802 }
803
804 cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
805
Matteo Crocef917d582008-05-14 00:58:32 +0200806 if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
807 cpmac_check_status(dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200808
809 return IRQ_HANDLED;
810}
811
812static void cpmac_tx_timeout(struct net_device *dev)
813{
Matteo Crocef917d582008-05-14 00:58:32 +0200814 struct cpmac_priv *priv = netdev_priv(dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200815
816 spin_lock(&priv->lock);
817 dev->stats.tx_errors++;
818 spin_unlock(&priv->lock);
819 if (netif_msg_tx_err(priv) && net_ratelimit())
820 printk(KERN_WARNING "%s: transmit timeout\n", dev->name);
Matteo Crocef917d582008-05-14 00:58:32 +0200821
822 atomic_inc(&priv->reset_pending);
823 barrier();
824 cpmac_clear_tx(dev);
825 barrier();
826 atomic_dec(&priv->reset_pending);
827
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700828 netif_tx_wake_all_queues(priv->dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200829}
830
831static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
832{
833 struct cpmac_priv *priv = netdev_priv(dev);
834 if (!(netif_running(dev)))
835 return -EINVAL;
836 if (!priv->phy)
837 return -EINVAL;
Matteo Croced95b39c2007-10-14 18:10:13 +0200838
Richard Cochran28b04112010-07-17 08:48:55 +0000839 return phy_mii_ioctl(priv->phy, ifr, cmd);
Matteo Croced95b39c2007-10-14 18:10:13 +0200840}
841
842static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
843{
844 struct cpmac_priv *priv = netdev_priv(dev);
845
846 if (priv->phy)
847 return phy_ethtool_gset(priv->phy, cmd);
848
849 return -EINVAL;
850}
851
852static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
853{
854 struct cpmac_priv *priv = netdev_priv(dev);
855
856 if (!capable(CAP_NET_ADMIN))
857 return -EPERM;
858
859 if (priv->phy)
860 return phy_ethtool_sset(priv->phy, cmd);
861
862 return -EINVAL;
863}
864
Florian Fainelli559764d2010-08-08 10:09:39 +0000865static void cpmac_get_ringparam(struct net_device *dev,
866 struct ethtool_ringparam *ring)
Matteo Croced95b39c2007-10-14 18:10:13 +0200867{
868 struct cpmac_priv *priv = netdev_priv(dev);
869
870 ring->rx_max_pending = 1024;
871 ring->rx_mini_max_pending = 1;
872 ring->rx_jumbo_max_pending = 1;
873 ring->tx_max_pending = 1;
874
875 ring->rx_pending = priv->ring_size;
876 ring->rx_mini_pending = 1;
877 ring->rx_jumbo_pending = 1;
878 ring->tx_pending = 1;
879}
880
Florian Fainelli559764d2010-08-08 10:09:39 +0000881static int cpmac_set_ringparam(struct net_device *dev,
882 struct ethtool_ringparam *ring)
Matteo Croced95b39c2007-10-14 18:10:13 +0200883{
884 struct cpmac_priv *priv = netdev_priv(dev);
885
Matteo Croce6cd043d2007-10-23 19:12:22 +0200886 if (netif_running(dev))
Matteo Croced95b39c2007-10-14 18:10:13 +0200887 return -EBUSY;
888 priv->ring_size = ring->rx_pending;
889 return 0;
890}
891
892static void cpmac_get_drvinfo(struct net_device *dev,
893 struct ethtool_drvinfo *info)
894{
Jiri Pirko7826d432013-01-06 00:44:26 +0000895 strlcpy(info->driver, "cpmac", sizeof(info->driver));
896 strlcpy(info->version, CPMAC_VERSION, sizeof(info->version));
897 snprintf(info->bus_info, sizeof(info->bus_info), "%s", "cpmac");
Matteo Croced95b39c2007-10-14 18:10:13 +0200898 info->regdump_len = 0;
899}
900
901static const struct ethtool_ops cpmac_ethtool_ops = {
902 .get_settings = cpmac_get_settings,
903 .set_settings = cpmac_set_settings,
904 .get_drvinfo = cpmac_get_drvinfo,
905 .get_link = ethtool_op_get_link,
906 .get_ringparam = cpmac_get_ringparam,
907 .set_ringparam = cpmac_set_ringparam,
908};
909
910static void cpmac_adjust_link(struct net_device *dev)
911{
912 struct cpmac_priv *priv = netdev_priv(dev);
913 int new_state = 0;
914
915 spin_lock(&priv->lock);
916 if (priv->phy->link) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700917 netif_tx_start_all_queues(dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200918 if (priv->phy->duplex != priv->oldduplex) {
919 new_state = 1;
920 priv->oldduplex = priv->phy->duplex;
921 }
922
923 if (priv->phy->speed != priv->oldspeed) {
924 new_state = 1;
925 priv->oldspeed = priv->phy->speed;
926 }
927
928 if (!priv->oldlink) {
929 new_state = 1;
930 priv->oldlink = 1;
Matteo Croced95b39c2007-10-14 18:10:13 +0200931 }
932 } else if (priv->oldlink) {
Matteo Croced95b39c2007-10-14 18:10:13 +0200933 new_state = 1;
934 priv->oldlink = 0;
935 priv->oldspeed = 0;
936 priv->oldduplex = -1;
937 }
938
939 if (new_state && netif_msg_link(priv) && net_ratelimit())
940 phy_print_status(priv->phy);
941
942 spin_unlock(&priv->lock);
943}
944
945static int cpmac_open(struct net_device *dev)
946{
947 int i, size, res;
948 struct cpmac_priv *priv = netdev_priv(dev);
949 struct resource *mem;
950 struct cpmac_desc *desc;
951 struct sk_buff *skb;
952
Matteo Croced95b39c2007-10-14 18:10:13 +0200953 mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
Dan Carpenter7e307c72010-06-30 13:12:01 -0700954 if (!request_mem_region(mem->start, resource_size(mem), dev->name)) {
Matteo Croced95b39c2007-10-14 18:10:13 +0200955 if (netif_msg_drv(priv))
956 printk(KERN_ERR "%s: failed to request registers\n",
957 dev->name);
958 res = -ENXIO;
959 goto fail_reserve;
960 }
961
Dan Carpenter7e307c72010-06-30 13:12:01 -0700962 priv->regs = ioremap(mem->start, resource_size(mem));
Matteo Croced95b39c2007-10-14 18:10:13 +0200963 if (!priv->regs) {
964 if (netif_msg_drv(priv))
965 printk(KERN_ERR "%s: failed to remap registers\n",
966 dev->name);
967 res = -ENXIO;
968 goto fail_remap;
969 }
970
971 size = priv->ring_size + CPMAC_QUEUES;
972 priv->desc_ring = dma_alloc_coherent(&dev->dev,
973 sizeof(struct cpmac_desc) * size,
974 &priv->dma_ring,
975 GFP_KERNEL);
976 if (!priv->desc_ring) {
977 res = -ENOMEM;
978 goto fail_alloc;
979 }
980
981 for (i = 0; i < size; i++)
982 priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
983
984 priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
985 for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
Eric Dumazet89d71a62009-10-13 05:34:20 +0000986 skb = netdev_alloc_skb_ip_align(dev, CPMAC_SKB_SIZE);
Matteo Croced95b39c2007-10-14 18:10:13 +0200987 if (unlikely(!skb)) {
988 res = -ENOMEM;
989 goto fail_desc;
990 }
Matteo Croced95b39c2007-10-14 18:10:13 +0200991 desc->skb = skb;
992 desc->data_mapping = dma_map_single(&dev->dev, skb->data,
993 CPMAC_SKB_SIZE,
994 DMA_FROM_DEVICE);
995 desc->hw_data = (u32)desc->data_mapping;
996 desc->buflen = CPMAC_SKB_SIZE;
997 desc->dataflags = CPMAC_OWN;
998 desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
Matteo Crocef917d582008-05-14 00:58:32 +0200999 desc->next->prev = desc;
Matteo Croced95b39c2007-10-14 18:10:13 +02001000 desc->hw_next = (u32)desc->next->mapping;
1001 }
1002
Matteo Crocef917d582008-05-14 00:58:32 +02001003 priv->rx_head->prev->hw_next = (u32)0;
1004
Florian Fainelli559764d2010-08-08 10:09:39 +00001005 res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED, dev->name, dev);
1006 if (res) {
Matteo Croced95b39c2007-10-14 18:10:13 +02001007 if (netif_msg_drv(priv))
1008 printk(KERN_ERR "%s: failed to obtain irq\n",
1009 dev->name);
1010 goto fail_irq;
1011 }
1012
Matteo Crocef917d582008-05-14 00:58:32 +02001013 atomic_set(&priv->reset_pending, 0);
Matteo Croced95b39c2007-10-14 18:10:13 +02001014 INIT_WORK(&priv->reset_work, cpmac_hw_error);
1015 cpmac_hw_start(dev);
1016
Eugene Konev67d129d2007-10-24 10:42:02 +08001017 napi_enable(&priv->napi);
Matteo Croced95b39c2007-10-14 18:10:13 +02001018 priv->phy->state = PHY_CHANGELINK;
1019 phy_start(priv->phy);
1020
1021 return 0;
1022
1023fail_irq:
1024fail_desc:
1025 for (i = 0; i < priv->ring_size; i++) {
1026 if (priv->rx_head[i].skb) {
1027 dma_unmap_single(&dev->dev,
1028 priv->rx_head[i].data_mapping,
1029 CPMAC_SKB_SIZE,
1030 DMA_FROM_DEVICE);
1031 kfree_skb(priv->rx_head[i].skb);
1032 }
1033 }
1034fail_alloc:
1035 kfree(priv->desc_ring);
1036 iounmap(priv->regs);
1037
1038fail_remap:
Dan Carpenter7e307c72010-06-30 13:12:01 -07001039 release_mem_region(mem->start, resource_size(mem));
Matteo Croced95b39c2007-10-14 18:10:13 +02001040
1041fail_reserve:
Matteo Croced95b39c2007-10-14 18:10:13 +02001042 return res;
1043}
1044
1045static int cpmac_stop(struct net_device *dev)
1046{
1047 int i;
1048 struct cpmac_priv *priv = netdev_priv(dev);
1049 struct resource *mem;
1050
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001051 netif_tx_stop_all_queues(dev);
Matteo Croced95b39c2007-10-14 18:10:13 +02001052
1053 cancel_work_sync(&priv->reset_work);
Eugene Konev67d129d2007-10-24 10:42:02 +08001054 napi_disable(&priv->napi);
Matteo Croced95b39c2007-10-14 18:10:13 +02001055 phy_stop(priv->phy);
Matteo Croced95b39c2007-10-14 18:10:13 +02001056
1057 cpmac_hw_stop(dev);
1058
1059 for (i = 0; i < 8; i++)
1060 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
1061 cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
1062 cpmac_write(priv->regs, CPMAC_MBP, 0);
1063
1064 free_irq(dev->irq, dev);
1065 iounmap(priv->regs);
1066 mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
Dan Carpenter7e307c72010-06-30 13:12:01 -07001067 release_mem_region(mem->start, resource_size(mem));
Matteo Croced95b39c2007-10-14 18:10:13 +02001068 priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
1069 for (i = 0; i < priv->ring_size; i++) {
1070 if (priv->rx_head[i].skb) {
1071 dma_unmap_single(&dev->dev,
1072 priv->rx_head[i].data_mapping,
1073 CPMAC_SKB_SIZE,
1074 DMA_FROM_DEVICE);
1075 kfree_skb(priv->rx_head[i].skb);
1076 }
1077 }
1078
1079 dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
1080 (CPMAC_QUEUES + priv->ring_size),
1081 priv->desc_ring, priv->dma_ring);
1082 return 0;
1083}
1084
Alexander Beregalov63ef7d82009-04-15 12:52:36 +00001085static const struct net_device_ops cpmac_netdev_ops = {
1086 .ndo_open = cpmac_open,
1087 .ndo_stop = cpmac_stop,
1088 .ndo_start_xmit = cpmac_start_xmit,
1089 .ndo_tx_timeout = cpmac_tx_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001090 .ndo_set_rx_mode = cpmac_set_multicast_list,
Florian Fainelli6a9b6542009-06-24 16:32:33 -07001091 .ndo_do_ioctl = cpmac_ioctl,
Alexander Beregalov63ef7d82009-04-15 12:52:36 +00001092 .ndo_change_mtu = eth_change_mtu,
1093 .ndo_validate_addr = eth_validate_addr,
1094 .ndo_set_mac_address = eth_mac_addr,
1095};
1096
Matteo Croced95b39c2007-10-14 18:10:13 +02001097static int external_switch;
1098
Bill Pembertonf57ae662012-12-03 09:23:43 -05001099static int cpmac_probe(struct platform_device *pdev)
Matteo Croced95b39c2007-10-14 18:10:13 +02001100{
Florian Fainelli69bd4ae2009-05-31 10:57:07 +00001101 int rc, phy_id;
Florian Fainelli762c6aa2009-09-15 21:44:22 +00001102 char mdio_bus_id[MII_BUS_ID_SIZE];
Matteo Croced95b39c2007-10-14 18:10:13 +02001103 struct resource *mem;
1104 struct cpmac_priv *priv;
1105 struct net_device *dev;
1106 struct plat_cpmac_data *pdata;
1107
Jingoo Hana0ea2ac2013-08-30 14:05:02 +09001108 pdata = dev_get_platdata(&pdev->dev);
Matteo Croced95b39c2007-10-14 18:10:13 +02001109
Florian Fainelli76e61ea2009-08-04 10:52:52 +00001110 if (external_switch || dumb_switch) {
Florian Fainellia19c5d62012-02-13 01:23:20 +00001111 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
Florian Fainelli76e61ea2009-08-04 10:52:52 +00001112 phy_id = pdev->id;
1113 } else {
1114 for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
1115 if (!(pdata->phy_mask & (1 << phy_id)))
1116 continue;
1117 if (!cpmac_mii->phy_map[phy_id])
1118 continue;
Florian Fainelli762c6aa2009-09-15 21:44:22 +00001119 strncpy(mdio_bus_id, cpmac_mii->id, MII_BUS_ID_SIZE);
Florian Fainelli76e61ea2009-08-04 10:52:52 +00001120 break;
1121 }
Matteo Croced95b39c2007-10-14 18:10:13 +02001122 }
1123
1124 if (phy_id == PHY_MAX_ADDR) {
Florian Fainelli559764d2010-08-08 10:09:39 +00001125 dev_err(&pdev->dev, "no PHY present, falling back "
1126 "to switch on MDIO bus 0\n");
Florian Fainellia19c5d62012-02-13 01:23:20 +00001127 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
Florian Fainelli9fba1c32010-03-07 00:55:47 +00001128 phy_id = pdev->id;
Matteo Croced95b39c2007-10-14 18:10:13 +02001129 }
1130
1131 dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
Joe Perches41de8d42012-01-29 13:47:52 +00001132 if (!dev)
Matteo Croced95b39c2007-10-14 18:10:13 +02001133 return -ENOMEM;
Matteo Croced95b39c2007-10-14 18:10:13 +02001134
1135 platform_set_drvdata(pdev, dev);
1136 priv = netdev_priv(dev);
1137
1138 priv->pdev = pdev;
1139 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
1140 if (!mem) {
1141 rc = -ENODEV;
1142 goto fail;
1143 }
1144
1145 dev->irq = platform_get_irq_byname(pdev, "irq");
1146
Alexander Beregalov63ef7d82009-04-15 12:52:36 +00001147 dev->netdev_ops = &cpmac_netdev_ops;
1148 dev->ethtool_ops = &cpmac_ethtool_ops;
Matteo Croced95b39c2007-10-14 18:10:13 +02001149
Eugene Konev67d129d2007-10-24 10:42:02 +08001150 netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
1151
Matteo Croced95b39c2007-10-14 18:10:13 +02001152 spin_lock_init(&priv->lock);
1153 spin_lock_init(&priv->rx_lock);
1154 priv->dev = dev;
1155 priv->ring_size = 64;
1156 priv->msg_enable = netif_msg_init(debug_level, 0xff);
Julia Lawall2447f2f2009-12-13 05:35:45 +00001157 memcpy(dev->dev_addr, pdata->dev_addr, sizeof(pdata->dev_addr));
Eugene Konevb88219f2007-10-24 10:42:03 +08001158
Florian Fainelli559764d2010-08-08 10:09:39 +00001159 snprintf(priv->phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
1160 mdio_bus_id, phy_id);
Florian Fainelli76e61ea2009-08-04 10:52:52 +00001161
Florian Fainellif9a8f832013-01-14 00:52:52 +00001162 priv->phy = phy_connect(dev, priv->phy_name, cpmac_adjust_link,
1163 PHY_INTERFACE_MODE_MII);
Florian Fainelli76e61ea2009-08-04 10:52:52 +00001164
Eugene Konevb88219f2007-10-24 10:42:03 +08001165 if (IS_ERR(priv->phy)) {
1166 if (netif_msg_drv(priv))
1167 printk(KERN_ERR "%s: Could not attach to PHY\n",
1168 dev->name);
Florian Fainellied770f02010-06-20 22:07:48 +00001169 rc = PTR_ERR(priv->phy);
1170 goto fail;
Eugene Konevb88219f2007-10-24 10:42:03 +08001171 }
Matteo Croced95b39c2007-10-14 18:10:13 +02001172
Florian Fainelli559764d2010-08-08 10:09:39 +00001173 rc = register_netdev(dev);
1174 if (rc) {
Matteo Croced95b39c2007-10-14 18:10:13 +02001175 printk(KERN_ERR "cpmac: error %i registering device %s\n", rc,
1176 dev->name);
1177 goto fail;
1178 }
1179
1180 if (netif_msg_probe(priv)) {
1181 printk(KERN_INFO
Eugene Konevdf523b52007-10-24 10:42:01 +08001182 "cpmac: device %s (regs: %p, irq: %d, phy: %s, "
Johannes Berge1749612008-10-27 15:59:26 -07001183 "mac: %pM)\n", dev->name, (void *)mem->start, dev->irq,
1184 priv->phy_name, dev->dev_addr);
Matteo Croced95b39c2007-10-14 18:10:13 +02001185 }
1186 return 0;
1187
1188fail:
1189 free_netdev(dev);
1190 return rc;
1191}
1192
Bill Pembertonf57ae662012-12-03 09:23:43 -05001193static int cpmac_remove(struct platform_device *pdev)
Matteo Croced95b39c2007-10-14 18:10:13 +02001194{
1195 struct net_device *dev = platform_get_drvdata(pdev);
1196 unregister_netdev(dev);
1197 free_netdev(dev);
1198 return 0;
1199}
1200
1201static struct platform_driver cpmac_driver = {
1202 .driver.name = "cpmac",
Kay Sievers72abb462008-04-18 13:50:44 -07001203 .driver.owner = THIS_MODULE,
Matteo Croced95b39c2007-10-14 18:10:13 +02001204 .probe = cpmac_probe,
Bill Pembertonf57ae662012-12-03 09:23:43 -05001205 .remove = cpmac_remove,
Matteo Croced95b39c2007-10-14 18:10:13 +02001206};
1207
Bill Pembertonf57ae662012-12-03 09:23:43 -05001208int cpmac_init(void)
Matteo Croced95b39c2007-10-14 18:10:13 +02001209{
1210 u32 mask;
1211 int i, res;
1212
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001213 cpmac_mii = mdiobus_alloc();
1214 if (cpmac_mii == NULL)
1215 return -ENOMEM;
Matteo Croced95b39c2007-10-14 18:10:13 +02001216
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001217 cpmac_mii->name = "cpmac-mii";
1218 cpmac_mii->read = cpmac_mdio_read;
1219 cpmac_mii->write = cpmac_mdio_write;
1220 cpmac_mii->reset = cpmac_mdio_reset;
1221 cpmac_mii->irq = mii_irqs;
1222
1223 cpmac_mii->priv = ioremap(AR7_REGS_MDIO, 256);
1224
1225 if (!cpmac_mii->priv) {
Matteo Croced95b39c2007-10-14 18:10:13 +02001226 printk(KERN_ERR "Can't ioremap mdio registers\n");
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001227 res = -ENXIO;
1228 goto fail_alloc;
Matteo Croced95b39c2007-10-14 18:10:13 +02001229 }
1230
1231#warning FIXME: unhardcode gpio&reset bits
1232 ar7_gpio_disable(26);
1233 ar7_gpio_disable(27);
1234 ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
1235 ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
1236 ar7_device_reset(AR7_RESET_BIT_EPHY);
1237
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001238 cpmac_mii->reset(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001239
Florian Fainelli559764d2010-08-08 10:09:39 +00001240 for (i = 0; i < 300; i++) {
1241 mask = cpmac_read(cpmac_mii->priv, CPMAC_MDIO_ALIVE);
1242 if (mask)
Matteo Croced95b39c2007-10-14 18:10:13 +02001243 break;
1244 else
Florian Fainellie4540aa2009-08-04 10:52:57 +00001245 msleep(10);
Florian Fainelli559764d2010-08-08 10:09:39 +00001246 }
Matteo Croced95b39c2007-10-14 18:10:13 +02001247
1248 mask &= 0x7fffffff;
1249 if (mask & (mask - 1)) {
1250 external_switch = 1;
1251 mask = 0;
1252 }
1253
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001254 cpmac_mii->phy_mask = ~(mask | 0x80000000);
Florian Fainellid1733f02012-01-09 23:59:21 +00001255 snprintf(cpmac_mii->id, MII_BUS_ID_SIZE, "cpmac-1");
Matteo Croced95b39c2007-10-14 18:10:13 +02001256
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001257 res = mdiobus_register(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001258 if (res)
1259 goto fail_mii;
1260
1261 res = platform_driver_register(&cpmac_driver);
1262 if (res)
1263 goto fail_cpmac;
1264
1265 return 0;
1266
1267fail_cpmac:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001268 mdiobus_unregister(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001269
1270fail_mii:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001271 iounmap(cpmac_mii->priv);
1272
1273fail_alloc:
1274 mdiobus_free(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001275
1276 return res;
1277}
1278
Bill Pembertonf57ae662012-12-03 09:23:43 -05001279void cpmac_exit(void)
Matteo Croced95b39c2007-10-14 18:10:13 +02001280{
1281 platform_driver_unregister(&cpmac_driver);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001282 mdiobus_unregister(cpmac_mii);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001283 iounmap(cpmac_mii->priv);
Dan Carpenter48a29512010-03-02 22:46:10 +00001284 mdiobus_free(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001285}
1286
1287module_init(cpmac_init);
1288module_exit(cpmac_exit);