blob: 60777fd90b330ce8cc09b57caa2ed9b972f38e10 [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>
20#include <linux/init.h>
21#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>
Matteo Croced95b39c2007-10-14 18:10:13 +020041#include <asm/gpio.h>
Matteo Crocef917d582008-05-14 00:58:32 +020042#include <asm/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
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
121/*
122 * Rx/Tx statistics
123 * 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
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
178
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)
230 printk("\n");
231 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)
265 printk("\n");
266 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
316static int cpmac_config(struct net_device *dev, struct ifmap *map)
317{
318 if (dev->flags & IFF_UP)
319 return -EBUSY;
320
321 /* Don't allow changing the I/O address */
322 if (map->base_addr != dev->base_addr)
323 return -EOPNOTSUPP;
324
325 /* ignore other fields */
326 return 0;
327}
328
329static void cpmac_set_multicast_list(struct net_device *dev)
330{
331 struct dev_mc_list *iter;
Matteo Croced95b39c2007-10-14 18:10:13 +0200332 u8 tmp;
333 u32 mbp, bit, hash[2] = { 0, };
334 struct cpmac_priv *priv = netdev_priv(dev);
335
336 mbp = cpmac_read(priv->regs, CPMAC_MBP);
337 if (dev->flags & IFF_PROMISC) {
338 cpmac_write(priv->regs, CPMAC_MBP, (mbp & ~MBP_PROMISCCHAN(0)) |
339 MBP_RXPROMISC);
340 } else {
341 cpmac_write(priv->regs, CPMAC_MBP, mbp & ~MBP_RXPROMISC);
342 if (dev->flags & IFF_ALLMULTI) {
343 /* enable all multicast mode */
344 cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, 0xffffffff);
345 cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, 0xffffffff);
346 } else {
347 /*
348 * cpmac uses some strange mac address hashing
349 * (not crc32)
350 */
Jiri Pirkoe1d44472010-02-17 11:09:31 +0000351 netdev_for_each_mc_addr(iter, dev) {
Matteo Croced95b39c2007-10-14 18:10:13 +0200352 bit = 0;
353 tmp = iter->dmi_addr[0];
354 bit ^= (tmp >> 2) ^ (tmp << 4);
355 tmp = iter->dmi_addr[1];
356 bit ^= (tmp >> 4) ^ (tmp << 2);
357 tmp = iter->dmi_addr[2];
358 bit ^= (tmp >> 6) ^ tmp;
359 tmp = iter->dmi_addr[3];
360 bit ^= (tmp >> 2) ^ (tmp << 4);
361 tmp = iter->dmi_addr[4];
362 bit ^= (tmp >> 4) ^ (tmp << 2);
363 tmp = iter->dmi_addr[5];
364 bit ^= (tmp >> 6) ^ tmp;
365 bit &= 0x3f;
366 hash[bit / 32] |= 1 << (bit % 32);
367 }
368
369 cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, hash[0]);
370 cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, hash[1]);
371 }
372 }
373}
374
Eugene Konev67d129d2007-10-24 10:42:02 +0800375static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
Matteo Croced95b39c2007-10-14 18:10:13 +0200376 struct cpmac_desc *desc)
377{
378 struct sk_buff *skb, *result = NULL;
379
380 if (unlikely(netif_msg_hw(priv)))
Eugene Konev67d129d2007-10-24 10:42:02 +0800381 cpmac_dump_desc(priv->dev, desc);
Matteo Croced95b39c2007-10-14 18:10:13 +0200382 cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
383 if (unlikely(!desc->datalen)) {
384 if (netif_msg_rx_err(priv) && net_ratelimit())
385 printk(KERN_WARNING "%s: rx: spurious interrupt\n",
Eugene Konev67d129d2007-10-24 10:42:02 +0800386 priv->dev->name);
Matteo Croced95b39c2007-10-14 18:10:13 +0200387 return NULL;
388 }
389
Eric Dumazet89d71a62009-10-13 05:34:20 +0000390 skb = netdev_alloc_skb_ip_align(priv->dev, CPMAC_SKB_SIZE);
Matteo Croced95b39c2007-10-14 18:10:13 +0200391 if (likely(skb)) {
Matteo Croced95b39c2007-10-14 18:10:13 +0200392 skb_put(desc->skb, desc->datalen);
Eugene Konev67d129d2007-10-24 10:42:02 +0800393 desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200394 desc->skb->ip_summed = CHECKSUM_NONE;
Eugene Konev67d129d2007-10-24 10:42:02 +0800395 priv->dev->stats.rx_packets++;
396 priv->dev->stats.rx_bytes += desc->datalen;
Matteo Croced95b39c2007-10-14 18:10:13 +0200397 result = desc->skb;
Eugene Konev67d129d2007-10-24 10:42:02 +0800398 dma_unmap_single(&priv->dev->dev, desc->data_mapping,
399 CPMAC_SKB_SIZE, DMA_FROM_DEVICE);
Matteo Croced95b39c2007-10-14 18:10:13 +0200400 desc->skb = skb;
Eugene Konev67d129d2007-10-24 10:42:02 +0800401 desc->data_mapping = dma_map_single(&priv->dev->dev, skb->data,
Matteo Croced95b39c2007-10-14 18:10:13 +0200402 CPMAC_SKB_SIZE,
403 DMA_FROM_DEVICE);
404 desc->hw_data = (u32)desc->data_mapping;
405 if (unlikely(netif_msg_pktdata(priv))) {
Eugene Konev67d129d2007-10-24 10:42:02 +0800406 printk(KERN_DEBUG "%s: received packet:\n",
407 priv->dev->name);
408 cpmac_dump_skb(priv->dev, result);
Matteo Croced95b39c2007-10-14 18:10:13 +0200409 }
410 } else {
411 if (netif_msg_rx_err(priv) && net_ratelimit())
412 printk(KERN_WARNING
Eugene Konev67d129d2007-10-24 10:42:02 +0800413 "%s: low on skbs, dropping packet\n",
414 priv->dev->name);
415 priv->dev->stats.rx_dropped++;
Matteo Croced95b39c2007-10-14 18:10:13 +0200416 }
417
418 desc->buflen = CPMAC_SKB_SIZE;
419 desc->dataflags = CPMAC_OWN;
420
421 return result;
422}
423
Eugene Konev67d129d2007-10-24 10:42:02 +0800424static int cpmac_poll(struct napi_struct *napi, int budget)
Matteo Croced95b39c2007-10-14 18:10:13 +0200425{
426 struct sk_buff *skb;
Matteo Crocef917d582008-05-14 00:58:32 +0200427 struct cpmac_desc *desc, *restart;
Eugene Konev67d129d2007-10-24 10:42:02 +0800428 struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
Matteo Crocef917d582008-05-14 00:58:32 +0200429 int received = 0, processed = 0;
Matteo Croced95b39c2007-10-14 18:10:13 +0200430
431 spin_lock(&priv->rx_lock);
432 if (unlikely(!priv->rx_head)) {
433 if (netif_msg_rx_err(priv) && net_ratelimit())
434 printk(KERN_WARNING "%s: rx: polling, but no queue\n",
Eugene Konev67d129d2007-10-24 10:42:02 +0800435 priv->dev->name);
Matteo Crocef917d582008-05-14 00:58:32 +0200436 spin_unlock(&priv->rx_lock);
Ben Hutchings288379f2009-01-19 16:43:59 -0800437 napi_complete(napi);
Matteo Croced95b39c2007-10-14 18:10:13 +0200438 return 0;
439 }
440
441 desc = priv->rx_head;
Matteo Crocef917d582008-05-14 00:58:32 +0200442 restart = NULL;
Eugene Konev67d129d2007-10-24 10:42:02 +0800443 while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
Matteo Crocef917d582008-05-14 00:58:32 +0200444 processed++;
445
446 if ((desc->dataflags & CPMAC_EOQ) != 0) {
447 /* The last update to eoq->hw_next didn't happen
448 * soon enough, and the receiver stopped here.
449 *Remember this descriptor so we can restart
450 * the receiver after freeing some space.
451 */
452 if (unlikely(restart)) {
453 if (netif_msg_rx_err(priv))
454 printk(KERN_ERR "%s: poll found a"
455 " duplicate EOQ: %p and %p\n",
456 priv->dev->name, restart, desc);
457 goto fatal_error;
458 }
459
460 restart = desc->next;
461 }
462
Eugene Konev67d129d2007-10-24 10:42:02 +0800463 skb = cpmac_rx_one(priv, desc);
Matteo Croced95b39c2007-10-14 18:10:13 +0200464 if (likely(skb)) {
465 netif_receive_skb(skb);
466 received++;
467 }
468 desc = desc->next;
469 }
470
Matteo Crocef917d582008-05-14 00:58:32 +0200471 if (desc != priv->rx_head) {
472 /* We freed some buffers, but not the whole ring,
473 * add what we did free to the rx list */
474 desc->prev->hw_next = (u32)0;
475 priv->rx_head->prev->hw_next = priv->rx_head->mapping;
476 }
477
478 /* Optimization: If we did not actually process an EOQ (perhaps because
479 * of quota limits), check to see if the tail of the queue has EOQ set.
480 * We should immediately restart in that case so that the receiver can
481 * restart and run in parallel with more packet processing.
482 * This lets us handle slightly larger bursts before running
483 * out of ring space (assuming dev->weight < ring_size) */
484
485 if (!restart &&
486 (priv->rx_head->prev->dataflags & (CPMAC_OWN|CPMAC_EOQ))
487 == CPMAC_EOQ &&
488 (priv->rx_head->dataflags & CPMAC_OWN) != 0) {
489 /* reset EOQ so the poll loop (above) doesn't try to
490 * restart this when it eventually gets to this descriptor.
491 */
492 priv->rx_head->prev->dataflags &= ~CPMAC_EOQ;
493 restart = priv->rx_head;
494 }
495
496 if (restart) {
497 priv->dev->stats.rx_errors++;
498 priv->dev->stats.rx_fifo_errors++;
499 if (netif_msg_rx_err(priv) && net_ratelimit())
500 printk(KERN_WARNING "%s: rx dma ring overrun\n",
501 priv->dev->name);
502
503 if (unlikely((restart->dataflags & CPMAC_OWN) == 0)) {
504 if (netif_msg_drv(priv))
505 printk(KERN_ERR "%s: cpmac_poll is trying to "
506 "restart rx from a descriptor that's "
507 "not free: %p\n",
508 priv->dev->name, restart);
509 goto fatal_error;
510 }
511
512 cpmac_write(priv->regs, CPMAC_RX_PTR(0), restart->mapping);
513 }
514
Matteo Croced95b39c2007-10-14 18:10:13 +0200515 priv->rx_head = desc;
516 spin_unlock(&priv->rx_lock);
Matteo Croced95b39c2007-10-14 18:10:13 +0200517 if (unlikely(netif_msg_rx_status(priv)))
Eugene Konev67d129d2007-10-24 10:42:02 +0800518 printk(KERN_DEBUG "%s: poll processed %d packets\n",
519 priv->dev->name, received);
Matteo Crocef917d582008-05-14 00:58:32 +0200520 if (processed == 0) {
521 /* we ran out of packets to read,
522 * revert to interrupt-driven mode */
Ben Hutchings288379f2009-01-19 16:43:59 -0800523 napi_complete(napi);
Matteo Croced95b39c2007-10-14 18:10:13 +0200524 cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
525 return 0;
526 }
527
528 return 1;
Matteo Crocef917d582008-05-14 00:58:32 +0200529
530fatal_error:
531 /* Something went horribly wrong.
532 * Reset hardware to try to recover rather than wedging. */
533
534 if (netif_msg_drv(priv)) {
535 printk(KERN_ERR "%s: cpmac_poll is confused. "
536 "Resetting hardware\n", priv->dev->name);
537 cpmac_dump_all_desc(priv->dev);
538 printk(KERN_DEBUG "%s: RX_PTR(0)=0x%08x RX_ACK(0)=0x%08x\n",
539 priv->dev->name,
540 cpmac_read(priv->regs, CPMAC_RX_PTR(0)),
541 cpmac_read(priv->regs, CPMAC_RX_ACK(0)));
542 }
543
544 spin_unlock(&priv->rx_lock);
Ben Hutchings288379f2009-01-19 16:43:59 -0800545 napi_complete(napi);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700546 netif_tx_stop_all_queues(priv->dev);
Matteo Crocef917d582008-05-14 00:58:32 +0200547 napi_disable(&priv->napi);
548
549 atomic_inc(&priv->reset_pending);
550 cpmac_hw_stop(priv->dev);
551 if (!schedule_work(&priv->reset_work))
552 atomic_dec(&priv->reset_pending);
553 return 0;
554
Matteo Croced95b39c2007-10-14 18:10:13 +0200555}
556
557static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
558{
559 int queue, len;
560 struct cpmac_desc *desc;
561 struct cpmac_priv *priv = netdev_priv(dev);
562
Matteo Crocef917d582008-05-14 00:58:32 +0200563 if (unlikely(atomic_read(&priv->reset_pending)))
564 return NETDEV_TX_BUSY;
565
Matteo Croce6cd043d2007-10-23 19:12:22 +0200566 if (unlikely(skb_padto(skb, ETH_ZLEN)))
567 return NETDEV_TX_OK;
Matteo Croced95b39c2007-10-14 18:10:13 +0200568
569 len = max(skb->len, ETH_ZLEN);
Matteo Croceba596a02008-01-12 19:05:23 +0100570 queue = skb_get_queue_mapping(skb);
Matteo Croced95b39c2007-10-14 18:10:13 +0200571 netif_stop_subqueue(dev, queue);
Matteo Croced95b39c2007-10-14 18:10:13 +0200572
573 desc = &priv->desc_ring[queue];
574 if (unlikely(desc->dataflags & CPMAC_OWN)) {
575 if (netif_msg_tx_err(priv) && net_ratelimit())
Matteo Croce6cd043d2007-10-23 19:12:22 +0200576 printk(KERN_WARNING "%s: tx dma ring full\n",
Matteo Croced95b39c2007-10-14 18:10:13 +0200577 dev->name);
Matteo Croce6cd043d2007-10-23 19:12:22 +0200578 return NETDEV_TX_BUSY;
Matteo Croced95b39c2007-10-14 18:10:13 +0200579 }
580
581 spin_lock(&priv->lock);
582 dev->trans_start = jiffies;
583 spin_unlock(&priv->lock);
584 desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
585 desc->skb = skb;
586 desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
587 DMA_TO_DEVICE);
588 desc->hw_data = (u32)desc->data_mapping;
589 desc->datalen = len;
590 desc->buflen = len;
591 if (unlikely(netif_msg_tx_queued(priv)))
592 printk(KERN_DEBUG "%s: sending 0x%p, len=%d\n", dev->name, skb,
593 skb->len);
594 if (unlikely(netif_msg_hw(priv)))
595 cpmac_dump_desc(dev, desc);
596 if (unlikely(netif_msg_pktdata(priv)))
597 cpmac_dump_skb(dev, skb);
598 cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
599
Matteo Croce6cd043d2007-10-23 19:12:22 +0200600 return NETDEV_TX_OK;
Matteo Croced95b39c2007-10-14 18:10:13 +0200601}
602
603static void cpmac_end_xmit(struct net_device *dev, int queue)
604{
605 struct cpmac_desc *desc;
606 struct cpmac_priv *priv = netdev_priv(dev);
607
608 desc = &priv->desc_ring[queue];
609 cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
610 if (likely(desc->skb)) {
611 spin_lock(&priv->lock);
612 dev->stats.tx_packets++;
613 dev->stats.tx_bytes += desc->skb->len;
614 spin_unlock(&priv->lock);
615 dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
616 DMA_TO_DEVICE);
617
618 if (unlikely(netif_msg_tx_done(priv)))
619 printk(KERN_DEBUG "%s: sent 0x%p, len=%d\n", dev->name,
620 desc->skb, desc->skb->len);
621
622 dev_kfree_skb_irq(desc->skb);
623 desc->skb = NULL;
Stefan Weil0220ff72009-05-31 10:59:15 +0000624 if (__netif_subqueue_stopped(dev, queue))
Matteo Croced95b39c2007-10-14 18:10:13 +0200625 netif_wake_subqueue(dev, queue);
Matteo Croced95b39c2007-10-14 18:10:13 +0200626 } else {
627 if (netif_msg_tx_err(priv) && net_ratelimit())
628 printk(KERN_WARNING
629 "%s: end_xmit: spurious interrupt\n", dev->name);
Stefan Weil0220ff72009-05-31 10:59:15 +0000630 if (__netif_subqueue_stopped(dev, queue))
Matteo Croced95b39c2007-10-14 18:10:13 +0200631 netif_wake_subqueue(dev, queue);
Matteo Croced95b39c2007-10-14 18:10:13 +0200632 }
633}
634
635static void cpmac_hw_stop(struct net_device *dev)
636{
637 int i;
638 struct cpmac_priv *priv = netdev_priv(dev);
639 struct plat_cpmac_data *pdata = priv->pdev->dev.platform_data;
640
641 ar7_device_reset(pdata->reset_bit);
642 cpmac_write(priv->regs, CPMAC_RX_CONTROL,
643 cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
644 cpmac_write(priv->regs, CPMAC_TX_CONTROL,
645 cpmac_read(priv->regs, CPMAC_TX_CONTROL) & ~1);
646 for (i = 0; i < 8; i++) {
647 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
648 cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
649 }
650 cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
651 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
652 cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
653 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
654 cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
655 cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
656}
657
658static void cpmac_hw_start(struct net_device *dev)
659{
660 int i;
661 struct cpmac_priv *priv = netdev_priv(dev);
662 struct plat_cpmac_data *pdata = priv->pdev->dev.platform_data;
663
664 ar7_device_reset(pdata->reset_bit);
665 for (i = 0; i < 8; i++) {
666 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
667 cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
668 }
669 cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
670
671 cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
672 MBP_RXMCAST);
673 cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
674 for (i = 0; i < 8; i++)
675 cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
676 cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
677 cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
678 (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
679 (dev->dev_addr[3] << 24));
680 cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
681 cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
682 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
683 cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
684 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
685 cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
686 cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
687 cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
688 cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
689
690 cpmac_write(priv->regs, CPMAC_RX_CONTROL,
691 cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
692 cpmac_write(priv->regs, CPMAC_TX_CONTROL,
693 cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
694 cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
695 cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
696 MAC_FDX);
697}
698
699static void cpmac_clear_rx(struct net_device *dev)
700{
701 struct cpmac_priv *priv = netdev_priv(dev);
702 struct cpmac_desc *desc;
703 int i;
704 if (unlikely(!priv->rx_head))
705 return;
706 desc = priv->rx_head;
707 for (i = 0; i < priv->ring_size; i++) {
708 if ((desc->dataflags & CPMAC_OWN) == 0) {
709 if (netif_msg_rx_err(priv) && net_ratelimit())
710 printk(KERN_WARNING "%s: packet dropped\n",
711 dev->name);
712 if (unlikely(netif_msg_hw(priv)))
713 cpmac_dump_desc(dev, desc);
714 desc->dataflags = CPMAC_OWN;
715 dev->stats.rx_dropped++;
716 }
Matteo Crocef917d582008-05-14 00:58:32 +0200717 desc->hw_next = desc->next->mapping;
Matteo Croced95b39c2007-10-14 18:10:13 +0200718 desc = desc->next;
719 }
Matteo Crocef917d582008-05-14 00:58:32 +0200720 priv->rx_head->prev->hw_next = 0;
Matteo Croced95b39c2007-10-14 18:10:13 +0200721}
722
723static void cpmac_clear_tx(struct net_device *dev)
724{
725 struct cpmac_priv *priv = netdev_priv(dev);
726 int i;
727 if (unlikely(!priv->desc_ring))
728 return;
Matteo Croce6cd043d2007-10-23 19:12:22 +0200729 for (i = 0; i < CPMAC_QUEUES; i++) {
730 priv->desc_ring[i].dataflags = 0;
Matteo Croced95b39c2007-10-14 18:10:13 +0200731 if (priv->desc_ring[i].skb) {
732 dev_kfree_skb_any(priv->desc_ring[i].skb);
Matteo Crocef917d582008-05-14 00:58:32 +0200733 priv->desc_ring[i].skb = NULL;
Matteo Croced95b39c2007-10-14 18:10:13 +0200734 }
Matteo Croce6cd043d2007-10-23 19:12:22 +0200735 }
Matteo Croced95b39c2007-10-14 18:10:13 +0200736}
737
738static void cpmac_hw_error(struct work_struct *work)
739{
740 struct cpmac_priv *priv =
741 container_of(work, struct cpmac_priv, reset_work);
742
743 spin_lock(&priv->rx_lock);
744 cpmac_clear_rx(priv->dev);
745 spin_unlock(&priv->rx_lock);
746 cpmac_clear_tx(priv->dev);
747 cpmac_hw_start(priv->dev);
Matteo Crocef917d582008-05-14 00:58:32 +0200748 barrier();
749 atomic_dec(&priv->reset_pending);
750
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700751 netif_tx_wake_all_queues(priv->dev);
Matteo Crocef917d582008-05-14 00:58:32 +0200752 cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
753}
754
755static void cpmac_check_status(struct net_device *dev)
756{
757 struct cpmac_priv *priv = netdev_priv(dev);
758
759 u32 macstatus = cpmac_read(priv->regs, CPMAC_MAC_STATUS);
760 int rx_channel = (macstatus >> 8) & 7;
761 int rx_code = (macstatus >> 12) & 15;
762 int tx_channel = (macstatus >> 16) & 7;
763 int tx_code = (macstatus >> 20) & 15;
764
765 if (rx_code || tx_code) {
766 if (netif_msg_drv(priv) && net_ratelimit()) {
767 /* Can't find any documentation on what these
768 *error codes actually are. So just log them and hope..
769 */
770 if (rx_code)
771 printk(KERN_WARNING "%s: host error %d on rx "
772 "channel %d (macstatus %08x), resetting\n",
773 dev->name, rx_code, rx_channel, macstatus);
774 if (tx_code)
775 printk(KERN_WARNING "%s: host error %d on tx "
776 "channel %d (macstatus %08x), resetting\n",
777 dev->name, tx_code, tx_channel, macstatus);
778 }
779
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700780 netif_tx_stop_all_queues(dev);
Matteo Crocef917d582008-05-14 00:58:32 +0200781 cpmac_hw_stop(dev);
782 if (schedule_work(&priv->reset_work))
783 atomic_inc(&priv->reset_pending);
784 if (unlikely(netif_msg_hw(priv)))
785 cpmac_dump_regs(dev);
786 }
787 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
Matteo Croced95b39c2007-10-14 18:10:13 +0200788}
789
790static irqreturn_t cpmac_irq(int irq, void *dev_id)
791{
792 struct net_device *dev = dev_id;
793 struct cpmac_priv *priv;
794 int queue;
795 u32 status;
796
Matteo Croced95b39c2007-10-14 18:10:13 +0200797 priv = netdev_priv(dev);
798
799 status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
800
801 if (unlikely(netif_msg_intr(priv)))
802 printk(KERN_DEBUG "%s: interrupt status: 0x%08x\n", dev->name,
803 status);
804
805 if (status & MAC_INT_TX)
806 cpmac_end_xmit(dev, (status & 7));
807
808 if (status & MAC_INT_RX) {
809 queue = (status >> 8) & 7;
Ben Hutchings288379f2009-01-19 16:43:59 -0800810 if (napi_schedule_prep(&priv->napi)) {
Eugene Konev67d129d2007-10-24 10:42:02 +0800811 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
Ben Hutchings288379f2009-01-19 16:43:59 -0800812 __napi_schedule(&priv->napi);
Eugene Konev67d129d2007-10-24 10:42:02 +0800813 }
Matteo Croced95b39c2007-10-14 18:10:13 +0200814 }
815
816 cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
817
Matteo Crocef917d582008-05-14 00:58:32 +0200818 if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
819 cpmac_check_status(dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200820
821 return IRQ_HANDLED;
822}
823
824static void cpmac_tx_timeout(struct net_device *dev)
825{
Matteo Crocef917d582008-05-14 00:58:32 +0200826 struct cpmac_priv *priv = netdev_priv(dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200827
828 spin_lock(&priv->lock);
829 dev->stats.tx_errors++;
830 spin_unlock(&priv->lock);
831 if (netif_msg_tx_err(priv) && net_ratelimit())
832 printk(KERN_WARNING "%s: transmit timeout\n", dev->name);
Matteo Crocef917d582008-05-14 00:58:32 +0200833
834 atomic_inc(&priv->reset_pending);
835 barrier();
836 cpmac_clear_tx(dev);
837 barrier();
838 atomic_dec(&priv->reset_pending);
839
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700840 netif_tx_wake_all_queues(priv->dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200841}
842
843static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
844{
845 struct cpmac_priv *priv = netdev_priv(dev);
846 if (!(netif_running(dev)))
847 return -EINVAL;
848 if (!priv->phy)
849 return -EINVAL;
850 if ((cmd == SIOCGMIIPHY) || (cmd == SIOCGMIIREG) ||
851 (cmd == SIOCSMIIREG))
852 return phy_mii_ioctl(priv->phy, if_mii(ifr), cmd);
853
854 return -EOPNOTSUPP;
855}
856
857static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
858{
859 struct cpmac_priv *priv = netdev_priv(dev);
860
861 if (priv->phy)
862 return phy_ethtool_gset(priv->phy, cmd);
863
864 return -EINVAL;
865}
866
867static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
868{
869 struct cpmac_priv *priv = netdev_priv(dev);
870
871 if (!capable(CAP_NET_ADMIN))
872 return -EPERM;
873
874 if (priv->phy)
875 return phy_ethtool_sset(priv->phy, cmd);
876
877 return -EINVAL;
878}
879
880static void cpmac_get_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
881{
882 struct cpmac_priv *priv = netdev_priv(dev);
883
884 ring->rx_max_pending = 1024;
885 ring->rx_mini_max_pending = 1;
886 ring->rx_jumbo_max_pending = 1;
887 ring->tx_max_pending = 1;
888
889 ring->rx_pending = priv->ring_size;
890 ring->rx_mini_pending = 1;
891 ring->rx_jumbo_pending = 1;
892 ring->tx_pending = 1;
893}
894
895static int cpmac_set_ringparam(struct net_device *dev, struct ethtool_ringparam* ring)
896{
897 struct cpmac_priv *priv = netdev_priv(dev);
898
Matteo Croce6cd043d2007-10-23 19:12:22 +0200899 if (netif_running(dev))
Matteo Croced95b39c2007-10-14 18:10:13 +0200900 return -EBUSY;
901 priv->ring_size = ring->rx_pending;
902 return 0;
903}
904
905static void cpmac_get_drvinfo(struct net_device *dev,
906 struct ethtool_drvinfo *info)
907{
908 strcpy(info->driver, "cpmac");
909 strcpy(info->version, CPMAC_VERSION);
910 info->fw_version[0] = '\0';
911 sprintf(info->bus_info, "%s", "cpmac");
912 info->regdump_len = 0;
913}
914
915static const struct ethtool_ops cpmac_ethtool_ops = {
916 .get_settings = cpmac_get_settings,
917 .set_settings = cpmac_set_settings,
918 .get_drvinfo = cpmac_get_drvinfo,
919 .get_link = ethtool_op_get_link,
920 .get_ringparam = cpmac_get_ringparam,
921 .set_ringparam = cpmac_set_ringparam,
922};
923
924static void cpmac_adjust_link(struct net_device *dev)
925{
926 struct cpmac_priv *priv = netdev_priv(dev);
927 int new_state = 0;
928
929 spin_lock(&priv->lock);
930 if (priv->phy->link) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700931 netif_tx_start_all_queues(dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200932 if (priv->phy->duplex != priv->oldduplex) {
933 new_state = 1;
934 priv->oldduplex = priv->phy->duplex;
935 }
936
937 if (priv->phy->speed != priv->oldspeed) {
938 new_state = 1;
939 priv->oldspeed = priv->phy->speed;
940 }
941
942 if (!priv->oldlink) {
943 new_state = 1;
944 priv->oldlink = 1;
Matteo Croced95b39c2007-10-14 18:10:13 +0200945 }
946 } else if (priv->oldlink) {
Matteo Croced95b39c2007-10-14 18:10:13 +0200947 new_state = 1;
948 priv->oldlink = 0;
949 priv->oldspeed = 0;
950 priv->oldduplex = -1;
951 }
952
953 if (new_state && netif_msg_link(priv) && net_ratelimit())
954 phy_print_status(priv->phy);
955
956 spin_unlock(&priv->lock);
957}
958
959static int cpmac_open(struct net_device *dev)
960{
961 int i, size, res;
962 struct cpmac_priv *priv = netdev_priv(dev);
963 struct resource *mem;
964 struct cpmac_desc *desc;
965 struct sk_buff *skb;
966
Matteo Croced95b39c2007-10-14 18:10:13 +0200967 mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
968 if (!request_mem_region(mem->start, mem->end - mem->start, dev->name)) {
969 if (netif_msg_drv(priv))
970 printk(KERN_ERR "%s: failed to request registers\n",
971 dev->name);
972 res = -ENXIO;
973 goto fail_reserve;
974 }
975
976 priv->regs = ioremap(mem->start, mem->end - mem->start);
977 if (!priv->regs) {
978 if (netif_msg_drv(priv))
979 printk(KERN_ERR "%s: failed to remap registers\n",
980 dev->name);
981 res = -ENXIO;
982 goto fail_remap;
983 }
984
985 size = priv->ring_size + CPMAC_QUEUES;
986 priv->desc_ring = dma_alloc_coherent(&dev->dev,
987 sizeof(struct cpmac_desc) * size,
988 &priv->dma_ring,
989 GFP_KERNEL);
990 if (!priv->desc_ring) {
991 res = -ENOMEM;
992 goto fail_alloc;
993 }
994
995 for (i = 0; i < size; i++)
996 priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
997
998 priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
999 for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
Eric Dumazet89d71a62009-10-13 05:34:20 +00001000 skb = netdev_alloc_skb_ip_align(dev, CPMAC_SKB_SIZE);
Matteo Croced95b39c2007-10-14 18:10:13 +02001001 if (unlikely(!skb)) {
1002 res = -ENOMEM;
1003 goto fail_desc;
1004 }
Matteo Croced95b39c2007-10-14 18:10:13 +02001005 desc->skb = skb;
1006 desc->data_mapping = dma_map_single(&dev->dev, skb->data,
1007 CPMAC_SKB_SIZE,
1008 DMA_FROM_DEVICE);
1009 desc->hw_data = (u32)desc->data_mapping;
1010 desc->buflen = CPMAC_SKB_SIZE;
1011 desc->dataflags = CPMAC_OWN;
1012 desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
Matteo Crocef917d582008-05-14 00:58:32 +02001013 desc->next->prev = desc;
Matteo Croced95b39c2007-10-14 18:10:13 +02001014 desc->hw_next = (u32)desc->next->mapping;
1015 }
1016
Matteo Crocef917d582008-05-14 00:58:32 +02001017 priv->rx_head->prev->hw_next = (u32)0;
1018
Matteo Croced95b39c2007-10-14 18:10:13 +02001019 if ((res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED,
1020 dev->name, dev))) {
1021 if (netif_msg_drv(priv))
1022 printk(KERN_ERR "%s: failed to obtain irq\n",
1023 dev->name);
1024 goto fail_irq;
1025 }
1026
Matteo Crocef917d582008-05-14 00:58:32 +02001027 atomic_set(&priv->reset_pending, 0);
Matteo Croced95b39c2007-10-14 18:10:13 +02001028 INIT_WORK(&priv->reset_work, cpmac_hw_error);
1029 cpmac_hw_start(dev);
1030
Eugene Konev67d129d2007-10-24 10:42:02 +08001031 napi_enable(&priv->napi);
Matteo Croced95b39c2007-10-14 18:10:13 +02001032 priv->phy->state = PHY_CHANGELINK;
1033 phy_start(priv->phy);
1034
1035 return 0;
1036
1037fail_irq:
1038fail_desc:
1039 for (i = 0; i < priv->ring_size; i++) {
1040 if (priv->rx_head[i].skb) {
1041 dma_unmap_single(&dev->dev,
1042 priv->rx_head[i].data_mapping,
1043 CPMAC_SKB_SIZE,
1044 DMA_FROM_DEVICE);
1045 kfree_skb(priv->rx_head[i].skb);
1046 }
1047 }
1048fail_alloc:
1049 kfree(priv->desc_ring);
1050 iounmap(priv->regs);
1051
1052fail_remap:
1053 release_mem_region(mem->start, mem->end - mem->start);
1054
1055fail_reserve:
Matteo Croced95b39c2007-10-14 18:10:13 +02001056 return res;
1057}
1058
1059static int cpmac_stop(struct net_device *dev)
1060{
1061 int i;
1062 struct cpmac_priv *priv = netdev_priv(dev);
1063 struct resource *mem;
1064
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001065 netif_tx_stop_all_queues(dev);
Matteo Croced95b39c2007-10-14 18:10:13 +02001066
1067 cancel_work_sync(&priv->reset_work);
Eugene Konev67d129d2007-10-24 10:42:02 +08001068 napi_disable(&priv->napi);
Matteo Croced95b39c2007-10-14 18:10:13 +02001069 phy_stop(priv->phy);
Matteo Croced95b39c2007-10-14 18:10:13 +02001070
1071 cpmac_hw_stop(dev);
1072
1073 for (i = 0; i < 8; i++)
1074 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
1075 cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
1076 cpmac_write(priv->regs, CPMAC_MBP, 0);
1077
1078 free_irq(dev->irq, dev);
1079 iounmap(priv->regs);
1080 mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
1081 release_mem_region(mem->start, mem->end - mem->start);
1082 priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
1083 for (i = 0; i < priv->ring_size; i++) {
1084 if (priv->rx_head[i].skb) {
1085 dma_unmap_single(&dev->dev,
1086 priv->rx_head[i].data_mapping,
1087 CPMAC_SKB_SIZE,
1088 DMA_FROM_DEVICE);
1089 kfree_skb(priv->rx_head[i].skb);
1090 }
1091 }
1092
1093 dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
1094 (CPMAC_QUEUES + priv->ring_size),
1095 priv->desc_ring, priv->dma_ring);
1096 return 0;
1097}
1098
Alexander Beregalov63ef7d82009-04-15 12:52:36 +00001099static const struct net_device_ops cpmac_netdev_ops = {
1100 .ndo_open = cpmac_open,
1101 .ndo_stop = cpmac_stop,
1102 .ndo_start_xmit = cpmac_start_xmit,
1103 .ndo_tx_timeout = cpmac_tx_timeout,
1104 .ndo_set_multicast_list = cpmac_set_multicast_list,
Florian Fainelli6a9b6542009-06-24 16:32:33 -07001105 .ndo_do_ioctl = cpmac_ioctl,
Alexander Beregalov63ef7d82009-04-15 12:52:36 +00001106 .ndo_set_config = cpmac_config,
1107 .ndo_change_mtu = eth_change_mtu,
1108 .ndo_validate_addr = eth_validate_addr,
1109 .ndo_set_mac_address = eth_mac_addr,
1110};
1111
Matteo Croced95b39c2007-10-14 18:10:13 +02001112static int external_switch;
1113
1114static int __devinit cpmac_probe(struct platform_device *pdev)
1115{
Florian Fainelli69bd4ae2009-05-31 10:57:07 +00001116 int rc, phy_id;
Florian Fainelli762c6aa2009-09-15 21:44:22 +00001117 char mdio_bus_id[MII_BUS_ID_SIZE];
Matteo Croced95b39c2007-10-14 18:10:13 +02001118 struct resource *mem;
1119 struct cpmac_priv *priv;
1120 struct net_device *dev;
1121 struct plat_cpmac_data *pdata;
1122
1123 pdata = pdev->dev.platform_data;
1124
Florian Fainelli76e61ea2009-08-04 10:52:52 +00001125 if (external_switch || dumb_switch) {
Florian Fainelli762c6aa2009-09-15 21:44:22 +00001126 strncpy(mdio_bus_id, "0", MII_BUS_ID_SIZE); /* fixed phys bus */
Florian Fainelli76e61ea2009-08-04 10:52:52 +00001127 phy_id = pdev->id;
1128 } else {
1129 for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
1130 if (!(pdata->phy_mask & (1 << phy_id)))
1131 continue;
1132 if (!cpmac_mii->phy_map[phy_id])
1133 continue;
Florian Fainelli762c6aa2009-09-15 21:44:22 +00001134 strncpy(mdio_bus_id, cpmac_mii->id, MII_BUS_ID_SIZE);
Florian Fainelli76e61ea2009-08-04 10:52:52 +00001135 break;
1136 }
Matteo Croced95b39c2007-10-14 18:10:13 +02001137 }
1138
1139 if (phy_id == PHY_MAX_ADDR) {
Florian Fainelli9fba1c32010-03-07 00:55:47 +00001140 dev_err(&pdev->dev, "no PHY present, falling back to switch on MDIO bus 0\n");
1141 strncpy(mdio_bus_id, "0", MII_BUS_ID_SIZE); /* fixed phys bus */
1142 phy_id = pdev->id;
Matteo Croced95b39c2007-10-14 18:10:13 +02001143 }
1144
1145 dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
1146
1147 if (!dev) {
1148 printk(KERN_ERR "cpmac: Unable to allocate net_device\n");
1149 return -ENOMEM;
1150 }
1151
1152 platform_set_drvdata(pdev, dev);
1153 priv = netdev_priv(dev);
1154
1155 priv->pdev = pdev;
1156 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
1157 if (!mem) {
1158 rc = -ENODEV;
1159 goto fail;
1160 }
1161
1162 dev->irq = platform_get_irq_byname(pdev, "irq");
1163
Alexander Beregalov63ef7d82009-04-15 12:52:36 +00001164 dev->netdev_ops = &cpmac_netdev_ops;
1165 dev->ethtool_ops = &cpmac_ethtool_ops;
Matteo Croced95b39c2007-10-14 18:10:13 +02001166
Eugene Konev67d129d2007-10-24 10:42:02 +08001167 netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
1168
Matteo Croced95b39c2007-10-14 18:10:13 +02001169 spin_lock_init(&priv->lock);
1170 spin_lock_init(&priv->rx_lock);
1171 priv->dev = dev;
1172 priv->ring_size = 64;
1173 priv->msg_enable = netif_msg_init(debug_level, 0xff);
Julia Lawall2447f2f2009-12-13 05:35:45 +00001174 memcpy(dev->dev_addr, pdata->dev_addr, sizeof(pdata->dev_addr));
Eugene Konevb88219f2007-10-24 10:42:03 +08001175
Florian Fainelli762c6aa2009-09-15 21:44:22 +00001176 snprintf(priv->phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT, mdio_bus_id, phy_id);
Florian Fainelli76e61ea2009-08-04 10:52:52 +00001177
1178 priv->phy = phy_connect(dev, priv->phy_name, &cpmac_adjust_link, 0,
1179 PHY_INTERFACE_MODE_MII);
1180
Eugene Konevb88219f2007-10-24 10:42:03 +08001181 if (IS_ERR(priv->phy)) {
1182 if (netif_msg_drv(priv))
1183 printk(KERN_ERR "%s: Could not attach to PHY\n",
1184 dev->name);
1185 return PTR_ERR(priv->phy);
1186 }
Matteo Croced95b39c2007-10-14 18:10:13 +02001187
1188 if ((rc = register_netdev(dev))) {
1189 printk(KERN_ERR "cpmac: error %i registering device %s\n", rc,
1190 dev->name);
1191 goto fail;
1192 }
1193
1194 if (netif_msg_probe(priv)) {
1195 printk(KERN_INFO
Eugene Konevdf523b52007-10-24 10:42:01 +08001196 "cpmac: device %s (regs: %p, irq: %d, phy: %s, "
Johannes Berge1749612008-10-27 15:59:26 -07001197 "mac: %pM)\n", dev->name, (void *)mem->start, dev->irq,
1198 priv->phy_name, dev->dev_addr);
Matteo Croced95b39c2007-10-14 18:10:13 +02001199 }
1200 return 0;
1201
1202fail:
1203 free_netdev(dev);
1204 return rc;
1205}
1206
1207static int __devexit cpmac_remove(struct platform_device *pdev)
1208{
1209 struct net_device *dev = platform_get_drvdata(pdev);
1210 unregister_netdev(dev);
1211 free_netdev(dev);
1212 return 0;
1213}
1214
1215static struct platform_driver cpmac_driver = {
1216 .driver.name = "cpmac",
Kay Sievers72abb462008-04-18 13:50:44 -07001217 .driver.owner = THIS_MODULE,
Matteo Croced95b39c2007-10-14 18:10:13 +02001218 .probe = cpmac_probe,
1219 .remove = __devexit_p(cpmac_remove),
1220};
1221
1222int __devinit cpmac_init(void)
1223{
1224 u32 mask;
1225 int i, res;
1226
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001227 cpmac_mii = mdiobus_alloc();
1228 if (cpmac_mii == NULL)
1229 return -ENOMEM;
Matteo Croced95b39c2007-10-14 18:10:13 +02001230
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001231 cpmac_mii->name = "cpmac-mii";
1232 cpmac_mii->read = cpmac_mdio_read;
1233 cpmac_mii->write = cpmac_mdio_write;
1234 cpmac_mii->reset = cpmac_mdio_reset;
1235 cpmac_mii->irq = mii_irqs;
1236
1237 cpmac_mii->priv = ioremap(AR7_REGS_MDIO, 256);
1238
1239 if (!cpmac_mii->priv) {
Matteo Croced95b39c2007-10-14 18:10:13 +02001240 printk(KERN_ERR "Can't ioremap mdio registers\n");
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001241 res = -ENXIO;
1242 goto fail_alloc;
Matteo Croced95b39c2007-10-14 18:10:13 +02001243 }
1244
1245#warning FIXME: unhardcode gpio&reset bits
1246 ar7_gpio_disable(26);
1247 ar7_gpio_disable(27);
1248 ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
1249 ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
1250 ar7_device_reset(AR7_RESET_BIT_EPHY);
1251
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001252 cpmac_mii->reset(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001253
Florian Fainellie4540aa2009-08-04 10:52:57 +00001254 for (i = 0; i < 300; i++)
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001255 if ((mask = cpmac_read(cpmac_mii->priv, CPMAC_MDIO_ALIVE)))
Matteo Croced95b39c2007-10-14 18:10:13 +02001256 break;
1257 else
Florian Fainellie4540aa2009-08-04 10:52:57 +00001258 msleep(10);
Matteo Croced95b39c2007-10-14 18:10:13 +02001259
1260 mask &= 0x7fffffff;
1261 if (mask & (mask - 1)) {
1262 external_switch = 1;
1263 mask = 0;
1264 }
1265
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001266 cpmac_mii->phy_mask = ~(mask | 0x80000000);
Florian Fainellid76c626b2009-08-04 10:52:41 +00001267 snprintf(cpmac_mii->id, MII_BUS_ID_SIZE, "1");
Matteo Croced95b39c2007-10-14 18:10:13 +02001268
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001269 res = mdiobus_register(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001270 if (res)
1271 goto fail_mii;
1272
1273 res = platform_driver_register(&cpmac_driver);
1274 if (res)
1275 goto fail_cpmac;
1276
1277 return 0;
1278
1279fail_cpmac:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001280 mdiobus_unregister(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001281
1282fail_mii:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001283 iounmap(cpmac_mii->priv);
1284
1285fail_alloc:
1286 mdiobus_free(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001287
1288 return res;
1289}
1290
1291void __devexit cpmac_exit(void)
1292{
1293 platform_driver_unregister(&cpmac_driver);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001294 mdiobus_unregister(cpmac_mii);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001295 iounmap(cpmac_mii->priv);
Dan Carpenter48a29512010-03-02 22:46:10 +00001296 mdiobus_free(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001297}
1298
1299module_init(cpmac_init);
1300module_exit(cpmac_exit);