blob: c90ddb61cc5661281e3498daf762fecde80287c6 [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
Phil Edworthy28c21372011-10-12 02:29:39 +000029 * LAN89218
Steve Glendinningfd9abb32008-11-05 00:35:37 +000030 *
31 */
32
Joe Perchesdffc6b22011-03-25 14:21:22 +000033#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
Steve Glendinningfd9abb32008-11-05 00:35:37 +000035#include <linux/crc32.h>
36#include <linux/delay.h>
37#include <linux/errno.h>
38#include <linux/etherdevice.h>
39#include <linux/ethtool.h>
40#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000041#include <linux/interrupt.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000042#include <linux/ioport.h>
43#include <linux/kernel.h>
44#include <linux/module.h>
45#include <linux/netdevice.h>
46#include <linux/platform_device.h>
47#include <linux/sched.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000048#include <linux/timer.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000049#include <linux/bug.h>
50#include <linux/bitops.h>
51#include <linux/irq.h>
52#include <linux/io.h>
Magnus Damm833cc672009-04-27 21:32:16 +000053#include <linux/swab.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000054#include <linux/phy.h>
55#include <linux/smsc911x.h>
Daniel Mack6cb87822009-08-05 08:29:31 +000056#include <linux/device.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000057#include "smsc911x.h"
58
59#define SMSC_CHIPNAME "smsc911x"
60#define SMSC_MDIONAME "smsc911x-mdio"
61#define SMSC_DRV_VERSION "2008-10-21"
62
63MODULE_LICENSE("GPL");
64MODULE_VERSION(SMSC_DRV_VERSION);
Vincent Stehlé62038e42010-09-26 18:50:05 -070065MODULE_ALIAS("platform:smsc911x");
Steve Glendinningfd9abb32008-11-05 00:35:37 +000066
67#if USE_DEBUG > 0
68static int debug = 16;
69#else
70static int debug = 3;
71#endif
72
73module_param(debug, int, 0);
74MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
75
Mathieu J. Poirierc326de82011-04-13 17:13:00 -070076struct smsc911x_data;
77
78struct smsc911x_ops {
79 u32 (*reg_read)(struct smsc911x_data *pdata, u32 reg);
80 void (*reg_write)(struct smsc911x_data *pdata, u32 reg, u32 val);
81 void (*rx_readfifo)(struct smsc911x_data *pdata,
82 unsigned int *buf, unsigned int wordcount);
83 void (*tx_writefifo)(struct smsc911x_data *pdata,
84 unsigned int *buf, unsigned int wordcount);
85};
86
Steve Glendinningfd9abb32008-11-05 00:35:37 +000087struct smsc911x_data {
88 void __iomem *ioaddr;
89
90 unsigned int idrev;
91
92 /* used to decide which workarounds apply */
93 unsigned int generation;
94
95 /* device configuration (copied from platform_data during probe) */
Steve Glendinning2107fb82008-11-05 00:35:38 +000096 struct smsc911x_platform_config config;
Steve Glendinningfd9abb32008-11-05 00:35:37 +000097
98 /* This needs to be acquired before calling any of below:
99 * smsc911x_mac_read(), smsc911x_mac_write()
100 */
101 spinlock_t mac_lock;
102
Catalin Marinas492c5d92010-07-19 13:36:21 -0700103 /* spinlock to ensure register accesses are serialised */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000104 spinlock_t dev_lock;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000105
106 struct phy_device *phy_dev;
107 struct mii_bus *mii_bus;
108 int phy_irq[PHY_MAX_ADDR];
109 unsigned int using_extphy;
110 int last_duplex;
111 int last_carrier;
112
113 u32 msg_enable;
114 unsigned int gpio_setting;
115 unsigned int gpio_orig_setting;
116 struct net_device *dev;
117 struct napi_struct napi;
118
119 unsigned int software_irq_signal;
120
121#ifdef USE_PHY_WORK_AROUND
122#define MIN_PACKET_SIZE (64)
123 char loopback_tx_pkt[MIN_PACKET_SIZE];
124 char loopback_rx_pkt[MIN_PACKET_SIZE];
125 unsigned int resetcount;
126#endif
127
128 /* Members for Multicast filter workaround */
129 unsigned int multicast_update_pending;
130 unsigned int set_bits_mask;
131 unsigned int clear_bits_mask;
132 unsigned int hashhi;
133 unsigned int hashlo;
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700134
135 /* register access functions */
136 const struct smsc911x_ops *ops;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000137};
138
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700139/* Easy access to information */
140#define __smsc_shift(pdata, reg) ((reg) << ((pdata)->config.shift))
141
Catalin Marinas492c5d92010-07-19 13:36:21 -0700142static inline u32 __smsc911x_reg_read(struct smsc911x_data *pdata, u32 reg)
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000143{
Steve Glendinning2107fb82008-11-05 00:35:38 +0000144 if (pdata->config.flags & SMSC911X_USE_32BIT)
145 return readl(pdata->ioaddr + reg);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000146
Catalin Marinas492c5d92010-07-19 13:36:21 -0700147 if (pdata->config.flags & SMSC911X_USE_16BIT)
148 return ((readw(pdata->ioaddr + reg) & 0xFFFF) |
Steve Glendinning2107fb82008-11-05 00:35:38 +0000149 ((readw(pdata->ioaddr + reg + 2) & 0xFFFF) << 16));
Steve Glendinning2107fb82008-11-05 00:35:38 +0000150
151 BUG();
Steve Glendinning702403a2009-01-11 00:14:27 -0800152 return 0;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000153}
154
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700155static inline u32
156__smsc911x_reg_read_shift(struct smsc911x_data *pdata, u32 reg)
157{
158 if (pdata->config.flags & SMSC911X_USE_32BIT)
159 return readl(pdata->ioaddr + __smsc_shift(pdata, reg));
160
161 if (pdata->config.flags & SMSC911X_USE_16BIT)
162 return (readw(pdata->ioaddr +
163 __smsc_shift(pdata, reg)) & 0xFFFF) |
164 ((readw(pdata->ioaddr +
165 __smsc_shift(pdata, reg + 2)) & 0xFFFF) << 16);
166
167 BUG();
168 return 0;
169}
170
Catalin Marinas492c5d92010-07-19 13:36:21 -0700171static inline u32 smsc911x_reg_read(struct smsc911x_data *pdata, u32 reg)
172{
173 u32 data;
174 unsigned long flags;
175
176 spin_lock_irqsave(&pdata->dev_lock, flags);
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700177 data = pdata->ops->reg_read(pdata, reg);
Catalin Marinas492c5d92010-07-19 13:36:21 -0700178 spin_unlock_irqrestore(&pdata->dev_lock, flags);
179
180 return data;
181}
182
183static inline void __smsc911x_reg_write(struct smsc911x_data *pdata, u32 reg,
184 u32 val)
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000185{
Steve Glendinning2107fb82008-11-05 00:35:38 +0000186 if (pdata->config.flags & SMSC911X_USE_32BIT) {
187 writel(val, pdata->ioaddr + reg);
188 return;
189 }
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000190
Steve Glendinning2107fb82008-11-05 00:35:38 +0000191 if (pdata->config.flags & SMSC911X_USE_16BIT) {
Steve Glendinning2107fb82008-11-05 00:35:38 +0000192 writew(val & 0xFFFF, pdata->ioaddr + reg);
193 writew((val >> 16) & 0xFFFF, pdata->ioaddr + reg + 2);
Steve Glendinning2107fb82008-11-05 00:35:38 +0000194 return;
195 }
196
197 BUG();
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000198}
199
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700200static inline void
201__smsc911x_reg_write_shift(struct smsc911x_data *pdata, u32 reg, u32 val)
202{
203 if (pdata->config.flags & SMSC911X_USE_32BIT) {
204 writel(val, pdata->ioaddr + __smsc_shift(pdata, reg));
205 return;
206 }
207
208 if (pdata->config.flags & SMSC911X_USE_16BIT) {
209 writew(val & 0xFFFF,
210 pdata->ioaddr + __smsc_shift(pdata, reg));
211 writew((val >> 16) & 0xFFFF,
212 pdata->ioaddr + __smsc_shift(pdata, reg + 2));
213 return;
214 }
215
216 BUG();
217}
218
Catalin Marinas492c5d92010-07-19 13:36:21 -0700219static inline void smsc911x_reg_write(struct smsc911x_data *pdata, u32 reg,
220 u32 val)
221{
222 unsigned long flags;
223
224 spin_lock_irqsave(&pdata->dev_lock, flags);
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700225 pdata->ops->reg_write(pdata, reg, val);
Catalin Marinas492c5d92010-07-19 13:36:21 -0700226 spin_unlock_irqrestore(&pdata->dev_lock, flags);
227}
228
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000229/* Writes a packet to the TX_DATA_FIFO */
230static inline void
231smsc911x_tx_writefifo(struct smsc911x_data *pdata, unsigned int *buf,
232 unsigned int wordcount)
233{
Catalin Marinas492c5d92010-07-19 13:36:21 -0700234 unsigned long flags;
235
236 spin_lock_irqsave(&pdata->dev_lock, flags);
237
Magnus Damm833cc672009-04-27 21:32:16 +0000238 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
239 while (wordcount--)
Catalin Marinas492c5d92010-07-19 13:36:21 -0700240 __smsc911x_reg_write(pdata, TX_DATA_FIFO,
241 swab32(*buf++));
242 goto out;
Magnus Damm833cc672009-04-27 21:32:16 +0000243 }
244
Steve Glendinning2107fb82008-11-05 00:35:38 +0000245 if (pdata->config.flags & SMSC911X_USE_32BIT) {
246 writesl(pdata->ioaddr + TX_DATA_FIFO, buf, wordcount);
Catalin Marinas492c5d92010-07-19 13:36:21 -0700247 goto out;
Steve Glendinning2107fb82008-11-05 00:35:38 +0000248 }
249
250 if (pdata->config.flags & SMSC911X_USE_16BIT) {
251 while (wordcount--)
Catalin Marinas492c5d92010-07-19 13:36:21 -0700252 __smsc911x_reg_write(pdata, TX_DATA_FIFO, *buf++);
253 goto out;
Steve Glendinning2107fb82008-11-05 00:35:38 +0000254 }
255
256 BUG();
Catalin Marinas492c5d92010-07-19 13:36:21 -0700257out:
258 spin_unlock_irqrestore(&pdata->dev_lock, flags);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000259}
260
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700261/* Writes a packet to the TX_DATA_FIFO - shifted version */
262static inline void
263smsc911x_tx_writefifo_shift(struct smsc911x_data *pdata, unsigned int *buf,
264 unsigned int wordcount)
265{
266 unsigned long flags;
267
268 spin_lock_irqsave(&pdata->dev_lock, flags);
269
270 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
271 while (wordcount--)
272 __smsc911x_reg_write_shift(pdata, TX_DATA_FIFO,
273 swab32(*buf++));
274 goto out;
275 }
276
277 if (pdata->config.flags & SMSC911X_USE_32BIT) {
278 writesl(pdata->ioaddr + __smsc_shift(pdata,
279 TX_DATA_FIFO), buf, wordcount);
280 goto out;
281 }
282
283 if (pdata->config.flags & SMSC911X_USE_16BIT) {
284 while (wordcount--)
285 __smsc911x_reg_write_shift(pdata,
286 TX_DATA_FIFO, *buf++);
287 goto out;
288 }
289
290 BUG();
291out:
292 spin_unlock_irqrestore(&pdata->dev_lock, flags);
293}
294
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000295/* Reads a packet out of the RX_DATA_FIFO */
296static inline void
297smsc911x_rx_readfifo(struct smsc911x_data *pdata, unsigned int *buf,
298 unsigned int wordcount)
299{
Catalin Marinas492c5d92010-07-19 13:36:21 -0700300 unsigned long flags;
301
302 spin_lock_irqsave(&pdata->dev_lock, flags);
303
Magnus Damm833cc672009-04-27 21:32:16 +0000304 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
305 while (wordcount--)
Catalin Marinas492c5d92010-07-19 13:36:21 -0700306 *buf++ = swab32(__smsc911x_reg_read(pdata,
307 RX_DATA_FIFO));
308 goto out;
Magnus Damm833cc672009-04-27 21:32:16 +0000309 }
310
Steve Glendinning2107fb82008-11-05 00:35:38 +0000311 if (pdata->config.flags & SMSC911X_USE_32BIT) {
312 readsl(pdata->ioaddr + RX_DATA_FIFO, buf, wordcount);
Catalin Marinas492c5d92010-07-19 13:36:21 -0700313 goto out;
Steve Glendinning2107fb82008-11-05 00:35:38 +0000314 }
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000315
Steve Glendinning2107fb82008-11-05 00:35:38 +0000316 if (pdata->config.flags & SMSC911X_USE_16BIT) {
317 while (wordcount--)
Catalin Marinas492c5d92010-07-19 13:36:21 -0700318 *buf++ = __smsc911x_reg_read(pdata, RX_DATA_FIFO);
319 goto out;
Steve Glendinning2107fb82008-11-05 00:35:38 +0000320 }
321
322 BUG();
Catalin Marinas492c5d92010-07-19 13:36:21 -0700323out:
324 spin_unlock_irqrestore(&pdata->dev_lock, flags);
Steve Glendinning2107fb82008-11-05 00:35:38 +0000325}
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000326
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700327/* Reads a packet out of the RX_DATA_FIFO - shifted version */
328static inline void
329smsc911x_rx_readfifo_shift(struct smsc911x_data *pdata, unsigned int *buf,
330 unsigned int wordcount)
331{
332 unsigned long flags;
333
334 spin_lock_irqsave(&pdata->dev_lock, flags);
335
336 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
337 while (wordcount--)
338 *buf++ = swab32(__smsc911x_reg_read_shift(pdata,
339 RX_DATA_FIFO));
340 goto out;
341 }
342
343 if (pdata->config.flags & SMSC911X_USE_32BIT) {
344 readsl(pdata->ioaddr + __smsc_shift(pdata,
345 RX_DATA_FIFO), buf, wordcount);
346 goto out;
347 }
348
349 if (pdata->config.flags & SMSC911X_USE_16BIT) {
350 while (wordcount--)
351 *buf++ = __smsc911x_reg_read_shift(pdata,
352 RX_DATA_FIFO);
353 goto out;
354 }
355
356 BUG();
357out:
358 spin_unlock_irqrestore(&pdata->dev_lock, flags);
359}
360
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000361/* waits for MAC not busy, with timeout. Only called by smsc911x_mac_read
362 * and smsc911x_mac_write, so assumes mac_lock is held */
363static int smsc911x_mac_complete(struct smsc911x_data *pdata)
364{
365 int i;
366 u32 val;
367
368 SMSC_ASSERT_MAC_LOCK(pdata);
369
370 for (i = 0; i < 40; i++) {
371 val = smsc911x_reg_read(pdata, MAC_CSR_CMD);
372 if (!(val & MAC_CSR_CMD_CSR_BUSY_))
373 return 0;
374 }
Joe Perchesdffc6b22011-03-25 14:21:22 +0000375 SMSC_WARN(pdata, hw, "Timed out waiting for MAC not BUSY. "
376 "MAC_CSR_CMD: 0x%08X", val);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000377 return -EIO;
378}
379
380/* Fetches a MAC register value. Assumes mac_lock is acquired */
381static u32 smsc911x_mac_read(struct smsc911x_data *pdata, unsigned int offset)
382{
383 unsigned int temp;
384
385 SMSC_ASSERT_MAC_LOCK(pdata);
386
387 temp = smsc911x_reg_read(pdata, MAC_CSR_CMD);
388 if (unlikely(temp & MAC_CSR_CMD_CSR_BUSY_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000389 SMSC_WARN(pdata, hw, "MAC busy at entry");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000390 return 0xFFFFFFFF;
391 }
392
393 /* Send the MAC cmd */
394 smsc911x_reg_write(pdata, MAC_CSR_CMD, ((offset & 0xFF) |
395 MAC_CSR_CMD_CSR_BUSY_ | MAC_CSR_CMD_R_NOT_W_));
396
397 /* Workaround for hardware read-after-write restriction */
398 temp = smsc911x_reg_read(pdata, BYTE_TEST);
399
400 /* Wait for the read to complete */
401 if (likely(smsc911x_mac_complete(pdata) == 0))
402 return smsc911x_reg_read(pdata, MAC_CSR_DATA);
403
Joe Perchesdffc6b22011-03-25 14:21:22 +0000404 SMSC_WARN(pdata, hw, "MAC busy after read");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000405 return 0xFFFFFFFF;
406}
407
408/* Set a mac register, mac_lock must be acquired before calling */
409static void smsc911x_mac_write(struct smsc911x_data *pdata,
410 unsigned int offset, u32 val)
411{
412 unsigned int temp;
413
414 SMSC_ASSERT_MAC_LOCK(pdata);
415
416 temp = smsc911x_reg_read(pdata, MAC_CSR_CMD);
417 if (unlikely(temp & MAC_CSR_CMD_CSR_BUSY_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000418 SMSC_WARN(pdata, hw,
419 "smsc911x_mac_write failed, MAC busy at entry");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000420 return;
421 }
422
423 /* Send data to write */
424 smsc911x_reg_write(pdata, MAC_CSR_DATA, val);
425
426 /* Write the actual data */
427 smsc911x_reg_write(pdata, MAC_CSR_CMD, ((offset & 0xFF) |
428 MAC_CSR_CMD_CSR_BUSY_));
429
430 /* Workaround for hardware read-after-write restriction */
431 temp = smsc911x_reg_read(pdata, BYTE_TEST);
432
433 /* Wait for the write to complete */
434 if (likely(smsc911x_mac_complete(pdata) == 0))
435 return;
436
Joe Perchesdffc6b22011-03-25 14:21:22 +0000437 SMSC_WARN(pdata, hw, "smsc911x_mac_write failed, MAC busy after write");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000438}
439
440/* Get a phy register */
441static int smsc911x_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
442{
443 struct smsc911x_data *pdata = (struct smsc911x_data *)bus->priv;
444 unsigned long flags;
445 unsigned int addr;
446 int i, reg;
447
448 spin_lock_irqsave(&pdata->mac_lock, flags);
449
450 /* Confirm MII not busy */
451 if (unlikely(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000452 SMSC_WARN(pdata, hw, "MII is busy in smsc911x_mii_read???");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000453 reg = -EIO;
454 goto out;
455 }
456
457 /* Set the address, index & direction (read from PHY) */
458 addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6);
459 smsc911x_mac_write(pdata, MII_ACC, addr);
460
461 /* Wait for read to complete w/ timeout */
462 for (i = 0; i < 100; i++)
463 if (!(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
464 reg = smsc911x_mac_read(pdata, MII_DATA);
465 goto out;
466 }
467
Joe Perchesdffc6b22011-03-25 14:21:22 +0000468 SMSC_WARN(pdata, hw, "Timed out waiting for MII read to finish");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000469 reg = -EIO;
470
471out:
472 spin_unlock_irqrestore(&pdata->mac_lock, flags);
473 return reg;
474}
475
476/* Set a phy register */
477static int smsc911x_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
478 u16 val)
479{
480 struct smsc911x_data *pdata = (struct smsc911x_data *)bus->priv;
481 unsigned long flags;
482 unsigned int addr;
483 int i, reg;
484
485 spin_lock_irqsave(&pdata->mac_lock, flags);
486
487 /* Confirm MII not busy */
488 if (unlikely(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000489 SMSC_WARN(pdata, hw, "MII is busy in smsc911x_mii_write???");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000490 reg = -EIO;
491 goto out;
492 }
493
494 /* Put the data to write in the MAC */
495 smsc911x_mac_write(pdata, MII_DATA, val);
496
497 /* Set the address, index & direction (write to PHY) */
498 addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6) |
499 MII_ACC_MII_WRITE_;
500 smsc911x_mac_write(pdata, MII_ACC, addr);
501
502 /* Wait for write to complete w/ timeout */
503 for (i = 0; i < 100; i++)
504 if (!(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
505 reg = 0;
506 goto out;
507 }
508
Joe Perchesdffc6b22011-03-25 14:21:22 +0000509 SMSC_WARN(pdata, hw, "Timed out waiting for MII write to finish");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000510 reg = -EIO;
511
512out:
513 spin_unlock_irqrestore(&pdata->mac_lock, flags);
514 return reg;
515}
516
Steve Glendinningd23f0282009-01-27 06:51:11 +0000517/* Switch to external phy. Assumes tx and rx are stopped. */
518static void smsc911x_phy_enable_external(struct smsc911x_data *pdata)
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000519{
520 unsigned int hwcfg = smsc911x_reg_read(pdata, HW_CFG);
521
Steve Glendinningd23f0282009-01-27 06:51:11 +0000522 /* Disable phy clocks to the MAC */
523 hwcfg &= (~HW_CFG_PHY_CLK_SEL_);
524 hwcfg |= HW_CFG_PHY_CLK_SEL_CLK_DIS_;
525 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
526 udelay(10); /* Enough time for clocks to stop */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000527
Steve Glendinningd23f0282009-01-27 06:51:11 +0000528 /* Switch to external phy */
529 hwcfg |= HW_CFG_EXT_PHY_EN_;
530 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000531
Steve Glendinningd23f0282009-01-27 06:51:11 +0000532 /* Enable phy clocks to the MAC */
533 hwcfg &= (~HW_CFG_PHY_CLK_SEL_);
534 hwcfg |= HW_CFG_PHY_CLK_SEL_EXT_PHY_;
535 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
536 udelay(10); /* Enough time for clocks to restart */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000537
Steve Glendinningd23f0282009-01-27 06:51:11 +0000538 hwcfg |= HW_CFG_SMI_SEL_;
539 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
540}
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000541
Steve Glendinningd23f0282009-01-27 06:51:11 +0000542/* Autodetects and enables external phy if present on supported chips.
543 * autodetection can be overridden by specifying SMSC911X_FORCE_INTERNAL_PHY
544 * or SMSC911X_FORCE_EXTERNAL_PHY in the platform_data flags. */
545static void smsc911x_phy_initialise_external(struct smsc911x_data *pdata)
546{
547 unsigned int hwcfg = smsc911x_reg_read(pdata, HW_CFG);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000548
Steve Glendinningd23f0282009-01-27 06:51:11 +0000549 if (pdata->config.flags & SMSC911X_FORCE_INTERNAL_PHY) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000550 SMSC_TRACE(pdata, hw, "Forcing internal PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +0000551 pdata->using_extphy = 0;
552 } else if (pdata->config.flags & SMSC911X_FORCE_EXTERNAL_PHY) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000553 SMSC_TRACE(pdata, hw, "Forcing external PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +0000554 smsc911x_phy_enable_external(pdata);
555 pdata->using_extphy = 1;
556 } else if (hwcfg & HW_CFG_EXT_PHY_DET_) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000557 SMSC_TRACE(pdata, hw,
558 "HW_CFG EXT_PHY_DET set, using external PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +0000559 smsc911x_phy_enable_external(pdata);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000560 pdata->using_extphy = 1;
561 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000562 SMSC_TRACE(pdata, hw,
563 "HW_CFG EXT_PHY_DET clear, using internal PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +0000564 pdata->using_extphy = 0;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000565 }
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000566}
567
568/* Fetches a tx status out of the status fifo */
569static unsigned int smsc911x_tx_get_txstatus(struct smsc911x_data *pdata)
570{
571 unsigned int result =
572 smsc911x_reg_read(pdata, TX_FIFO_INF) & TX_FIFO_INF_TSUSED_;
573
574 if (result != 0)
575 result = smsc911x_reg_read(pdata, TX_STATUS_FIFO);
576
577 return result;
578}
579
580/* Fetches the next rx status */
581static unsigned int smsc911x_rx_get_rxstatus(struct smsc911x_data *pdata)
582{
583 unsigned int result =
584 smsc911x_reg_read(pdata, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED_;
585
586 if (result != 0)
587 result = smsc911x_reg_read(pdata, RX_STATUS_FIFO);
588
589 return result;
590}
591
592#ifdef USE_PHY_WORK_AROUND
593static int smsc911x_phy_check_loopbackpkt(struct smsc911x_data *pdata)
594{
595 unsigned int tries;
596 u32 wrsz;
597 u32 rdsz;
598 ulong bufp;
599
600 for (tries = 0; tries < 10; tries++) {
601 unsigned int txcmd_a;
602 unsigned int txcmd_b;
603 unsigned int status;
604 unsigned int pktlength;
605 unsigned int i;
606
607 /* Zero-out rx packet memory */
608 memset(pdata->loopback_rx_pkt, 0, MIN_PACKET_SIZE);
609
610 /* Write tx packet to 118 */
611 txcmd_a = (u32)((ulong)pdata->loopback_tx_pkt & 0x03) << 16;
612 txcmd_a |= TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
613 txcmd_a |= MIN_PACKET_SIZE;
614
615 txcmd_b = MIN_PACKET_SIZE << 16 | MIN_PACKET_SIZE;
616
617 smsc911x_reg_write(pdata, TX_DATA_FIFO, txcmd_a);
618 smsc911x_reg_write(pdata, TX_DATA_FIFO, txcmd_b);
619
620 bufp = (ulong)pdata->loopback_tx_pkt & (~0x3);
621 wrsz = MIN_PACKET_SIZE + 3;
622 wrsz += (u32)((ulong)pdata->loopback_tx_pkt & 0x3);
623 wrsz >>= 2;
624
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700625 pdata->ops->tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000626
627 /* Wait till transmit is done */
628 i = 60;
629 do {
630 udelay(5);
631 status = smsc911x_tx_get_txstatus(pdata);
632 } while ((i--) && (!status));
633
634 if (!status) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000635 SMSC_WARN(pdata, hw,
636 "Failed to transmit during loopback test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000637 continue;
638 }
639 if (status & TX_STS_ES_) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000640 SMSC_WARN(pdata, hw,
641 "Transmit encountered errors during loopback test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000642 continue;
643 }
644
645 /* Wait till receive is done */
646 i = 60;
647 do {
648 udelay(5);
649 status = smsc911x_rx_get_rxstatus(pdata);
650 } while ((i--) && (!status));
651
652 if (!status) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000653 SMSC_WARN(pdata, hw,
654 "Failed to receive during loopback test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000655 continue;
656 }
657 if (status & RX_STS_ES_) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000658 SMSC_WARN(pdata, hw,
659 "Receive encountered errors during loopback test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000660 continue;
661 }
662
663 pktlength = ((status & 0x3FFF0000UL) >> 16);
664 bufp = (ulong)pdata->loopback_rx_pkt;
665 rdsz = pktlength + 3;
666 rdsz += (u32)((ulong)pdata->loopback_rx_pkt & 0x3);
667 rdsz >>= 2;
668
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700669 pdata->ops->rx_readfifo(pdata, (unsigned int *)bufp, rdsz);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000670
671 if (pktlength != (MIN_PACKET_SIZE + 4)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000672 SMSC_WARN(pdata, hw, "Unexpected packet size "
673 "during loop back test, size=%d, will retry",
674 pktlength);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000675 } else {
676 unsigned int j;
677 int mismatch = 0;
678 for (j = 0; j < MIN_PACKET_SIZE; j++) {
679 if (pdata->loopback_tx_pkt[j]
680 != pdata->loopback_rx_pkt[j]) {
681 mismatch = 1;
682 break;
683 }
684 }
685 if (!mismatch) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000686 SMSC_TRACE(pdata, hw, "Successfully verified "
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000687 "loopback packet");
688 return 0;
689 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000690 SMSC_WARN(pdata, hw, "Data mismatch "
691 "during loop back test, will retry");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000692 }
693 }
694 }
695
696 return -EIO;
697}
698
699static int smsc911x_phy_reset(struct smsc911x_data *pdata)
700{
701 struct phy_device *phy_dev = pdata->phy_dev;
702 unsigned int temp;
703 unsigned int i = 100000;
704
705 BUG_ON(!phy_dev);
706 BUG_ON(!phy_dev->bus);
707
Joe Perchesdffc6b22011-03-25 14:21:22 +0000708 SMSC_TRACE(pdata, hw, "Performing PHY BCR Reset");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000709 smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR, BMCR_RESET);
710 do {
711 msleep(1);
712 temp = smsc911x_mii_read(phy_dev->bus, phy_dev->addr,
713 MII_BMCR);
714 } while ((i--) && (temp & BMCR_RESET));
715
716 if (temp & BMCR_RESET) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000717 SMSC_WARN(pdata, hw, "PHY reset failed to complete");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000718 return -EIO;
719 }
720 /* Extra delay required because the phy may not be completed with
721 * its reset when BMCR_RESET is cleared. Specs say 256 uS is
722 * enough delay but using 1ms here to be safe */
723 msleep(1);
724
725 return 0;
726}
727
728static int smsc911x_phy_loopbacktest(struct net_device *dev)
729{
730 struct smsc911x_data *pdata = netdev_priv(dev);
731 struct phy_device *phy_dev = pdata->phy_dev;
732 int result = -EIO;
733 unsigned int i, val;
734 unsigned long flags;
735
736 /* Initialise tx packet using broadcast destination address */
737 memset(pdata->loopback_tx_pkt, 0xff, ETH_ALEN);
738
739 /* Use incrementing source address */
740 for (i = 6; i < 12; i++)
741 pdata->loopback_tx_pkt[i] = (char)i;
742
743 /* Set length type field */
744 pdata->loopback_tx_pkt[12] = 0x00;
745 pdata->loopback_tx_pkt[13] = 0x00;
746
747 for (i = 14; i < MIN_PACKET_SIZE; i++)
748 pdata->loopback_tx_pkt[i] = (char)i;
749
750 val = smsc911x_reg_read(pdata, HW_CFG);
751 val &= HW_CFG_TX_FIF_SZ_;
752 val |= HW_CFG_SF_;
753 smsc911x_reg_write(pdata, HW_CFG, val);
754
755 smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
756 smsc911x_reg_write(pdata, RX_CFG,
757 (u32)((ulong)pdata->loopback_rx_pkt & 0x03) << 8);
758
759 for (i = 0; i < 10; i++) {
760 /* Set PHY to 10/FD, no ANEG, and loopback mode */
761 smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR,
762 BMCR_LOOPBACK | BMCR_FULLDPLX);
763
764 /* Enable MAC tx/rx, FD */
765 spin_lock_irqsave(&pdata->mac_lock, flags);
766 smsc911x_mac_write(pdata, MAC_CR, MAC_CR_FDPX_
767 | MAC_CR_TXEN_ | MAC_CR_RXEN_);
768 spin_unlock_irqrestore(&pdata->mac_lock, flags);
769
770 if (smsc911x_phy_check_loopbackpkt(pdata) == 0) {
771 result = 0;
772 break;
773 }
774 pdata->resetcount++;
775
776 /* Disable MAC rx */
777 spin_lock_irqsave(&pdata->mac_lock, flags);
778 smsc911x_mac_write(pdata, MAC_CR, 0);
779 spin_unlock_irqrestore(&pdata->mac_lock, flags);
780
781 smsc911x_phy_reset(pdata);
782 }
783
784 /* Disable MAC */
785 spin_lock_irqsave(&pdata->mac_lock, flags);
786 smsc911x_mac_write(pdata, MAC_CR, 0);
787 spin_unlock_irqrestore(&pdata->mac_lock, flags);
788
789 /* Cancel PHY loopback mode */
790 smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR, 0);
791
792 smsc911x_reg_write(pdata, TX_CFG, 0);
793 smsc911x_reg_write(pdata, RX_CFG, 0);
794
795 return result;
796}
797#endif /* USE_PHY_WORK_AROUND */
798
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000799static void smsc911x_phy_update_flowcontrol(struct smsc911x_data *pdata)
800{
801 struct phy_device *phy_dev = pdata->phy_dev;
802 u32 afc = smsc911x_reg_read(pdata, AFC_CFG);
803 u32 flow;
804 unsigned long flags;
805
806 if (phy_dev->duplex == DUPLEX_FULL) {
807 u16 lcladv = phy_read(phy_dev, MII_ADVERTISE);
808 u16 rmtadv = phy_read(phy_dev, MII_LPA);
Steve Glendinningbc02ff92008-12-16 02:00:48 -0800809 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000810
811 if (cap & FLOW_CTRL_RX)
812 flow = 0xFFFF0002;
813 else
814 flow = 0;
815
816 if (cap & FLOW_CTRL_TX)
817 afc |= 0xF;
818 else
819 afc &= ~0xF;
820
Joe Perchesdffc6b22011-03-25 14:21:22 +0000821 SMSC_TRACE(pdata, hw, "rx pause %s, tx pause %s",
822 (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
823 (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000824 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000825 SMSC_TRACE(pdata, hw, "half duplex");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000826 flow = 0;
827 afc |= 0xF;
828 }
829
830 spin_lock_irqsave(&pdata->mac_lock, flags);
831 smsc911x_mac_write(pdata, FLOW, flow);
832 spin_unlock_irqrestore(&pdata->mac_lock, flags);
833
834 smsc911x_reg_write(pdata, AFC_CFG, afc);
835}
836
837/* Update link mode if anything has changed. Called periodically when the
838 * PHY is in polling mode, even if nothing has changed. */
839static void smsc911x_phy_adjust_link(struct net_device *dev)
840{
841 struct smsc911x_data *pdata = netdev_priv(dev);
842 struct phy_device *phy_dev = pdata->phy_dev;
843 unsigned long flags;
844 int carrier;
845
846 if (phy_dev->duplex != pdata->last_duplex) {
847 unsigned int mac_cr;
Joe Perchesdffc6b22011-03-25 14:21:22 +0000848 SMSC_TRACE(pdata, hw, "duplex state has changed");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000849
850 spin_lock_irqsave(&pdata->mac_lock, flags);
851 mac_cr = smsc911x_mac_read(pdata, MAC_CR);
852 if (phy_dev->duplex) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000853 SMSC_TRACE(pdata, hw,
854 "configuring for full duplex mode");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000855 mac_cr |= MAC_CR_FDPX_;
856 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000857 SMSC_TRACE(pdata, hw,
858 "configuring for half duplex mode");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000859 mac_cr &= ~MAC_CR_FDPX_;
860 }
861 smsc911x_mac_write(pdata, MAC_CR, mac_cr);
862 spin_unlock_irqrestore(&pdata->mac_lock, flags);
863
864 smsc911x_phy_update_flowcontrol(pdata);
865 pdata->last_duplex = phy_dev->duplex;
866 }
867
868 carrier = netif_carrier_ok(dev);
869 if (carrier != pdata->last_carrier) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000870 SMSC_TRACE(pdata, hw, "carrier state has changed");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000871 if (carrier) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000872 SMSC_TRACE(pdata, hw, "configuring for carrier OK");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000873 if ((pdata->gpio_orig_setting & GPIO_CFG_LED1_EN_) &&
874 (!pdata->using_extphy)) {
Thomas Weber88393162010-03-16 11:47:56 +0100875 /* Restore original GPIO configuration */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000876 pdata->gpio_setting = pdata->gpio_orig_setting;
877 smsc911x_reg_write(pdata, GPIO_CFG,
878 pdata->gpio_setting);
879 }
880 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000881 SMSC_TRACE(pdata, hw, "configuring for no carrier");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000882 /* Check global setting that LED1
883 * usage is 10/100 indicator */
884 pdata->gpio_setting = smsc911x_reg_read(pdata,
885 GPIO_CFG);
Joe Perches8e95a202009-12-03 07:58:21 +0000886 if ((pdata->gpio_setting & GPIO_CFG_LED1_EN_) &&
887 (!pdata->using_extphy)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000888 /* Force 10/100 LED off, after saving
Thomas Weber88393162010-03-16 11:47:56 +0100889 * original GPIO configuration */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000890 pdata->gpio_orig_setting = pdata->gpio_setting;
891
892 pdata->gpio_setting &= ~GPIO_CFG_LED1_EN_;
893 pdata->gpio_setting |= (GPIO_CFG_GPIOBUF0_
894 | GPIO_CFG_GPIODIR0_
895 | GPIO_CFG_GPIOD0_);
896 smsc911x_reg_write(pdata, GPIO_CFG,
897 pdata->gpio_setting);
898 }
899 }
900 pdata->last_carrier = carrier;
901 }
902}
903
904static int smsc911x_mii_probe(struct net_device *dev)
905{
906 struct smsc911x_data *pdata = netdev_priv(dev);
907 struct phy_device *phydev = NULL;
kirjanov@gmail.come4a474f2010-02-16 21:54:58 +0000908 int ret;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000909
910 /* find the first phy */
kirjanov@gmail.come4a474f2010-02-16 21:54:58 +0000911 phydev = phy_find_first(pdata->mii_bus);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000912 if (!phydev) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000913 netdev_err(dev, "no PHY found\n");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000914 return -ENODEV;
915 }
916
Joe Perchesdffc6b22011-03-25 14:21:22 +0000917 SMSC_TRACE(pdata, probe, "PHY: addr %d, phy_id 0x%08X",
918 phydev->addr, phydev->phy_id);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000919
kirjanov@gmail.come4a474f2010-02-16 21:54:58 +0000920 ret = phy_connect_direct(dev, phydev,
921 &smsc911x_phy_adjust_link, 0,
922 pdata->config.phy_interface);
923
924 if (ret) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000925 netdev_err(dev, "Could not attach to PHY\n");
kirjanov@gmail.come4a474f2010-02-16 21:54:58 +0000926 return ret;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000927 }
928
Joe Perchesdffc6b22011-03-25 14:21:22 +0000929 netdev_info(dev,
930 "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
931 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000932
933 /* mask with MAC supported features */
934 phydev->supported &= (PHY_BASIC_FEATURES | SUPPORTED_Pause |
935 SUPPORTED_Asym_Pause);
936 phydev->advertising = phydev->supported;
937
938 pdata->phy_dev = phydev;
939 pdata->last_duplex = -1;
940 pdata->last_carrier = -1;
941
942#ifdef USE_PHY_WORK_AROUND
943 if (smsc911x_phy_loopbacktest(dev) < 0) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000944 SMSC_WARN(pdata, hw, "Failed Loop Back Test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000945 return -ENODEV;
946 }
Joe Perchesdffc6b22011-03-25 14:21:22 +0000947 SMSC_TRACE(pdata, hw, "Passed Loop Back Test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000948#endif /* USE_PHY_WORK_AROUND */
949
Joe Perchesdffc6b22011-03-25 14:21:22 +0000950 SMSC_TRACE(pdata, hw, "phy initialised successfully");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000951 return 0;
952}
953
954static int __devinit smsc911x_mii_init(struct platform_device *pdev,
955 struct net_device *dev)
956{
957 struct smsc911x_data *pdata = netdev_priv(dev);
958 int err = -ENXIO, i;
959
960 pdata->mii_bus = mdiobus_alloc();
961 if (!pdata->mii_bus) {
962 err = -ENOMEM;
963 goto err_out_1;
964 }
965
966 pdata->mii_bus->name = SMSC_MDIONAME;
967 snprintf(pdata->mii_bus->id, MII_BUS_ID_SIZE, "%x", pdev->id);
968 pdata->mii_bus->priv = pdata;
969 pdata->mii_bus->read = smsc911x_mii_read;
970 pdata->mii_bus->write = smsc911x_mii_write;
971 pdata->mii_bus->irq = pdata->phy_irq;
972 for (i = 0; i < PHY_MAX_ADDR; ++i)
973 pdata->mii_bus->irq[i] = PHY_POLL;
974
975 pdata->mii_bus->parent = &pdev->dev;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000976
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000977 switch (pdata->idrev & 0xFFFF0000) {
978 case 0x01170000:
979 case 0x01150000:
980 case 0x117A0000:
981 case 0x115A0000:
982 /* External PHY supported, try to autodetect */
Steve Glendinningd23f0282009-01-27 06:51:11 +0000983 smsc911x_phy_initialise_external(pdata);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000984 break;
985 default:
Joe Perchesdffc6b22011-03-25 14:21:22 +0000986 SMSC_TRACE(pdata, hw, "External PHY is not supported, "
987 "using internal PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +0000988 pdata->using_extphy = 0;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000989 break;
990 }
991
992 if (!pdata->using_extphy) {
993 /* Mask all PHYs except ID 1 (internal) */
994 pdata->mii_bus->phy_mask = ~(1 << 1);
995 }
996
997 if (mdiobus_register(pdata->mii_bus)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000998 SMSC_WARN(pdata, probe, "Error registering mii bus");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000999 goto err_out_free_bus_2;
1000 }
1001
1002 if (smsc911x_mii_probe(dev) < 0) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001003 SMSC_WARN(pdata, probe, "Error registering mii bus");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001004 goto err_out_unregister_bus_3;
1005 }
1006
1007 return 0;
1008
1009err_out_unregister_bus_3:
1010 mdiobus_unregister(pdata->mii_bus);
1011err_out_free_bus_2:
1012 mdiobus_free(pdata->mii_bus);
1013err_out_1:
1014 return err;
1015}
1016
1017/* Gets the number of tx statuses in the fifo */
1018static unsigned int smsc911x_tx_get_txstatcount(struct smsc911x_data *pdata)
1019{
1020 return (smsc911x_reg_read(pdata, TX_FIFO_INF)
1021 & TX_FIFO_INF_TSUSED_) >> 16;
1022}
1023
1024/* Reads tx statuses and increments counters where necessary */
1025static void smsc911x_tx_update_txcounters(struct net_device *dev)
1026{
1027 struct smsc911x_data *pdata = netdev_priv(dev);
1028 unsigned int tx_stat;
1029
1030 while ((tx_stat = smsc911x_tx_get_txstatus(pdata)) != 0) {
1031 if (unlikely(tx_stat & 0x80000000)) {
1032 /* In this driver the packet tag is used as the packet
1033 * length. Since a packet length can never reach the
1034 * size of 0x8000, this bit is reserved. It is worth
1035 * noting that the "reserved bit" in the warning above
1036 * does not reference a hardware defined reserved bit
1037 * but rather a driver defined one.
1038 */
Joe Perchesdffc6b22011-03-25 14:21:22 +00001039 SMSC_WARN(pdata, hw, "Packet tag reserved bit is high");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001040 } else {
Steve Glendinning785b6f92009-03-19 00:24:44 +00001041 if (unlikely(tx_stat & TX_STS_ES_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001042 dev->stats.tx_errors++;
1043 } else {
1044 dev->stats.tx_packets++;
1045 dev->stats.tx_bytes += (tx_stat >> 16);
1046 }
Steve Glendinning785b6f92009-03-19 00:24:44 +00001047 if (unlikely(tx_stat & TX_STS_EXCESS_COL_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001048 dev->stats.collisions += 16;
1049 dev->stats.tx_aborted_errors += 1;
1050 } else {
1051 dev->stats.collisions +=
1052 ((tx_stat >> 3) & 0xF);
1053 }
Steve Glendinning785b6f92009-03-19 00:24:44 +00001054 if (unlikely(tx_stat & TX_STS_LOST_CARRIER_))
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001055 dev->stats.tx_carrier_errors += 1;
Steve Glendinning785b6f92009-03-19 00:24:44 +00001056 if (unlikely(tx_stat & TX_STS_LATE_COL_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001057 dev->stats.collisions++;
1058 dev->stats.tx_aborted_errors++;
1059 }
1060 }
1061 }
1062}
1063
1064/* Increments the Rx error counters */
1065static void
1066smsc911x_rx_counterrors(struct net_device *dev, unsigned int rxstat)
1067{
1068 int crc_err = 0;
1069
Steve Glendinning785b6f92009-03-19 00:24:44 +00001070 if (unlikely(rxstat & RX_STS_ES_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001071 dev->stats.rx_errors++;
Steve Glendinning785b6f92009-03-19 00:24:44 +00001072 if (unlikely(rxstat & RX_STS_CRC_ERR_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001073 dev->stats.rx_crc_errors++;
1074 crc_err = 1;
1075 }
1076 }
1077 if (likely(!crc_err)) {
Steve Glendinning785b6f92009-03-19 00:24:44 +00001078 if (unlikely((rxstat & RX_STS_FRAME_TYPE_) &&
1079 (rxstat & RX_STS_LENGTH_ERR_)))
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001080 dev->stats.rx_length_errors++;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001081 if (rxstat & RX_STS_MCAST_)
1082 dev->stats.multicast++;
1083 }
1084}
1085
1086/* Quickly dumps bad packets */
1087static void
1088smsc911x_rx_fastforward(struct smsc911x_data *pdata, unsigned int pktbytes)
1089{
1090 unsigned int pktwords = (pktbytes + NET_IP_ALIGN + 3) >> 2;
1091
1092 if (likely(pktwords >= 4)) {
1093 unsigned int timeout = 500;
1094 unsigned int val;
1095 smsc911x_reg_write(pdata, RX_DP_CTRL, RX_DP_CTRL_RX_FFWD_);
1096 do {
1097 udelay(1);
1098 val = smsc911x_reg_read(pdata, RX_DP_CTRL);
Steve Glendinning8dacd542009-03-04 07:33:24 +00001099 } while ((val & RX_DP_CTRL_RX_FFWD_) && --timeout);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001100
1101 if (unlikely(timeout == 0))
Joe Perchesdffc6b22011-03-25 14:21:22 +00001102 SMSC_WARN(pdata, hw, "Timed out waiting for "
1103 "RX FFWD to finish, RX_DP_CTRL: 0x%08X", val);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001104 } else {
1105 unsigned int temp;
1106 while (pktwords--)
1107 temp = smsc911x_reg_read(pdata, RX_DATA_FIFO);
1108 }
1109}
1110
1111/* NAPI poll function */
1112static int smsc911x_poll(struct napi_struct *napi, int budget)
1113{
1114 struct smsc911x_data *pdata =
1115 container_of(napi, struct smsc911x_data, napi);
1116 struct net_device *dev = pdata->dev;
1117 int npackets = 0;
1118
Sriramf88c5b92009-11-12 02:14:38 +00001119 while (npackets < budget) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001120 unsigned int pktlength;
1121 unsigned int pktwords;
1122 struct sk_buff *skb;
1123 unsigned int rxstat = smsc911x_rx_get_rxstatus(pdata);
1124
1125 if (!rxstat) {
1126 unsigned int temp;
1127 /* We processed all packets available. Tell NAPI it can
1128 * stop polling then re-enable rx interrupts */
1129 smsc911x_reg_write(pdata, INT_STS, INT_STS_RSFL_);
Ben Hutchings288379f2009-01-19 16:43:59 -08001130 napi_complete(napi);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001131 temp = smsc911x_reg_read(pdata, INT_EN);
1132 temp |= INT_EN_RSFL_EN_;
1133 smsc911x_reg_write(pdata, INT_EN, temp);
1134 break;
1135 }
1136
1137 /* Count packet for NAPI scheduling, even if it has an error.
1138 * Error packets still require cycles to discard */
1139 npackets++;
1140
1141 pktlength = ((rxstat & 0x3FFF0000) >> 16);
1142 pktwords = (pktlength + NET_IP_ALIGN + 3) >> 2;
1143 smsc911x_rx_counterrors(dev, rxstat);
1144
1145 if (unlikely(rxstat & RX_STS_ES_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001146 SMSC_WARN(pdata, rx_err,
1147 "Discarding packet with error bit set");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001148 /* Packet has an error, discard it and continue with
1149 * the next */
1150 smsc911x_rx_fastforward(pdata, pktwords);
1151 dev->stats.rx_dropped++;
1152 continue;
1153 }
1154
1155 skb = netdev_alloc_skb(dev, pktlength + NET_IP_ALIGN);
1156 if (unlikely(!skb)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001157 SMSC_WARN(pdata, rx_err,
1158 "Unable to allocate skb for rx packet");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001159 /* Drop the packet and stop this polling iteration */
1160 smsc911x_rx_fastforward(pdata, pktwords);
1161 dev->stats.rx_dropped++;
1162 break;
1163 }
1164
1165 skb->data = skb->head;
1166 skb_reset_tail_pointer(skb);
1167
1168 /* Align IP on 16B boundary */
1169 skb_reserve(skb, NET_IP_ALIGN);
1170 skb_put(skb, pktlength - 4);
Mathieu J. Poirierc326de82011-04-13 17:13:00 -07001171 pdata->ops->rx_readfifo(pdata,
1172 (unsigned int *)skb->head, pktwords);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001173 skb->protocol = eth_type_trans(skb, dev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001174 skb_checksum_none_assert(skb);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001175 netif_receive_skb(skb);
1176
1177 /* Update counters */
1178 dev->stats.rx_packets++;
1179 dev->stats.rx_bytes += (pktlength - 4);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001180 }
1181
1182 /* Return total received packets */
1183 return npackets;
1184}
1185
1186/* Returns hash bit number for given MAC address
1187 * Example:
1188 * 01 00 5E 00 00 01 -> returns bit number 31 */
1189static unsigned int smsc911x_hash(char addr[ETH_ALEN])
1190{
1191 return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
1192}
1193
1194static void smsc911x_rx_multicast_update(struct smsc911x_data *pdata)
1195{
1196 /* Performs the multicast & mac_cr update. This is called when
1197 * safe on the current hardware, and with the mac_lock held */
1198 unsigned int mac_cr;
1199
1200 SMSC_ASSERT_MAC_LOCK(pdata);
1201
1202 mac_cr = smsc911x_mac_read(pdata, MAC_CR);
1203 mac_cr |= pdata->set_bits_mask;
1204 mac_cr &= ~(pdata->clear_bits_mask);
1205 smsc911x_mac_write(pdata, MAC_CR, mac_cr);
1206 smsc911x_mac_write(pdata, HASHH, pdata->hashhi);
1207 smsc911x_mac_write(pdata, HASHL, pdata->hashlo);
Joe Perchesdffc6b22011-03-25 14:21:22 +00001208 SMSC_TRACE(pdata, hw, "maccr 0x%08X, HASHH 0x%08X, HASHL 0x%08X",
1209 mac_cr, pdata->hashhi, pdata->hashlo);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001210}
1211
1212static void smsc911x_rx_multicast_update_workaround(struct smsc911x_data *pdata)
1213{
1214 unsigned int mac_cr;
1215
1216 /* This function is only called for older LAN911x devices
1217 * (revA or revB), where MAC_CR, HASHH and HASHL should not
1218 * be modified during Rx - newer devices immediately update the
1219 * registers.
1220 *
1221 * This is called from interrupt context */
1222
1223 spin_lock(&pdata->mac_lock);
1224
1225 /* Check Rx has stopped */
1226 if (smsc911x_mac_read(pdata, MAC_CR) & MAC_CR_RXEN_)
Joe Perchesdffc6b22011-03-25 14:21:22 +00001227 SMSC_WARN(pdata, drv, "Rx not stopped");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001228
1229 /* Perform the update - safe to do now Rx has stopped */
1230 smsc911x_rx_multicast_update(pdata);
1231
1232 /* Re-enable Rx */
1233 mac_cr = smsc911x_mac_read(pdata, MAC_CR);
1234 mac_cr |= MAC_CR_RXEN_;
1235 smsc911x_mac_write(pdata, MAC_CR, mac_cr);
1236
1237 pdata->multicast_update_pending = 0;
1238
1239 spin_unlock(&pdata->mac_lock);
1240}
1241
1242static int smsc911x_soft_reset(struct smsc911x_data *pdata)
1243{
1244 unsigned int timeout;
1245 unsigned int temp;
1246
1247 /* Reset the LAN911x */
1248 smsc911x_reg_write(pdata, HW_CFG, HW_CFG_SRST_);
1249 timeout = 10;
1250 do {
1251 udelay(10);
1252 temp = smsc911x_reg_read(pdata, HW_CFG);
1253 } while ((--timeout) && (temp & HW_CFG_SRST_));
1254
1255 if (unlikely(temp & HW_CFG_SRST_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001256 SMSC_WARN(pdata, drv, "Failed to complete reset");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001257 return -EIO;
1258 }
1259 return 0;
1260}
1261
1262/* Sets the device MAC address to dev_addr, called with mac_lock held */
1263static void
Steve Glendinning225ddf42009-03-19 00:24:46 +00001264smsc911x_set_hw_mac_address(struct smsc911x_data *pdata, u8 dev_addr[6])
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001265{
1266 u32 mac_high16 = (dev_addr[5] << 8) | dev_addr[4];
1267 u32 mac_low32 = (dev_addr[3] << 24) | (dev_addr[2] << 16) |
1268 (dev_addr[1] << 8) | dev_addr[0];
1269
1270 SMSC_ASSERT_MAC_LOCK(pdata);
1271
1272 smsc911x_mac_write(pdata, ADDRH, mac_high16);
1273 smsc911x_mac_write(pdata, ADDRL, mac_low32);
1274}
1275
1276static int smsc911x_open(struct net_device *dev)
1277{
1278 struct smsc911x_data *pdata = netdev_priv(dev);
1279 unsigned int timeout;
1280 unsigned int temp;
1281 unsigned int intcfg;
1282
1283 /* if the phy is not yet registered, retry later*/
1284 if (!pdata->phy_dev) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001285 SMSC_WARN(pdata, hw, "phy_dev is NULL");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001286 return -EAGAIN;
1287 }
1288
1289 if (!is_valid_ether_addr(dev->dev_addr)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001290 SMSC_WARN(pdata, hw, "dev_addr is not a valid MAC address");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001291 return -EADDRNOTAVAIL;
1292 }
1293
1294 /* Reset the LAN911x */
1295 if (smsc911x_soft_reset(pdata)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001296 SMSC_WARN(pdata, hw, "soft reset failed");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001297 return -EIO;
1298 }
1299
1300 smsc911x_reg_write(pdata, HW_CFG, 0x00050000);
1301 smsc911x_reg_write(pdata, AFC_CFG, 0x006E3740);
1302
Göran Weinholtf277e652011-03-02 04:07:21 +00001303 /* Increase the legal frame size of VLAN tagged frames to 1522 bytes */
1304 spin_lock_irq(&pdata->mac_lock);
1305 smsc911x_mac_write(pdata, VLAN1, ETH_P_8021Q);
1306 spin_unlock_irq(&pdata->mac_lock);
1307
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001308 /* Make sure EEPROM has finished loading before setting GPIO_CFG */
1309 timeout = 50;
Steve Glendinningf7efb6c2009-03-04 07:33:25 +00001310 while ((smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) &&
1311 --timeout) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001312 udelay(10);
1313 }
1314
1315 if (unlikely(timeout == 0))
Joe Perchesdffc6b22011-03-25 14:21:22 +00001316 SMSC_WARN(pdata, ifup,
1317 "Timed out waiting for EEPROM busy bit to clear");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001318
1319 smsc911x_reg_write(pdata, GPIO_CFG, 0x70070000);
1320
1321 /* The soft reset above cleared the device's MAC address,
1322 * restore it from local copy (set in probe) */
1323 spin_lock_irq(&pdata->mac_lock);
Steve Glendinning225ddf42009-03-19 00:24:46 +00001324 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001325 spin_unlock_irq(&pdata->mac_lock);
1326
1327 /* Initialise irqs, but leave all sources disabled */
1328 smsc911x_reg_write(pdata, INT_EN, 0);
1329 smsc911x_reg_write(pdata, INT_STS, 0xFFFFFFFF);
1330
1331 /* Set interrupt deassertion to 100uS */
1332 intcfg = ((10 << 24) | INT_CFG_IRQ_EN_);
1333
Steve Glendinning2107fb82008-11-05 00:35:38 +00001334 if (pdata->config.irq_polarity) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001335 SMSC_TRACE(pdata, ifup, "irq polarity: active high");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001336 intcfg |= INT_CFG_IRQ_POL_;
1337 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001338 SMSC_TRACE(pdata, ifup, "irq polarity: active low");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001339 }
1340
Steve Glendinning2107fb82008-11-05 00:35:38 +00001341 if (pdata->config.irq_type) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001342 SMSC_TRACE(pdata, ifup, "irq type: push-pull");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001343 intcfg |= INT_CFG_IRQ_TYPE_;
1344 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001345 SMSC_TRACE(pdata, ifup, "irq type: open drain");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001346 }
1347
1348 smsc911x_reg_write(pdata, INT_CFG, intcfg);
1349
Joe Perchesdffc6b22011-03-25 14:21:22 +00001350 SMSC_TRACE(pdata, ifup, "Testing irq handler using IRQ %d", dev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001351 pdata->software_irq_signal = 0;
1352 smp_wmb();
1353
1354 temp = smsc911x_reg_read(pdata, INT_EN);
1355 temp |= INT_EN_SW_INT_EN_;
1356 smsc911x_reg_write(pdata, INT_EN, temp);
1357
1358 timeout = 1000;
1359 while (timeout--) {
1360 if (pdata->software_irq_signal)
1361 break;
1362 msleep(1);
1363 }
1364
1365 if (!pdata->software_irq_signal) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001366 netdev_warn(dev, "ISR failed signaling test (IRQ %d)\n",
1367 dev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001368 return -ENODEV;
1369 }
Joe Perchesdffc6b22011-03-25 14:21:22 +00001370 SMSC_TRACE(pdata, ifup, "IRQ handler passed test using IRQ %d",
1371 dev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001372
Joe Perchesdffc6b22011-03-25 14:21:22 +00001373 netdev_info(dev, "SMSC911x/921x identified at %#08lx, IRQ: %d\n",
1374 (unsigned long)pdata->ioaddr, dev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001375
Steve Glendinning44c1d6f2009-03-18 23:37:18 -07001376 /* Reset the last known duplex and carrier */
1377 pdata->last_duplex = -1;
1378 pdata->last_carrier = -1;
1379
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001380 /* Bring the PHY up */
1381 phy_start(pdata->phy_dev);
1382
1383 temp = smsc911x_reg_read(pdata, HW_CFG);
1384 /* Preserve TX FIFO size and external PHY configuration */
1385 temp &= (HW_CFG_TX_FIF_SZ_|0x00000FFF);
1386 temp |= HW_CFG_SF_;
1387 smsc911x_reg_write(pdata, HW_CFG, temp);
1388
1389 temp = smsc911x_reg_read(pdata, FIFO_INT);
1390 temp |= FIFO_INT_TX_AVAIL_LEVEL_;
1391 temp &= ~(FIFO_INT_RX_STS_LEVEL_);
1392 smsc911x_reg_write(pdata, FIFO_INT, temp);
1393
1394 /* set RX Data offset to 2 bytes for alignment */
1395 smsc911x_reg_write(pdata, RX_CFG, (2 << 8));
1396
1397 /* enable NAPI polling before enabling RX interrupts */
1398 napi_enable(&pdata->napi);
1399
1400 temp = smsc911x_reg_read(pdata, INT_EN);
Steve Glendinning1373c0f2009-01-26 21:33:16 -08001401 temp |= (INT_EN_TDFA_EN_ | INT_EN_RSFL_EN_ | INT_EN_RXSTOP_INT_EN_);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001402 smsc911x_reg_write(pdata, INT_EN, temp);
1403
1404 spin_lock_irq(&pdata->mac_lock);
1405 temp = smsc911x_mac_read(pdata, MAC_CR);
1406 temp |= (MAC_CR_TXEN_ | MAC_CR_RXEN_ | MAC_CR_HBDIS_);
1407 smsc911x_mac_write(pdata, MAC_CR, temp);
1408 spin_unlock_irq(&pdata->mac_lock);
1409
1410 smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
1411
1412 netif_start_queue(dev);
1413 return 0;
1414}
1415
1416/* Entry point for stopping the interface */
1417static int smsc911x_stop(struct net_device *dev)
1418{
1419 struct smsc911x_data *pdata = netdev_priv(dev);
1420 unsigned int temp;
1421
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001422 /* Disable all device interrupts */
1423 temp = smsc911x_reg_read(pdata, INT_CFG);
1424 temp &= ~INT_CFG_IRQ_EN_;
1425 smsc911x_reg_write(pdata, INT_CFG, temp);
1426
1427 /* Stop Tx and Rx polling */
1428 netif_stop_queue(dev);
1429 napi_disable(&pdata->napi);
1430
1431 /* At this point all Rx and Tx activity is stopped */
1432 dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
1433 smsc911x_tx_update_txcounters(dev);
1434
1435 /* Bring the PHY down */
Steve Glendinningdd045192008-12-25 16:40:19 -08001436 if (pdata->phy_dev)
1437 phy_stop(pdata->phy_dev);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001438
Joe Perchesdffc6b22011-03-25 14:21:22 +00001439 SMSC_TRACE(pdata, ifdown, "Interface stopped");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001440 return 0;
1441}
1442
1443/* Entry point for transmitting a packet */
1444static int smsc911x_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1445{
1446 struct smsc911x_data *pdata = netdev_priv(dev);
1447 unsigned int freespace;
1448 unsigned int tx_cmd_a;
1449 unsigned int tx_cmd_b;
1450 unsigned int temp;
1451 u32 wrsz;
1452 ulong bufp;
1453
1454 freespace = smsc911x_reg_read(pdata, TX_FIFO_INF) & TX_FIFO_INF_TDFREE_;
1455
1456 if (unlikely(freespace < TX_FIFO_LOW_THRESHOLD))
Joe Perchesdffc6b22011-03-25 14:21:22 +00001457 SMSC_WARN(pdata, tx_err,
1458 "Tx data fifo low, space available: %d", freespace);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001459
1460 /* Word alignment adjustment */
1461 tx_cmd_a = (u32)((ulong)skb->data & 0x03) << 16;
1462 tx_cmd_a |= TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
1463 tx_cmd_a |= (unsigned int)skb->len;
1464
1465 tx_cmd_b = ((unsigned int)skb->len) << 16;
1466 tx_cmd_b |= (unsigned int)skb->len;
1467
1468 smsc911x_reg_write(pdata, TX_DATA_FIFO, tx_cmd_a);
1469 smsc911x_reg_write(pdata, TX_DATA_FIFO, tx_cmd_b);
1470
1471 bufp = (ulong)skb->data & (~0x3);
1472 wrsz = (u32)skb->len + 3;
1473 wrsz += (u32)((ulong)skb->data & 0x3);
1474 wrsz >>= 2;
1475
Mathieu J. Poirierc326de82011-04-13 17:13:00 -07001476 pdata->ops->tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001477 freespace -= (skb->len + 32);
Richard Cochran8c0069a2011-06-19 21:51:30 +00001478 skb_tx_timestamp(skb);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001479 dev_kfree_skb(skb);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001480
1481 if (unlikely(smsc911x_tx_get_txstatcount(pdata) >= 30))
1482 smsc911x_tx_update_txcounters(dev);
1483
1484 if (freespace < TX_FIFO_LOW_THRESHOLD) {
1485 netif_stop_queue(dev);
1486 temp = smsc911x_reg_read(pdata, FIFO_INT);
1487 temp &= 0x00FFFFFF;
1488 temp |= 0x32000000;
1489 smsc911x_reg_write(pdata, FIFO_INT, temp);
1490 }
1491
1492 return NETDEV_TX_OK;
1493}
1494
1495/* Entry point for getting status counters */
1496static struct net_device_stats *smsc911x_get_stats(struct net_device *dev)
1497{
1498 struct smsc911x_data *pdata = netdev_priv(dev);
1499 smsc911x_tx_update_txcounters(dev);
1500 dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
1501 return &dev->stats;
1502}
1503
1504/* Entry point for setting addressing modes */
1505static void smsc911x_set_multicast_list(struct net_device *dev)
1506{
1507 struct smsc911x_data *pdata = netdev_priv(dev);
1508 unsigned long flags;
1509
1510 if (dev->flags & IFF_PROMISC) {
1511 /* Enabling promiscuous mode */
1512 pdata->set_bits_mask = MAC_CR_PRMS_;
1513 pdata->clear_bits_mask = (MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
1514 pdata->hashhi = 0;
1515 pdata->hashlo = 0;
1516 } else if (dev->flags & IFF_ALLMULTI) {
1517 /* Enabling all multicast mode */
1518 pdata->set_bits_mask = MAC_CR_MCPAS_;
1519 pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_HPFILT_);
1520 pdata->hashhi = 0;
1521 pdata->hashlo = 0;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001522 } else if (!netdev_mc_empty(dev)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001523 /* Enabling specific multicast addresses */
1524 unsigned int hash_high = 0;
1525 unsigned int hash_low = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001526 struct netdev_hw_addr *ha;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001527
1528 pdata->set_bits_mask = MAC_CR_HPFILT_;
1529 pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_MCPAS_);
1530
Jiri Pirko22bedad32010-04-01 21:22:57 +00001531 netdev_for_each_mc_addr(ha, dev) {
1532 unsigned int bitnum = smsc911x_hash(ha->addr);
Jiri Pirko2a0d18f2010-02-17 23:22:38 +00001533 unsigned int mask = 0x01 << (bitnum & 0x1F);
1534
1535 if (bitnum & 0x20)
1536 hash_high |= mask;
1537 else
1538 hash_low |= mask;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001539 }
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001540
1541 pdata->hashhi = hash_high;
1542 pdata->hashlo = hash_low;
1543 } else {
1544 /* Enabling local MAC address only */
1545 pdata->set_bits_mask = 0;
1546 pdata->clear_bits_mask =
1547 (MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
1548 pdata->hashhi = 0;
1549 pdata->hashlo = 0;
1550 }
1551
1552 spin_lock_irqsave(&pdata->mac_lock, flags);
1553
1554 if (pdata->generation <= 1) {
1555 /* Older hardware revision - cannot change these flags while
1556 * receiving data */
1557 if (!pdata->multicast_update_pending) {
1558 unsigned int temp;
Joe Perchesdffc6b22011-03-25 14:21:22 +00001559 SMSC_TRACE(pdata, hw, "scheduling mcast update");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001560 pdata->multicast_update_pending = 1;
1561
1562 /* Request the hardware to stop, then perform the
1563 * update when we get an RX_STOP interrupt */
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001564 temp = smsc911x_mac_read(pdata, MAC_CR);
1565 temp &= ~(MAC_CR_RXEN_);
1566 smsc911x_mac_write(pdata, MAC_CR, temp);
1567 } else {
1568 /* There is another update pending, this should now
1569 * use the newer values */
1570 }
1571 } else {
1572 /* Newer hardware revision - can write immediately */
1573 smsc911x_rx_multicast_update(pdata);
1574 }
1575
1576 spin_unlock_irqrestore(&pdata->mac_lock, flags);
1577}
1578
1579static irqreturn_t smsc911x_irqhandler(int irq, void *dev_id)
1580{
1581 struct net_device *dev = dev_id;
1582 struct smsc911x_data *pdata = netdev_priv(dev);
1583 u32 intsts = smsc911x_reg_read(pdata, INT_STS);
1584 u32 inten = smsc911x_reg_read(pdata, INT_EN);
1585 int serviced = IRQ_NONE;
1586 u32 temp;
1587
1588 if (unlikely(intsts & inten & INT_STS_SW_INT_)) {
1589 temp = smsc911x_reg_read(pdata, INT_EN);
1590 temp &= (~INT_EN_SW_INT_EN_);
1591 smsc911x_reg_write(pdata, INT_EN, temp);
1592 smsc911x_reg_write(pdata, INT_STS, INT_STS_SW_INT_);
1593 pdata->software_irq_signal = 1;
1594 smp_wmb();
1595 serviced = IRQ_HANDLED;
1596 }
1597
1598 if (unlikely(intsts & inten & INT_STS_RXSTOP_INT_)) {
1599 /* Called when there is a multicast update scheduled and
1600 * it is now safe to complete the update */
Joe Perchesdffc6b22011-03-25 14:21:22 +00001601 SMSC_TRACE(pdata, intr, "RX Stop interrupt");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001602 smsc911x_reg_write(pdata, INT_STS, INT_STS_RXSTOP_INT_);
Steve Glendinning1373c0f2009-01-26 21:33:16 -08001603 if (pdata->multicast_update_pending)
1604 smsc911x_rx_multicast_update_workaround(pdata);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001605 serviced = IRQ_HANDLED;
1606 }
1607
1608 if (intsts & inten & INT_STS_TDFA_) {
1609 temp = smsc911x_reg_read(pdata, FIFO_INT);
1610 temp |= FIFO_INT_TX_AVAIL_LEVEL_;
1611 smsc911x_reg_write(pdata, FIFO_INT, temp);
1612 smsc911x_reg_write(pdata, INT_STS, INT_STS_TDFA_);
1613 netif_wake_queue(dev);
1614 serviced = IRQ_HANDLED;
1615 }
1616
1617 if (unlikely(intsts & inten & INT_STS_RXE_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001618 SMSC_TRACE(pdata, intr, "RX Error interrupt");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001619 smsc911x_reg_write(pdata, INT_STS, INT_STS_RXE_);
1620 serviced = IRQ_HANDLED;
1621 }
1622
1623 if (likely(intsts & inten & INT_STS_RSFL_)) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001624 if (likely(napi_schedule_prep(&pdata->napi))) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001625 /* Disable Rx interrupts */
1626 temp = smsc911x_reg_read(pdata, INT_EN);
1627 temp &= (~INT_EN_RSFL_EN_);
1628 smsc911x_reg_write(pdata, INT_EN, temp);
1629 /* Schedule a NAPI poll */
Ben Hutchings288379f2009-01-19 16:43:59 -08001630 __napi_schedule(&pdata->napi);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001631 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001632 SMSC_WARN(pdata, rx_err, "napi_schedule_prep failed");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001633 }
1634 serviced = IRQ_HANDLED;
1635 }
1636
1637 return serviced;
1638}
1639
1640#ifdef CONFIG_NET_POLL_CONTROLLER
Steve Glendinning1757ab22008-12-12 22:31:16 -08001641static void smsc911x_poll_controller(struct net_device *dev)
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001642{
1643 disable_irq(dev->irq);
1644 smsc911x_irqhandler(0, dev);
1645 enable_irq(dev->irq);
1646}
1647#endif /* CONFIG_NET_POLL_CONTROLLER */
1648
Steve Glendinning225ddf42009-03-19 00:24:46 +00001649static int smsc911x_set_mac_address(struct net_device *dev, void *p)
1650{
1651 struct smsc911x_data *pdata = netdev_priv(dev);
1652 struct sockaddr *addr = p;
1653
1654 /* On older hardware revisions we cannot change the mac address
1655 * registers while receiving data. Newer devices can safely change
1656 * this at any time. */
1657 if (pdata->generation <= 1 && netif_running(dev))
1658 return -EBUSY;
1659
1660 if (!is_valid_ether_addr(addr->sa_data))
1661 return -EADDRNOTAVAIL;
1662
1663 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
1664
1665 spin_lock_irq(&pdata->mac_lock);
1666 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
1667 spin_unlock_irq(&pdata->mac_lock);
1668
Joe Perchesdffc6b22011-03-25 14:21:22 +00001669 netdev_info(dev, "MAC Address: %pM\n", dev->dev_addr);
Steve Glendinning225ddf42009-03-19 00:24:46 +00001670
1671 return 0;
1672}
1673
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001674/* Standard ioctls for mii-tool */
1675static int smsc911x_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1676{
1677 struct smsc911x_data *pdata = netdev_priv(dev);
1678
1679 if (!netif_running(dev) || !pdata->phy_dev)
1680 return -EINVAL;
1681
Richard Cochran28b04112010-07-17 08:48:55 +00001682 return phy_mii_ioctl(pdata->phy_dev, ifr, cmd);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001683}
1684
1685static int
1686smsc911x_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1687{
1688 struct smsc911x_data *pdata = netdev_priv(dev);
1689
1690 cmd->maxtxpkt = 1;
1691 cmd->maxrxpkt = 1;
1692 return phy_ethtool_gset(pdata->phy_dev, cmd);
1693}
1694
1695static int
1696smsc911x_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1697{
1698 struct smsc911x_data *pdata = netdev_priv(dev);
1699
1700 return phy_ethtool_sset(pdata->phy_dev, cmd);
1701}
1702
1703static void smsc911x_ethtool_getdrvinfo(struct net_device *dev,
1704 struct ethtool_drvinfo *info)
1705{
1706 strlcpy(info->driver, SMSC_CHIPNAME, sizeof(info->driver));
1707 strlcpy(info->version, SMSC_DRV_VERSION, sizeof(info->version));
Kay Sieversdb1d7bf2009-01-26 21:12:58 -08001708 strlcpy(info->bus_info, dev_name(dev->dev.parent),
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001709 sizeof(info->bus_info));
1710}
1711
1712static int smsc911x_ethtool_nwayreset(struct net_device *dev)
1713{
1714 struct smsc911x_data *pdata = netdev_priv(dev);
1715
1716 return phy_start_aneg(pdata->phy_dev);
1717}
1718
1719static u32 smsc911x_ethtool_getmsglevel(struct net_device *dev)
1720{
1721 struct smsc911x_data *pdata = netdev_priv(dev);
1722 return pdata->msg_enable;
1723}
1724
1725static void smsc911x_ethtool_setmsglevel(struct net_device *dev, u32 level)
1726{
1727 struct smsc911x_data *pdata = netdev_priv(dev);
1728 pdata->msg_enable = level;
1729}
1730
1731static int smsc911x_ethtool_getregslen(struct net_device *dev)
1732{
1733 return (((E2P_DATA - ID_REV) / 4 + 1) + (WUCSR - MAC_CR) + 1 + 32) *
1734 sizeof(u32);
1735}
1736
1737static void
1738smsc911x_ethtool_getregs(struct net_device *dev, struct ethtool_regs *regs,
1739 void *buf)
1740{
1741 struct smsc911x_data *pdata = netdev_priv(dev);
1742 struct phy_device *phy_dev = pdata->phy_dev;
1743 unsigned long flags;
1744 unsigned int i;
1745 unsigned int j = 0;
1746 u32 *data = buf;
1747
1748 regs->version = pdata->idrev;
1749 for (i = ID_REV; i <= E2P_DATA; i += (sizeof(u32)))
1750 data[j++] = smsc911x_reg_read(pdata, i);
1751
1752 for (i = MAC_CR; i <= WUCSR; i++) {
1753 spin_lock_irqsave(&pdata->mac_lock, flags);
1754 data[j++] = smsc911x_mac_read(pdata, i);
1755 spin_unlock_irqrestore(&pdata->mac_lock, flags);
1756 }
1757
1758 for (i = 0; i <= 31; i++)
1759 data[j++] = smsc911x_mii_read(phy_dev->bus, phy_dev->addr, i);
1760}
1761
1762static void smsc911x_eeprom_enable_access(struct smsc911x_data *pdata)
1763{
1764 unsigned int temp = smsc911x_reg_read(pdata, GPIO_CFG);
1765 temp &= ~GPIO_CFG_EEPR_EN_;
1766 smsc911x_reg_write(pdata, GPIO_CFG, temp);
1767 msleep(1);
1768}
1769
1770static int smsc911x_eeprom_send_cmd(struct smsc911x_data *pdata, u32 op)
1771{
1772 int timeout = 100;
1773 u32 e2cmd;
1774
Joe Perchesdffc6b22011-03-25 14:21:22 +00001775 SMSC_TRACE(pdata, drv, "op 0x%08x", op);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001776 if (smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001777 SMSC_WARN(pdata, drv, "Busy at start");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001778 return -EBUSY;
1779 }
1780
1781 e2cmd = op | E2P_CMD_EPC_BUSY_;
1782 smsc911x_reg_write(pdata, E2P_CMD, e2cmd);
1783
1784 do {
1785 msleep(1);
1786 e2cmd = smsc911x_reg_read(pdata, E2P_CMD);
Roel Kluin2cf0dbe2009-02-20 00:52:19 -08001787 } while ((e2cmd & E2P_CMD_EPC_BUSY_) && (--timeout));
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001788
1789 if (!timeout) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001790 SMSC_TRACE(pdata, drv, "TIMED OUT");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001791 return -EAGAIN;
1792 }
1793
1794 if (e2cmd & E2P_CMD_EPC_TIMEOUT_) {
David S. Miller1c01a802011-04-11 13:44:25 -07001795 SMSC_TRACE(pdata, drv, "Error occurred during eeprom operation");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001796 return -EINVAL;
1797 }
1798
1799 return 0;
1800}
1801
1802static int smsc911x_eeprom_read_location(struct smsc911x_data *pdata,
1803 u8 address, u8 *data)
1804{
1805 u32 op = E2P_CMD_EPC_CMD_READ_ | address;
1806 int ret;
1807
Joe Perchesdffc6b22011-03-25 14:21:22 +00001808 SMSC_TRACE(pdata, drv, "address 0x%x", address);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001809 ret = smsc911x_eeprom_send_cmd(pdata, op);
1810
1811 if (!ret)
1812 data[address] = smsc911x_reg_read(pdata, E2P_DATA);
1813
1814 return ret;
1815}
1816
1817static int smsc911x_eeprom_write_location(struct smsc911x_data *pdata,
1818 u8 address, u8 data)
1819{
1820 u32 op = E2P_CMD_EPC_CMD_ERASE_ | address;
Steve Glendinning58add9f2009-03-26 07:14:36 +00001821 u32 temp;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001822 int ret;
1823
Joe Perchesdffc6b22011-03-25 14:21:22 +00001824 SMSC_TRACE(pdata, drv, "address 0x%x, data 0x%x", address, data);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001825 ret = smsc911x_eeprom_send_cmd(pdata, op);
1826
1827 if (!ret) {
1828 op = E2P_CMD_EPC_CMD_WRITE_ | address;
1829 smsc911x_reg_write(pdata, E2P_DATA, (u32)data);
Steve Glendinning58add9f2009-03-26 07:14:36 +00001830
1831 /* Workaround for hardware read-after-write restriction */
1832 temp = smsc911x_reg_read(pdata, BYTE_TEST);
1833
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001834 ret = smsc911x_eeprom_send_cmd(pdata, op);
1835 }
1836
1837 return ret;
1838}
1839
1840static int smsc911x_ethtool_get_eeprom_len(struct net_device *dev)
1841{
1842 return SMSC911X_EEPROM_SIZE;
1843}
1844
1845static int smsc911x_ethtool_get_eeprom(struct net_device *dev,
1846 struct ethtool_eeprom *eeprom, u8 *data)
1847{
1848 struct smsc911x_data *pdata = netdev_priv(dev);
1849 u8 eeprom_data[SMSC911X_EEPROM_SIZE];
1850 int len;
1851 int i;
1852
1853 smsc911x_eeprom_enable_access(pdata);
1854
1855 len = min(eeprom->len, SMSC911X_EEPROM_SIZE);
1856 for (i = 0; i < len; i++) {
1857 int ret = smsc911x_eeprom_read_location(pdata, i, eeprom_data);
1858 if (ret < 0) {
1859 eeprom->len = 0;
1860 return ret;
1861 }
1862 }
1863
1864 memcpy(data, &eeprom_data[eeprom->offset], len);
1865 eeprom->len = len;
1866 return 0;
1867}
1868
1869static int smsc911x_ethtool_set_eeprom(struct net_device *dev,
1870 struct ethtool_eeprom *eeprom, u8 *data)
1871{
1872 int ret;
1873 struct smsc911x_data *pdata = netdev_priv(dev);
1874
1875 smsc911x_eeprom_enable_access(pdata);
1876 smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWEN_);
1877 ret = smsc911x_eeprom_write_location(pdata, eeprom->offset, *data);
1878 smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWDS_);
1879
1880 /* Single byte write, according to man page */
1881 eeprom->len = 1;
1882
1883 return ret;
1884}
1885
Steve Glendinningcb5b04f2008-12-25 16:41:09 -08001886static const struct ethtool_ops smsc911x_ethtool_ops = {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001887 .get_settings = smsc911x_ethtool_getsettings,
1888 .set_settings = smsc911x_ethtool_setsettings,
1889 .get_link = ethtool_op_get_link,
1890 .get_drvinfo = smsc911x_ethtool_getdrvinfo,
1891 .nway_reset = smsc911x_ethtool_nwayreset,
1892 .get_msglevel = smsc911x_ethtool_getmsglevel,
1893 .set_msglevel = smsc911x_ethtool_setmsglevel,
1894 .get_regs_len = smsc911x_ethtool_getregslen,
1895 .get_regs = smsc911x_ethtool_getregs,
1896 .get_eeprom_len = smsc911x_ethtool_get_eeprom_len,
1897 .get_eeprom = smsc911x_ethtool_get_eeprom,
1898 .set_eeprom = smsc911x_ethtool_set_eeprom,
1899};
1900
Steve Glendinning631b7562008-12-25 16:40:47 -08001901static const struct net_device_ops smsc911x_netdev_ops = {
1902 .ndo_open = smsc911x_open,
1903 .ndo_stop = smsc911x_stop,
1904 .ndo_start_xmit = smsc911x_hard_start_xmit,
1905 .ndo_get_stats = smsc911x_get_stats,
1906 .ndo_set_multicast_list = smsc911x_set_multicast_list,
1907 .ndo_do_ioctl = smsc911x_do_ioctl,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00001908 .ndo_change_mtu = eth_change_mtu,
Steve Glendinning631b7562008-12-25 16:40:47 -08001909 .ndo_validate_addr = eth_validate_addr,
Steve Glendinning225ddf42009-03-19 00:24:46 +00001910 .ndo_set_mac_address = smsc911x_set_mac_address,
Steve Glendinning631b7562008-12-25 16:40:47 -08001911#ifdef CONFIG_NET_POLL_CONTROLLER
1912 .ndo_poll_controller = smsc911x_poll_controller,
1913#endif
1914};
1915
Steve Glendinning31f45742009-01-27 06:51:12 +00001916/* copies the current mac address from hardware to dev->dev_addr */
1917static void __devinit smsc911x_read_mac_address(struct net_device *dev)
1918{
1919 struct smsc911x_data *pdata = netdev_priv(dev);
1920 u32 mac_high16 = smsc911x_mac_read(pdata, ADDRH);
1921 u32 mac_low32 = smsc911x_mac_read(pdata, ADDRL);
1922
1923 dev->dev_addr[0] = (u8)(mac_low32);
1924 dev->dev_addr[1] = (u8)(mac_low32 >> 8);
1925 dev->dev_addr[2] = (u8)(mac_low32 >> 16);
1926 dev->dev_addr[3] = (u8)(mac_low32 >> 24);
1927 dev->dev_addr[4] = (u8)(mac_high16);
1928 dev->dev_addr[5] = (u8)(mac_high16 >> 8);
1929}
1930
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001931/* Initializing private device structures, only called from probe */
1932static int __devinit smsc911x_init(struct net_device *dev)
1933{
1934 struct smsc911x_data *pdata = netdev_priv(dev);
1935 unsigned int byte_test;
1936
Joe Perchesdffc6b22011-03-25 14:21:22 +00001937 SMSC_TRACE(pdata, probe, "Driver Parameters:");
1938 SMSC_TRACE(pdata, probe, "LAN base: 0x%08lX",
1939 (unsigned long)pdata->ioaddr);
1940 SMSC_TRACE(pdata, probe, "IRQ: %d", dev->irq);
1941 SMSC_TRACE(pdata, probe, "PHY will be autodetected.");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001942
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001943 spin_lock_init(&pdata->dev_lock);
Enric Balletbo i Serra35a67ed2011-04-05 06:52:49 +00001944 spin_lock_init(&pdata->mac_lock);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001945
1946 if (pdata->ioaddr == 0) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001947 SMSC_WARN(pdata, probe, "pdata->ioaddr: 0x00000000");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001948 return -ENODEV;
1949 }
1950
1951 /* Check byte ordering */
1952 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
Joe Perchesdffc6b22011-03-25 14:21:22 +00001953 SMSC_TRACE(pdata, probe, "BYTE_TEST: 0x%08X", byte_test);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001954 if (byte_test == 0x43218765) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001955 SMSC_TRACE(pdata, probe, "BYTE_TEST looks swapped, "
1956 "applying WORD_SWAP");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001957 smsc911x_reg_write(pdata, WORD_SWAP, 0xffffffff);
1958
1959 /* 1 dummy read of BYTE_TEST is needed after a write to
1960 * WORD_SWAP before its contents are valid */
1961 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
1962
1963 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
1964 }
1965
1966 if (byte_test != 0x87654321) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001967 SMSC_WARN(pdata, drv, "BYTE_TEST: 0x%08X", byte_test);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001968 if (((byte_test >> 16) & 0xFFFF) == (byte_test & 0xFFFF)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001969 SMSC_WARN(pdata, probe,
1970 "top 16 bits equal to bottom 16 bits");
1971 SMSC_TRACE(pdata, probe,
1972 "This may mean the chip is set "
1973 "for 32 bit while the bus is reading 16 bit");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001974 }
1975 return -ENODEV;
1976 }
1977
1978 /* Default generation to zero (all workarounds apply) */
1979 pdata->generation = 0;
1980
1981 pdata->idrev = smsc911x_reg_read(pdata, ID_REV);
1982 switch (pdata->idrev & 0xFFFF0000) {
1983 case 0x01180000:
1984 case 0x01170000:
1985 case 0x01160000:
1986 case 0x01150000:
Phil Edworthy28c21372011-10-12 02:29:39 +00001987 case 0x218A0000:
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001988 /* LAN911[5678] family */
1989 pdata->generation = pdata->idrev & 0x0000FFFF;
1990 break;
1991
1992 case 0x118A0000:
1993 case 0x117A0000:
1994 case 0x116A0000:
1995 case 0x115A0000:
1996 /* LAN921[5678] family */
1997 pdata->generation = 3;
1998 break;
1999
2000 case 0x92100000:
2001 case 0x92110000:
2002 case 0x92200000:
2003 case 0x92210000:
2004 /* LAN9210/LAN9211/LAN9220/LAN9221 */
2005 pdata->generation = 4;
2006 break;
2007
2008 default:
Joe Perchesdffc6b22011-03-25 14:21:22 +00002009 SMSC_WARN(pdata, probe, "LAN911x not identified, idrev: 0x%08X",
2010 pdata->idrev);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002011 return -ENODEV;
2012 }
2013
Joe Perchesdffc6b22011-03-25 14:21:22 +00002014 SMSC_TRACE(pdata, probe,
2015 "LAN911x identified, idrev: 0x%08X, generation: %d",
2016 pdata->idrev, pdata->generation);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002017
2018 if (pdata->generation == 0)
Joe Perchesdffc6b22011-03-25 14:21:22 +00002019 SMSC_WARN(pdata, probe,
2020 "This driver is not intended for this chip revision");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002021
Steve Glendinning31f45742009-01-27 06:51:12 +00002022 /* workaround for platforms without an eeprom, where the mac address
2023 * is stored elsewhere and set by the bootloader. This saves the
2024 * mac address before resetting the device */
Enric Balletbo i Serra35a67ed2011-04-05 06:52:49 +00002025 if (pdata->config.flags & SMSC911X_SAVE_MAC_ADDRESS) {
2026 spin_lock_irq(&pdata->mac_lock);
Steve Glendinning31f45742009-01-27 06:51:12 +00002027 smsc911x_read_mac_address(dev);
Enric Balletbo i Serra35a67ed2011-04-05 06:52:49 +00002028 spin_unlock_irq(&pdata->mac_lock);
2029 }
Steve Glendinning31f45742009-01-27 06:51:12 +00002030
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002031 /* Reset the LAN911x */
2032 if (smsc911x_soft_reset(pdata))
2033 return -ENODEV;
2034
2035 /* Disable all interrupt sources until we bring the device up */
2036 smsc911x_reg_write(pdata, INT_EN, 0);
2037
2038 ether_setup(dev);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002039 dev->flags |= IFF_MULTICAST;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002040 netif_napi_add(dev, &pdata->napi, smsc911x_poll, SMSC_NAPI_WEIGHT);
Steve Glendinning631b7562008-12-25 16:40:47 -08002041 dev->netdev_ops = &smsc911x_netdev_ops;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002042 dev->ethtool_ops = &smsc911x_ethtool_ops;
2043
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002044 return 0;
2045}
2046
2047static int __devexit smsc911x_drv_remove(struct platform_device *pdev)
2048{
2049 struct net_device *dev;
2050 struct smsc911x_data *pdata;
2051 struct resource *res;
2052
2053 dev = platform_get_drvdata(pdev);
2054 BUG_ON(!dev);
2055 pdata = netdev_priv(dev);
2056 BUG_ON(!pdata);
2057 BUG_ON(!pdata->ioaddr);
2058 BUG_ON(!pdata->phy_dev);
2059
Joe Perchesdffc6b22011-03-25 14:21:22 +00002060 SMSC_TRACE(pdata, ifdown, "Stopping driver");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002061
2062 phy_disconnect(pdata->phy_dev);
2063 pdata->phy_dev = NULL;
2064 mdiobus_unregister(pdata->mii_bus);
2065 mdiobus_free(pdata->mii_bus);
2066
2067 platform_set_drvdata(pdev, NULL);
2068 unregister_netdev(dev);
2069 free_irq(dev->irq, dev);
2070 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2071 "smsc911x-memory");
2072 if (!res)
Steve Glendinningd4522732008-12-25 16:44:01 -08002073 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002074
Julia Lawall39424532009-07-04 11:31:47 +00002075 release_mem_region(res->start, resource_size(res));
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002076
2077 iounmap(pdata->ioaddr);
2078
2079 free_netdev(dev);
2080
2081 return 0;
2082}
2083
Mathieu J. Poirierc326de82011-04-13 17:13:00 -07002084/* standard register acces */
2085static const struct smsc911x_ops standard_smsc911x_ops = {
2086 .reg_read = __smsc911x_reg_read,
2087 .reg_write = __smsc911x_reg_write,
2088 .rx_readfifo = smsc911x_rx_readfifo,
2089 .tx_writefifo = smsc911x_tx_writefifo,
2090};
2091
2092/* shifted register access */
2093static const struct smsc911x_ops shifted_smsc911x_ops = {
2094 .reg_read = __smsc911x_reg_read_shift,
2095 .reg_write = __smsc911x_reg_write_shift,
2096 .rx_readfifo = smsc911x_rx_readfifo_shift,
2097 .tx_writefifo = smsc911x_tx_writefifo_shift,
2098};
2099
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002100static int __devinit smsc911x_drv_probe(struct platform_device *pdev)
2101{
2102 struct net_device *dev;
2103 struct smsc911x_data *pdata;
Steve Glendinning2107fb82008-11-05 00:35:38 +00002104 struct smsc911x_platform_config *config = pdev->dev.platform_data;
Steve Glendinning61307ed2009-01-27 06:51:09 +00002105 struct resource *res, *irq_res;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002106 unsigned int intcfg = 0;
Steve Glendinning61307ed2009-01-27 06:51:09 +00002107 int res_size, irq_flags;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002108 int retval;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002109
Joe Perchesdffc6b22011-03-25 14:21:22 +00002110 pr_info("Driver version %s\n", SMSC_DRV_VERSION);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002111
Steve Glendinning2107fb82008-11-05 00:35:38 +00002112 /* platform data specifies irq & dynamic bus configuration */
2113 if (!pdev->dev.platform_data) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002114 pr_warn("platform_data not provided\n");
Steve Glendinning2107fb82008-11-05 00:35:38 +00002115 retval = -ENODEV;
2116 goto out_0;
2117 }
2118
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002119 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2120 "smsc911x-memory");
2121 if (!res)
2122 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2123 if (!res) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002124 pr_warn("Could not allocate resource\n");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002125 retval = -ENODEV;
2126 goto out_0;
2127 }
Julia Lawall39424532009-07-04 11:31:47 +00002128 res_size = resource_size(res);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002129
Steve Glendinning61307ed2009-01-27 06:51:09 +00002130 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2131 if (!irq_res) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002132 pr_warn("Could not allocate irq resource\n");
Steve Glendinning61307ed2009-01-27 06:51:09 +00002133 retval = -ENODEV;
2134 goto out_0;
2135 }
2136
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002137 if (!request_mem_region(res->start, res_size, SMSC_CHIPNAME)) {
2138 retval = -EBUSY;
2139 goto out_0;
2140 }
2141
2142 dev = alloc_etherdev(sizeof(struct smsc911x_data));
2143 if (!dev) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002144 pr_warn("Could not allocate device\n");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002145 retval = -ENOMEM;
2146 goto out_release_io_1;
2147 }
2148
2149 SET_NETDEV_DEV(dev, &pdev->dev);
2150
2151 pdata = netdev_priv(dev);
2152
Steve Glendinning61307ed2009-01-27 06:51:09 +00002153 dev->irq = irq_res->start;
2154 irq_flags = irq_res->flags & IRQF_TRIGGER_MASK;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002155 pdata->ioaddr = ioremap_nocache(res->start, res_size);
2156
Steve Glendinning2107fb82008-11-05 00:35:38 +00002157 /* copy config parameters across to pdata */
2158 memcpy(&pdata->config, config, sizeof(pdata->config));
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002159
2160 pdata->dev = dev;
2161 pdata->msg_enable = ((1 << debug) - 1);
2162
2163 if (pdata->ioaddr == NULL) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002164 SMSC_WARN(pdata, probe, "Error smsc911x base address invalid");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002165 retval = -ENOMEM;
2166 goto out_free_netdev_2;
2167 }
2168
Mathieu J. Poirierc326de82011-04-13 17:13:00 -07002169 /* assume standard, non-shifted, access to HW registers */
2170 pdata->ops = &standard_smsc911x_ops;
2171 /* apply the right access if shifting is needed */
2172 if (config->shift)
2173 pdata->ops = &shifted_smsc911x_ops;
2174
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002175 retval = smsc911x_init(dev);
2176 if (retval < 0)
2177 goto out_unmap_io_3;
2178
2179 /* configure irq polarity and type before connecting isr */
Steve Glendinning2107fb82008-11-05 00:35:38 +00002180 if (pdata->config.irq_polarity == SMSC911X_IRQ_POLARITY_ACTIVE_HIGH)
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002181 intcfg |= INT_CFG_IRQ_POL_;
2182
Steve Glendinning2107fb82008-11-05 00:35:38 +00002183 if (pdata->config.irq_type == SMSC911X_IRQ_TYPE_PUSH_PULL)
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002184 intcfg |= INT_CFG_IRQ_TYPE_;
2185
2186 smsc911x_reg_write(pdata, INT_CFG, intcfg);
2187
2188 /* Ensure interrupts are globally disabled before connecting ISR */
2189 smsc911x_reg_write(pdata, INT_EN, 0);
2190 smsc911x_reg_write(pdata, INT_STS, 0xFFFFFFFF);
2191
Steve Glendinning61307ed2009-01-27 06:51:09 +00002192 retval = request_irq(dev->irq, smsc911x_irqhandler,
Steve Glendinninge81259b2009-01-27 06:51:10 +00002193 irq_flags | IRQF_SHARED, dev->name, dev);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002194 if (retval) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002195 SMSC_WARN(pdata, probe,
2196 "Unable to claim requested irq: %d", dev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002197 goto out_unmap_io_3;
2198 }
2199
2200 platform_set_drvdata(pdev, dev);
2201
2202 retval = register_netdev(dev);
2203 if (retval) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002204 SMSC_WARN(pdata, probe, "Error %i registering device", retval);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002205 goto out_unset_drvdata_4;
2206 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002207 SMSC_TRACE(pdata, probe,
2208 "Network interface: \"%s\"", dev->name);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002209 }
2210
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002211 retval = smsc911x_mii_init(pdev, dev);
2212 if (retval) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002213 SMSC_WARN(pdata, probe, "Error %i initialising mii", retval);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002214 goto out_unregister_netdev_5;
2215 }
2216
2217 spin_lock_irq(&pdata->mac_lock);
2218
2219 /* Check if mac address has been specified when bringing interface up */
2220 if (is_valid_ether_addr(dev->dev_addr)) {
Steve Glendinning225ddf42009-03-19 00:24:46 +00002221 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
Joe Perchesdffc6b22011-03-25 14:21:22 +00002222 SMSC_TRACE(pdata, probe,
2223 "MAC Address is specified by configuration");
Manuel Laussaace4952009-10-13 07:25:49 +00002224 } else if (is_valid_ether_addr(pdata->config.mac)) {
2225 memcpy(dev->dev_addr, pdata->config.mac, 6);
Joe Perchesdffc6b22011-03-25 14:21:22 +00002226 SMSC_TRACE(pdata, probe,
2227 "MAC Address specified by platform data");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002228 } else {
2229 /* Try reading mac address from device. if EEPROM is present
2230 * it will already have been set */
Akira Takeuchi62747cd2010-10-27 17:28:58 +01002231 smsc_get_mac(dev);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002232
2233 if (is_valid_ether_addr(dev->dev_addr)) {
2234 /* eeprom values are valid so use them */
Joe Perchesdffc6b22011-03-25 14:21:22 +00002235 SMSC_TRACE(pdata, probe,
2236 "Mac Address is read from LAN911x EEPROM");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002237 } else {
2238 /* eeprom values are invalid, generate random MAC */
2239 random_ether_addr(dev->dev_addr);
Steve Glendinning225ddf42009-03-19 00:24:46 +00002240 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
Joe Perchesdffc6b22011-03-25 14:21:22 +00002241 SMSC_TRACE(pdata, probe,
2242 "MAC Address is set to random_ether_addr");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002243 }
2244 }
2245
2246 spin_unlock_irq(&pdata->mac_lock);
2247
Joe Perchesdffc6b22011-03-25 14:21:22 +00002248 netdev_info(dev, "MAC Address: %pM\n", dev->dev_addr);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002249
2250 return 0;
2251
2252out_unregister_netdev_5:
2253 unregister_netdev(dev);
2254out_unset_drvdata_4:
2255 platform_set_drvdata(pdev, NULL);
2256 free_irq(dev->irq, dev);
2257out_unmap_io_3:
2258 iounmap(pdata->ioaddr);
2259out_free_netdev_2:
2260 free_netdev(dev);
2261out_release_io_1:
Julia Lawall39424532009-07-04 11:31:47 +00002262 release_mem_region(res->start, resource_size(res));
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002263out_0:
2264 return retval;
2265}
2266
Daniel Mackb6907b02009-05-05 12:22:53 -07002267#ifdef CONFIG_PM
2268/* This implementation assumes the devices remains powered on its VDDVARIO
2269 * pins during suspend. */
2270
Daniel Mack6cb87822009-08-05 08:29:31 +00002271/* TODO: implement freeze/thaw callbacks for hibernation.*/
2272
2273static int smsc911x_suspend(struct device *dev)
Daniel Mackb6907b02009-05-05 12:22:53 -07002274{
Daniel Mack6cb87822009-08-05 08:29:31 +00002275 struct net_device *ndev = dev_get_drvdata(dev);
2276 struct smsc911x_data *pdata = netdev_priv(ndev);
Daniel Mackb6907b02009-05-05 12:22:53 -07002277
2278 /* enable wake on LAN, energy detection and the external PME
2279 * signal. */
2280 smsc911x_reg_write(pdata, PMT_CTRL,
2281 PMT_CTRL_PM_MODE_D1_ | PMT_CTRL_WOL_EN_ |
2282 PMT_CTRL_ED_EN_ | PMT_CTRL_PME_EN_);
2283
2284 return 0;
2285}
2286
Daniel Mack6cb87822009-08-05 08:29:31 +00002287static int smsc911x_resume(struct device *dev)
Daniel Mackb6907b02009-05-05 12:22:53 -07002288{
Daniel Mack6cb87822009-08-05 08:29:31 +00002289 struct net_device *ndev = dev_get_drvdata(dev);
2290 struct smsc911x_data *pdata = netdev_priv(ndev);
Daniel Mackb6907b02009-05-05 12:22:53 -07002291 unsigned int to = 100;
2292
2293 /* Note 3.11 from the datasheet:
2294 * "When the LAN9220 is in a power saving state, a write of any
2295 * data to the BYTE_TEST register will wake-up the device."
2296 */
2297 smsc911x_reg_write(pdata, BYTE_TEST, 0);
2298
2299 /* poll the READY bit in PMT_CTRL. Any other access to the device is
2300 * forbidden while this bit isn't set. Try for 100ms and return -EIO
2301 * if it failed. */
2302 while (!(smsc911x_reg_read(pdata, PMT_CTRL) & PMT_CTRL_READY_) && --to)
2303 udelay(1000);
2304
2305 return (to == 0) ? -EIO : 0;
2306}
2307
Alexey Dobriyan47145212009-12-14 18:00:08 -08002308static const struct dev_pm_ops smsc911x_pm_ops = {
Daniel Mack6cb87822009-08-05 08:29:31 +00002309 .suspend = smsc911x_suspend,
2310 .resume = smsc911x_resume,
2311};
2312
2313#define SMSC911X_PM_OPS (&smsc911x_pm_ops)
2314
Daniel Mackb6907b02009-05-05 12:22:53 -07002315#else
Daniel Mack6cb87822009-08-05 08:29:31 +00002316#define SMSC911X_PM_OPS NULL
Daniel Mackb6907b02009-05-05 12:22:53 -07002317#endif
2318
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002319static struct platform_driver smsc911x_driver = {
2320 .probe = smsc911x_drv_probe,
Mike Frysingerdf911e22009-06-05 14:37:20 +00002321 .remove = __devexit_p(smsc911x_drv_remove),
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002322 .driver = {
Daniel Mack6cb87822009-08-05 08:29:31 +00002323 .name = SMSC_CHIPNAME,
2324 .owner = THIS_MODULE,
2325 .pm = SMSC911X_PM_OPS,
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002326 },
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002327};
2328
2329/* Entry point for loading the module */
2330static int __init smsc911x_init_module(void)
2331{
Akira Takeuchi62747cd2010-10-27 17:28:58 +01002332 SMSC_INITIALIZE();
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002333 return platform_driver_register(&smsc911x_driver);
2334}
2335
2336/* entry point for unloading the module */
2337static void __exit smsc911x_cleanup_module(void)
2338{
2339 platform_driver_unregister(&smsc911x_driver);
2340}
2341
2342module_init(smsc911x_init_module);
2343module_exit(smsc911x_cleanup_module);