blob: 788c4fdab9c22bdd707426d42511d2038df7da2e [file] [log] [blame]
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001/***************************************************************************
2 *
3 * Copyright (C) 2004-2008 SMSC
4 * Copyright (C) 2005-2008 ARM
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 *
20 ***************************************************************************
21 * Rewritten, heavily based on smsc911x simple driver by SMSC.
22 * Partly uses io macros from smc91x.c by Nicolas Pitre
23 *
24 * Supported devices:
25 * LAN9115, LAN9116, LAN9117, LAN9118
26 * LAN9215, LAN9216, LAN9217, LAN9218
27 * LAN9210, LAN9211
28 * LAN9220, LAN9221
29 *
30 */
31
Joe Perchesdffc6b22011-03-25 14:21:22 +000032#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33
Steve Glendinningfd9abb32008-11-05 00:35:37 +000034#include <linux/crc32.h>
35#include <linux/delay.h>
36#include <linux/errno.h>
37#include <linux/etherdevice.h>
38#include <linux/ethtool.h>
39#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000040#include <linux/interrupt.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000041#include <linux/ioport.h>
42#include <linux/kernel.h>
43#include <linux/module.h>
44#include <linux/netdevice.h>
45#include <linux/platform_device.h>
46#include <linux/sched.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000047#include <linux/timer.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000048#include <linux/bug.h>
49#include <linux/bitops.h>
50#include <linux/irq.h>
51#include <linux/io.h>
Magnus Damm833cc672009-04-27 21:32:16 +000052#include <linux/swab.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000053#include <linux/phy.h>
54#include <linux/smsc911x.h>
Daniel Mack6cb87822009-08-05 08:29:31 +000055#include <linux/device.h>
Shawn Guo79f88ee2011-07-30 08:26:00 +000056#include <linux/of.h>
57#include <linux/of_device.h>
58#include <linux/of_gpio.h>
59#include <linux/of_net.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000060#include "smsc911x.h"
61
62#define SMSC_CHIPNAME "smsc911x"
63#define SMSC_MDIONAME "smsc911x-mdio"
64#define SMSC_DRV_VERSION "2008-10-21"
65
66MODULE_LICENSE("GPL");
67MODULE_VERSION(SMSC_DRV_VERSION);
Vincent Stehlé62038e42010-09-26 18:50:05 -070068MODULE_ALIAS("platform:smsc911x");
Steve Glendinningfd9abb32008-11-05 00:35:37 +000069
70#if USE_DEBUG > 0
71static int debug = 16;
72#else
73static int debug = 3;
74#endif
75
76module_param(debug, int, 0);
77MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
78
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -070079struct smsc911x_data;
80
81struct smsc911x_ops {
82 u32 (*reg_read)(struct smsc911x_data *pdata, u32 reg);
83 void (*reg_write)(struct smsc911x_data *pdata, u32 reg, u32 val);
84 void (*rx_readfifo)(struct smsc911x_data *pdata,
85 unsigned int *buf, unsigned int wordcount);
86 void (*tx_writefifo)(struct smsc911x_data *pdata,
87 unsigned int *buf, unsigned int wordcount);
88};
89
Steve Glendinningfd9abb32008-11-05 00:35:37 +000090struct smsc911x_data {
91 void __iomem *ioaddr;
92
93 unsigned int idrev;
94
95 /* used to decide which workarounds apply */
96 unsigned int generation;
97
98 /* device configuration (copied from platform_data during probe) */
Steve Glendinning2107fb82008-11-05 00:35:38 +000099 struct smsc911x_platform_config config;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000100
101 /* This needs to be acquired before calling any of below:
102 * smsc911x_mac_read(), smsc911x_mac_write()
103 */
104 spinlock_t mac_lock;
105
Catalin Marinas492c5d92010-07-19 13:36:21 -0700106 /* spinlock to ensure register accesses are serialised */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000107 spinlock_t dev_lock;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000108
109 struct phy_device *phy_dev;
110 struct mii_bus *mii_bus;
111 int phy_irq[PHY_MAX_ADDR];
112 unsigned int using_extphy;
113 int last_duplex;
114 int last_carrier;
115
116 u32 msg_enable;
117 unsigned int gpio_setting;
118 unsigned int gpio_orig_setting;
119 struct net_device *dev;
120 struct napi_struct napi;
121
122 unsigned int software_irq_signal;
123
124#ifdef USE_PHY_WORK_AROUND
125#define MIN_PACKET_SIZE (64)
126 char loopback_tx_pkt[MIN_PACKET_SIZE];
127 char loopback_rx_pkt[MIN_PACKET_SIZE];
128 unsigned int resetcount;
129#endif
130
131 /* Members for Multicast filter workaround */
132 unsigned int multicast_update_pending;
133 unsigned int set_bits_mask;
134 unsigned int clear_bits_mask;
135 unsigned int hashhi;
136 unsigned int hashlo;
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -0700137
138 /* register access functions */
139 const struct smsc911x_ops *ops;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000140};
141
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -0700142/* Easy access to information */
143#define __smsc_shift(pdata, reg) ((reg) << ((pdata)->config.shift))
144
Catalin Marinas492c5d92010-07-19 13:36:21 -0700145static inline u32 __smsc911x_reg_read(struct smsc911x_data *pdata, u32 reg)
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000146{
Steve Glendinning2107fb82008-11-05 00:35:38 +0000147 if (pdata->config.flags & SMSC911X_USE_32BIT)
148 return readl(pdata->ioaddr + reg);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000149
Catalin Marinas492c5d92010-07-19 13:36:21 -0700150 if (pdata->config.flags & SMSC911X_USE_16BIT)
151 return ((readw(pdata->ioaddr + reg) & 0xFFFF) |
Steve Glendinning2107fb82008-11-05 00:35:38 +0000152 ((readw(pdata->ioaddr + reg + 2) & 0xFFFF) << 16));
Steve Glendinning2107fb82008-11-05 00:35:38 +0000153
154 BUG();
Steve Glendinning702403a2009-01-11 00:14:27 -0800155 return 0;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000156}
157
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -0700158static inline u32
159__smsc911x_reg_read_shift(struct smsc911x_data *pdata, u32 reg)
160{
161 if (pdata->config.flags & SMSC911X_USE_32BIT)
162 return readl(pdata->ioaddr + __smsc_shift(pdata, reg));
163
164 if (pdata->config.flags & SMSC911X_USE_16BIT)
165 return (readw(pdata->ioaddr +
166 __smsc_shift(pdata, reg)) & 0xFFFF) |
167 ((readw(pdata->ioaddr +
168 __smsc_shift(pdata, reg + 2)) & 0xFFFF) << 16);
169
170 BUG();
171 return 0;
172}
173
Catalin Marinas492c5d92010-07-19 13:36:21 -0700174static inline u32 smsc911x_reg_read(struct smsc911x_data *pdata, u32 reg)
175{
176 u32 data;
177 unsigned long flags;
178
179 spin_lock_irqsave(&pdata->dev_lock, flags);
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -0700180 data = pdata->ops->reg_read(pdata, reg);
Catalin Marinas492c5d92010-07-19 13:36:21 -0700181 spin_unlock_irqrestore(&pdata->dev_lock, flags);
182
183 return data;
184}
185
186static inline void __smsc911x_reg_write(struct smsc911x_data *pdata, u32 reg,
187 u32 val)
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000188{
Steve Glendinning2107fb82008-11-05 00:35:38 +0000189 if (pdata->config.flags & SMSC911X_USE_32BIT) {
190 writel(val, pdata->ioaddr + reg);
191 return;
192 }
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000193
Steve Glendinning2107fb82008-11-05 00:35:38 +0000194 if (pdata->config.flags & SMSC911X_USE_16BIT) {
Steve Glendinning2107fb82008-11-05 00:35:38 +0000195 writew(val & 0xFFFF, pdata->ioaddr + reg);
196 writew((val >> 16) & 0xFFFF, pdata->ioaddr + reg + 2);
Steve Glendinning2107fb82008-11-05 00:35:38 +0000197 return;
198 }
199
200 BUG();
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000201}
202
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -0700203static inline void
204__smsc911x_reg_write_shift(struct smsc911x_data *pdata, u32 reg, u32 val)
205{
206 if (pdata->config.flags & SMSC911X_USE_32BIT) {
207 writel(val, pdata->ioaddr + __smsc_shift(pdata, reg));
208 return;
209 }
210
211 if (pdata->config.flags & SMSC911X_USE_16BIT) {
212 writew(val & 0xFFFF,
213 pdata->ioaddr + __smsc_shift(pdata, reg));
214 writew((val >> 16) & 0xFFFF,
215 pdata->ioaddr + __smsc_shift(pdata, reg + 2));
216 return;
217 }
218
219 BUG();
220}
221
Catalin Marinas492c5d92010-07-19 13:36:21 -0700222static inline void smsc911x_reg_write(struct smsc911x_data *pdata, u32 reg,
223 u32 val)
224{
225 unsigned long flags;
226
227 spin_lock_irqsave(&pdata->dev_lock, flags);
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -0700228 pdata->ops->reg_write(pdata, reg, val);
Catalin Marinas492c5d92010-07-19 13:36:21 -0700229 spin_unlock_irqrestore(&pdata->dev_lock, flags);
230}
231
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000232/* Writes a packet to the TX_DATA_FIFO */
233static inline void
234smsc911x_tx_writefifo(struct smsc911x_data *pdata, unsigned int *buf,
235 unsigned int wordcount)
236{
Catalin Marinas492c5d92010-07-19 13:36:21 -0700237 unsigned long flags;
238
239 spin_lock_irqsave(&pdata->dev_lock, flags);
240
Magnus Damm833cc672009-04-27 21:32:16 +0000241 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
242 while (wordcount--)
Catalin Marinas492c5d92010-07-19 13:36:21 -0700243 __smsc911x_reg_write(pdata, TX_DATA_FIFO,
244 swab32(*buf++));
245 goto out;
Magnus Damm833cc672009-04-27 21:32:16 +0000246 }
247
Steve Glendinning2107fb82008-11-05 00:35:38 +0000248 if (pdata->config.flags & SMSC911X_USE_32BIT) {
249 writesl(pdata->ioaddr + TX_DATA_FIFO, buf, wordcount);
Catalin Marinas492c5d92010-07-19 13:36:21 -0700250 goto out;
Steve Glendinning2107fb82008-11-05 00:35:38 +0000251 }
252
253 if (pdata->config.flags & SMSC911X_USE_16BIT) {
254 while (wordcount--)
Catalin Marinas492c5d92010-07-19 13:36:21 -0700255 __smsc911x_reg_write(pdata, TX_DATA_FIFO, *buf++);
256 goto out;
Steve Glendinning2107fb82008-11-05 00:35:38 +0000257 }
258
259 BUG();
Catalin Marinas492c5d92010-07-19 13:36:21 -0700260out:
261 spin_unlock_irqrestore(&pdata->dev_lock, flags);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000262}
263
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -0700264/* Writes a packet to the TX_DATA_FIFO - shifted version */
265static inline void
266smsc911x_tx_writefifo_shift(struct smsc911x_data *pdata, unsigned int *buf,
267 unsigned int wordcount)
268{
269 unsigned long flags;
270
271 spin_lock_irqsave(&pdata->dev_lock, flags);
272
273 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
274 while (wordcount--)
275 __smsc911x_reg_write_shift(pdata, TX_DATA_FIFO,
276 swab32(*buf++));
277 goto out;
278 }
279
280 if (pdata->config.flags & SMSC911X_USE_32BIT) {
281 writesl(pdata->ioaddr + __smsc_shift(pdata,
282 TX_DATA_FIFO), buf, wordcount);
283 goto out;
284 }
285
286 if (pdata->config.flags & SMSC911X_USE_16BIT) {
287 while (wordcount--)
288 __smsc911x_reg_write_shift(pdata,
289 TX_DATA_FIFO, *buf++);
290 goto out;
291 }
292
293 BUG();
294out:
295 spin_unlock_irqrestore(&pdata->dev_lock, flags);
296}
297
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000298/* Reads a packet out of the RX_DATA_FIFO */
299static inline void
300smsc911x_rx_readfifo(struct smsc911x_data *pdata, unsigned int *buf,
301 unsigned int wordcount)
302{
Catalin Marinas492c5d92010-07-19 13:36:21 -0700303 unsigned long flags;
304
305 spin_lock_irqsave(&pdata->dev_lock, flags);
306
Magnus Damm833cc672009-04-27 21:32:16 +0000307 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
308 while (wordcount--)
Catalin Marinas492c5d92010-07-19 13:36:21 -0700309 *buf++ = swab32(__smsc911x_reg_read(pdata,
310 RX_DATA_FIFO));
311 goto out;
Magnus Damm833cc672009-04-27 21:32:16 +0000312 }
313
Steve Glendinning2107fb82008-11-05 00:35:38 +0000314 if (pdata->config.flags & SMSC911X_USE_32BIT) {
315 readsl(pdata->ioaddr + RX_DATA_FIFO, buf, wordcount);
Catalin Marinas492c5d92010-07-19 13:36:21 -0700316 goto out;
Steve Glendinning2107fb82008-11-05 00:35:38 +0000317 }
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000318
Steve Glendinning2107fb82008-11-05 00:35:38 +0000319 if (pdata->config.flags & SMSC911X_USE_16BIT) {
320 while (wordcount--)
Catalin Marinas492c5d92010-07-19 13:36:21 -0700321 *buf++ = __smsc911x_reg_read(pdata, RX_DATA_FIFO);
322 goto out;
Steve Glendinning2107fb82008-11-05 00:35:38 +0000323 }
324
325 BUG();
Catalin Marinas492c5d92010-07-19 13:36:21 -0700326out:
327 spin_unlock_irqrestore(&pdata->dev_lock, flags);
Steve Glendinning2107fb82008-11-05 00:35:38 +0000328}
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000329
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -0700330/* Reads a packet out of the RX_DATA_FIFO - shifted version */
331static inline void
332smsc911x_rx_readfifo_shift(struct smsc911x_data *pdata, unsigned int *buf,
333 unsigned int wordcount)
334{
335 unsigned long flags;
336
337 spin_lock_irqsave(&pdata->dev_lock, flags);
338
339 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
340 while (wordcount--)
341 *buf++ = swab32(__smsc911x_reg_read_shift(pdata,
342 RX_DATA_FIFO));
343 goto out;
344 }
345
346 if (pdata->config.flags & SMSC911X_USE_32BIT) {
347 readsl(pdata->ioaddr + __smsc_shift(pdata,
348 RX_DATA_FIFO), buf, wordcount);
349 goto out;
350 }
351
352 if (pdata->config.flags & SMSC911X_USE_16BIT) {
353 while (wordcount--)
354 *buf++ = __smsc911x_reg_read_shift(pdata,
355 RX_DATA_FIFO);
356 goto out;
357 }
358
359 BUG();
360out:
361 spin_unlock_irqrestore(&pdata->dev_lock, flags);
362}
363
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000364/* waits for MAC not busy, with timeout. Only called by smsc911x_mac_read
365 * and smsc911x_mac_write, so assumes mac_lock is held */
366static int smsc911x_mac_complete(struct smsc911x_data *pdata)
367{
368 int i;
369 u32 val;
370
371 SMSC_ASSERT_MAC_LOCK(pdata);
372
373 for (i = 0; i < 40; i++) {
374 val = smsc911x_reg_read(pdata, MAC_CSR_CMD);
375 if (!(val & MAC_CSR_CMD_CSR_BUSY_))
376 return 0;
377 }
Joe Perchesdffc6b22011-03-25 14:21:22 +0000378 SMSC_WARN(pdata, hw, "Timed out waiting for MAC not BUSY. "
379 "MAC_CSR_CMD: 0x%08X", val);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000380 return -EIO;
381}
382
383/* Fetches a MAC register value. Assumes mac_lock is acquired */
384static u32 smsc911x_mac_read(struct smsc911x_data *pdata, unsigned int offset)
385{
386 unsigned int temp;
387
388 SMSC_ASSERT_MAC_LOCK(pdata);
389
390 temp = smsc911x_reg_read(pdata, MAC_CSR_CMD);
391 if (unlikely(temp & MAC_CSR_CMD_CSR_BUSY_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000392 SMSC_WARN(pdata, hw, "MAC busy at entry");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000393 return 0xFFFFFFFF;
394 }
395
396 /* Send the MAC cmd */
397 smsc911x_reg_write(pdata, MAC_CSR_CMD, ((offset & 0xFF) |
398 MAC_CSR_CMD_CSR_BUSY_ | MAC_CSR_CMD_R_NOT_W_));
399
400 /* Workaround for hardware read-after-write restriction */
401 temp = smsc911x_reg_read(pdata, BYTE_TEST);
402
403 /* Wait for the read to complete */
404 if (likely(smsc911x_mac_complete(pdata) == 0))
405 return smsc911x_reg_read(pdata, MAC_CSR_DATA);
406
Joe Perchesdffc6b22011-03-25 14:21:22 +0000407 SMSC_WARN(pdata, hw, "MAC busy after read");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000408 return 0xFFFFFFFF;
409}
410
411/* Set a mac register, mac_lock must be acquired before calling */
412static void smsc911x_mac_write(struct smsc911x_data *pdata,
413 unsigned int offset, u32 val)
414{
415 unsigned int temp;
416
417 SMSC_ASSERT_MAC_LOCK(pdata);
418
419 temp = smsc911x_reg_read(pdata, MAC_CSR_CMD);
420 if (unlikely(temp & MAC_CSR_CMD_CSR_BUSY_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000421 SMSC_WARN(pdata, hw,
422 "smsc911x_mac_write failed, MAC busy at entry");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000423 return;
424 }
425
426 /* Send data to write */
427 smsc911x_reg_write(pdata, MAC_CSR_DATA, val);
428
429 /* Write the actual data */
430 smsc911x_reg_write(pdata, MAC_CSR_CMD, ((offset & 0xFF) |
431 MAC_CSR_CMD_CSR_BUSY_));
432
433 /* Workaround for hardware read-after-write restriction */
434 temp = smsc911x_reg_read(pdata, BYTE_TEST);
435
436 /* Wait for the write to complete */
437 if (likely(smsc911x_mac_complete(pdata) == 0))
438 return;
439
Joe Perchesdffc6b22011-03-25 14:21:22 +0000440 SMSC_WARN(pdata, hw, "smsc911x_mac_write failed, MAC busy after write");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000441}
442
443/* Get a phy register */
444static int smsc911x_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
445{
446 struct smsc911x_data *pdata = (struct smsc911x_data *)bus->priv;
447 unsigned long flags;
448 unsigned int addr;
449 int i, reg;
450
451 spin_lock_irqsave(&pdata->mac_lock, flags);
452
453 /* Confirm MII not busy */
454 if (unlikely(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000455 SMSC_WARN(pdata, hw, "MII is busy in smsc911x_mii_read???");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000456 reg = -EIO;
457 goto out;
458 }
459
460 /* Set the address, index & direction (read from PHY) */
461 addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6);
462 smsc911x_mac_write(pdata, MII_ACC, addr);
463
464 /* Wait for read to complete w/ timeout */
465 for (i = 0; i < 100; i++)
466 if (!(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
467 reg = smsc911x_mac_read(pdata, MII_DATA);
468 goto out;
469 }
470
Joe Perchesdffc6b22011-03-25 14:21:22 +0000471 SMSC_WARN(pdata, hw, "Timed out waiting for MII read to finish");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000472 reg = -EIO;
473
474out:
475 spin_unlock_irqrestore(&pdata->mac_lock, flags);
476 return reg;
477}
478
479/* Set a phy register */
480static int smsc911x_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
481 u16 val)
482{
483 struct smsc911x_data *pdata = (struct smsc911x_data *)bus->priv;
484 unsigned long flags;
485 unsigned int addr;
486 int i, reg;
487
488 spin_lock_irqsave(&pdata->mac_lock, flags);
489
490 /* Confirm MII not busy */
491 if (unlikely(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000492 SMSC_WARN(pdata, hw, "MII is busy in smsc911x_mii_write???");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000493 reg = -EIO;
494 goto out;
495 }
496
497 /* Put the data to write in the MAC */
498 smsc911x_mac_write(pdata, MII_DATA, val);
499
500 /* Set the address, index & direction (write to PHY) */
501 addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6) |
502 MII_ACC_MII_WRITE_;
503 smsc911x_mac_write(pdata, MII_ACC, addr);
504
505 /* Wait for write to complete w/ timeout */
506 for (i = 0; i < 100; i++)
507 if (!(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
508 reg = 0;
509 goto out;
510 }
511
Joe Perchesdffc6b22011-03-25 14:21:22 +0000512 SMSC_WARN(pdata, hw, "Timed out waiting for MII write to finish");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000513 reg = -EIO;
514
515out:
516 spin_unlock_irqrestore(&pdata->mac_lock, flags);
517 return reg;
518}
519
Steve Glendinningd23f0282009-01-27 06:51:11 +0000520/* Switch to external phy. Assumes tx and rx are stopped. */
521static void smsc911x_phy_enable_external(struct smsc911x_data *pdata)
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000522{
523 unsigned int hwcfg = smsc911x_reg_read(pdata, HW_CFG);
524
Steve Glendinningd23f0282009-01-27 06:51:11 +0000525 /* Disable phy clocks to the MAC */
526 hwcfg &= (~HW_CFG_PHY_CLK_SEL_);
527 hwcfg |= HW_CFG_PHY_CLK_SEL_CLK_DIS_;
528 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
529 udelay(10); /* Enough time for clocks to stop */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000530
Steve Glendinningd23f0282009-01-27 06:51:11 +0000531 /* Switch to external phy */
532 hwcfg |= HW_CFG_EXT_PHY_EN_;
533 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000534
Steve Glendinningd23f0282009-01-27 06:51:11 +0000535 /* Enable phy clocks to the MAC */
536 hwcfg &= (~HW_CFG_PHY_CLK_SEL_);
537 hwcfg |= HW_CFG_PHY_CLK_SEL_EXT_PHY_;
538 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
539 udelay(10); /* Enough time for clocks to restart */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000540
Steve Glendinningd23f0282009-01-27 06:51:11 +0000541 hwcfg |= HW_CFG_SMI_SEL_;
542 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
543}
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000544
Steve Glendinningd23f0282009-01-27 06:51:11 +0000545/* Autodetects and enables external phy if present on supported chips.
546 * autodetection can be overridden by specifying SMSC911X_FORCE_INTERNAL_PHY
547 * or SMSC911X_FORCE_EXTERNAL_PHY in the platform_data flags. */
548static void smsc911x_phy_initialise_external(struct smsc911x_data *pdata)
549{
550 unsigned int hwcfg = smsc911x_reg_read(pdata, HW_CFG);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000551
Steve Glendinningd23f0282009-01-27 06:51:11 +0000552 if (pdata->config.flags & SMSC911X_FORCE_INTERNAL_PHY) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000553 SMSC_TRACE(pdata, hw, "Forcing internal PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +0000554 pdata->using_extphy = 0;
555 } else if (pdata->config.flags & SMSC911X_FORCE_EXTERNAL_PHY) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000556 SMSC_TRACE(pdata, hw, "Forcing external PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +0000557 smsc911x_phy_enable_external(pdata);
558 pdata->using_extphy = 1;
559 } else if (hwcfg & HW_CFG_EXT_PHY_DET_) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000560 SMSC_TRACE(pdata, hw,
561 "HW_CFG EXT_PHY_DET set, using external PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +0000562 smsc911x_phy_enable_external(pdata);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000563 pdata->using_extphy = 1;
564 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000565 SMSC_TRACE(pdata, hw,
566 "HW_CFG EXT_PHY_DET clear, using internal PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +0000567 pdata->using_extphy = 0;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000568 }
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000569}
570
571/* Fetches a tx status out of the status fifo */
572static unsigned int smsc911x_tx_get_txstatus(struct smsc911x_data *pdata)
573{
574 unsigned int result =
575 smsc911x_reg_read(pdata, TX_FIFO_INF) & TX_FIFO_INF_TSUSED_;
576
577 if (result != 0)
578 result = smsc911x_reg_read(pdata, TX_STATUS_FIFO);
579
580 return result;
581}
582
583/* Fetches the next rx status */
584static unsigned int smsc911x_rx_get_rxstatus(struct smsc911x_data *pdata)
585{
586 unsigned int result =
587 smsc911x_reg_read(pdata, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED_;
588
589 if (result != 0)
590 result = smsc911x_reg_read(pdata, RX_STATUS_FIFO);
591
592 return result;
593}
594
595#ifdef USE_PHY_WORK_AROUND
596static int smsc911x_phy_check_loopbackpkt(struct smsc911x_data *pdata)
597{
598 unsigned int tries;
599 u32 wrsz;
600 u32 rdsz;
601 ulong bufp;
602
603 for (tries = 0; tries < 10; tries++) {
604 unsigned int txcmd_a;
605 unsigned int txcmd_b;
606 unsigned int status;
607 unsigned int pktlength;
608 unsigned int i;
609
610 /* Zero-out rx packet memory */
611 memset(pdata->loopback_rx_pkt, 0, MIN_PACKET_SIZE);
612
613 /* Write tx packet to 118 */
614 txcmd_a = (u32)((ulong)pdata->loopback_tx_pkt & 0x03) << 16;
615 txcmd_a |= TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
616 txcmd_a |= MIN_PACKET_SIZE;
617
618 txcmd_b = MIN_PACKET_SIZE << 16 | MIN_PACKET_SIZE;
619
620 smsc911x_reg_write(pdata, TX_DATA_FIFO, txcmd_a);
621 smsc911x_reg_write(pdata, TX_DATA_FIFO, txcmd_b);
622
623 bufp = (ulong)pdata->loopback_tx_pkt & (~0x3);
624 wrsz = MIN_PACKET_SIZE + 3;
625 wrsz += (u32)((ulong)pdata->loopback_tx_pkt & 0x3);
626 wrsz >>= 2;
627
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -0700628 pdata->ops->tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000629
630 /* Wait till transmit is done */
631 i = 60;
632 do {
633 udelay(5);
634 status = smsc911x_tx_get_txstatus(pdata);
635 } while ((i--) && (!status));
636
637 if (!status) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000638 SMSC_WARN(pdata, hw,
639 "Failed to transmit during loopback test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000640 continue;
641 }
642 if (status & TX_STS_ES_) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000643 SMSC_WARN(pdata, hw,
644 "Transmit encountered errors during loopback test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000645 continue;
646 }
647
648 /* Wait till receive is done */
649 i = 60;
650 do {
651 udelay(5);
652 status = smsc911x_rx_get_rxstatus(pdata);
653 } while ((i--) && (!status));
654
655 if (!status) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000656 SMSC_WARN(pdata, hw,
657 "Failed to receive during loopback test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000658 continue;
659 }
660 if (status & RX_STS_ES_) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000661 SMSC_WARN(pdata, hw,
662 "Receive encountered errors during loopback test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000663 continue;
664 }
665
666 pktlength = ((status & 0x3FFF0000UL) >> 16);
667 bufp = (ulong)pdata->loopback_rx_pkt;
668 rdsz = pktlength + 3;
669 rdsz += (u32)((ulong)pdata->loopback_rx_pkt & 0x3);
670 rdsz >>= 2;
671
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -0700672 pdata->ops->rx_readfifo(pdata, (unsigned int *)bufp, rdsz);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000673
674 if (pktlength != (MIN_PACKET_SIZE + 4)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000675 SMSC_WARN(pdata, hw, "Unexpected packet size "
676 "during loop back test, size=%d, will retry",
677 pktlength);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000678 } else {
679 unsigned int j;
680 int mismatch = 0;
681 for (j = 0; j < MIN_PACKET_SIZE; j++) {
682 if (pdata->loopback_tx_pkt[j]
683 != pdata->loopback_rx_pkt[j]) {
684 mismatch = 1;
685 break;
686 }
687 }
688 if (!mismatch) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000689 SMSC_TRACE(pdata, hw, "Successfully verified "
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000690 "loopback packet");
691 return 0;
692 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000693 SMSC_WARN(pdata, hw, "Data mismatch "
694 "during loop back test, will retry");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000695 }
696 }
697 }
698
699 return -EIO;
700}
701
702static int smsc911x_phy_reset(struct smsc911x_data *pdata)
703{
704 struct phy_device *phy_dev = pdata->phy_dev;
705 unsigned int temp;
706 unsigned int i = 100000;
707
708 BUG_ON(!phy_dev);
709 BUG_ON(!phy_dev->bus);
710
Joe Perchesdffc6b22011-03-25 14:21:22 +0000711 SMSC_TRACE(pdata, hw, "Performing PHY BCR Reset");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000712 smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR, BMCR_RESET);
713 do {
714 msleep(1);
715 temp = smsc911x_mii_read(phy_dev->bus, phy_dev->addr,
716 MII_BMCR);
717 } while ((i--) && (temp & BMCR_RESET));
718
719 if (temp & BMCR_RESET) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000720 SMSC_WARN(pdata, hw, "PHY reset failed to complete");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000721 return -EIO;
722 }
723 /* Extra delay required because the phy may not be completed with
724 * its reset when BMCR_RESET is cleared. Specs say 256 uS is
725 * enough delay but using 1ms here to be safe */
726 msleep(1);
727
728 return 0;
729}
730
731static int smsc911x_phy_loopbacktest(struct net_device *dev)
732{
733 struct smsc911x_data *pdata = netdev_priv(dev);
734 struct phy_device *phy_dev = pdata->phy_dev;
735 int result = -EIO;
736 unsigned int i, val;
737 unsigned long flags;
738
739 /* Initialise tx packet using broadcast destination address */
740 memset(pdata->loopback_tx_pkt, 0xff, ETH_ALEN);
741
742 /* Use incrementing source address */
743 for (i = 6; i < 12; i++)
744 pdata->loopback_tx_pkt[i] = (char)i;
745
746 /* Set length type field */
747 pdata->loopback_tx_pkt[12] = 0x00;
748 pdata->loopback_tx_pkt[13] = 0x00;
749
750 for (i = 14; i < MIN_PACKET_SIZE; i++)
751 pdata->loopback_tx_pkt[i] = (char)i;
752
753 val = smsc911x_reg_read(pdata, HW_CFG);
754 val &= HW_CFG_TX_FIF_SZ_;
755 val |= HW_CFG_SF_;
756 smsc911x_reg_write(pdata, HW_CFG, val);
757
758 smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
759 smsc911x_reg_write(pdata, RX_CFG,
760 (u32)((ulong)pdata->loopback_rx_pkt & 0x03) << 8);
761
762 for (i = 0; i < 10; i++) {
763 /* Set PHY to 10/FD, no ANEG, and loopback mode */
764 smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR,
765 BMCR_LOOPBACK | BMCR_FULLDPLX);
766
767 /* Enable MAC tx/rx, FD */
768 spin_lock_irqsave(&pdata->mac_lock, flags);
769 smsc911x_mac_write(pdata, MAC_CR, MAC_CR_FDPX_
770 | MAC_CR_TXEN_ | MAC_CR_RXEN_);
771 spin_unlock_irqrestore(&pdata->mac_lock, flags);
772
773 if (smsc911x_phy_check_loopbackpkt(pdata) == 0) {
774 result = 0;
775 break;
776 }
777 pdata->resetcount++;
778
779 /* Disable MAC rx */
780 spin_lock_irqsave(&pdata->mac_lock, flags);
781 smsc911x_mac_write(pdata, MAC_CR, 0);
782 spin_unlock_irqrestore(&pdata->mac_lock, flags);
783
784 smsc911x_phy_reset(pdata);
785 }
786
787 /* Disable MAC */
788 spin_lock_irqsave(&pdata->mac_lock, flags);
789 smsc911x_mac_write(pdata, MAC_CR, 0);
790 spin_unlock_irqrestore(&pdata->mac_lock, flags);
791
792 /* Cancel PHY loopback mode */
793 smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR, 0);
794
795 smsc911x_reg_write(pdata, TX_CFG, 0);
796 smsc911x_reg_write(pdata, RX_CFG, 0);
797
798 return result;
799}
800#endif /* USE_PHY_WORK_AROUND */
801
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000802static void smsc911x_phy_update_flowcontrol(struct smsc911x_data *pdata)
803{
804 struct phy_device *phy_dev = pdata->phy_dev;
805 u32 afc = smsc911x_reg_read(pdata, AFC_CFG);
806 u32 flow;
807 unsigned long flags;
808
809 if (phy_dev->duplex == DUPLEX_FULL) {
810 u16 lcladv = phy_read(phy_dev, MII_ADVERTISE);
811 u16 rmtadv = phy_read(phy_dev, MII_LPA);
Steve Glendinningbc02ff92008-12-16 02:00:48 -0800812 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000813
814 if (cap & FLOW_CTRL_RX)
815 flow = 0xFFFF0002;
816 else
817 flow = 0;
818
819 if (cap & FLOW_CTRL_TX)
820 afc |= 0xF;
821 else
822 afc &= ~0xF;
823
Joe Perchesdffc6b22011-03-25 14:21:22 +0000824 SMSC_TRACE(pdata, hw, "rx pause %s, tx pause %s",
825 (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
826 (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000827 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000828 SMSC_TRACE(pdata, hw, "half duplex");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000829 flow = 0;
830 afc |= 0xF;
831 }
832
833 spin_lock_irqsave(&pdata->mac_lock, flags);
834 smsc911x_mac_write(pdata, FLOW, flow);
835 spin_unlock_irqrestore(&pdata->mac_lock, flags);
836
837 smsc911x_reg_write(pdata, AFC_CFG, afc);
838}
839
840/* Update link mode if anything has changed. Called periodically when the
841 * PHY is in polling mode, even if nothing has changed. */
842static void smsc911x_phy_adjust_link(struct net_device *dev)
843{
844 struct smsc911x_data *pdata = netdev_priv(dev);
845 struct phy_device *phy_dev = pdata->phy_dev;
846 unsigned long flags;
847 int carrier;
848
849 if (phy_dev->duplex != pdata->last_duplex) {
850 unsigned int mac_cr;
Joe Perchesdffc6b22011-03-25 14:21:22 +0000851 SMSC_TRACE(pdata, hw, "duplex state has changed");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000852
853 spin_lock_irqsave(&pdata->mac_lock, flags);
854 mac_cr = smsc911x_mac_read(pdata, MAC_CR);
855 if (phy_dev->duplex) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000856 SMSC_TRACE(pdata, hw,
857 "configuring for full duplex mode");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000858 mac_cr |= MAC_CR_FDPX_;
859 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000860 SMSC_TRACE(pdata, hw,
861 "configuring for half duplex mode");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000862 mac_cr &= ~MAC_CR_FDPX_;
863 }
864 smsc911x_mac_write(pdata, MAC_CR, mac_cr);
865 spin_unlock_irqrestore(&pdata->mac_lock, flags);
866
867 smsc911x_phy_update_flowcontrol(pdata);
868 pdata->last_duplex = phy_dev->duplex;
869 }
870
871 carrier = netif_carrier_ok(dev);
872 if (carrier != pdata->last_carrier) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000873 SMSC_TRACE(pdata, hw, "carrier state has changed");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000874 if (carrier) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000875 SMSC_TRACE(pdata, hw, "configuring for carrier OK");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000876 if ((pdata->gpio_orig_setting & GPIO_CFG_LED1_EN_) &&
877 (!pdata->using_extphy)) {
Thomas Weber88393162010-03-16 11:47:56 +0100878 /* Restore original GPIO configuration */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000879 pdata->gpio_setting = pdata->gpio_orig_setting;
880 smsc911x_reg_write(pdata, GPIO_CFG,
881 pdata->gpio_setting);
882 }
883 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000884 SMSC_TRACE(pdata, hw, "configuring for no carrier");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000885 /* Check global setting that LED1
886 * usage is 10/100 indicator */
887 pdata->gpio_setting = smsc911x_reg_read(pdata,
888 GPIO_CFG);
Joe Perches8e95a202009-12-03 07:58:21 +0000889 if ((pdata->gpio_setting & GPIO_CFG_LED1_EN_) &&
890 (!pdata->using_extphy)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000891 /* Force 10/100 LED off, after saving
Thomas Weber88393162010-03-16 11:47:56 +0100892 * original GPIO configuration */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000893 pdata->gpio_orig_setting = pdata->gpio_setting;
894
895 pdata->gpio_setting &= ~GPIO_CFG_LED1_EN_;
896 pdata->gpio_setting |= (GPIO_CFG_GPIOBUF0_
897 | GPIO_CFG_GPIODIR0_
898 | GPIO_CFG_GPIOD0_);
899 smsc911x_reg_write(pdata, GPIO_CFG,
900 pdata->gpio_setting);
901 }
902 }
903 pdata->last_carrier = carrier;
904 }
905}
906
907static int smsc911x_mii_probe(struct net_device *dev)
908{
909 struct smsc911x_data *pdata = netdev_priv(dev);
910 struct phy_device *phydev = NULL;
kirjanov@gmail.come4a474f2010-02-16 21:54:58 +0000911 int ret;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000912
913 /* find the first phy */
kirjanov@gmail.come4a474f2010-02-16 21:54:58 +0000914 phydev = phy_find_first(pdata->mii_bus);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000915 if (!phydev) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000916 netdev_err(dev, "no PHY found\n");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000917 return -ENODEV;
918 }
919
Joe Perchesdffc6b22011-03-25 14:21:22 +0000920 SMSC_TRACE(pdata, probe, "PHY: addr %d, phy_id 0x%08X",
921 phydev->addr, phydev->phy_id);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000922
kirjanov@gmail.come4a474f2010-02-16 21:54:58 +0000923 ret = phy_connect_direct(dev, phydev,
924 &smsc911x_phy_adjust_link, 0,
925 pdata->config.phy_interface);
926
927 if (ret) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000928 netdev_err(dev, "Could not attach to PHY\n");
kirjanov@gmail.come4a474f2010-02-16 21:54:58 +0000929 return ret;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000930 }
931
Joe Perchesdffc6b22011-03-25 14:21:22 +0000932 netdev_info(dev,
933 "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
934 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000935
936 /* mask with MAC supported features */
937 phydev->supported &= (PHY_BASIC_FEATURES | SUPPORTED_Pause |
938 SUPPORTED_Asym_Pause);
939 phydev->advertising = phydev->supported;
940
941 pdata->phy_dev = phydev;
942 pdata->last_duplex = -1;
943 pdata->last_carrier = -1;
944
945#ifdef USE_PHY_WORK_AROUND
946 if (smsc911x_phy_loopbacktest(dev) < 0) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000947 SMSC_WARN(pdata, hw, "Failed Loop Back Test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000948 return -ENODEV;
949 }
Joe Perchesdffc6b22011-03-25 14:21:22 +0000950 SMSC_TRACE(pdata, hw, "Passed Loop Back Test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000951#endif /* USE_PHY_WORK_AROUND */
952
Joe Perchesdffc6b22011-03-25 14:21:22 +0000953 SMSC_TRACE(pdata, hw, "phy initialised successfully");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000954 return 0;
955}
956
957static int __devinit smsc911x_mii_init(struct platform_device *pdev,
958 struct net_device *dev)
959{
960 struct smsc911x_data *pdata = netdev_priv(dev);
961 int err = -ENXIO, i;
962
963 pdata->mii_bus = mdiobus_alloc();
964 if (!pdata->mii_bus) {
965 err = -ENOMEM;
966 goto err_out_1;
967 }
968
969 pdata->mii_bus->name = SMSC_MDIONAME;
970 snprintf(pdata->mii_bus->id, MII_BUS_ID_SIZE, "%x", pdev->id);
971 pdata->mii_bus->priv = pdata;
972 pdata->mii_bus->read = smsc911x_mii_read;
973 pdata->mii_bus->write = smsc911x_mii_write;
974 pdata->mii_bus->irq = pdata->phy_irq;
975 for (i = 0; i < PHY_MAX_ADDR; ++i)
976 pdata->mii_bus->irq[i] = PHY_POLL;
977
978 pdata->mii_bus->parent = &pdev->dev;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000979
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000980 switch (pdata->idrev & 0xFFFF0000) {
981 case 0x01170000:
982 case 0x01150000:
983 case 0x117A0000:
984 case 0x115A0000:
985 /* External PHY supported, try to autodetect */
Steve Glendinningd23f0282009-01-27 06:51:11 +0000986 smsc911x_phy_initialise_external(pdata);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000987 break;
988 default:
Joe Perchesdffc6b22011-03-25 14:21:22 +0000989 SMSC_TRACE(pdata, hw, "External PHY is not supported, "
990 "using internal PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +0000991 pdata->using_extphy = 0;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000992 break;
993 }
994
995 if (!pdata->using_extphy) {
996 /* Mask all PHYs except ID 1 (internal) */
997 pdata->mii_bus->phy_mask = ~(1 << 1);
998 }
999
1000 if (mdiobus_register(pdata->mii_bus)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001001 SMSC_WARN(pdata, probe, "Error registering mii bus");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001002 goto err_out_free_bus_2;
1003 }
1004
1005 if (smsc911x_mii_probe(dev) < 0) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001006 SMSC_WARN(pdata, probe, "Error registering mii bus");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001007 goto err_out_unregister_bus_3;
1008 }
1009
1010 return 0;
1011
1012err_out_unregister_bus_3:
1013 mdiobus_unregister(pdata->mii_bus);
1014err_out_free_bus_2:
1015 mdiobus_free(pdata->mii_bus);
1016err_out_1:
1017 return err;
1018}
1019
1020/* Gets the number of tx statuses in the fifo */
1021static unsigned int smsc911x_tx_get_txstatcount(struct smsc911x_data *pdata)
1022{
1023 return (smsc911x_reg_read(pdata, TX_FIFO_INF)
1024 & TX_FIFO_INF_TSUSED_) >> 16;
1025}
1026
1027/* Reads tx statuses and increments counters where necessary */
1028static void smsc911x_tx_update_txcounters(struct net_device *dev)
1029{
1030 struct smsc911x_data *pdata = netdev_priv(dev);
1031 unsigned int tx_stat;
1032
1033 while ((tx_stat = smsc911x_tx_get_txstatus(pdata)) != 0) {
1034 if (unlikely(tx_stat & 0x80000000)) {
1035 /* In this driver the packet tag is used as the packet
1036 * length. Since a packet length can never reach the
1037 * size of 0x8000, this bit is reserved. It is worth
1038 * noting that the "reserved bit" in the warning above
1039 * does not reference a hardware defined reserved bit
1040 * but rather a driver defined one.
1041 */
Joe Perchesdffc6b22011-03-25 14:21:22 +00001042 SMSC_WARN(pdata, hw, "Packet tag reserved bit is high");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001043 } else {
Steve Glendinning785b6f92009-03-19 00:24:44 +00001044 if (unlikely(tx_stat & TX_STS_ES_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001045 dev->stats.tx_errors++;
1046 } else {
1047 dev->stats.tx_packets++;
1048 dev->stats.tx_bytes += (tx_stat >> 16);
1049 }
Steve Glendinning785b6f92009-03-19 00:24:44 +00001050 if (unlikely(tx_stat & TX_STS_EXCESS_COL_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001051 dev->stats.collisions += 16;
1052 dev->stats.tx_aborted_errors += 1;
1053 } else {
1054 dev->stats.collisions +=
1055 ((tx_stat >> 3) & 0xF);
1056 }
Steve Glendinning785b6f92009-03-19 00:24:44 +00001057 if (unlikely(tx_stat & TX_STS_LOST_CARRIER_))
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001058 dev->stats.tx_carrier_errors += 1;
Steve Glendinning785b6f92009-03-19 00:24:44 +00001059 if (unlikely(tx_stat & TX_STS_LATE_COL_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001060 dev->stats.collisions++;
1061 dev->stats.tx_aborted_errors++;
1062 }
1063 }
1064 }
1065}
1066
1067/* Increments the Rx error counters */
1068static void
1069smsc911x_rx_counterrors(struct net_device *dev, unsigned int rxstat)
1070{
1071 int crc_err = 0;
1072
Steve Glendinning785b6f92009-03-19 00:24:44 +00001073 if (unlikely(rxstat & RX_STS_ES_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001074 dev->stats.rx_errors++;
Steve Glendinning785b6f92009-03-19 00:24:44 +00001075 if (unlikely(rxstat & RX_STS_CRC_ERR_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001076 dev->stats.rx_crc_errors++;
1077 crc_err = 1;
1078 }
1079 }
1080 if (likely(!crc_err)) {
Steve Glendinning785b6f92009-03-19 00:24:44 +00001081 if (unlikely((rxstat & RX_STS_FRAME_TYPE_) &&
1082 (rxstat & RX_STS_LENGTH_ERR_)))
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001083 dev->stats.rx_length_errors++;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001084 if (rxstat & RX_STS_MCAST_)
1085 dev->stats.multicast++;
1086 }
1087}
1088
1089/* Quickly dumps bad packets */
1090static void
1091smsc911x_rx_fastforward(struct smsc911x_data *pdata, unsigned int pktbytes)
1092{
1093 unsigned int pktwords = (pktbytes + NET_IP_ALIGN + 3) >> 2;
1094
1095 if (likely(pktwords >= 4)) {
1096 unsigned int timeout = 500;
1097 unsigned int val;
1098 smsc911x_reg_write(pdata, RX_DP_CTRL, RX_DP_CTRL_RX_FFWD_);
1099 do {
1100 udelay(1);
1101 val = smsc911x_reg_read(pdata, RX_DP_CTRL);
Steve Glendinning8dacd542009-03-04 07:33:24 +00001102 } while ((val & RX_DP_CTRL_RX_FFWD_) && --timeout);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001103
1104 if (unlikely(timeout == 0))
Joe Perchesdffc6b22011-03-25 14:21:22 +00001105 SMSC_WARN(pdata, hw, "Timed out waiting for "
1106 "RX FFWD to finish, RX_DP_CTRL: 0x%08X", val);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001107 } else {
1108 unsigned int temp;
1109 while (pktwords--)
1110 temp = smsc911x_reg_read(pdata, RX_DATA_FIFO);
1111 }
1112}
1113
1114/* NAPI poll function */
1115static int smsc911x_poll(struct napi_struct *napi, int budget)
1116{
1117 struct smsc911x_data *pdata =
1118 container_of(napi, struct smsc911x_data, napi);
1119 struct net_device *dev = pdata->dev;
1120 int npackets = 0;
1121
Sriramf88c5b92009-11-12 02:14:38 +00001122 while (npackets < budget) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001123 unsigned int pktlength;
1124 unsigned int pktwords;
1125 struct sk_buff *skb;
1126 unsigned int rxstat = smsc911x_rx_get_rxstatus(pdata);
1127
1128 if (!rxstat) {
1129 unsigned int temp;
1130 /* We processed all packets available. Tell NAPI it can
1131 * stop polling then re-enable rx interrupts */
1132 smsc911x_reg_write(pdata, INT_STS, INT_STS_RSFL_);
Ben Hutchings288379f2009-01-19 16:43:59 -08001133 napi_complete(napi);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001134 temp = smsc911x_reg_read(pdata, INT_EN);
1135 temp |= INT_EN_RSFL_EN_;
1136 smsc911x_reg_write(pdata, INT_EN, temp);
1137 break;
1138 }
1139
1140 /* Count packet for NAPI scheduling, even if it has an error.
1141 * Error packets still require cycles to discard */
1142 npackets++;
1143
1144 pktlength = ((rxstat & 0x3FFF0000) >> 16);
1145 pktwords = (pktlength + NET_IP_ALIGN + 3) >> 2;
1146 smsc911x_rx_counterrors(dev, rxstat);
1147
1148 if (unlikely(rxstat & RX_STS_ES_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001149 SMSC_WARN(pdata, rx_err,
1150 "Discarding packet with error bit set");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001151 /* Packet has an error, discard it and continue with
1152 * the next */
1153 smsc911x_rx_fastforward(pdata, pktwords);
1154 dev->stats.rx_dropped++;
1155 continue;
1156 }
1157
1158 skb = netdev_alloc_skb(dev, pktlength + NET_IP_ALIGN);
1159 if (unlikely(!skb)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001160 SMSC_WARN(pdata, rx_err,
1161 "Unable to allocate skb for rx packet");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001162 /* Drop the packet and stop this polling iteration */
1163 smsc911x_rx_fastforward(pdata, pktwords);
1164 dev->stats.rx_dropped++;
1165 break;
1166 }
1167
1168 skb->data = skb->head;
1169 skb_reset_tail_pointer(skb);
1170
1171 /* Align IP on 16B boundary */
1172 skb_reserve(skb, NET_IP_ALIGN);
1173 skb_put(skb, pktlength - 4);
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -07001174 pdata->ops->rx_readfifo(pdata,
1175 (unsigned int *)skb->head, pktwords);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001176 skb->protocol = eth_type_trans(skb, dev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001177 skb_checksum_none_assert(skb);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001178 netif_receive_skb(skb);
1179
1180 /* Update counters */
1181 dev->stats.rx_packets++;
1182 dev->stats.rx_bytes += (pktlength - 4);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001183 }
1184
1185 /* Return total received packets */
1186 return npackets;
1187}
1188
1189/* Returns hash bit number for given MAC address
1190 * Example:
1191 * 01 00 5E 00 00 01 -> returns bit number 31 */
1192static unsigned int smsc911x_hash(char addr[ETH_ALEN])
1193{
1194 return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
1195}
1196
1197static void smsc911x_rx_multicast_update(struct smsc911x_data *pdata)
1198{
1199 /* Performs the multicast & mac_cr update. This is called when
1200 * safe on the current hardware, and with the mac_lock held */
1201 unsigned int mac_cr;
1202
1203 SMSC_ASSERT_MAC_LOCK(pdata);
1204
1205 mac_cr = smsc911x_mac_read(pdata, MAC_CR);
1206 mac_cr |= pdata->set_bits_mask;
1207 mac_cr &= ~(pdata->clear_bits_mask);
1208 smsc911x_mac_write(pdata, MAC_CR, mac_cr);
1209 smsc911x_mac_write(pdata, HASHH, pdata->hashhi);
1210 smsc911x_mac_write(pdata, HASHL, pdata->hashlo);
Joe Perchesdffc6b22011-03-25 14:21:22 +00001211 SMSC_TRACE(pdata, hw, "maccr 0x%08X, HASHH 0x%08X, HASHL 0x%08X",
1212 mac_cr, pdata->hashhi, pdata->hashlo);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001213}
1214
1215static void smsc911x_rx_multicast_update_workaround(struct smsc911x_data *pdata)
1216{
1217 unsigned int mac_cr;
1218
1219 /* This function is only called for older LAN911x devices
1220 * (revA or revB), where MAC_CR, HASHH and HASHL should not
1221 * be modified during Rx - newer devices immediately update the
1222 * registers.
1223 *
1224 * This is called from interrupt context */
1225
1226 spin_lock(&pdata->mac_lock);
1227
1228 /* Check Rx has stopped */
1229 if (smsc911x_mac_read(pdata, MAC_CR) & MAC_CR_RXEN_)
Joe Perchesdffc6b22011-03-25 14:21:22 +00001230 SMSC_WARN(pdata, drv, "Rx not stopped");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001231
1232 /* Perform the update - safe to do now Rx has stopped */
1233 smsc911x_rx_multicast_update(pdata);
1234
1235 /* Re-enable Rx */
1236 mac_cr = smsc911x_mac_read(pdata, MAC_CR);
1237 mac_cr |= MAC_CR_RXEN_;
1238 smsc911x_mac_write(pdata, MAC_CR, mac_cr);
1239
1240 pdata->multicast_update_pending = 0;
1241
1242 spin_unlock(&pdata->mac_lock);
1243}
1244
1245static int smsc911x_soft_reset(struct smsc911x_data *pdata)
1246{
1247 unsigned int timeout;
1248 unsigned int temp;
1249
1250 /* Reset the LAN911x */
1251 smsc911x_reg_write(pdata, HW_CFG, HW_CFG_SRST_);
1252 timeout = 10;
1253 do {
1254 udelay(10);
1255 temp = smsc911x_reg_read(pdata, HW_CFG);
1256 } while ((--timeout) && (temp & HW_CFG_SRST_));
1257
1258 if (unlikely(temp & HW_CFG_SRST_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001259 SMSC_WARN(pdata, drv, "Failed to complete reset");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001260 return -EIO;
1261 }
1262 return 0;
1263}
1264
1265/* Sets the device MAC address to dev_addr, called with mac_lock held */
1266static void
Steve Glendinning225ddf42009-03-19 00:24:46 +00001267smsc911x_set_hw_mac_address(struct smsc911x_data *pdata, u8 dev_addr[6])
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001268{
1269 u32 mac_high16 = (dev_addr[5] << 8) | dev_addr[4];
1270 u32 mac_low32 = (dev_addr[3] << 24) | (dev_addr[2] << 16) |
1271 (dev_addr[1] << 8) | dev_addr[0];
1272
1273 SMSC_ASSERT_MAC_LOCK(pdata);
1274
1275 smsc911x_mac_write(pdata, ADDRH, mac_high16);
1276 smsc911x_mac_write(pdata, ADDRL, mac_low32);
1277}
1278
1279static int smsc911x_open(struct net_device *dev)
1280{
1281 struct smsc911x_data *pdata = netdev_priv(dev);
1282 unsigned int timeout;
1283 unsigned int temp;
1284 unsigned int intcfg;
1285
1286 /* if the phy is not yet registered, retry later*/
1287 if (!pdata->phy_dev) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001288 SMSC_WARN(pdata, hw, "phy_dev is NULL");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001289 return -EAGAIN;
1290 }
1291
1292 if (!is_valid_ether_addr(dev->dev_addr)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001293 SMSC_WARN(pdata, hw, "dev_addr is not a valid MAC address");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001294 return -EADDRNOTAVAIL;
1295 }
1296
1297 /* Reset the LAN911x */
1298 if (smsc911x_soft_reset(pdata)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001299 SMSC_WARN(pdata, hw, "soft reset failed");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001300 return -EIO;
1301 }
1302
1303 smsc911x_reg_write(pdata, HW_CFG, 0x00050000);
1304 smsc911x_reg_write(pdata, AFC_CFG, 0x006E3740);
1305
Göran Weinholtf277e652011-03-02 04:07:21 +00001306 /* Increase the legal frame size of VLAN tagged frames to 1522 bytes */
1307 spin_lock_irq(&pdata->mac_lock);
1308 smsc911x_mac_write(pdata, VLAN1, ETH_P_8021Q);
1309 spin_unlock_irq(&pdata->mac_lock);
1310
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001311 /* Make sure EEPROM has finished loading before setting GPIO_CFG */
1312 timeout = 50;
Steve Glendinningf7efb6c2009-03-04 07:33:25 +00001313 while ((smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) &&
1314 --timeout) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001315 udelay(10);
1316 }
1317
1318 if (unlikely(timeout == 0))
Joe Perchesdffc6b22011-03-25 14:21:22 +00001319 SMSC_WARN(pdata, ifup,
1320 "Timed out waiting for EEPROM busy bit to clear");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001321
1322 smsc911x_reg_write(pdata, GPIO_CFG, 0x70070000);
1323
1324 /* The soft reset above cleared the device's MAC address,
1325 * restore it from local copy (set in probe) */
1326 spin_lock_irq(&pdata->mac_lock);
Steve Glendinning225ddf42009-03-19 00:24:46 +00001327 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001328 spin_unlock_irq(&pdata->mac_lock);
1329
1330 /* Initialise irqs, but leave all sources disabled */
1331 smsc911x_reg_write(pdata, INT_EN, 0);
1332 smsc911x_reg_write(pdata, INT_STS, 0xFFFFFFFF);
1333
1334 /* Set interrupt deassertion to 100uS */
1335 intcfg = ((10 << 24) | INT_CFG_IRQ_EN_);
1336
Steve Glendinning2107fb82008-11-05 00:35:38 +00001337 if (pdata->config.irq_polarity) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001338 SMSC_TRACE(pdata, ifup, "irq polarity: active high");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001339 intcfg |= INT_CFG_IRQ_POL_;
1340 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001341 SMSC_TRACE(pdata, ifup, "irq polarity: active low");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001342 }
1343
Steve Glendinning2107fb82008-11-05 00:35:38 +00001344 if (pdata->config.irq_type) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001345 SMSC_TRACE(pdata, ifup, "irq type: push-pull");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001346 intcfg |= INT_CFG_IRQ_TYPE_;
1347 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001348 SMSC_TRACE(pdata, ifup, "irq type: open drain");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001349 }
1350
1351 smsc911x_reg_write(pdata, INT_CFG, intcfg);
1352
Joe Perchesdffc6b22011-03-25 14:21:22 +00001353 SMSC_TRACE(pdata, ifup, "Testing irq handler using IRQ %d", dev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001354 pdata->software_irq_signal = 0;
1355 smp_wmb();
1356
1357 temp = smsc911x_reg_read(pdata, INT_EN);
1358 temp |= INT_EN_SW_INT_EN_;
1359 smsc911x_reg_write(pdata, INT_EN, temp);
1360
1361 timeout = 1000;
1362 while (timeout--) {
1363 if (pdata->software_irq_signal)
1364 break;
1365 msleep(1);
1366 }
1367
1368 if (!pdata->software_irq_signal) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001369 netdev_warn(dev, "ISR failed signaling test (IRQ %d)\n",
1370 dev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001371 return -ENODEV;
1372 }
Joe Perchesdffc6b22011-03-25 14:21:22 +00001373 SMSC_TRACE(pdata, ifup, "IRQ handler passed test using IRQ %d",
1374 dev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001375
Joe Perchesdffc6b22011-03-25 14:21:22 +00001376 netdev_info(dev, "SMSC911x/921x identified at %#08lx, IRQ: %d\n",
1377 (unsigned long)pdata->ioaddr, dev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001378
Steve Glendinning44c1d6f2009-03-18 23:37:18 -07001379 /* Reset the last known duplex and carrier */
1380 pdata->last_duplex = -1;
1381 pdata->last_carrier = -1;
1382
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001383 /* Bring the PHY up */
1384 phy_start(pdata->phy_dev);
1385
1386 temp = smsc911x_reg_read(pdata, HW_CFG);
1387 /* Preserve TX FIFO size and external PHY configuration */
1388 temp &= (HW_CFG_TX_FIF_SZ_|0x00000FFF);
1389 temp |= HW_CFG_SF_;
1390 smsc911x_reg_write(pdata, HW_CFG, temp);
1391
1392 temp = smsc911x_reg_read(pdata, FIFO_INT);
1393 temp |= FIFO_INT_TX_AVAIL_LEVEL_;
1394 temp &= ~(FIFO_INT_RX_STS_LEVEL_);
1395 smsc911x_reg_write(pdata, FIFO_INT, temp);
1396
1397 /* set RX Data offset to 2 bytes for alignment */
1398 smsc911x_reg_write(pdata, RX_CFG, (2 << 8));
1399
1400 /* enable NAPI polling before enabling RX interrupts */
1401 napi_enable(&pdata->napi);
1402
1403 temp = smsc911x_reg_read(pdata, INT_EN);
Steve Glendinning1373c0f2009-01-26 21:33:16 -08001404 temp |= (INT_EN_TDFA_EN_ | INT_EN_RSFL_EN_ | INT_EN_RXSTOP_INT_EN_);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001405 smsc911x_reg_write(pdata, INT_EN, temp);
1406
1407 spin_lock_irq(&pdata->mac_lock);
1408 temp = smsc911x_mac_read(pdata, MAC_CR);
1409 temp |= (MAC_CR_TXEN_ | MAC_CR_RXEN_ | MAC_CR_HBDIS_);
1410 smsc911x_mac_write(pdata, MAC_CR, temp);
1411 spin_unlock_irq(&pdata->mac_lock);
1412
1413 smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
1414
1415 netif_start_queue(dev);
1416 return 0;
1417}
1418
1419/* Entry point for stopping the interface */
1420static int smsc911x_stop(struct net_device *dev)
1421{
1422 struct smsc911x_data *pdata = netdev_priv(dev);
1423 unsigned int temp;
1424
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001425 /* Disable all device interrupts */
1426 temp = smsc911x_reg_read(pdata, INT_CFG);
1427 temp &= ~INT_CFG_IRQ_EN_;
1428 smsc911x_reg_write(pdata, INT_CFG, temp);
1429
1430 /* Stop Tx and Rx polling */
1431 netif_stop_queue(dev);
1432 napi_disable(&pdata->napi);
1433
1434 /* At this point all Rx and Tx activity is stopped */
1435 dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
1436 smsc911x_tx_update_txcounters(dev);
1437
1438 /* Bring the PHY down */
Steve Glendinningdd045192008-12-25 16:40:19 -08001439 if (pdata->phy_dev)
1440 phy_stop(pdata->phy_dev);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001441
Joe Perchesdffc6b22011-03-25 14:21:22 +00001442 SMSC_TRACE(pdata, ifdown, "Interface stopped");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001443 return 0;
1444}
1445
1446/* Entry point for transmitting a packet */
1447static int smsc911x_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1448{
1449 struct smsc911x_data *pdata = netdev_priv(dev);
1450 unsigned int freespace;
1451 unsigned int tx_cmd_a;
1452 unsigned int tx_cmd_b;
1453 unsigned int temp;
1454 u32 wrsz;
1455 ulong bufp;
1456
1457 freespace = smsc911x_reg_read(pdata, TX_FIFO_INF) & TX_FIFO_INF_TDFREE_;
1458
1459 if (unlikely(freespace < TX_FIFO_LOW_THRESHOLD))
Joe Perchesdffc6b22011-03-25 14:21:22 +00001460 SMSC_WARN(pdata, tx_err,
1461 "Tx data fifo low, space available: %d", freespace);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001462
1463 /* Word alignment adjustment */
1464 tx_cmd_a = (u32)((ulong)skb->data & 0x03) << 16;
1465 tx_cmd_a |= TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
1466 tx_cmd_a |= (unsigned int)skb->len;
1467
1468 tx_cmd_b = ((unsigned int)skb->len) << 16;
1469 tx_cmd_b |= (unsigned int)skb->len;
1470
1471 smsc911x_reg_write(pdata, TX_DATA_FIFO, tx_cmd_a);
1472 smsc911x_reg_write(pdata, TX_DATA_FIFO, tx_cmd_b);
1473
1474 bufp = (ulong)skb->data & (~0x3);
1475 wrsz = (u32)skb->len + 3;
1476 wrsz += (u32)((ulong)skb->data & 0x3);
1477 wrsz >>= 2;
1478
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -07001479 pdata->ops->tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001480 freespace -= (skb->len + 32);
Richard Cochran8c0069a2011-06-19 21:51:30 +00001481 skb_tx_timestamp(skb);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001482 dev_kfree_skb(skb);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001483
1484 if (unlikely(smsc911x_tx_get_txstatcount(pdata) >= 30))
1485 smsc911x_tx_update_txcounters(dev);
1486
1487 if (freespace < TX_FIFO_LOW_THRESHOLD) {
1488 netif_stop_queue(dev);
1489 temp = smsc911x_reg_read(pdata, FIFO_INT);
1490 temp &= 0x00FFFFFF;
1491 temp |= 0x32000000;
1492 smsc911x_reg_write(pdata, FIFO_INT, temp);
1493 }
1494
1495 return NETDEV_TX_OK;
1496}
1497
1498/* Entry point for getting status counters */
1499static struct net_device_stats *smsc911x_get_stats(struct net_device *dev)
1500{
1501 struct smsc911x_data *pdata = netdev_priv(dev);
1502 smsc911x_tx_update_txcounters(dev);
1503 dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
1504 return &dev->stats;
1505}
1506
1507/* Entry point for setting addressing modes */
1508static void smsc911x_set_multicast_list(struct net_device *dev)
1509{
1510 struct smsc911x_data *pdata = netdev_priv(dev);
1511 unsigned long flags;
1512
1513 if (dev->flags & IFF_PROMISC) {
1514 /* Enabling promiscuous mode */
1515 pdata->set_bits_mask = MAC_CR_PRMS_;
1516 pdata->clear_bits_mask = (MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
1517 pdata->hashhi = 0;
1518 pdata->hashlo = 0;
1519 } else if (dev->flags & IFF_ALLMULTI) {
1520 /* Enabling all multicast mode */
1521 pdata->set_bits_mask = MAC_CR_MCPAS_;
1522 pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_HPFILT_);
1523 pdata->hashhi = 0;
1524 pdata->hashlo = 0;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001525 } else if (!netdev_mc_empty(dev)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001526 /* Enabling specific multicast addresses */
1527 unsigned int hash_high = 0;
1528 unsigned int hash_low = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001529 struct netdev_hw_addr *ha;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001530
1531 pdata->set_bits_mask = MAC_CR_HPFILT_;
1532 pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_MCPAS_);
1533
Jiri Pirko22bedad32010-04-01 21:22:57 +00001534 netdev_for_each_mc_addr(ha, dev) {
1535 unsigned int bitnum = smsc911x_hash(ha->addr);
Jiri Pirko2a0d18f2010-02-17 23:22:38 +00001536 unsigned int mask = 0x01 << (bitnum & 0x1F);
1537
1538 if (bitnum & 0x20)
1539 hash_high |= mask;
1540 else
1541 hash_low |= mask;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001542 }
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001543
1544 pdata->hashhi = hash_high;
1545 pdata->hashlo = hash_low;
1546 } else {
1547 /* Enabling local MAC address only */
1548 pdata->set_bits_mask = 0;
1549 pdata->clear_bits_mask =
1550 (MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
1551 pdata->hashhi = 0;
1552 pdata->hashlo = 0;
1553 }
1554
1555 spin_lock_irqsave(&pdata->mac_lock, flags);
1556
1557 if (pdata->generation <= 1) {
1558 /* Older hardware revision - cannot change these flags while
1559 * receiving data */
1560 if (!pdata->multicast_update_pending) {
1561 unsigned int temp;
Joe Perchesdffc6b22011-03-25 14:21:22 +00001562 SMSC_TRACE(pdata, hw, "scheduling mcast update");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001563 pdata->multicast_update_pending = 1;
1564
1565 /* Request the hardware to stop, then perform the
1566 * update when we get an RX_STOP interrupt */
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001567 temp = smsc911x_mac_read(pdata, MAC_CR);
1568 temp &= ~(MAC_CR_RXEN_);
1569 smsc911x_mac_write(pdata, MAC_CR, temp);
1570 } else {
1571 /* There is another update pending, this should now
1572 * use the newer values */
1573 }
1574 } else {
1575 /* Newer hardware revision - can write immediately */
1576 smsc911x_rx_multicast_update(pdata);
1577 }
1578
1579 spin_unlock_irqrestore(&pdata->mac_lock, flags);
1580}
1581
1582static irqreturn_t smsc911x_irqhandler(int irq, void *dev_id)
1583{
1584 struct net_device *dev = dev_id;
1585 struct smsc911x_data *pdata = netdev_priv(dev);
1586 u32 intsts = smsc911x_reg_read(pdata, INT_STS);
1587 u32 inten = smsc911x_reg_read(pdata, INT_EN);
1588 int serviced = IRQ_NONE;
1589 u32 temp;
1590
1591 if (unlikely(intsts & inten & INT_STS_SW_INT_)) {
1592 temp = smsc911x_reg_read(pdata, INT_EN);
1593 temp &= (~INT_EN_SW_INT_EN_);
1594 smsc911x_reg_write(pdata, INT_EN, temp);
1595 smsc911x_reg_write(pdata, INT_STS, INT_STS_SW_INT_);
1596 pdata->software_irq_signal = 1;
1597 smp_wmb();
1598 serviced = IRQ_HANDLED;
1599 }
1600
1601 if (unlikely(intsts & inten & INT_STS_RXSTOP_INT_)) {
1602 /* Called when there is a multicast update scheduled and
1603 * it is now safe to complete the update */
Joe Perchesdffc6b22011-03-25 14:21:22 +00001604 SMSC_TRACE(pdata, intr, "RX Stop interrupt");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001605 smsc911x_reg_write(pdata, INT_STS, INT_STS_RXSTOP_INT_);
Steve Glendinning1373c0f2009-01-26 21:33:16 -08001606 if (pdata->multicast_update_pending)
1607 smsc911x_rx_multicast_update_workaround(pdata);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001608 serviced = IRQ_HANDLED;
1609 }
1610
1611 if (intsts & inten & INT_STS_TDFA_) {
1612 temp = smsc911x_reg_read(pdata, FIFO_INT);
1613 temp |= FIFO_INT_TX_AVAIL_LEVEL_;
1614 smsc911x_reg_write(pdata, FIFO_INT, temp);
1615 smsc911x_reg_write(pdata, INT_STS, INT_STS_TDFA_);
1616 netif_wake_queue(dev);
1617 serviced = IRQ_HANDLED;
1618 }
1619
1620 if (unlikely(intsts & inten & INT_STS_RXE_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001621 SMSC_TRACE(pdata, intr, "RX Error interrupt");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001622 smsc911x_reg_write(pdata, INT_STS, INT_STS_RXE_);
1623 serviced = IRQ_HANDLED;
1624 }
1625
1626 if (likely(intsts & inten & INT_STS_RSFL_)) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001627 if (likely(napi_schedule_prep(&pdata->napi))) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001628 /* Disable Rx interrupts */
1629 temp = smsc911x_reg_read(pdata, INT_EN);
1630 temp &= (~INT_EN_RSFL_EN_);
1631 smsc911x_reg_write(pdata, INT_EN, temp);
1632 /* Schedule a NAPI poll */
Ben Hutchings288379f2009-01-19 16:43:59 -08001633 __napi_schedule(&pdata->napi);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001634 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001635 SMSC_WARN(pdata, rx_err, "napi_schedule_prep failed");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001636 }
1637 serviced = IRQ_HANDLED;
1638 }
1639
1640 return serviced;
1641}
1642
1643#ifdef CONFIG_NET_POLL_CONTROLLER
Steve Glendinning1757ab22008-12-12 22:31:16 -08001644static void smsc911x_poll_controller(struct net_device *dev)
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001645{
1646 disable_irq(dev->irq);
1647 smsc911x_irqhandler(0, dev);
1648 enable_irq(dev->irq);
1649}
1650#endif /* CONFIG_NET_POLL_CONTROLLER */
1651
Steve Glendinning225ddf42009-03-19 00:24:46 +00001652static int smsc911x_set_mac_address(struct net_device *dev, void *p)
1653{
1654 struct smsc911x_data *pdata = netdev_priv(dev);
1655 struct sockaddr *addr = p;
1656
1657 /* On older hardware revisions we cannot change the mac address
1658 * registers while receiving data. Newer devices can safely change
1659 * this at any time. */
1660 if (pdata->generation <= 1 && netif_running(dev))
1661 return -EBUSY;
1662
1663 if (!is_valid_ether_addr(addr->sa_data))
1664 return -EADDRNOTAVAIL;
1665
1666 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
1667
1668 spin_lock_irq(&pdata->mac_lock);
1669 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
1670 spin_unlock_irq(&pdata->mac_lock);
1671
Joe Perchesdffc6b22011-03-25 14:21:22 +00001672 netdev_info(dev, "MAC Address: %pM\n", dev->dev_addr);
Steve Glendinning225ddf42009-03-19 00:24:46 +00001673
1674 return 0;
1675}
1676
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001677/* Standard ioctls for mii-tool */
1678static int smsc911x_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1679{
1680 struct smsc911x_data *pdata = netdev_priv(dev);
1681
1682 if (!netif_running(dev) || !pdata->phy_dev)
1683 return -EINVAL;
1684
Richard Cochran28b04112010-07-17 08:48:55 +00001685 return phy_mii_ioctl(pdata->phy_dev, ifr, cmd);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001686}
1687
1688static int
1689smsc911x_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1690{
1691 struct smsc911x_data *pdata = netdev_priv(dev);
1692
1693 cmd->maxtxpkt = 1;
1694 cmd->maxrxpkt = 1;
1695 return phy_ethtool_gset(pdata->phy_dev, cmd);
1696}
1697
1698static int
1699smsc911x_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1700{
1701 struct smsc911x_data *pdata = netdev_priv(dev);
1702
1703 return phy_ethtool_sset(pdata->phy_dev, cmd);
1704}
1705
1706static void smsc911x_ethtool_getdrvinfo(struct net_device *dev,
1707 struct ethtool_drvinfo *info)
1708{
1709 strlcpy(info->driver, SMSC_CHIPNAME, sizeof(info->driver));
1710 strlcpy(info->version, SMSC_DRV_VERSION, sizeof(info->version));
Kay Sieversdb1d7bf2009-01-26 21:12:58 -08001711 strlcpy(info->bus_info, dev_name(dev->dev.parent),
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001712 sizeof(info->bus_info));
1713}
1714
1715static int smsc911x_ethtool_nwayreset(struct net_device *dev)
1716{
1717 struct smsc911x_data *pdata = netdev_priv(dev);
1718
1719 return phy_start_aneg(pdata->phy_dev);
1720}
1721
1722static u32 smsc911x_ethtool_getmsglevel(struct net_device *dev)
1723{
1724 struct smsc911x_data *pdata = netdev_priv(dev);
1725 return pdata->msg_enable;
1726}
1727
1728static void smsc911x_ethtool_setmsglevel(struct net_device *dev, u32 level)
1729{
1730 struct smsc911x_data *pdata = netdev_priv(dev);
1731 pdata->msg_enable = level;
1732}
1733
1734static int smsc911x_ethtool_getregslen(struct net_device *dev)
1735{
1736 return (((E2P_DATA - ID_REV) / 4 + 1) + (WUCSR - MAC_CR) + 1 + 32) *
1737 sizeof(u32);
1738}
1739
1740static void
1741smsc911x_ethtool_getregs(struct net_device *dev, struct ethtool_regs *regs,
1742 void *buf)
1743{
1744 struct smsc911x_data *pdata = netdev_priv(dev);
1745 struct phy_device *phy_dev = pdata->phy_dev;
1746 unsigned long flags;
1747 unsigned int i;
1748 unsigned int j = 0;
1749 u32 *data = buf;
1750
1751 regs->version = pdata->idrev;
1752 for (i = ID_REV; i <= E2P_DATA; i += (sizeof(u32)))
1753 data[j++] = smsc911x_reg_read(pdata, i);
1754
1755 for (i = MAC_CR; i <= WUCSR; i++) {
1756 spin_lock_irqsave(&pdata->mac_lock, flags);
1757 data[j++] = smsc911x_mac_read(pdata, i);
1758 spin_unlock_irqrestore(&pdata->mac_lock, flags);
1759 }
1760
1761 for (i = 0; i <= 31; i++)
1762 data[j++] = smsc911x_mii_read(phy_dev->bus, phy_dev->addr, i);
1763}
1764
1765static void smsc911x_eeprom_enable_access(struct smsc911x_data *pdata)
1766{
1767 unsigned int temp = smsc911x_reg_read(pdata, GPIO_CFG);
1768 temp &= ~GPIO_CFG_EEPR_EN_;
1769 smsc911x_reg_write(pdata, GPIO_CFG, temp);
1770 msleep(1);
1771}
1772
1773static int smsc911x_eeprom_send_cmd(struct smsc911x_data *pdata, u32 op)
1774{
1775 int timeout = 100;
1776 u32 e2cmd;
1777
Joe Perchesdffc6b22011-03-25 14:21:22 +00001778 SMSC_TRACE(pdata, drv, "op 0x%08x", op);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001779 if (smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001780 SMSC_WARN(pdata, drv, "Busy at start");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001781 return -EBUSY;
1782 }
1783
1784 e2cmd = op | E2P_CMD_EPC_BUSY_;
1785 smsc911x_reg_write(pdata, E2P_CMD, e2cmd);
1786
1787 do {
1788 msleep(1);
1789 e2cmd = smsc911x_reg_read(pdata, E2P_CMD);
Roel Kluin2cf0dbe2009-02-20 00:52:19 -08001790 } while ((e2cmd & E2P_CMD_EPC_BUSY_) && (--timeout));
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001791
1792 if (!timeout) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001793 SMSC_TRACE(pdata, drv, "TIMED OUT");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001794 return -EAGAIN;
1795 }
1796
1797 if (e2cmd & E2P_CMD_EPC_TIMEOUT_) {
David S. Miller1c01a802011-04-11 13:44:25 -07001798 SMSC_TRACE(pdata, drv, "Error occurred during eeprom operation");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001799 return -EINVAL;
1800 }
1801
1802 return 0;
1803}
1804
1805static int smsc911x_eeprom_read_location(struct smsc911x_data *pdata,
1806 u8 address, u8 *data)
1807{
1808 u32 op = E2P_CMD_EPC_CMD_READ_ | address;
1809 int ret;
1810
Joe Perchesdffc6b22011-03-25 14:21:22 +00001811 SMSC_TRACE(pdata, drv, "address 0x%x", address);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001812 ret = smsc911x_eeprom_send_cmd(pdata, op);
1813
1814 if (!ret)
1815 data[address] = smsc911x_reg_read(pdata, E2P_DATA);
1816
1817 return ret;
1818}
1819
1820static int smsc911x_eeprom_write_location(struct smsc911x_data *pdata,
1821 u8 address, u8 data)
1822{
1823 u32 op = E2P_CMD_EPC_CMD_ERASE_ | address;
Steve Glendinning58add9f2009-03-26 07:14:36 +00001824 u32 temp;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001825 int ret;
1826
Joe Perchesdffc6b22011-03-25 14:21:22 +00001827 SMSC_TRACE(pdata, drv, "address 0x%x, data 0x%x", address, data);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001828 ret = smsc911x_eeprom_send_cmd(pdata, op);
1829
1830 if (!ret) {
1831 op = E2P_CMD_EPC_CMD_WRITE_ | address;
1832 smsc911x_reg_write(pdata, E2P_DATA, (u32)data);
Steve Glendinning58add9f2009-03-26 07:14:36 +00001833
1834 /* Workaround for hardware read-after-write restriction */
1835 temp = smsc911x_reg_read(pdata, BYTE_TEST);
1836
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001837 ret = smsc911x_eeprom_send_cmd(pdata, op);
1838 }
1839
1840 return ret;
1841}
1842
1843static int smsc911x_ethtool_get_eeprom_len(struct net_device *dev)
1844{
1845 return SMSC911X_EEPROM_SIZE;
1846}
1847
1848static int smsc911x_ethtool_get_eeprom(struct net_device *dev,
1849 struct ethtool_eeprom *eeprom, u8 *data)
1850{
1851 struct smsc911x_data *pdata = netdev_priv(dev);
1852 u8 eeprom_data[SMSC911X_EEPROM_SIZE];
1853 int len;
1854 int i;
1855
1856 smsc911x_eeprom_enable_access(pdata);
1857
1858 len = min(eeprom->len, SMSC911X_EEPROM_SIZE);
1859 for (i = 0; i < len; i++) {
1860 int ret = smsc911x_eeprom_read_location(pdata, i, eeprom_data);
1861 if (ret < 0) {
1862 eeprom->len = 0;
1863 return ret;
1864 }
1865 }
1866
1867 memcpy(data, &eeprom_data[eeprom->offset], len);
1868 eeprom->len = len;
1869 return 0;
1870}
1871
1872static int smsc911x_ethtool_set_eeprom(struct net_device *dev,
1873 struct ethtool_eeprom *eeprom, u8 *data)
1874{
1875 int ret;
1876 struct smsc911x_data *pdata = netdev_priv(dev);
1877
1878 smsc911x_eeprom_enable_access(pdata);
1879 smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWEN_);
1880 ret = smsc911x_eeprom_write_location(pdata, eeprom->offset, *data);
1881 smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWDS_);
1882
1883 /* Single byte write, according to man page */
1884 eeprom->len = 1;
1885
1886 return ret;
1887}
1888
Steve Glendinningcb5b04f2008-12-25 16:41:09 -08001889static const struct ethtool_ops smsc911x_ethtool_ops = {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001890 .get_settings = smsc911x_ethtool_getsettings,
1891 .set_settings = smsc911x_ethtool_setsettings,
1892 .get_link = ethtool_op_get_link,
1893 .get_drvinfo = smsc911x_ethtool_getdrvinfo,
1894 .nway_reset = smsc911x_ethtool_nwayreset,
1895 .get_msglevel = smsc911x_ethtool_getmsglevel,
1896 .set_msglevel = smsc911x_ethtool_setmsglevel,
1897 .get_regs_len = smsc911x_ethtool_getregslen,
1898 .get_regs = smsc911x_ethtool_getregs,
1899 .get_eeprom_len = smsc911x_ethtool_get_eeprom_len,
1900 .get_eeprom = smsc911x_ethtool_get_eeprom,
1901 .set_eeprom = smsc911x_ethtool_set_eeprom,
1902};
1903
Steve Glendinning631b7562008-12-25 16:40:47 -08001904static const struct net_device_ops smsc911x_netdev_ops = {
1905 .ndo_open = smsc911x_open,
1906 .ndo_stop = smsc911x_stop,
1907 .ndo_start_xmit = smsc911x_hard_start_xmit,
1908 .ndo_get_stats = smsc911x_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001909 .ndo_set_rx_mode = smsc911x_set_multicast_list,
Steve Glendinning631b7562008-12-25 16:40:47 -08001910 .ndo_do_ioctl = smsc911x_do_ioctl,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001911 .ndo_change_mtu = eth_change_mtu,
Steve Glendinning631b7562008-12-25 16:40:47 -08001912 .ndo_validate_addr = eth_validate_addr,
Steve Glendinning225ddf42009-03-19 00:24:46 +00001913 .ndo_set_mac_address = smsc911x_set_mac_address,
Steve Glendinning631b7562008-12-25 16:40:47 -08001914#ifdef CONFIG_NET_POLL_CONTROLLER
1915 .ndo_poll_controller = smsc911x_poll_controller,
1916#endif
1917};
1918
Steve Glendinning31f45742009-01-27 06:51:12 +00001919/* copies the current mac address from hardware to dev->dev_addr */
1920static void __devinit smsc911x_read_mac_address(struct net_device *dev)
1921{
1922 struct smsc911x_data *pdata = netdev_priv(dev);
1923 u32 mac_high16 = smsc911x_mac_read(pdata, ADDRH);
1924 u32 mac_low32 = smsc911x_mac_read(pdata, ADDRL);
1925
1926 dev->dev_addr[0] = (u8)(mac_low32);
1927 dev->dev_addr[1] = (u8)(mac_low32 >> 8);
1928 dev->dev_addr[2] = (u8)(mac_low32 >> 16);
1929 dev->dev_addr[3] = (u8)(mac_low32 >> 24);
1930 dev->dev_addr[4] = (u8)(mac_high16);
1931 dev->dev_addr[5] = (u8)(mac_high16 >> 8);
1932}
1933
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001934/* Initializing private device structures, only called from probe */
1935static int __devinit smsc911x_init(struct net_device *dev)
1936{
1937 struct smsc911x_data *pdata = netdev_priv(dev);
1938 unsigned int byte_test;
1939
Joe Perchesdffc6b22011-03-25 14:21:22 +00001940 SMSC_TRACE(pdata, probe, "Driver Parameters:");
1941 SMSC_TRACE(pdata, probe, "LAN base: 0x%08lX",
1942 (unsigned long)pdata->ioaddr);
1943 SMSC_TRACE(pdata, probe, "IRQ: %d", dev->irq);
1944 SMSC_TRACE(pdata, probe, "PHY will be autodetected.");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001945
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001946 spin_lock_init(&pdata->dev_lock);
Enric Balletbo i Serra35a67ed2011-04-05 06:52:49 +00001947 spin_lock_init(&pdata->mac_lock);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001948
1949 if (pdata->ioaddr == 0) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001950 SMSC_WARN(pdata, probe, "pdata->ioaddr: 0x00000000");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001951 return -ENODEV;
1952 }
1953
1954 /* Check byte ordering */
1955 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
Joe Perchesdffc6b22011-03-25 14:21:22 +00001956 SMSC_TRACE(pdata, probe, "BYTE_TEST: 0x%08X", byte_test);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001957 if (byte_test == 0x43218765) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001958 SMSC_TRACE(pdata, probe, "BYTE_TEST looks swapped, "
1959 "applying WORD_SWAP");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001960 smsc911x_reg_write(pdata, WORD_SWAP, 0xffffffff);
1961
1962 /* 1 dummy read of BYTE_TEST is needed after a write to
1963 * WORD_SWAP before its contents are valid */
1964 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
1965
1966 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
1967 }
1968
1969 if (byte_test != 0x87654321) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001970 SMSC_WARN(pdata, drv, "BYTE_TEST: 0x%08X", byte_test);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001971 if (((byte_test >> 16) & 0xFFFF) == (byte_test & 0xFFFF)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001972 SMSC_WARN(pdata, probe,
1973 "top 16 bits equal to bottom 16 bits");
1974 SMSC_TRACE(pdata, probe,
1975 "This may mean the chip is set "
1976 "for 32 bit while the bus is reading 16 bit");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001977 }
1978 return -ENODEV;
1979 }
1980
1981 /* Default generation to zero (all workarounds apply) */
1982 pdata->generation = 0;
1983
1984 pdata->idrev = smsc911x_reg_read(pdata, ID_REV);
1985 switch (pdata->idrev & 0xFFFF0000) {
1986 case 0x01180000:
1987 case 0x01170000:
1988 case 0x01160000:
1989 case 0x01150000:
1990 /* LAN911[5678] family */
1991 pdata->generation = pdata->idrev & 0x0000FFFF;
1992 break;
1993
1994 case 0x118A0000:
1995 case 0x117A0000:
1996 case 0x116A0000:
1997 case 0x115A0000:
1998 /* LAN921[5678] family */
1999 pdata->generation = 3;
2000 break;
2001
2002 case 0x92100000:
2003 case 0x92110000:
2004 case 0x92200000:
2005 case 0x92210000:
2006 /* LAN9210/LAN9211/LAN9220/LAN9221 */
2007 pdata->generation = 4;
2008 break;
2009
2010 default:
Joe Perchesdffc6b22011-03-25 14:21:22 +00002011 SMSC_WARN(pdata, probe, "LAN911x not identified, idrev: 0x%08X",
2012 pdata->idrev);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002013 return -ENODEV;
2014 }
2015
Joe Perchesdffc6b22011-03-25 14:21:22 +00002016 SMSC_TRACE(pdata, probe,
2017 "LAN911x identified, idrev: 0x%08X, generation: %d",
2018 pdata->idrev, pdata->generation);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002019
2020 if (pdata->generation == 0)
Joe Perchesdffc6b22011-03-25 14:21:22 +00002021 SMSC_WARN(pdata, probe,
2022 "This driver is not intended for this chip revision");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002023
Steve Glendinning31f45742009-01-27 06:51:12 +00002024 /* workaround for platforms without an eeprom, where the mac address
2025 * is stored elsewhere and set by the bootloader. This saves the
2026 * mac address before resetting the device */
Enric Balletbo i Serra35a67ed2011-04-05 06:52:49 +00002027 if (pdata->config.flags & SMSC911X_SAVE_MAC_ADDRESS) {
2028 spin_lock_irq(&pdata->mac_lock);
Steve Glendinning31f45742009-01-27 06:51:12 +00002029 smsc911x_read_mac_address(dev);
Enric Balletbo i Serra35a67ed2011-04-05 06:52:49 +00002030 spin_unlock_irq(&pdata->mac_lock);
2031 }
Steve Glendinning31f45742009-01-27 06:51:12 +00002032
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002033 /* Reset the LAN911x */
2034 if (smsc911x_soft_reset(pdata))
2035 return -ENODEV;
2036
2037 /* Disable all interrupt sources until we bring the device up */
2038 smsc911x_reg_write(pdata, INT_EN, 0);
2039
2040 ether_setup(dev);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002041 dev->flags |= IFF_MULTICAST;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002042 netif_napi_add(dev, &pdata->napi, smsc911x_poll, SMSC_NAPI_WEIGHT);
Steve Glendinning631b7562008-12-25 16:40:47 -08002043 dev->netdev_ops = &smsc911x_netdev_ops;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002044 dev->ethtool_ops = &smsc911x_ethtool_ops;
2045
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002046 return 0;
2047}
2048
2049static int __devexit smsc911x_drv_remove(struct platform_device *pdev)
2050{
2051 struct net_device *dev;
2052 struct smsc911x_data *pdata;
2053 struct resource *res;
2054
2055 dev = platform_get_drvdata(pdev);
2056 BUG_ON(!dev);
2057 pdata = netdev_priv(dev);
2058 BUG_ON(!pdata);
2059 BUG_ON(!pdata->ioaddr);
2060 BUG_ON(!pdata->phy_dev);
2061
Joe Perchesdffc6b22011-03-25 14:21:22 +00002062 SMSC_TRACE(pdata, ifdown, "Stopping driver");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002063
2064 phy_disconnect(pdata->phy_dev);
2065 pdata->phy_dev = NULL;
2066 mdiobus_unregister(pdata->mii_bus);
2067 mdiobus_free(pdata->mii_bus);
2068
2069 platform_set_drvdata(pdev, NULL);
2070 unregister_netdev(dev);
2071 free_irq(dev->irq, dev);
2072 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2073 "smsc911x-memory");
2074 if (!res)
Steve Glendinningd4522732008-12-25 16:44:01 -08002075 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002076
Julia Lawall39424532009-07-04 11:31:47 +00002077 release_mem_region(res->start, resource_size(res));
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002078
2079 iounmap(pdata->ioaddr);
2080
2081 free_netdev(dev);
2082
2083 return 0;
2084}
2085
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -07002086/* standard register acces */
2087static const struct smsc911x_ops standard_smsc911x_ops = {
2088 .reg_read = __smsc911x_reg_read,
2089 .reg_write = __smsc911x_reg_write,
2090 .rx_readfifo = smsc911x_rx_readfifo,
2091 .tx_writefifo = smsc911x_tx_writefifo,
2092};
2093
2094/* shifted register access */
2095static const struct smsc911x_ops shifted_smsc911x_ops = {
2096 .reg_read = __smsc911x_reg_read_shift,
2097 .reg_write = __smsc911x_reg_write_shift,
2098 .rx_readfifo = smsc911x_rx_readfifo_shift,
2099 .tx_writefifo = smsc911x_tx_writefifo_shift,
2100};
2101
Shawn Guo79f88ee2011-07-30 08:26:00 +00002102#ifdef CONFIG_OF
2103static int __devinit smsc911x_probe_config_dt(
2104 struct smsc911x_platform_config *config,
2105 struct device_node *np)
2106{
2107 const char *mac;
2108 u32 width = 0;
2109
2110 if (!np)
2111 return -ENODEV;
2112
2113 config->phy_interface = of_get_phy_mode(np);
2114
2115 mac = of_get_mac_address(np);
2116 if (mac)
2117 memcpy(config->mac, mac, ETH_ALEN);
2118
2119 of_property_read_u32(np, "reg-shift", &config->shift);
2120
2121 of_property_read_u32(np, "reg-io-width", &width);
2122 if (width == 4)
2123 config->flags |= SMSC911X_USE_32BIT;
2124
2125 if (of_get_property(np, "smsc,irq-active-high", NULL))
2126 config->irq_polarity = SMSC911X_IRQ_POLARITY_ACTIVE_HIGH;
2127
2128 if (of_get_property(np, "smsc,irq-push-pull", NULL))
2129 config->irq_type = SMSC911X_IRQ_TYPE_PUSH_PULL;
2130
2131 if (of_get_property(np, "smsc,force-internal-phy", NULL))
2132 config->flags |= SMSC911X_FORCE_INTERNAL_PHY;
2133
2134 if (of_get_property(np, "smsc,force-external-phy", NULL))
2135 config->flags |= SMSC911X_FORCE_EXTERNAL_PHY;
2136
2137 if (of_get_property(np, "smsc,save-mac-address", NULL))
2138 config->flags |= SMSC911X_SAVE_MAC_ADDRESS;
2139
2140 return 0;
2141}
2142#else
2143static inline int smsc911x_probe_config_dt(
2144 struct smsc911x_platform_config *config,
2145 struct device_node *np)
2146{
2147 return -ENODEV;
2148}
2149#endif /* CONFIG_OF */
2150
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002151static int __devinit smsc911x_drv_probe(struct platform_device *pdev)
2152{
Shawn Guo79f88ee2011-07-30 08:26:00 +00002153 struct device_node *np = pdev->dev.of_node;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002154 struct net_device *dev;
2155 struct smsc911x_data *pdata;
Steve Glendinning2107fb82008-11-05 00:35:38 +00002156 struct smsc911x_platform_config *config = pdev->dev.platform_data;
Steve Glendinning61307ed2009-01-27 06:51:09 +00002157 struct resource *res, *irq_res;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002158 unsigned int intcfg = 0;
Steve Glendinning61307ed2009-01-27 06:51:09 +00002159 int res_size, irq_flags;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002160 int retval;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002161
Joe Perchesdffc6b22011-03-25 14:21:22 +00002162 pr_info("Driver version %s\n", SMSC_DRV_VERSION);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002163
2164 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2165 "smsc911x-memory");
2166 if (!res)
2167 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2168 if (!res) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002169 pr_warn("Could not allocate resource\n");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002170 retval = -ENODEV;
2171 goto out_0;
2172 }
Julia Lawall39424532009-07-04 11:31:47 +00002173 res_size = resource_size(res);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002174
Steve Glendinning61307ed2009-01-27 06:51:09 +00002175 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2176 if (!irq_res) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002177 pr_warn("Could not allocate irq resource\n");
Steve Glendinning61307ed2009-01-27 06:51:09 +00002178 retval = -ENODEV;
2179 goto out_0;
2180 }
2181
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002182 if (!request_mem_region(res->start, res_size, SMSC_CHIPNAME)) {
2183 retval = -EBUSY;
2184 goto out_0;
2185 }
2186
2187 dev = alloc_etherdev(sizeof(struct smsc911x_data));
2188 if (!dev) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002189 pr_warn("Could not allocate device\n");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002190 retval = -ENOMEM;
2191 goto out_release_io_1;
2192 }
2193
2194 SET_NETDEV_DEV(dev, &pdev->dev);
2195
2196 pdata = netdev_priv(dev);
2197
Steve Glendinning61307ed2009-01-27 06:51:09 +00002198 dev->irq = irq_res->start;
2199 irq_flags = irq_res->flags & IRQF_TRIGGER_MASK;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002200 pdata->ioaddr = ioremap_nocache(res->start, res_size);
2201
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002202 pdata->dev = dev;
2203 pdata->msg_enable = ((1 << debug) - 1);
2204
2205 if (pdata->ioaddr == NULL) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002206 SMSC_WARN(pdata, probe, "Error smsc911x base address invalid");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002207 retval = -ENOMEM;
2208 goto out_free_netdev_2;
2209 }
2210
Shawn Guo79f88ee2011-07-30 08:26:00 +00002211 retval = smsc911x_probe_config_dt(&pdata->config, np);
2212 if (retval && config) {
2213 /* copy config parameters across to pdata */
2214 memcpy(&pdata->config, config, sizeof(pdata->config));
2215 retval = 0;
2216 }
2217
2218 if (retval) {
2219 SMSC_WARN(pdata, probe, "Error smsc911x config not found");
2220 goto out_unmap_io_3;
2221 }
2222
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -07002223 /* assume standard, non-shifted, access to HW registers */
2224 pdata->ops = &standard_smsc911x_ops;
2225 /* apply the right access if shifting is needed */
Shawn Guo79f88ee2011-07-30 08:26:00 +00002226 if (pdata->config.shift)
Mathieu J. Poirierc326de88b2011-04-13 17:13:00 -07002227 pdata->ops = &shifted_smsc911x_ops;
2228
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002229 retval = smsc911x_init(dev);
2230 if (retval < 0)
2231 goto out_unmap_io_3;
2232
2233 /* configure irq polarity and type before connecting isr */
Steve Glendinning2107fb82008-11-05 00:35:38 +00002234 if (pdata->config.irq_polarity == SMSC911X_IRQ_POLARITY_ACTIVE_HIGH)
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002235 intcfg |= INT_CFG_IRQ_POL_;
2236
Steve Glendinning2107fb82008-11-05 00:35:38 +00002237 if (pdata->config.irq_type == SMSC911X_IRQ_TYPE_PUSH_PULL)
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002238 intcfg |= INT_CFG_IRQ_TYPE_;
2239
2240 smsc911x_reg_write(pdata, INT_CFG, intcfg);
2241
2242 /* Ensure interrupts are globally disabled before connecting ISR */
2243 smsc911x_reg_write(pdata, INT_EN, 0);
2244 smsc911x_reg_write(pdata, INT_STS, 0xFFFFFFFF);
2245
Steve Glendinning61307ed2009-01-27 06:51:09 +00002246 retval = request_irq(dev->irq, smsc911x_irqhandler,
Steve Glendinninge81259b2009-01-27 06:51:10 +00002247 irq_flags | IRQF_SHARED, dev->name, dev);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002248 if (retval) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002249 SMSC_WARN(pdata, probe,
2250 "Unable to claim requested irq: %d", dev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002251 goto out_unmap_io_3;
2252 }
2253
2254 platform_set_drvdata(pdev, dev);
2255
2256 retval = register_netdev(dev);
2257 if (retval) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002258 SMSC_WARN(pdata, probe, "Error %i registering device", retval);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002259 goto out_unset_drvdata_4;
2260 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002261 SMSC_TRACE(pdata, probe,
2262 "Network interface: \"%s\"", dev->name);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002263 }
2264
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002265 retval = smsc911x_mii_init(pdev, dev);
2266 if (retval) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002267 SMSC_WARN(pdata, probe, "Error %i initialising mii", retval);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002268 goto out_unregister_netdev_5;
2269 }
2270
2271 spin_lock_irq(&pdata->mac_lock);
2272
2273 /* Check if mac address has been specified when bringing interface up */
2274 if (is_valid_ether_addr(dev->dev_addr)) {
Steve Glendinning225ddf42009-03-19 00:24:46 +00002275 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
Joe Perchesdffc6b22011-03-25 14:21:22 +00002276 SMSC_TRACE(pdata, probe,
2277 "MAC Address is specified by configuration");
Manuel Laussaace4952009-10-13 07:25:49 +00002278 } else if (is_valid_ether_addr(pdata->config.mac)) {
2279 memcpy(dev->dev_addr, pdata->config.mac, 6);
Joe Perchesdffc6b22011-03-25 14:21:22 +00002280 SMSC_TRACE(pdata, probe,
2281 "MAC Address specified by platform data");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002282 } else {
2283 /* Try reading mac address from device. if EEPROM is present
2284 * it will already have been set */
Akira Takeuchi62747cd2010-10-27 17:28:58 +01002285 smsc_get_mac(dev);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002286
2287 if (is_valid_ether_addr(dev->dev_addr)) {
2288 /* eeprom values are valid so use them */
Joe Perchesdffc6b22011-03-25 14:21:22 +00002289 SMSC_TRACE(pdata, probe,
2290 "Mac Address is read from LAN911x EEPROM");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002291 } else {
2292 /* eeprom values are invalid, generate random MAC */
2293 random_ether_addr(dev->dev_addr);
Steve Glendinning225ddf42009-03-19 00:24:46 +00002294 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
Joe Perchesdffc6b22011-03-25 14:21:22 +00002295 SMSC_TRACE(pdata, probe,
2296 "MAC Address is set to random_ether_addr");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002297 }
2298 }
2299
2300 spin_unlock_irq(&pdata->mac_lock);
2301
Joe Perchesdffc6b22011-03-25 14:21:22 +00002302 netdev_info(dev, "MAC Address: %pM\n", dev->dev_addr);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002303
2304 return 0;
2305
2306out_unregister_netdev_5:
2307 unregister_netdev(dev);
2308out_unset_drvdata_4:
2309 platform_set_drvdata(pdev, NULL);
2310 free_irq(dev->irq, dev);
2311out_unmap_io_3:
2312 iounmap(pdata->ioaddr);
2313out_free_netdev_2:
2314 free_netdev(dev);
2315out_release_io_1:
Julia Lawall39424532009-07-04 11:31:47 +00002316 release_mem_region(res->start, resource_size(res));
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002317out_0:
2318 return retval;
2319}
2320
Daniel Mackb6907b02009-05-05 12:22:53 -07002321#ifdef CONFIG_PM
2322/* This implementation assumes the devices remains powered on its VDDVARIO
2323 * pins during suspend. */
2324
Daniel Mack6cb87822009-08-05 08:29:31 +00002325/* TODO: implement freeze/thaw callbacks for hibernation.*/
2326
2327static int smsc911x_suspend(struct device *dev)
Daniel Mackb6907b02009-05-05 12:22:53 -07002328{
Daniel Mack6cb87822009-08-05 08:29:31 +00002329 struct net_device *ndev = dev_get_drvdata(dev);
2330 struct smsc911x_data *pdata = netdev_priv(ndev);
Daniel Mackb6907b02009-05-05 12:22:53 -07002331
2332 /* enable wake on LAN, energy detection and the external PME
2333 * signal. */
2334 smsc911x_reg_write(pdata, PMT_CTRL,
2335 PMT_CTRL_PM_MODE_D1_ | PMT_CTRL_WOL_EN_ |
2336 PMT_CTRL_ED_EN_ | PMT_CTRL_PME_EN_);
2337
2338 return 0;
2339}
2340
Daniel Mack6cb87822009-08-05 08:29:31 +00002341static int smsc911x_resume(struct device *dev)
Daniel Mackb6907b02009-05-05 12:22:53 -07002342{
Daniel Mack6cb87822009-08-05 08:29:31 +00002343 struct net_device *ndev = dev_get_drvdata(dev);
2344 struct smsc911x_data *pdata = netdev_priv(ndev);
Daniel Mackb6907b02009-05-05 12:22:53 -07002345 unsigned int to = 100;
2346
2347 /* Note 3.11 from the datasheet:
2348 * "When the LAN9220 is in a power saving state, a write of any
2349 * data to the BYTE_TEST register will wake-up the device."
2350 */
2351 smsc911x_reg_write(pdata, BYTE_TEST, 0);
2352
2353 /* poll the READY bit in PMT_CTRL. Any other access to the device is
2354 * forbidden while this bit isn't set. Try for 100ms and return -EIO
2355 * if it failed. */
2356 while (!(smsc911x_reg_read(pdata, PMT_CTRL) & PMT_CTRL_READY_) && --to)
2357 udelay(1000);
2358
2359 return (to == 0) ? -EIO : 0;
2360}
2361
Alexey Dobriyan47145212009-12-14 18:00:08 -08002362static const struct dev_pm_ops smsc911x_pm_ops = {
Daniel Mack6cb87822009-08-05 08:29:31 +00002363 .suspend = smsc911x_suspend,
2364 .resume = smsc911x_resume,
2365};
2366
2367#define SMSC911X_PM_OPS (&smsc911x_pm_ops)
2368
Daniel Mackb6907b02009-05-05 12:22:53 -07002369#else
Daniel Mack6cb87822009-08-05 08:29:31 +00002370#define SMSC911X_PM_OPS NULL
Daniel Mackb6907b02009-05-05 12:22:53 -07002371#endif
2372
Shawn Guo79f88ee2011-07-30 08:26:00 +00002373static const struct of_device_id smsc911x_dt_ids[] = {
2374 { .compatible = "smsc,lan9115", },
2375 { /* sentinel */ }
2376};
2377MODULE_DEVICE_TABLE(of, smsc911x_dt_ids);
2378
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002379static struct platform_driver smsc911x_driver = {
2380 .probe = smsc911x_drv_probe,
Mike Frysingerdf911e2d2009-06-05 14:37:20 +00002381 .remove = __devexit_p(smsc911x_drv_remove),
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002382 .driver = {
Daniel Mack6cb87822009-08-05 08:29:31 +00002383 .name = SMSC_CHIPNAME,
2384 .owner = THIS_MODULE,
2385 .pm = SMSC911X_PM_OPS,
Shawn Guo79f88ee2011-07-30 08:26:00 +00002386 .of_match_table = smsc911x_dt_ids,
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002387 },
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002388};
2389
2390/* Entry point for loading the module */
2391static int __init smsc911x_init_module(void)
2392{
Akira Takeuchi62747cd2010-10-27 17:28:58 +01002393 SMSC_INITIALIZE();
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002394 return platform_driver_register(&smsc911x_driver);
2395}
2396
2397/* entry point for unloading the module */
2398static void __exit smsc911x_cleanup_module(void)
2399{
2400 platform_driver_unregister(&smsc911x_driver);
2401}
2402
2403module_init(smsc911x_init_module);
2404module_exit(smsc911x_cleanup_module);