blob: 1520c190cfd30c7a17d8c867865a2e4f80ba41c1 [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");
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530231 netdev_dbg(dev, "reg[%p]:", priv->regs + i);
Matteo Croced95b39c2007-10-14 18:10:13 +0200232 }
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530233 pr_debug(" %08x", cpmac_read(priv->regs, i));
Matteo Croced95b39c2007-10-14 18:10:13 +0200234 }
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530235 pr_debug("\n");
Matteo Croced95b39c2007-10-14 18:10:13 +0200236}
237
238static void cpmac_dump_desc(struct net_device *dev, struct cpmac_desc *desc)
239{
240 int i;
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530241 netdev_dbg(dev, "desc[%p]:", desc);
Matteo Croced95b39c2007-10-14 18:10:13 +0200242 for (i = 0; i < sizeof(*desc) / 4; i++)
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530243 pr_debug(" %08x", ((u32 *)desc)[i]);
244 pr_debug("\n");
Matteo Croced95b39c2007-10-14 18:10:13 +0200245}
246
Matteo Crocef917d582008-05-14 00:58:32 +0200247static void cpmac_dump_all_desc(struct net_device *dev)
248{
249 struct cpmac_priv *priv = netdev_priv(dev);
250 struct cpmac_desc *dump = priv->rx_head;
251 do {
252 cpmac_dump_desc(dev, dump);
253 dump = dump->next;
254 } while (dump != priv->rx_head);
255}
256
Matteo Croced95b39c2007-10-14 18:10:13 +0200257static void cpmac_dump_skb(struct net_device *dev, struct sk_buff *skb)
258{
259 int i;
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530260 netdev_dbg(dev, "skb 0x%p, len=%d\n", skb, skb->len);
Matteo Croced95b39c2007-10-14 18:10:13 +0200261 for (i = 0; i < skb->len; i++) {
262 if (i % 16 == 0) {
263 if (i)
Florian Fainelli559764d2010-08-08 10:09:39 +0000264 pr_cont("\n");
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530265 netdev_dbg(dev, "data[%p]:", skb->data + i);
Matteo Croced95b39c2007-10-14 18:10:13 +0200266 }
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530267 pr_debug(" %02x", ((u8 *)skb->data)[i]);
Matteo Croced95b39c2007-10-14 18:10:13 +0200268 }
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530269 pr_debug("\n");
Matteo Croced95b39c2007-10-14 18:10:13 +0200270}
271
272static int cpmac_mdio_read(struct mii_bus *bus, int phy_id, int reg)
273{
274 u32 val;
275
276 while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
277 cpu_relax();
278 cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_REG(reg) |
279 MDIO_PHY(phy_id));
280 while ((val = cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0))) & MDIO_BUSY)
281 cpu_relax();
282 return MDIO_DATA(val);
283}
284
285static int cpmac_mdio_write(struct mii_bus *bus, int phy_id,
286 int reg, u16 val)
287{
288 while (cpmac_read(bus->priv, CPMAC_MDIO_ACCESS(0)) & MDIO_BUSY)
289 cpu_relax();
290 cpmac_write(bus->priv, CPMAC_MDIO_ACCESS(0), MDIO_BUSY | MDIO_WRITE |
291 MDIO_REG(reg) | MDIO_PHY(phy_id) | MDIO_DATA(val));
292 return 0;
293}
294
295static int cpmac_mdio_reset(struct mii_bus *bus)
296{
Florian Fainelli780019d2010-01-27 09:10:06 +0100297 struct clk *cpmac_clk;
298
299 cpmac_clk = clk_get(&bus->dev, "cpmac");
300 if (IS_ERR(cpmac_clk)) {
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530301 pr_err("unable to get cpmac clock\n");
Florian Fainelli780019d2010-01-27 09:10:06 +0100302 return -1;
303 }
Matteo Croced95b39c2007-10-14 18:10:13 +0200304 ar7_device_reset(AR7_RESET_BIT_MDIO);
305 cpmac_write(bus->priv, CPMAC_MDIO_CONTROL, MDIOC_ENABLE |
Florian Fainelli780019d2010-01-27 09:10:06 +0100306 MDIOC_CLKDIV(clk_get_rate(cpmac_clk) / 2200000 - 1));
Matteo Croced95b39c2007-10-14 18:10:13 +0200307 return 0;
308}
309
310static int mii_irqs[PHY_MAX_ADDR] = { PHY_POLL, };
311
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700312static struct mii_bus *cpmac_mii;
Matteo Croced95b39c2007-10-14 18:10:13 +0200313
Matteo Croced95b39c2007-10-14 18:10:13 +0200314static void cpmac_set_multicast_list(struct net_device *dev)
315{
Jiri Pirko22bedad32010-04-01 21:22:57 +0000316 struct netdev_hw_addr *ha;
Matteo Croced95b39c2007-10-14 18:10:13 +0200317 u8 tmp;
318 u32 mbp, bit, hash[2] = { 0, };
319 struct cpmac_priv *priv = netdev_priv(dev);
320
321 mbp = cpmac_read(priv->regs, CPMAC_MBP);
322 if (dev->flags & IFF_PROMISC) {
323 cpmac_write(priv->regs, CPMAC_MBP, (mbp & ~MBP_PROMISCCHAN(0)) |
324 MBP_RXPROMISC);
325 } else {
326 cpmac_write(priv->regs, CPMAC_MBP, mbp & ~MBP_RXPROMISC);
327 if (dev->flags & IFF_ALLMULTI) {
328 /* enable all multicast mode */
329 cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, 0xffffffff);
330 cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, 0xffffffff);
331 } else {
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530332 /* cpmac uses some strange mac address hashing
Matteo Croced95b39c2007-10-14 18:10:13 +0200333 * (not crc32)
334 */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000335 netdev_for_each_mc_addr(ha, dev) {
Matteo Croced95b39c2007-10-14 18:10:13 +0200336 bit = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000337 tmp = ha->addr[0];
Matteo Croced95b39c2007-10-14 18:10:13 +0200338 bit ^= (tmp >> 2) ^ (tmp << 4);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000339 tmp = ha->addr[1];
Matteo Croced95b39c2007-10-14 18:10:13 +0200340 bit ^= (tmp >> 4) ^ (tmp << 2);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000341 tmp = ha->addr[2];
Matteo Croced95b39c2007-10-14 18:10:13 +0200342 bit ^= (tmp >> 6) ^ tmp;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000343 tmp = ha->addr[3];
Matteo Croced95b39c2007-10-14 18:10:13 +0200344 bit ^= (tmp >> 2) ^ (tmp << 4);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000345 tmp = ha->addr[4];
Matteo Croced95b39c2007-10-14 18:10:13 +0200346 bit ^= (tmp >> 4) ^ (tmp << 2);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000347 tmp = ha->addr[5];
Matteo Croced95b39c2007-10-14 18:10:13 +0200348 bit ^= (tmp >> 6) ^ tmp;
349 bit &= 0x3f;
350 hash[bit / 32] |= 1 << (bit % 32);
351 }
352
353 cpmac_write(priv->regs, CPMAC_MAC_HASH_LO, hash[0]);
354 cpmac_write(priv->regs, CPMAC_MAC_HASH_HI, hash[1]);
355 }
356 }
357}
358
Eugene Konev67d129d2007-10-24 10:42:02 +0800359static struct sk_buff *cpmac_rx_one(struct cpmac_priv *priv,
Matteo Croced95b39c2007-10-14 18:10:13 +0200360 struct cpmac_desc *desc)
361{
362 struct sk_buff *skb, *result = NULL;
363
364 if (unlikely(netif_msg_hw(priv)))
Eugene Konev67d129d2007-10-24 10:42:02 +0800365 cpmac_dump_desc(priv->dev, desc);
Matteo Croced95b39c2007-10-14 18:10:13 +0200366 cpmac_write(priv->regs, CPMAC_RX_ACK(0), (u32)desc->mapping);
367 if (unlikely(!desc->datalen)) {
368 if (netif_msg_rx_err(priv) && net_ratelimit())
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530369 netdev_warn(priv->dev, "rx: spurious interrupt\n");
370
Matteo Croced95b39c2007-10-14 18:10:13 +0200371 return NULL;
372 }
373
Eric Dumazet89d71a62009-10-13 05:34:20 +0000374 skb = netdev_alloc_skb_ip_align(priv->dev, CPMAC_SKB_SIZE);
Matteo Croced95b39c2007-10-14 18:10:13 +0200375 if (likely(skb)) {
Matteo Croced95b39c2007-10-14 18:10:13 +0200376 skb_put(desc->skb, desc->datalen);
Eugene Konev67d129d2007-10-24 10:42:02 +0800377 desc->skb->protocol = eth_type_trans(desc->skb, priv->dev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700378 skb_checksum_none_assert(desc->skb);
Eugene Konev67d129d2007-10-24 10:42:02 +0800379 priv->dev->stats.rx_packets++;
380 priv->dev->stats.rx_bytes += desc->datalen;
Matteo Croced95b39c2007-10-14 18:10:13 +0200381 result = desc->skb;
Eugene Konev67d129d2007-10-24 10:42:02 +0800382 dma_unmap_single(&priv->dev->dev, desc->data_mapping,
383 CPMAC_SKB_SIZE, DMA_FROM_DEVICE);
Matteo Croced95b39c2007-10-14 18:10:13 +0200384 desc->skb = skb;
Eugene Konev67d129d2007-10-24 10:42:02 +0800385 desc->data_mapping = dma_map_single(&priv->dev->dev, skb->data,
Matteo Croced95b39c2007-10-14 18:10:13 +0200386 CPMAC_SKB_SIZE,
387 DMA_FROM_DEVICE);
388 desc->hw_data = (u32)desc->data_mapping;
389 if (unlikely(netif_msg_pktdata(priv))) {
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530390 netdev_dbg(priv->dev, "received packet:\n");
Eugene Konev67d129d2007-10-24 10:42:02 +0800391 cpmac_dump_skb(priv->dev, result);
Matteo Croced95b39c2007-10-14 18:10:13 +0200392 }
393 } else {
394 if (netif_msg_rx_err(priv) && net_ratelimit())
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530395 netdev_warn(priv->dev,
396 "low on skbs, dropping packet\n");
397
Eugene Konev67d129d2007-10-24 10:42:02 +0800398 priv->dev->stats.rx_dropped++;
Matteo Croced95b39c2007-10-14 18:10:13 +0200399 }
400
401 desc->buflen = CPMAC_SKB_SIZE;
402 desc->dataflags = CPMAC_OWN;
403
404 return result;
405}
406
Eugene Konev67d129d2007-10-24 10:42:02 +0800407static int cpmac_poll(struct napi_struct *napi, int budget)
Matteo Croced95b39c2007-10-14 18:10:13 +0200408{
409 struct sk_buff *skb;
Matteo Crocef917d582008-05-14 00:58:32 +0200410 struct cpmac_desc *desc, *restart;
Eugene Konev67d129d2007-10-24 10:42:02 +0800411 struct cpmac_priv *priv = container_of(napi, struct cpmac_priv, napi);
Matteo Crocef917d582008-05-14 00:58:32 +0200412 int received = 0, processed = 0;
Matteo Croced95b39c2007-10-14 18:10:13 +0200413
414 spin_lock(&priv->rx_lock);
415 if (unlikely(!priv->rx_head)) {
416 if (netif_msg_rx_err(priv) && net_ratelimit())
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530417 netdev_warn(priv->dev, "rx: polling, but no queue\n");
418
Matteo Crocef917d582008-05-14 00:58:32 +0200419 spin_unlock(&priv->rx_lock);
Ben Hutchings288379f2009-01-19 16:43:59 -0800420 napi_complete(napi);
Matteo Croced95b39c2007-10-14 18:10:13 +0200421 return 0;
422 }
423
424 desc = priv->rx_head;
Matteo Crocef917d582008-05-14 00:58:32 +0200425 restart = NULL;
Eugene Konev67d129d2007-10-24 10:42:02 +0800426 while (((desc->dataflags & CPMAC_OWN) == 0) && (received < budget)) {
Matteo Crocef917d582008-05-14 00:58:32 +0200427 processed++;
428
429 if ((desc->dataflags & CPMAC_EOQ) != 0) {
430 /* The last update to eoq->hw_next didn't happen
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530431 * soon enough, and the receiver stopped here.
432 * Remember this descriptor so we can restart
433 * the receiver after freeing some space.
434 */
Matteo Crocef917d582008-05-14 00:58:32 +0200435 if (unlikely(restart)) {
436 if (netif_msg_rx_err(priv))
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530437 netdev_err(priv->dev, "poll found a"
438 " duplicate EOQ: %p and %p\n",
439 restart, desc);
Matteo Crocef917d582008-05-14 00:58:32 +0200440 goto fatal_error;
441 }
442
443 restart = desc->next;
444 }
445
Eugene Konev67d129d2007-10-24 10:42:02 +0800446 skb = cpmac_rx_one(priv, desc);
Matteo Croced95b39c2007-10-14 18:10:13 +0200447 if (likely(skb)) {
448 netif_receive_skb(skb);
449 received++;
450 }
451 desc = desc->next;
452 }
453
Matteo Crocef917d582008-05-14 00:58:32 +0200454 if (desc != priv->rx_head) {
455 /* We freed some buffers, but not the whole ring,
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530456 * add what we did free to the rx list
457 */
Matteo Crocef917d582008-05-14 00:58:32 +0200458 desc->prev->hw_next = (u32)0;
459 priv->rx_head->prev->hw_next = priv->rx_head->mapping;
460 }
461
462 /* Optimization: If we did not actually process an EOQ (perhaps because
463 * of quota limits), check to see if the tail of the queue has EOQ set.
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530464 * We should immediately restart in that case so that the receiver can
465 * restart and run in parallel with more packet processing.
466 * This lets us handle slightly larger bursts before running
467 * out of ring space (assuming dev->weight < ring_size)
468 */
Matteo Crocef917d582008-05-14 00:58:32 +0200469
470 if (!restart &&
471 (priv->rx_head->prev->dataflags & (CPMAC_OWN|CPMAC_EOQ))
472 == CPMAC_EOQ &&
473 (priv->rx_head->dataflags & CPMAC_OWN) != 0) {
474 /* reset EOQ so the poll loop (above) doesn't try to
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530475 * restart this when it eventually gets to this descriptor.
476 */
Matteo Crocef917d582008-05-14 00:58:32 +0200477 priv->rx_head->prev->dataflags &= ~CPMAC_EOQ;
478 restart = priv->rx_head;
479 }
480
481 if (restart) {
482 priv->dev->stats.rx_errors++;
483 priv->dev->stats.rx_fifo_errors++;
484 if (netif_msg_rx_err(priv) && net_ratelimit())
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530485 netdev_warn(priv->dev, "rx dma ring overrun\n");
Matteo Crocef917d582008-05-14 00:58:32 +0200486
487 if (unlikely((restart->dataflags & CPMAC_OWN) == 0)) {
488 if (netif_msg_drv(priv))
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530489 netdev_err(priv->dev, "cpmac_poll is trying "
490 "to restart rx from a descriptor "
491 "that's not free: %p\n", restart);
Julia Lawall9e1634a2010-08-05 10:28:31 +0000492 goto fatal_error;
Matteo Crocef917d582008-05-14 00:58:32 +0200493 }
494
495 cpmac_write(priv->regs, CPMAC_RX_PTR(0), restart->mapping);
496 }
497
Matteo Croced95b39c2007-10-14 18:10:13 +0200498 priv->rx_head = desc;
499 spin_unlock(&priv->rx_lock);
Matteo Croced95b39c2007-10-14 18:10:13 +0200500 if (unlikely(netif_msg_rx_status(priv)))
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530501 netdev_dbg(priv->dev, "poll processed %d packets\n", received);
502
Matteo Crocef917d582008-05-14 00:58:32 +0200503 if (processed == 0) {
504 /* we ran out of packets to read,
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530505 * revert to interrupt-driven mode
506 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800507 napi_complete(napi);
Matteo Croced95b39c2007-10-14 18:10:13 +0200508 cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
509 return 0;
510 }
511
512 return 1;
Matteo Crocef917d582008-05-14 00:58:32 +0200513
514fatal_error:
515 /* Something went horribly wrong.
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530516 * Reset hardware to try to recover rather than wedging.
517 */
Matteo Crocef917d582008-05-14 00:58:32 +0200518 if (netif_msg_drv(priv)) {
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530519 netdev_err(priv->dev, "cpmac_poll is confused. "
520 "Resetting hardware\n");
Matteo Crocef917d582008-05-14 00:58:32 +0200521 cpmac_dump_all_desc(priv->dev);
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530522 netdev_dbg(priv->dev, "RX_PTR(0)=0x%08x RX_ACK(0)=0x%08x\n",
523 cpmac_read(priv->regs, CPMAC_RX_PTR(0)),
524 cpmac_read(priv->regs, CPMAC_RX_ACK(0)));
Matteo Crocef917d582008-05-14 00:58:32 +0200525 }
526
527 spin_unlock(&priv->rx_lock);
Ben Hutchings288379f2009-01-19 16:43:59 -0800528 napi_complete(napi);
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700529 netif_tx_stop_all_queues(priv->dev);
Matteo Crocef917d582008-05-14 00:58:32 +0200530 napi_disable(&priv->napi);
531
532 atomic_inc(&priv->reset_pending);
533 cpmac_hw_stop(priv->dev);
534 if (!schedule_work(&priv->reset_work))
535 atomic_dec(&priv->reset_pending);
536 return 0;
537
Matteo Croced95b39c2007-10-14 18:10:13 +0200538}
539
540static int cpmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
541{
542 int queue, len;
543 struct cpmac_desc *desc;
544 struct cpmac_priv *priv = netdev_priv(dev);
545
Matteo Crocef917d582008-05-14 00:58:32 +0200546 if (unlikely(atomic_read(&priv->reset_pending)))
547 return NETDEV_TX_BUSY;
548
Matteo Croce6cd043d2007-10-23 19:12:22 +0200549 if (unlikely(skb_padto(skb, ETH_ZLEN)))
550 return NETDEV_TX_OK;
Matteo Croced95b39c2007-10-14 18:10:13 +0200551
552 len = max(skb->len, ETH_ZLEN);
Matteo Croceba596a02008-01-12 19:05:23 +0100553 queue = skb_get_queue_mapping(skb);
Matteo Croced95b39c2007-10-14 18:10:13 +0200554 netif_stop_subqueue(dev, queue);
Matteo Croced95b39c2007-10-14 18:10:13 +0200555
556 desc = &priv->desc_ring[queue];
557 if (unlikely(desc->dataflags & CPMAC_OWN)) {
558 if (netif_msg_tx_err(priv) && net_ratelimit())
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530559 netdev_warn(dev, "tx dma ring full\n");
560
Matteo Croce6cd043d2007-10-23 19:12:22 +0200561 return NETDEV_TX_BUSY;
Matteo Croced95b39c2007-10-14 18:10:13 +0200562 }
563
564 spin_lock(&priv->lock);
Matteo Croced95b39c2007-10-14 18:10:13 +0200565 spin_unlock(&priv->lock);
566 desc->dataflags = CPMAC_SOP | CPMAC_EOP | CPMAC_OWN;
567 desc->skb = skb;
568 desc->data_mapping = dma_map_single(&dev->dev, skb->data, len,
569 DMA_TO_DEVICE);
570 desc->hw_data = (u32)desc->data_mapping;
571 desc->datalen = len;
572 desc->buflen = len;
573 if (unlikely(netif_msg_tx_queued(priv)))
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530574 netdev_dbg(dev, "sending 0x%p, len=%d\n", skb, skb->len);
Matteo Croced95b39c2007-10-14 18:10:13 +0200575 if (unlikely(netif_msg_hw(priv)))
576 cpmac_dump_desc(dev, desc);
577 if (unlikely(netif_msg_pktdata(priv)))
578 cpmac_dump_skb(dev, skb);
579 cpmac_write(priv->regs, CPMAC_TX_PTR(queue), (u32)desc->mapping);
580
Matteo Croce6cd043d2007-10-23 19:12:22 +0200581 return NETDEV_TX_OK;
Matteo Croced95b39c2007-10-14 18:10:13 +0200582}
583
584static void cpmac_end_xmit(struct net_device *dev, int queue)
585{
586 struct cpmac_desc *desc;
587 struct cpmac_priv *priv = netdev_priv(dev);
588
589 desc = &priv->desc_ring[queue];
590 cpmac_write(priv->regs, CPMAC_TX_ACK(queue), (u32)desc->mapping);
591 if (likely(desc->skb)) {
592 spin_lock(&priv->lock);
593 dev->stats.tx_packets++;
594 dev->stats.tx_bytes += desc->skb->len;
595 spin_unlock(&priv->lock);
596 dma_unmap_single(&dev->dev, desc->data_mapping, desc->skb->len,
597 DMA_TO_DEVICE);
598
599 if (unlikely(netif_msg_tx_done(priv)))
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530600 netdev_dbg(dev, "sent 0x%p, len=%d\n",
601 desc->skb, desc->skb->len);
Matteo Croced95b39c2007-10-14 18:10:13 +0200602
603 dev_kfree_skb_irq(desc->skb);
604 desc->skb = NULL;
Stefan Weil0220ff72009-05-31 10:59:15 +0000605 if (__netif_subqueue_stopped(dev, queue))
Matteo Croced95b39c2007-10-14 18:10:13 +0200606 netif_wake_subqueue(dev, queue);
Matteo Croced95b39c2007-10-14 18:10:13 +0200607 } else {
608 if (netif_msg_tx_err(priv) && net_ratelimit())
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530609 netdev_warn(dev, "end_xmit: spurious interrupt\n");
Stefan Weil0220ff72009-05-31 10:59:15 +0000610 if (__netif_subqueue_stopped(dev, queue))
Matteo Croced95b39c2007-10-14 18:10:13 +0200611 netif_wake_subqueue(dev, queue);
Matteo Croced95b39c2007-10-14 18:10:13 +0200612 }
613}
614
615static void cpmac_hw_stop(struct net_device *dev)
616{
617 int i;
618 struct cpmac_priv *priv = netdev_priv(dev);
Jingoo Hana0ea2ac2013-08-30 14:05:02 +0900619 struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200620
621 ar7_device_reset(pdata->reset_bit);
622 cpmac_write(priv->regs, CPMAC_RX_CONTROL,
623 cpmac_read(priv->regs, CPMAC_RX_CONTROL) & ~1);
624 cpmac_write(priv->regs, CPMAC_TX_CONTROL,
625 cpmac_read(priv->regs, CPMAC_TX_CONTROL) & ~1);
626 for (i = 0; i < 8; i++) {
627 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
628 cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
629 }
630 cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
631 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
632 cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
633 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
634 cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
635 cpmac_read(priv->regs, CPMAC_MAC_CONTROL) & ~MAC_MII);
636}
637
638static void cpmac_hw_start(struct net_device *dev)
639{
640 int i;
641 struct cpmac_priv *priv = netdev_priv(dev);
Jingoo Hana0ea2ac2013-08-30 14:05:02 +0900642 struct plat_cpmac_data *pdata = dev_get_platdata(&priv->pdev->dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200643
644 ar7_device_reset(pdata->reset_bit);
645 for (i = 0; i < 8; i++) {
646 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
647 cpmac_write(priv->regs, CPMAC_RX_PTR(i), 0);
648 }
649 cpmac_write(priv->regs, CPMAC_RX_PTR(0), priv->rx_head->mapping);
650
651 cpmac_write(priv->regs, CPMAC_MBP, MBP_RXSHORT | MBP_RXBCAST |
652 MBP_RXMCAST);
653 cpmac_write(priv->regs, CPMAC_BUFFER_OFFSET, 0);
654 for (i = 0; i < 8; i++)
655 cpmac_write(priv->regs, CPMAC_MAC_ADDR_LO(i), dev->dev_addr[5]);
656 cpmac_write(priv->regs, CPMAC_MAC_ADDR_MID, dev->dev_addr[4]);
657 cpmac_write(priv->regs, CPMAC_MAC_ADDR_HI, dev->dev_addr[0] |
658 (dev->dev_addr[1] << 8) | (dev->dev_addr[2] << 16) |
659 (dev->dev_addr[3] << 24));
660 cpmac_write(priv->regs, CPMAC_MAX_LENGTH, CPMAC_SKB_SIZE);
661 cpmac_write(priv->regs, CPMAC_UNICAST_CLEAR, 0xff);
662 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 0xff);
663 cpmac_write(priv->regs, CPMAC_TX_INT_CLEAR, 0xff);
664 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
665 cpmac_write(priv->regs, CPMAC_UNICAST_ENABLE, 1);
666 cpmac_write(priv->regs, CPMAC_RX_INT_ENABLE, 1);
667 cpmac_write(priv->regs, CPMAC_TX_INT_ENABLE, 0xff);
668 cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
669
670 cpmac_write(priv->regs, CPMAC_RX_CONTROL,
671 cpmac_read(priv->regs, CPMAC_RX_CONTROL) | 1);
672 cpmac_write(priv->regs, CPMAC_TX_CONTROL,
673 cpmac_read(priv->regs, CPMAC_TX_CONTROL) | 1);
674 cpmac_write(priv->regs, CPMAC_MAC_CONTROL,
675 cpmac_read(priv->regs, CPMAC_MAC_CONTROL) | MAC_MII |
676 MAC_FDX);
677}
678
679static void cpmac_clear_rx(struct net_device *dev)
680{
681 struct cpmac_priv *priv = netdev_priv(dev);
682 struct cpmac_desc *desc;
683 int i;
684 if (unlikely(!priv->rx_head))
685 return;
686 desc = priv->rx_head;
687 for (i = 0; i < priv->ring_size; i++) {
688 if ((desc->dataflags & CPMAC_OWN) == 0) {
689 if (netif_msg_rx_err(priv) && net_ratelimit())
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530690 netdev_warn(dev, "packet dropped\n");
Matteo Croced95b39c2007-10-14 18:10:13 +0200691 if (unlikely(netif_msg_hw(priv)))
692 cpmac_dump_desc(dev, desc);
693 desc->dataflags = CPMAC_OWN;
694 dev->stats.rx_dropped++;
695 }
Matteo Crocef917d582008-05-14 00:58:32 +0200696 desc->hw_next = desc->next->mapping;
Matteo Croced95b39c2007-10-14 18:10:13 +0200697 desc = desc->next;
698 }
Matteo Crocef917d582008-05-14 00:58:32 +0200699 priv->rx_head->prev->hw_next = 0;
Matteo Croced95b39c2007-10-14 18:10:13 +0200700}
701
702static void cpmac_clear_tx(struct net_device *dev)
703{
704 struct cpmac_priv *priv = netdev_priv(dev);
705 int i;
706 if (unlikely(!priv->desc_ring))
707 return;
Matteo Croce6cd043d2007-10-23 19:12:22 +0200708 for (i = 0; i < CPMAC_QUEUES; i++) {
709 priv->desc_ring[i].dataflags = 0;
Matteo Croced95b39c2007-10-14 18:10:13 +0200710 if (priv->desc_ring[i].skb) {
711 dev_kfree_skb_any(priv->desc_ring[i].skb);
Matteo Crocef917d582008-05-14 00:58:32 +0200712 priv->desc_ring[i].skb = NULL;
Matteo Croced95b39c2007-10-14 18:10:13 +0200713 }
Matteo Croce6cd043d2007-10-23 19:12:22 +0200714 }
Matteo Croced95b39c2007-10-14 18:10:13 +0200715}
716
717static void cpmac_hw_error(struct work_struct *work)
718{
719 struct cpmac_priv *priv =
720 container_of(work, struct cpmac_priv, reset_work);
721
722 spin_lock(&priv->rx_lock);
723 cpmac_clear_rx(priv->dev);
724 spin_unlock(&priv->rx_lock);
725 cpmac_clear_tx(priv->dev);
726 cpmac_hw_start(priv->dev);
Matteo Crocef917d582008-05-14 00:58:32 +0200727 barrier();
728 atomic_dec(&priv->reset_pending);
729
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700730 netif_tx_wake_all_queues(priv->dev);
Matteo Crocef917d582008-05-14 00:58:32 +0200731 cpmac_write(priv->regs, CPMAC_MAC_INT_ENABLE, 3);
732}
733
734static void cpmac_check_status(struct net_device *dev)
735{
736 struct cpmac_priv *priv = netdev_priv(dev);
737
738 u32 macstatus = cpmac_read(priv->regs, CPMAC_MAC_STATUS);
739 int rx_channel = (macstatus >> 8) & 7;
740 int rx_code = (macstatus >> 12) & 15;
741 int tx_channel = (macstatus >> 16) & 7;
742 int tx_code = (macstatus >> 20) & 15;
743
744 if (rx_code || tx_code) {
745 if (netif_msg_drv(priv) && net_ratelimit()) {
746 /* Can't find any documentation on what these
Varka Bhadram8bcd5c62014-07-10 11:05:40 +0530747 * error codes actually are. So just log them and hope..
Matteo Crocef917d582008-05-14 00:58:32 +0200748 */
749 if (rx_code)
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530750 netdev_warn(dev, "host error %d on rx "
751 "channel %d (macstatus %08x), resetting\n",
752 rx_code, rx_channel, macstatus);
Matteo Crocef917d582008-05-14 00:58:32 +0200753 if (tx_code)
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530754 netdev_warn(dev, "host error %d on tx "
755 "channel %d (macstatus %08x), resetting\n",
756 tx_code, tx_channel, macstatus);
Matteo Crocef917d582008-05-14 00:58:32 +0200757 }
758
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700759 netif_tx_stop_all_queues(dev);
Matteo Crocef917d582008-05-14 00:58:32 +0200760 cpmac_hw_stop(dev);
761 if (schedule_work(&priv->reset_work))
762 atomic_inc(&priv->reset_pending);
763 if (unlikely(netif_msg_hw(priv)))
764 cpmac_dump_regs(dev);
765 }
766 cpmac_write(priv->regs, CPMAC_MAC_INT_CLEAR, 0xff);
Matteo Croced95b39c2007-10-14 18:10:13 +0200767}
768
769static irqreturn_t cpmac_irq(int irq, void *dev_id)
770{
771 struct net_device *dev = dev_id;
772 struct cpmac_priv *priv;
773 int queue;
774 u32 status;
775
Matteo Croced95b39c2007-10-14 18:10:13 +0200776 priv = netdev_priv(dev);
777
778 status = cpmac_read(priv->regs, CPMAC_MAC_INT_VECTOR);
779
780 if (unlikely(netif_msg_intr(priv)))
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530781 netdev_dbg(dev, "interrupt status: 0x%08x\n", status);
Matteo Croced95b39c2007-10-14 18:10:13 +0200782
783 if (status & MAC_INT_TX)
784 cpmac_end_xmit(dev, (status & 7));
785
786 if (status & MAC_INT_RX) {
787 queue = (status >> 8) & 7;
Ben Hutchings288379f2009-01-19 16:43:59 -0800788 if (napi_schedule_prep(&priv->napi)) {
Eugene Konev67d129d2007-10-24 10:42:02 +0800789 cpmac_write(priv->regs, CPMAC_RX_INT_CLEAR, 1 << queue);
Ben Hutchings288379f2009-01-19 16:43:59 -0800790 __napi_schedule(&priv->napi);
Eugene Konev67d129d2007-10-24 10:42:02 +0800791 }
Matteo Croced95b39c2007-10-14 18:10:13 +0200792 }
793
794 cpmac_write(priv->regs, CPMAC_MAC_EOI_VECTOR, 0);
795
Matteo Crocef917d582008-05-14 00:58:32 +0200796 if (unlikely(status & (MAC_INT_HOST | MAC_INT_STATUS)))
797 cpmac_check_status(dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200798
799 return IRQ_HANDLED;
800}
801
802static void cpmac_tx_timeout(struct net_device *dev)
803{
Matteo Crocef917d582008-05-14 00:58:32 +0200804 struct cpmac_priv *priv = netdev_priv(dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200805
806 spin_lock(&priv->lock);
807 dev->stats.tx_errors++;
808 spin_unlock(&priv->lock);
809 if (netif_msg_tx_err(priv) && net_ratelimit())
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530810 netdev_warn(dev, "transmit timeout\n");
Matteo Crocef917d582008-05-14 00:58:32 +0200811
812 atomic_inc(&priv->reset_pending);
813 barrier();
814 cpmac_clear_tx(dev);
815 barrier();
816 atomic_dec(&priv->reset_pending);
817
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700818 netif_tx_wake_all_queues(priv->dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200819}
820
821static int cpmac_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
822{
823 struct cpmac_priv *priv = netdev_priv(dev);
824 if (!(netif_running(dev)))
825 return -EINVAL;
826 if (!priv->phy)
827 return -EINVAL;
Matteo Croced95b39c2007-10-14 18:10:13 +0200828
Richard Cochran28b04112010-07-17 08:48:55 +0000829 return phy_mii_ioctl(priv->phy, ifr, cmd);
Matteo Croced95b39c2007-10-14 18:10:13 +0200830}
831
832static int cpmac_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
833{
834 struct cpmac_priv *priv = netdev_priv(dev);
835
836 if (priv->phy)
837 return phy_ethtool_gset(priv->phy, cmd);
838
839 return -EINVAL;
840}
841
842static int cpmac_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
843{
844 struct cpmac_priv *priv = netdev_priv(dev);
845
846 if (!capable(CAP_NET_ADMIN))
847 return -EPERM;
848
849 if (priv->phy)
850 return phy_ethtool_sset(priv->phy, cmd);
851
852 return -EINVAL;
853}
854
Florian Fainelli559764d2010-08-08 10:09:39 +0000855static void cpmac_get_ringparam(struct net_device *dev,
856 struct ethtool_ringparam *ring)
Matteo Croced95b39c2007-10-14 18:10:13 +0200857{
858 struct cpmac_priv *priv = netdev_priv(dev);
859
860 ring->rx_max_pending = 1024;
861 ring->rx_mini_max_pending = 1;
862 ring->rx_jumbo_max_pending = 1;
863 ring->tx_max_pending = 1;
864
865 ring->rx_pending = priv->ring_size;
866 ring->rx_mini_pending = 1;
867 ring->rx_jumbo_pending = 1;
868 ring->tx_pending = 1;
869}
870
Florian Fainelli559764d2010-08-08 10:09:39 +0000871static int cpmac_set_ringparam(struct net_device *dev,
872 struct ethtool_ringparam *ring)
Matteo Croced95b39c2007-10-14 18:10:13 +0200873{
874 struct cpmac_priv *priv = netdev_priv(dev);
875
Matteo Croce6cd043d2007-10-23 19:12:22 +0200876 if (netif_running(dev))
Matteo Croced95b39c2007-10-14 18:10:13 +0200877 return -EBUSY;
878 priv->ring_size = ring->rx_pending;
879 return 0;
880}
881
882static void cpmac_get_drvinfo(struct net_device *dev,
883 struct ethtool_drvinfo *info)
884{
Jiri Pirko7826d432013-01-06 00:44:26 +0000885 strlcpy(info->driver, "cpmac", sizeof(info->driver));
886 strlcpy(info->version, CPMAC_VERSION, sizeof(info->version));
887 snprintf(info->bus_info, sizeof(info->bus_info), "%s", "cpmac");
Matteo Croced95b39c2007-10-14 18:10:13 +0200888 info->regdump_len = 0;
889}
890
891static const struct ethtool_ops cpmac_ethtool_ops = {
892 .get_settings = cpmac_get_settings,
893 .set_settings = cpmac_set_settings,
894 .get_drvinfo = cpmac_get_drvinfo,
895 .get_link = ethtool_op_get_link,
896 .get_ringparam = cpmac_get_ringparam,
897 .set_ringparam = cpmac_set_ringparam,
898};
899
900static void cpmac_adjust_link(struct net_device *dev)
901{
902 struct cpmac_priv *priv = netdev_priv(dev);
903 int new_state = 0;
904
905 spin_lock(&priv->lock);
906 if (priv->phy->link) {
David S. Millerfd2ea0a2008-07-17 01:56:23 -0700907 netif_tx_start_all_queues(dev);
Matteo Croced95b39c2007-10-14 18:10:13 +0200908 if (priv->phy->duplex != priv->oldduplex) {
909 new_state = 1;
910 priv->oldduplex = priv->phy->duplex;
911 }
912
913 if (priv->phy->speed != priv->oldspeed) {
914 new_state = 1;
915 priv->oldspeed = priv->phy->speed;
916 }
917
918 if (!priv->oldlink) {
919 new_state = 1;
920 priv->oldlink = 1;
Matteo Croced95b39c2007-10-14 18:10:13 +0200921 }
922 } else if (priv->oldlink) {
Matteo Croced95b39c2007-10-14 18:10:13 +0200923 new_state = 1;
924 priv->oldlink = 0;
925 priv->oldspeed = 0;
926 priv->oldduplex = -1;
927 }
928
929 if (new_state && netif_msg_link(priv) && net_ratelimit())
930 phy_print_status(priv->phy);
931
932 spin_unlock(&priv->lock);
933}
934
935static int cpmac_open(struct net_device *dev)
936{
937 int i, size, res;
938 struct cpmac_priv *priv = netdev_priv(dev);
939 struct resource *mem;
940 struct cpmac_desc *desc;
941 struct sk_buff *skb;
942
Matteo Croced95b39c2007-10-14 18:10:13 +0200943 mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
Dan Carpenter7e307c72010-06-30 13:12:01 -0700944 if (!request_mem_region(mem->start, resource_size(mem), dev->name)) {
Matteo Croced95b39c2007-10-14 18:10:13 +0200945 if (netif_msg_drv(priv))
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530946 netdev_err(dev, "failed to request registers\n");
947
Matteo Croced95b39c2007-10-14 18:10:13 +0200948 res = -ENXIO;
949 goto fail_reserve;
950 }
951
Dan Carpenter7e307c72010-06-30 13:12:01 -0700952 priv->regs = ioremap(mem->start, resource_size(mem));
Matteo Croced95b39c2007-10-14 18:10:13 +0200953 if (!priv->regs) {
954 if (netif_msg_drv(priv))
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530955 netdev_err(dev, "failed to remap registers\n");
956
Matteo Croced95b39c2007-10-14 18:10:13 +0200957 res = -ENXIO;
958 goto fail_remap;
959 }
960
961 size = priv->ring_size + CPMAC_QUEUES;
962 priv->desc_ring = dma_alloc_coherent(&dev->dev,
963 sizeof(struct cpmac_desc) * size,
964 &priv->dma_ring,
965 GFP_KERNEL);
966 if (!priv->desc_ring) {
967 res = -ENOMEM;
968 goto fail_alloc;
969 }
970
971 for (i = 0; i < size; i++)
972 priv->desc_ring[i].mapping = priv->dma_ring + sizeof(*desc) * i;
973
974 priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
975 for (i = 0, desc = priv->rx_head; i < priv->ring_size; i++, desc++) {
Eric Dumazet89d71a62009-10-13 05:34:20 +0000976 skb = netdev_alloc_skb_ip_align(dev, CPMAC_SKB_SIZE);
Matteo Croced95b39c2007-10-14 18:10:13 +0200977 if (unlikely(!skb)) {
978 res = -ENOMEM;
979 goto fail_desc;
980 }
Matteo Croced95b39c2007-10-14 18:10:13 +0200981 desc->skb = skb;
982 desc->data_mapping = dma_map_single(&dev->dev, skb->data,
983 CPMAC_SKB_SIZE,
984 DMA_FROM_DEVICE);
985 desc->hw_data = (u32)desc->data_mapping;
986 desc->buflen = CPMAC_SKB_SIZE;
987 desc->dataflags = CPMAC_OWN;
988 desc->next = &priv->rx_head[(i + 1) % priv->ring_size];
Matteo Crocef917d582008-05-14 00:58:32 +0200989 desc->next->prev = desc;
Matteo Croced95b39c2007-10-14 18:10:13 +0200990 desc->hw_next = (u32)desc->next->mapping;
991 }
992
Matteo Crocef917d582008-05-14 00:58:32 +0200993 priv->rx_head->prev->hw_next = (u32)0;
994
Florian Fainelli559764d2010-08-08 10:09:39 +0000995 res = request_irq(dev->irq, cpmac_irq, IRQF_SHARED, dev->name, dev);
996 if (res) {
Matteo Croced95b39c2007-10-14 18:10:13 +0200997 if (netif_msg_drv(priv))
Varka Bhadramf160a2d2014-07-10 11:05:41 +0530998 netdev_err(dev, "failed to obtain irq\n");
999
Matteo Croced95b39c2007-10-14 18:10:13 +02001000 goto fail_irq;
1001 }
1002
Matteo Crocef917d582008-05-14 00:58:32 +02001003 atomic_set(&priv->reset_pending, 0);
Matteo Croced95b39c2007-10-14 18:10:13 +02001004 INIT_WORK(&priv->reset_work, cpmac_hw_error);
1005 cpmac_hw_start(dev);
1006
Eugene Konev67d129d2007-10-24 10:42:02 +08001007 napi_enable(&priv->napi);
Matteo Croced95b39c2007-10-14 18:10:13 +02001008 priv->phy->state = PHY_CHANGELINK;
1009 phy_start(priv->phy);
1010
1011 return 0;
1012
1013fail_irq:
1014fail_desc:
1015 for (i = 0; i < priv->ring_size; i++) {
1016 if (priv->rx_head[i].skb) {
1017 dma_unmap_single(&dev->dev,
1018 priv->rx_head[i].data_mapping,
1019 CPMAC_SKB_SIZE,
1020 DMA_FROM_DEVICE);
1021 kfree_skb(priv->rx_head[i].skb);
1022 }
1023 }
1024fail_alloc:
1025 kfree(priv->desc_ring);
1026 iounmap(priv->regs);
1027
1028fail_remap:
Dan Carpenter7e307c72010-06-30 13:12:01 -07001029 release_mem_region(mem->start, resource_size(mem));
Matteo Croced95b39c2007-10-14 18:10:13 +02001030
1031fail_reserve:
Matteo Croced95b39c2007-10-14 18:10:13 +02001032 return res;
1033}
1034
1035static int cpmac_stop(struct net_device *dev)
1036{
1037 int i;
1038 struct cpmac_priv *priv = netdev_priv(dev);
1039 struct resource *mem;
1040
David S. Millerfd2ea0a2008-07-17 01:56:23 -07001041 netif_tx_stop_all_queues(dev);
Matteo Croced95b39c2007-10-14 18:10:13 +02001042
1043 cancel_work_sync(&priv->reset_work);
Eugene Konev67d129d2007-10-24 10:42:02 +08001044 napi_disable(&priv->napi);
Matteo Croced95b39c2007-10-14 18:10:13 +02001045 phy_stop(priv->phy);
Matteo Croced95b39c2007-10-14 18:10:13 +02001046
1047 cpmac_hw_stop(dev);
1048
1049 for (i = 0; i < 8; i++)
1050 cpmac_write(priv->regs, CPMAC_TX_PTR(i), 0);
1051 cpmac_write(priv->regs, CPMAC_RX_PTR(0), 0);
1052 cpmac_write(priv->regs, CPMAC_MBP, 0);
1053
1054 free_irq(dev->irq, dev);
1055 iounmap(priv->regs);
1056 mem = platform_get_resource_byname(priv->pdev, IORESOURCE_MEM, "regs");
Dan Carpenter7e307c72010-06-30 13:12:01 -07001057 release_mem_region(mem->start, resource_size(mem));
Matteo Croced95b39c2007-10-14 18:10:13 +02001058 priv->rx_head = &priv->desc_ring[CPMAC_QUEUES];
1059 for (i = 0; i < priv->ring_size; i++) {
1060 if (priv->rx_head[i].skb) {
1061 dma_unmap_single(&dev->dev,
1062 priv->rx_head[i].data_mapping,
1063 CPMAC_SKB_SIZE,
1064 DMA_FROM_DEVICE);
1065 kfree_skb(priv->rx_head[i].skb);
1066 }
1067 }
1068
1069 dma_free_coherent(&dev->dev, sizeof(struct cpmac_desc) *
1070 (CPMAC_QUEUES + priv->ring_size),
1071 priv->desc_ring, priv->dma_ring);
1072 return 0;
1073}
1074
Alexander Beregalov63ef7d82009-04-15 12:52:36 +00001075static const struct net_device_ops cpmac_netdev_ops = {
1076 .ndo_open = cpmac_open,
1077 .ndo_stop = cpmac_stop,
1078 .ndo_start_xmit = cpmac_start_xmit,
1079 .ndo_tx_timeout = cpmac_tx_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001080 .ndo_set_rx_mode = cpmac_set_multicast_list,
Florian Fainelli6a9b6542009-06-24 16:32:33 -07001081 .ndo_do_ioctl = cpmac_ioctl,
Alexander Beregalov63ef7d82009-04-15 12:52:36 +00001082 .ndo_change_mtu = eth_change_mtu,
1083 .ndo_validate_addr = eth_validate_addr,
1084 .ndo_set_mac_address = eth_mac_addr,
1085};
1086
Matteo Croced95b39c2007-10-14 18:10:13 +02001087static int external_switch;
1088
Bill Pembertonf57ae662012-12-03 09:23:43 -05001089static int cpmac_probe(struct platform_device *pdev)
Matteo Croced95b39c2007-10-14 18:10:13 +02001090{
Florian Fainelli69bd4ae2009-05-31 10:57:07 +00001091 int rc, phy_id;
Florian Fainelli762c6aa2009-09-15 21:44:22 +00001092 char mdio_bus_id[MII_BUS_ID_SIZE];
Matteo Croced95b39c2007-10-14 18:10:13 +02001093 struct resource *mem;
1094 struct cpmac_priv *priv;
1095 struct net_device *dev;
1096 struct plat_cpmac_data *pdata;
1097
Jingoo Hana0ea2ac2013-08-30 14:05:02 +09001098 pdata = dev_get_platdata(&pdev->dev);
Matteo Croced95b39c2007-10-14 18:10:13 +02001099
Florian Fainelli76e61ea2009-08-04 10:52:52 +00001100 if (external_switch || dumb_switch) {
Florian Fainellia19c5d62012-02-13 01:23:20 +00001101 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
Florian Fainelli76e61ea2009-08-04 10:52:52 +00001102 phy_id = pdev->id;
1103 } else {
1104 for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
1105 if (!(pdata->phy_mask & (1 << phy_id)))
1106 continue;
1107 if (!cpmac_mii->phy_map[phy_id])
1108 continue;
Florian Fainelli762c6aa2009-09-15 21:44:22 +00001109 strncpy(mdio_bus_id, cpmac_mii->id, MII_BUS_ID_SIZE);
Florian Fainelli76e61ea2009-08-04 10:52:52 +00001110 break;
1111 }
Matteo Croced95b39c2007-10-14 18:10:13 +02001112 }
1113
1114 if (phy_id == PHY_MAX_ADDR) {
Florian Fainelli559764d2010-08-08 10:09:39 +00001115 dev_err(&pdev->dev, "no PHY present, falling back "
Varka Bhadramf160a2d2014-07-10 11:05:41 +05301116 "to switch on MDIO bus 0\n");
Florian Fainellia19c5d62012-02-13 01:23:20 +00001117 strncpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); /* fixed phys bus */
Florian Fainelli9fba1c32010-03-07 00:55:47 +00001118 phy_id = pdev->id;
Matteo Croced95b39c2007-10-14 18:10:13 +02001119 }
1120
1121 dev = alloc_etherdev_mq(sizeof(*priv), CPMAC_QUEUES);
Joe Perches41de8d42012-01-29 13:47:52 +00001122 if (!dev)
Matteo Croced95b39c2007-10-14 18:10:13 +02001123 return -ENOMEM;
Matteo Croced95b39c2007-10-14 18:10:13 +02001124
1125 platform_set_drvdata(pdev, dev);
1126 priv = netdev_priv(dev);
1127
1128 priv->pdev = pdev;
1129 mem = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
1130 if (!mem) {
1131 rc = -ENODEV;
1132 goto fail;
1133 }
1134
1135 dev->irq = platform_get_irq_byname(pdev, "irq");
1136
Alexander Beregalov63ef7d82009-04-15 12:52:36 +00001137 dev->netdev_ops = &cpmac_netdev_ops;
1138 dev->ethtool_ops = &cpmac_ethtool_ops;
Matteo Croced95b39c2007-10-14 18:10:13 +02001139
Eugene Konev67d129d2007-10-24 10:42:02 +08001140 netif_napi_add(dev, &priv->napi, cpmac_poll, 64);
1141
Matteo Croced95b39c2007-10-14 18:10:13 +02001142 spin_lock_init(&priv->lock);
1143 spin_lock_init(&priv->rx_lock);
1144 priv->dev = dev;
1145 priv->ring_size = 64;
1146 priv->msg_enable = netif_msg_init(debug_level, 0xff);
Julia Lawall2447f2f2009-12-13 05:35:45 +00001147 memcpy(dev->dev_addr, pdata->dev_addr, sizeof(pdata->dev_addr));
Eugene Konevb88219f2007-10-24 10:42:03 +08001148
Florian Fainelli559764d2010-08-08 10:09:39 +00001149 snprintf(priv->phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
1150 mdio_bus_id, phy_id);
Florian Fainelli76e61ea2009-08-04 10:52:52 +00001151
Florian Fainellif9a8f832013-01-14 00:52:52 +00001152 priv->phy = phy_connect(dev, priv->phy_name, cpmac_adjust_link,
1153 PHY_INTERFACE_MODE_MII);
Florian Fainelli76e61ea2009-08-04 10:52:52 +00001154
Eugene Konevb88219f2007-10-24 10:42:03 +08001155 if (IS_ERR(priv->phy)) {
1156 if (netif_msg_drv(priv))
Varka Bhadramf160a2d2014-07-10 11:05:41 +05301157 dev_err(&pdev->dev, "Could not attach to PHY\n");
1158
Florian Fainellied770f02010-06-20 22:07:48 +00001159 rc = PTR_ERR(priv->phy);
1160 goto fail;
Eugene Konevb88219f2007-10-24 10:42:03 +08001161 }
Matteo Croced95b39c2007-10-14 18:10:13 +02001162
Florian Fainelli559764d2010-08-08 10:09:39 +00001163 rc = register_netdev(dev);
1164 if (rc) {
Varka Bhadramf160a2d2014-07-10 11:05:41 +05301165 dev_err(&pdev->dev, "Could not register net device\n");
Matteo Croced95b39c2007-10-14 18:10:13 +02001166 goto fail;
1167 }
1168
1169 if (netif_msg_probe(priv)) {
Varka Bhadramf160a2d2014-07-10 11:05:41 +05301170 dev_info(&pdev->dev, "regs: %p, irq: %d, phy: %s, "
1171 "mac: %pM\n", (void *)mem->start, dev->irq,
1172 priv->phy_name, dev->dev_addr);
Matteo Croced95b39c2007-10-14 18:10:13 +02001173 }
1174 return 0;
1175
1176fail:
1177 free_netdev(dev);
1178 return rc;
1179}
1180
Bill Pembertonf57ae662012-12-03 09:23:43 -05001181static int cpmac_remove(struct platform_device *pdev)
Matteo Croced95b39c2007-10-14 18:10:13 +02001182{
1183 struct net_device *dev = platform_get_drvdata(pdev);
1184 unregister_netdev(dev);
1185 free_netdev(dev);
1186 return 0;
1187}
1188
1189static struct platform_driver cpmac_driver = {
Varka Bhadram96a8d3c2014-07-10 11:05:42 +05301190 .driver = {
1191 .name = "cpmac",
1192 .owner = THIS_MODULE,
1193 },
1194 .probe = cpmac_probe,
Bill Pembertonf57ae662012-12-03 09:23:43 -05001195 .remove = cpmac_remove,
Matteo Croced95b39c2007-10-14 18:10:13 +02001196};
1197
Bill Pembertonf57ae662012-12-03 09:23:43 -05001198int cpmac_init(void)
Matteo Croced95b39c2007-10-14 18:10:13 +02001199{
1200 u32 mask;
1201 int i, res;
1202
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001203 cpmac_mii = mdiobus_alloc();
1204 if (cpmac_mii == NULL)
1205 return -ENOMEM;
Matteo Croced95b39c2007-10-14 18:10:13 +02001206
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001207 cpmac_mii->name = "cpmac-mii";
1208 cpmac_mii->read = cpmac_mdio_read;
1209 cpmac_mii->write = cpmac_mdio_write;
1210 cpmac_mii->reset = cpmac_mdio_reset;
1211 cpmac_mii->irq = mii_irqs;
1212
1213 cpmac_mii->priv = ioremap(AR7_REGS_MDIO, 256);
1214
1215 if (!cpmac_mii->priv) {
Varka Bhadramf160a2d2014-07-10 11:05:41 +05301216 pr_err("Can't ioremap mdio registers\n");
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001217 res = -ENXIO;
1218 goto fail_alloc;
Matteo Croced95b39c2007-10-14 18:10:13 +02001219 }
1220
1221#warning FIXME: unhardcode gpio&reset bits
1222 ar7_gpio_disable(26);
1223 ar7_gpio_disable(27);
1224 ar7_device_reset(AR7_RESET_BIT_CPMAC_LO);
1225 ar7_device_reset(AR7_RESET_BIT_CPMAC_HI);
1226 ar7_device_reset(AR7_RESET_BIT_EPHY);
1227
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001228 cpmac_mii->reset(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001229
Florian Fainelli559764d2010-08-08 10:09:39 +00001230 for (i = 0; i < 300; i++) {
1231 mask = cpmac_read(cpmac_mii->priv, CPMAC_MDIO_ALIVE);
1232 if (mask)
Matteo Croced95b39c2007-10-14 18:10:13 +02001233 break;
1234 else
Florian Fainellie4540aa2009-08-04 10:52:57 +00001235 msleep(10);
Florian Fainelli559764d2010-08-08 10:09:39 +00001236 }
Matteo Croced95b39c2007-10-14 18:10:13 +02001237
1238 mask &= 0x7fffffff;
1239 if (mask & (mask - 1)) {
1240 external_switch = 1;
1241 mask = 0;
1242 }
1243
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001244 cpmac_mii->phy_mask = ~(mask | 0x80000000);
Florian Fainellid1733f02012-01-09 23:59:21 +00001245 snprintf(cpmac_mii->id, MII_BUS_ID_SIZE, "cpmac-1");
Matteo Croced95b39c2007-10-14 18:10:13 +02001246
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001247 res = mdiobus_register(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001248 if (res)
1249 goto fail_mii;
1250
1251 res = platform_driver_register(&cpmac_driver);
1252 if (res)
1253 goto fail_cpmac;
1254
1255 return 0;
1256
1257fail_cpmac:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001258 mdiobus_unregister(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001259
1260fail_mii:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001261 iounmap(cpmac_mii->priv);
1262
1263fail_alloc:
1264 mdiobus_free(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001265
1266 return res;
1267}
1268
Bill Pembertonf57ae662012-12-03 09:23:43 -05001269void cpmac_exit(void)
Matteo Croced95b39c2007-10-14 18:10:13 +02001270{
1271 platform_driver_unregister(&cpmac_driver);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001272 mdiobus_unregister(cpmac_mii);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07001273 iounmap(cpmac_mii->priv);
Dan Carpenter48a29512010-03-02 22:46:10 +00001274 mdiobus_free(cpmac_mii);
Matteo Croced95b39c2007-10-14 18:10:13 +02001275}
1276
1277module_init(cpmac_init);
1278module_exit(cpmac_exit);