blob: b0d4c4cd8e06d5994ed5b5419e56b7a3683d70fb [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
David S. Miller1805b2f2011-10-24 18:18:09 -040029 * 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>
Steve Mucklef132c6c2012-06-06 18:30:57 -070047#include <linux/gpio.h>
Robert Marklundc7e963f2011-11-24 01:03:07 +000048#include <linux/regulator/consumer.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000049#include <linux/sched.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000050#include <linux/timer.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000051#include <linux/bug.h>
52#include <linux/bitops.h>
53#include <linux/irq.h>
54#include <linux/io.h>
Magnus Damm833cc672009-04-27 21:32:16 +000055#include <linux/swab.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000056#include <linux/phy.h>
57#include <linux/smsc911x.h>
Daniel Mack6cb87822009-08-05 08:29:31 +000058#include <linux/device.h>
Shawn Guo79f88ee2011-07-30 08:26:00 +000059#include <linux/of.h>
60#include <linux/of_device.h>
61#include <linux/of_gpio.h>
62#include <linux/of_net.h>
Steve Glendinningfd9abb32008-11-05 00:35:37 +000063#include "smsc911x.h"
64
65#define SMSC_CHIPNAME "smsc911x"
66#define SMSC_MDIONAME "smsc911x-mdio"
67#define SMSC_DRV_VERSION "2008-10-21"
68
69MODULE_LICENSE("GPL");
70MODULE_VERSION(SMSC_DRV_VERSION);
Vincent Stehlé62038e42010-09-26 18:50:05 -070071MODULE_ALIAS("platform:smsc911x");
Steve Glendinningfd9abb32008-11-05 00:35:37 +000072
73#if USE_DEBUG > 0
74static int debug = 16;
75#else
76static int debug = 3;
77#endif
78
79module_param(debug, int, 0);
80MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
81
Mathieu J. Poirierc326de82011-04-13 17:13:00 -070082struct smsc911x_data;
83
84struct smsc911x_ops {
85 u32 (*reg_read)(struct smsc911x_data *pdata, u32 reg);
86 void (*reg_write)(struct smsc911x_data *pdata, u32 reg, u32 val);
87 void (*rx_readfifo)(struct smsc911x_data *pdata,
88 unsigned int *buf, unsigned int wordcount);
89 void (*tx_writefifo)(struct smsc911x_data *pdata,
90 unsigned int *buf, unsigned int wordcount);
91};
92
Robert Marklundc7e963f2011-11-24 01:03:07 +000093#define SMSC911X_NUM_SUPPLIES 2
94
Steve Glendinningfd9abb32008-11-05 00:35:37 +000095struct smsc911x_data {
96 void __iomem *ioaddr;
97
98 unsigned int idrev;
99
100 /* used to decide which workarounds apply */
101 unsigned int generation;
102
103 /* device configuration (copied from platform_data during probe) */
Steve Glendinning2107fb82008-11-05 00:35:38 +0000104 struct smsc911x_platform_config config;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000105
106 /* This needs to be acquired before calling any of below:
107 * smsc911x_mac_read(), smsc911x_mac_write()
108 */
109 spinlock_t mac_lock;
110
Catalin Marinas492c5d92010-07-19 13:36:21 -0700111 /* spinlock to ensure register accesses are serialised */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000112 spinlock_t dev_lock;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000113
114 struct phy_device *phy_dev;
115 struct mii_bus *mii_bus;
116 int phy_irq[PHY_MAX_ADDR];
117 unsigned int using_extphy;
118 int last_duplex;
119 int last_carrier;
120
121 u32 msg_enable;
122 unsigned int gpio_setting;
123 unsigned int gpio_orig_setting;
124 struct net_device *dev;
125 struct napi_struct napi;
126
127 unsigned int software_irq_signal;
128
129#ifdef USE_PHY_WORK_AROUND
130#define MIN_PACKET_SIZE (64)
131 char loopback_tx_pkt[MIN_PACKET_SIZE];
132 char loopback_rx_pkt[MIN_PACKET_SIZE];
133 unsigned int resetcount;
134#endif
135
136 /* Members for Multicast filter workaround */
137 unsigned int multicast_update_pending;
138 unsigned int set_bits_mask;
139 unsigned int clear_bits_mask;
140 unsigned int hashhi;
141 unsigned int hashlo;
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700142
143 /* register access functions */
144 const struct smsc911x_ops *ops;
Robert Marklundc7e963f2011-11-24 01:03:07 +0000145
146 /* regulators */
147 struct regulator_bulk_data supplies[SMSC911X_NUM_SUPPLIES];
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000148};
149
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700150/* Easy access to information */
151#define __smsc_shift(pdata, reg) ((reg) << ((pdata)->config.shift))
152
Catalin Marinas492c5d92010-07-19 13:36:21 -0700153static inline u32 __smsc911x_reg_read(struct smsc911x_data *pdata, u32 reg)
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000154{
Steve Glendinning2107fb82008-11-05 00:35:38 +0000155 if (pdata->config.flags & SMSC911X_USE_32BIT)
156 return readl(pdata->ioaddr + reg);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000157
Catalin Marinas492c5d92010-07-19 13:36:21 -0700158 if (pdata->config.flags & SMSC911X_USE_16BIT)
159 return ((readw(pdata->ioaddr + reg) & 0xFFFF) |
Steve Glendinning2107fb82008-11-05 00:35:38 +0000160 ((readw(pdata->ioaddr + reg + 2) & 0xFFFF) << 16));
Steve Glendinning2107fb82008-11-05 00:35:38 +0000161
162 BUG();
Steve Glendinning702403a2009-01-11 00:14:27 -0800163 return 0;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000164}
165
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700166static inline u32
167__smsc911x_reg_read_shift(struct smsc911x_data *pdata, u32 reg)
168{
169 if (pdata->config.flags & SMSC911X_USE_32BIT)
170 return readl(pdata->ioaddr + __smsc_shift(pdata, reg));
171
172 if (pdata->config.flags & SMSC911X_USE_16BIT)
173 return (readw(pdata->ioaddr +
174 __smsc_shift(pdata, reg)) & 0xFFFF) |
175 ((readw(pdata->ioaddr +
176 __smsc_shift(pdata, reg + 2)) & 0xFFFF) << 16);
177
178 BUG();
179 return 0;
180}
181
Catalin Marinas492c5d92010-07-19 13:36:21 -0700182static inline u32 smsc911x_reg_read(struct smsc911x_data *pdata, u32 reg)
183{
184 u32 data;
185 unsigned long flags;
186
187 spin_lock_irqsave(&pdata->dev_lock, flags);
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700188 data = pdata->ops->reg_read(pdata, reg);
Catalin Marinas492c5d92010-07-19 13:36:21 -0700189 spin_unlock_irqrestore(&pdata->dev_lock, flags);
190
191 return data;
192}
193
194static inline void __smsc911x_reg_write(struct smsc911x_data *pdata, u32 reg,
195 u32 val)
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000196{
Steve Glendinning2107fb82008-11-05 00:35:38 +0000197 if (pdata->config.flags & SMSC911X_USE_32BIT) {
198 writel(val, pdata->ioaddr + reg);
199 return;
200 }
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000201
Steve Glendinning2107fb82008-11-05 00:35:38 +0000202 if (pdata->config.flags & SMSC911X_USE_16BIT) {
Steve Glendinning2107fb82008-11-05 00:35:38 +0000203 writew(val & 0xFFFF, pdata->ioaddr + reg);
204 writew((val >> 16) & 0xFFFF, pdata->ioaddr + reg + 2);
Steve Glendinning2107fb82008-11-05 00:35:38 +0000205 return;
206 }
207
208 BUG();
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000209}
210
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700211static inline void
212__smsc911x_reg_write_shift(struct smsc911x_data *pdata, u32 reg, u32 val)
213{
214 if (pdata->config.flags & SMSC911X_USE_32BIT) {
215 writel(val, pdata->ioaddr + __smsc_shift(pdata, reg));
216 return;
217 }
218
219 if (pdata->config.flags & SMSC911X_USE_16BIT) {
220 writew(val & 0xFFFF,
221 pdata->ioaddr + __smsc_shift(pdata, reg));
222 writew((val >> 16) & 0xFFFF,
223 pdata->ioaddr + __smsc_shift(pdata, reg + 2));
224 return;
225 }
226
227 BUG();
228}
229
Catalin Marinas492c5d92010-07-19 13:36:21 -0700230static inline void smsc911x_reg_write(struct smsc911x_data *pdata, u32 reg,
231 u32 val)
232{
233 unsigned long flags;
234
235 spin_lock_irqsave(&pdata->dev_lock, flags);
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700236 pdata->ops->reg_write(pdata, reg, val);
Catalin Marinas492c5d92010-07-19 13:36:21 -0700237 spin_unlock_irqrestore(&pdata->dev_lock, flags);
238}
239
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000240/* Writes a packet to the TX_DATA_FIFO */
241static inline void
242smsc911x_tx_writefifo(struct smsc911x_data *pdata, unsigned int *buf,
243 unsigned int wordcount)
244{
Catalin Marinas492c5d92010-07-19 13:36:21 -0700245 unsigned long flags;
246
247 spin_lock_irqsave(&pdata->dev_lock, flags);
248
Magnus Damm833cc672009-04-27 21:32:16 +0000249 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
250 while (wordcount--)
Catalin Marinas492c5d92010-07-19 13:36:21 -0700251 __smsc911x_reg_write(pdata, TX_DATA_FIFO,
252 swab32(*buf++));
253 goto out;
Magnus Damm833cc672009-04-27 21:32:16 +0000254 }
255
Steve Glendinning2107fb82008-11-05 00:35:38 +0000256 if (pdata->config.flags & SMSC911X_USE_32BIT) {
257 writesl(pdata->ioaddr + TX_DATA_FIFO, buf, wordcount);
Catalin Marinas492c5d92010-07-19 13:36:21 -0700258 goto out;
Steve Glendinning2107fb82008-11-05 00:35:38 +0000259 }
260
261 if (pdata->config.flags & SMSC911X_USE_16BIT) {
262 while (wordcount--)
Catalin Marinas492c5d92010-07-19 13:36:21 -0700263 __smsc911x_reg_write(pdata, TX_DATA_FIFO, *buf++);
264 goto out;
Steve Glendinning2107fb82008-11-05 00:35:38 +0000265 }
266
267 BUG();
Catalin Marinas492c5d92010-07-19 13:36:21 -0700268out:
269 spin_unlock_irqrestore(&pdata->dev_lock, flags);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000270}
271
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700272/* Writes a packet to the TX_DATA_FIFO - shifted version */
273static inline void
274smsc911x_tx_writefifo_shift(struct smsc911x_data *pdata, unsigned int *buf,
275 unsigned int wordcount)
276{
277 unsigned long flags;
278
279 spin_lock_irqsave(&pdata->dev_lock, flags);
280
281 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
282 while (wordcount--)
283 __smsc911x_reg_write_shift(pdata, TX_DATA_FIFO,
284 swab32(*buf++));
285 goto out;
286 }
287
288 if (pdata->config.flags & SMSC911X_USE_32BIT) {
289 writesl(pdata->ioaddr + __smsc_shift(pdata,
290 TX_DATA_FIFO), buf, wordcount);
291 goto out;
292 }
293
294 if (pdata->config.flags & SMSC911X_USE_16BIT) {
295 while (wordcount--)
296 __smsc911x_reg_write_shift(pdata,
297 TX_DATA_FIFO, *buf++);
298 goto out;
299 }
300
301 BUG();
302out:
303 spin_unlock_irqrestore(&pdata->dev_lock, flags);
304}
305
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000306/* Reads a packet out of the RX_DATA_FIFO */
307static inline void
308smsc911x_rx_readfifo(struct smsc911x_data *pdata, unsigned int *buf,
309 unsigned int wordcount)
310{
Catalin Marinas492c5d92010-07-19 13:36:21 -0700311 unsigned long flags;
312
313 spin_lock_irqsave(&pdata->dev_lock, flags);
314
Magnus Damm833cc672009-04-27 21:32:16 +0000315 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
316 while (wordcount--)
Catalin Marinas492c5d92010-07-19 13:36:21 -0700317 *buf++ = swab32(__smsc911x_reg_read(pdata,
318 RX_DATA_FIFO));
319 goto out;
Magnus Damm833cc672009-04-27 21:32:16 +0000320 }
321
Steve Glendinning2107fb82008-11-05 00:35:38 +0000322 if (pdata->config.flags & SMSC911X_USE_32BIT) {
323 readsl(pdata->ioaddr + RX_DATA_FIFO, buf, wordcount);
Catalin Marinas492c5d92010-07-19 13:36:21 -0700324 goto out;
Steve Glendinning2107fb82008-11-05 00:35:38 +0000325 }
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000326
Steve Glendinning2107fb82008-11-05 00:35:38 +0000327 if (pdata->config.flags & SMSC911X_USE_16BIT) {
328 while (wordcount--)
Catalin Marinas492c5d92010-07-19 13:36:21 -0700329 *buf++ = __smsc911x_reg_read(pdata, RX_DATA_FIFO);
330 goto out;
Steve Glendinning2107fb82008-11-05 00:35:38 +0000331 }
332
333 BUG();
Catalin Marinas492c5d92010-07-19 13:36:21 -0700334out:
335 spin_unlock_irqrestore(&pdata->dev_lock, flags);
Steve Glendinning2107fb82008-11-05 00:35:38 +0000336}
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000337
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700338/* Reads a packet out of the RX_DATA_FIFO - shifted version */
339static inline void
340smsc911x_rx_readfifo_shift(struct smsc911x_data *pdata, unsigned int *buf,
341 unsigned int wordcount)
342{
343 unsigned long flags;
344
345 spin_lock_irqsave(&pdata->dev_lock, flags);
346
347 if (pdata->config.flags & SMSC911X_SWAP_FIFO) {
348 while (wordcount--)
349 *buf++ = swab32(__smsc911x_reg_read_shift(pdata,
350 RX_DATA_FIFO));
351 goto out;
352 }
353
354 if (pdata->config.flags & SMSC911X_USE_32BIT) {
355 readsl(pdata->ioaddr + __smsc_shift(pdata,
356 RX_DATA_FIFO), buf, wordcount);
357 goto out;
358 }
359
360 if (pdata->config.flags & SMSC911X_USE_16BIT) {
361 while (wordcount--)
362 *buf++ = __smsc911x_reg_read_shift(pdata,
363 RX_DATA_FIFO);
364 goto out;
365 }
366
367 BUG();
368out:
369 spin_unlock_irqrestore(&pdata->dev_lock, flags);
370}
371
Robert Marklundc7e963f2011-11-24 01:03:07 +0000372/*
373 * enable resources, currently just regulators.
374 */
375static int smsc911x_enable_resources(struct platform_device *pdev)
376{
377 struct net_device *ndev = platform_get_drvdata(pdev);
378 struct smsc911x_data *pdata = netdev_priv(ndev);
379 int ret = 0;
380
381 ret = regulator_bulk_enable(ARRAY_SIZE(pdata->supplies),
382 pdata->supplies);
383 if (ret)
384 netdev_err(ndev, "failed to enable regulators %d\n",
385 ret);
386 return ret;
387}
388
389/*
390 * disable resources, currently just regulators.
391 */
392static int smsc911x_disable_resources(struct platform_device *pdev)
393{
394 struct net_device *ndev = platform_get_drvdata(pdev);
395 struct smsc911x_data *pdata = netdev_priv(ndev);
396 int ret = 0;
397
398 ret = regulator_bulk_disable(ARRAY_SIZE(pdata->supplies),
399 pdata->supplies);
400 return ret;
401}
402
403/*
404 * Request resources, currently just regulators.
405 *
406 * The SMSC911x has two power pins: vddvario and vdd33a, in designs where
407 * these are not always-on we need to request regulators to be turned on
408 * before we can try to access the device registers.
409 */
410static int smsc911x_request_resources(struct platform_device *pdev)
411{
412 struct net_device *ndev = platform_get_drvdata(pdev);
413 struct smsc911x_data *pdata = netdev_priv(ndev);
414 int ret = 0;
415
416 /* Request regulators */
417 pdata->supplies[0].supply = "vdd33a";
418 pdata->supplies[1].supply = "vddvario";
419 ret = regulator_bulk_get(&pdev->dev,
420 ARRAY_SIZE(pdata->supplies),
421 pdata->supplies);
422 if (ret)
423 netdev_err(ndev, "couldn't get regulators %d\n",
424 ret);
425 return ret;
426}
427
428/*
429 * Free resources, currently just regulators.
430 *
431 */
432static void smsc911x_free_resources(struct platform_device *pdev)
433{
434 struct net_device *ndev = platform_get_drvdata(pdev);
435 struct smsc911x_data *pdata = netdev_priv(ndev);
436
437 /* Free regulators */
438 regulator_bulk_free(ARRAY_SIZE(pdata->supplies),
439 pdata->supplies);
440}
441
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000442/* waits for MAC not busy, with timeout. Only called by smsc911x_mac_read
443 * and smsc911x_mac_write, so assumes mac_lock is held */
444static int smsc911x_mac_complete(struct smsc911x_data *pdata)
445{
446 int i;
447 u32 val;
448
449 SMSC_ASSERT_MAC_LOCK(pdata);
450
451 for (i = 0; i < 40; i++) {
452 val = smsc911x_reg_read(pdata, MAC_CSR_CMD);
453 if (!(val & MAC_CSR_CMD_CSR_BUSY_))
454 return 0;
455 }
Joe Perchesdffc6b22011-03-25 14:21:22 +0000456 SMSC_WARN(pdata, hw, "Timed out waiting for MAC not BUSY. "
457 "MAC_CSR_CMD: 0x%08X", val);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000458 return -EIO;
459}
460
461/* Fetches a MAC register value. Assumes mac_lock is acquired */
462static u32 smsc911x_mac_read(struct smsc911x_data *pdata, unsigned int offset)
463{
464 unsigned int temp;
465
466 SMSC_ASSERT_MAC_LOCK(pdata);
467
468 temp = smsc911x_reg_read(pdata, MAC_CSR_CMD);
469 if (unlikely(temp & MAC_CSR_CMD_CSR_BUSY_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000470 SMSC_WARN(pdata, hw, "MAC busy at entry");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000471 return 0xFFFFFFFF;
472 }
473
474 /* Send the MAC cmd */
475 smsc911x_reg_write(pdata, MAC_CSR_CMD, ((offset & 0xFF) |
476 MAC_CSR_CMD_CSR_BUSY_ | MAC_CSR_CMD_R_NOT_W_));
477
478 /* Workaround for hardware read-after-write restriction */
479 temp = smsc911x_reg_read(pdata, BYTE_TEST);
480
481 /* Wait for the read to complete */
482 if (likely(smsc911x_mac_complete(pdata) == 0))
483 return smsc911x_reg_read(pdata, MAC_CSR_DATA);
484
Joe Perchesdffc6b22011-03-25 14:21:22 +0000485 SMSC_WARN(pdata, hw, "MAC busy after read");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000486 return 0xFFFFFFFF;
487}
488
489/* Set a mac register, mac_lock must be acquired before calling */
490static void smsc911x_mac_write(struct smsc911x_data *pdata,
491 unsigned int offset, u32 val)
492{
493 unsigned int temp;
494
495 SMSC_ASSERT_MAC_LOCK(pdata);
496
497 temp = smsc911x_reg_read(pdata, MAC_CSR_CMD);
498 if (unlikely(temp & MAC_CSR_CMD_CSR_BUSY_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000499 SMSC_WARN(pdata, hw,
500 "smsc911x_mac_write failed, MAC busy at entry");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000501 return;
502 }
503
504 /* Send data to write */
505 smsc911x_reg_write(pdata, MAC_CSR_DATA, val);
506
507 /* Write the actual data */
508 smsc911x_reg_write(pdata, MAC_CSR_CMD, ((offset & 0xFF) |
509 MAC_CSR_CMD_CSR_BUSY_));
510
511 /* Workaround for hardware read-after-write restriction */
512 temp = smsc911x_reg_read(pdata, BYTE_TEST);
513
514 /* Wait for the write to complete */
515 if (likely(smsc911x_mac_complete(pdata) == 0))
516 return;
517
Joe Perchesdffc6b22011-03-25 14:21:22 +0000518 SMSC_WARN(pdata, hw, "smsc911x_mac_write failed, MAC busy after write");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000519}
520
521/* Get a phy register */
522static int smsc911x_mii_read(struct mii_bus *bus, int phyaddr, int regidx)
523{
524 struct smsc911x_data *pdata = (struct smsc911x_data *)bus->priv;
525 unsigned long flags;
526 unsigned int addr;
527 int i, reg;
528
529 spin_lock_irqsave(&pdata->mac_lock, flags);
530
531 /* Confirm MII not busy */
532 if (unlikely(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000533 SMSC_WARN(pdata, hw, "MII is busy in smsc911x_mii_read???");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000534 reg = -EIO;
535 goto out;
536 }
537
538 /* Set the address, index & direction (read from PHY) */
539 addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6);
540 smsc911x_mac_write(pdata, MII_ACC, addr);
541
542 /* Wait for read to complete w/ timeout */
543 for (i = 0; i < 100; i++)
544 if (!(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
545 reg = smsc911x_mac_read(pdata, MII_DATA);
546 goto out;
547 }
548
Joe Perchesdffc6b22011-03-25 14:21:22 +0000549 SMSC_WARN(pdata, hw, "Timed out waiting for MII read to finish");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000550 reg = -EIO;
551
552out:
553 spin_unlock_irqrestore(&pdata->mac_lock, flags);
554 return reg;
555}
556
557/* Set a phy register */
558static int smsc911x_mii_write(struct mii_bus *bus, int phyaddr, int regidx,
559 u16 val)
560{
561 struct smsc911x_data *pdata = (struct smsc911x_data *)bus->priv;
562 unsigned long flags;
563 unsigned int addr;
564 int i, reg;
565
566 spin_lock_irqsave(&pdata->mac_lock, flags);
567
568 /* Confirm MII not busy */
569 if (unlikely(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000570 SMSC_WARN(pdata, hw, "MII is busy in smsc911x_mii_write???");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000571 reg = -EIO;
572 goto out;
573 }
574
575 /* Put the data to write in the MAC */
576 smsc911x_mac_write(pdata, MII_DATA, val);
577
578 /* Set the address, index & direction (write to PHY) */
579 addr = ((phyaddr & 0x1F) << 11) | ((regidx & 0x1F) << 6) |
580 MII_ACC_MII_WRITE_;
581 smsc911x_mac_write(pdata, MII_ACC, addr);
582
583 /* Wait for write to complete w/ timeout */
584 for (i = 0; i < 100; i++)
585 if (!(smsc911x_mac_read(pdata, MII_ACC) & MII_ACC_MII_BUSY_)) {
586 reg = 0;
587 goto out;
588 }
589
Joe Perchesdffc6b22011-03-25 14:21:22 +0000590 SMSC_WARN(pdata, hw, "Timed out waiting for MII write to finish");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000591 reg = -EIO;
592
593out:
594 spin_unlock_irqrestore(&pdata->mac_lock, flags);
595 return reg;
596}
597
Steve Glendinningd23f0282009-01-27 06:51:11 +0000598/* Switch to external phy. Assumes tx and rx are stopped. */
599static void smsc911x_phy_enable_external(struct smsc911x_data *pdata)
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000600{
601 unsigned int hwcfg = smsc911x_reg_read(pdata, HW_CFG);
602
Steve Glendinningd23f0282009-01-27 06:51:11 +0000603 /* Disable phy clocks to the MAC */
604 hwcfg &= (~HW_CFG_PHY_CLK_SEL_);
605 hwcfg |= HW_CFG_PHY_CLK_SEL_CLK_DIS_;
606 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
607 udelay(10); /* Enough time for clocks to stop */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000608
Steve Glendinningd23f0282009-01-27 06:51:11 +0000609 /* Switch to external phy */
610 hwcfg |= HW_CFG_EXT_PHY_EN_;
611 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000612
Steve Glendinningd23f0282009-01-27 06:51:11 +0000613 /* Enable phy clocks to the MAC */
614 hwcfg &= (~HW_CFG_PHY_CLK_SEL_);
615 hwcfg |= HW_CFG_PHY_CLK_SEL_EXT_PHY_;
616 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
617 udelay(10); /* Enough time for clocks to restart */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000618
Steve Glendinningd23f0282009-01-27 06:51:11 +0000619 hwcfg |= HW_CFG_SMI_SEL_;
620 smsc911x_reg_write(pdata, HW_CFG, hwcfg);
621}
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000622
Steve Glendinningd23f0282009-01-27 06:51:11 +0000623/* Autodetects and enables external phy if present on supported chips.
624 * autodetection can be overridden by specifying SMSC911X_FORCE_INTERNAL_PHY
625 * or SMSC911X_FORCE_EXTERNAL_PHY in the platform_data flags. */
626static void smsc911x_phy_initialise_external(struct smsc911x_data *pdata)
627{
628 unsigned int hwcfg = smsc911x_reg_read(pdata, HW_CFG);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000629
Steve Glendinningd23f0282009-01-27 06:51:11 +0000630 if (pdata->config.flags & SMSC911X_FORCE_INTERNAL_PHY) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000631 SMSC_TRACE(pdata, hw, "Forcing internal PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +0000632 pdata->using_extphy = 0;
633 } else if (pdata->config.flags & SMSC911X_FORCE_EXTERNAL_PHY) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000634 SMSC_TRACE(pdata, hw, "Forcing external PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +0000635 smsc911x_phy_enable_external(pdata);
636 pdata->using_extphy = 1;
637 } else if (hwcfg & HW_CFG_EXT_PHY_DET_) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000638 SMSC_TRACE(pdata, hw,
639 "HW_CFG EXT_PHY_DET set, using external PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +0000640 smsc911x_phy_enable_external(pdata);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000641 pdata->using_extphy = 1;
642 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000643 SMSC_TRACE(pdata, hw,
644 "HW_CFG EXT_PHY_DET clear, using internal PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +0000645 pdata->using_extphy = 0;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000646 }
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000647}
648
649/* Fetches a tx status out of the status fifo */
650static unsigned int smsc911x_tx_get_txstatus(struct smsc911x_data *pdata)
651{
652 unsigned int result =
653 smsc911x_reg_read(pdata, TX_FIFO_INF) & TX_FIFO_INF_TSUSED_;
654
655 if (result != 0)
656 result = smsc911x_reg_read(pdata, TX_STATUS_FIFO);
657
658 return result;
659}
660
661/* Fetches the next rx status */
662static unsigned int smsc911x_rx_get_rxstatus(struct smsc911x_data *pdata)
663{
664 unsigned int result =
665 smsc911x_reg_read(pdata, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED_;
666
667 if (result != 0)
668 result = smsc911x_reg_read(pdata, RX_STATUS_FIFO);
669
670 return result;
671}
672
673#ifdef USE_PHY_WORK_AROUND
674static int smsc911x_phy_check_loopbackpkt(struct smsc911x_data *pdata)
675{
676 unsigned int tries;
677 u32 wrsz;
678 u32 rdsz;
679 ulong bufp;
680
681 for (tries = 0; tries < 10; tries++) {
682 unsigned int txcmd_a;
683 unsigned int txcmd_b;
684 unsigned int status;
685 unsigned int pktlength;
686 unsigned int i;
687
688 /* Zero-out rx packet memory */
689 memset(pdata->loopback_rx_pkt, 0, MIN_PACKET_SIZE);
690
691 /* Write tx packet to 118 */
692 txcmd_a = (u32)((ulong)pdata->loopback_tx_pkt & 0x03) << 16;
693 txcmd_a |= TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
694 txcmd_a |= MIN_PACKET_SIZE;
695
696 txcmd_b = MIN_PACKET_SIZE << 16 | MIN_PACKET_SIZE;
697
698 smsc911x_reg_write(pdata, TX_DATA_FIFO, txcmd_a);
699 smsc911x_reg_write(pdata, TX_DATA_FIFO, txcmd_b);
700
701 bufp = (ulong)pdata->loopback_tx_pkt & (~0x3);
702 wrsz = MIN_PACKET_SIZE + 3;
703 wrsz += (u32)((ulong)pdata->loopback_tx_pkt & 0x3);
704 wrsz >>= 2;
705
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700706 pdata->ops->tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000707
708 /* Wait till transmit is done */
709 i = 60;
710 do {
711 udelay(5);
712 status = smsc911x_tx_get_txstatus(pdata);
713 } while ((i--) && (!status));
714
715 if (!status) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000716 SMSC_WARN(pdata, hw,
717 "Failed to transmit during loopback test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000718 continue;
719 }
720 if (status & TX_STS_ES_) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000721 SMSC_WARN(pdata, hw,
722 "Transmit encountered errors during loopback test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000723 continue;
724 }
725
726 /* Wait till receive is done */
727 i = 60;
728 do {
729 udelay(5);
730 status = smsc911x_rx_get_rxstatus(pdata);
731 } while ((i--) && (!status));
732
733 if (!status) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000734 SMSC_WARN(pdata, hw,
735 "Failed to receive during loopback test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000736 continue;
737 }
738 if (status & RX_STS_ES_) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000739 SMSC_WARN(pdata, hw,
740 "Receive encountered errors during loopback test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000741 continue;
742 }
743
744 pktlength = ((status & 0x3FFF0000UL) >> 16);
745 bufp = (ulong)pdata->loopback_rx_pkt;
746 rdsz = pktlength + 3;
747 rdsz += (u32)((ulong)pdata->loopback_rx_pkt & 0x3);
748 rdsz >>= 2;
749
Mathieu J. Poirierc326de82011-04-13 17:13:00 -0700750 pdata->ops->rx_readfifo(pdata, (unsigned int *)bufp, rdsz);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000751
752 if (pktlength != (MIN_PACKET_SIZE + 4)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000753 SMSC_WARN(pdata, hw, "Unexpected packet size "
754 "during loop back test, size=%d, will retry",
755 pktlength);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000756 } else {
757 unsigned int j;
758 int mismatch = 0;
759 for (j = 0; j < MIN_PACKET_SIZE; j++) {
760 if (pdata->loopback_tx_pkt[j]
761 != pdata->loopback_rx_pkt[j]) {
762 mismatch = 1;
763 break;
764 }
765 }
766 if (!mismatch) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000767 SMSC_TRACE(pdata, hw, "Successfully verified "
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000768 "loopback packet");
769 return 0;
770 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000771 SMSC_WARN(pdata, hw, "Data mismatch "
772 "during loop back test, will retry");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000773 }
774 }
775 }
776
777 return -EIO;
778}
779
780static int smsc911x_phy_reset(struct smsc911x_data *pdata)
781{
782 struct phy_device *phy_dev = pdata->phy_dev;
783 unsigned int temp;
784 unsigned int i = 100000;
785
786 BUG_ON(!phy_dev);
787 BUG_ON(!phy_dev->bus);
788
Joe Perchesdffc6b22011-03-25 14:21:22 +0000789 SMSC_TRACE(pdata, hw, "Performing PHY BCR Reset");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000790 smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR, BMCR_RESET);
791 do {
792 msleep(1);
793 temp = smsc911x_mii_read(phy_dev->bus, phy_dev->addr,
794 MII_BMCR);
795 } while ((i--) && (temp & BMCR_RESET));
796
797 if (temp & BMCR_RESET) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000798 SMSC_WARN(pdata, hw, "PHY reset failed to complete");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000799 return -EIO;
800 }
801 /* Extra delay required because the phy may not be completed with
802 * its reset when BMCR_RESET is cleared. Specs say 256 uS is
803 * enough delay but using 1ms here to be safe */
804 msleep(1);
805
806 return 0;
807}
808
809static int smsc911x_phy_loopbacktest(struct net_device *dev)
810{
811 struct smsc911x_data *pdata = netdev_priv(dev);
812 struct phy_device *phy_dev = pdata->phy_dev;
813 int result = -EIO;
814 unsigned int i, val;
815 unsigned long flags;
816
817 /* Initialise tx packet using broadcast destination address */
818 memset(pdata->loopback_tx_pkt, 0xff, ETH_ALEN);
819
820 /* Use incrementing source address */
821 for (i = 6; i < 12; i++)
822 pdata->loopback_tx_pkt[i] = (char)i;
823
824 /* Set length type field */
825 pdata->loopback_tx_pkt[12] = 0x00;
826 pdata->loopback_tx_pkt[13] = 0x00;
827
828 for (i = 14; i < MIN_PACKET_SIZE; i++)
829 pdata->loopback_tx_pkt[i] = (char)i;
830
831 val = smsc911x_reg_read(pdata, HW_CFG);
832 val &= HW_CFG_TX_FIF_SZ_;
833 val |= HW_CFG_SF_;
834 smsc911x_reg_write(pdata, HW_CFG, val);
835
836 smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
837 smsc911x_reg_write(pdata, RX_CFG,
838 (u32)((ulong)pdata->loopback_rx_pkt & 0x03) << 8);
839
840 for (i = 0; i < 10; i++) {
841 /* Set PHY to 10/FD, no ANEG, and loopback mode */
842 smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR,
843 BMCR_LOOPBACK | BMCR_FULLDPLX);
844
845 /* Enable MAC tx/rx, FD */
846 spin_lock_irqsave(&pdata->mac_lock, flags);
847 smsc911x_mac_write(pdata, MAC_CR, MAC_CR_FDPX_
848 | MAC_CR_TXEN_ | MAC_CR_RXEN_);
849 spin_unlock_irqrestore(&pdata->mac_lock, flags);
850
851 if (smsc911x_phy_check_loopbackpkt(pdata) == 0) {
852 result = 0;
853 break;
854 }
855 pdata->resetcount++;
856
857 /* Disable MAC rx */
858 spin_lock_irqsave(&pdata->mac_lock, flags);
859 smsc911x_mac_write(pdata, MAC_CR, 0);
860 spin_unlock_irqrestore(&pdata->mac_lock, flags);
861
862 smsc911x_phy_reset(pdata);
863 }
864
865 /* Disable MAC */
866 spin_lock_irqsave(&pdata->mac_lock, flags);
867 smsc911x_mac_write(pdata, MAC_CR, 0);
868 spin_unlock_irqrestore(&pdata->mac_lock, flags);
869
870 /* Cancel PHY loopback mode */
871 smsc911x_mii_write(phy_dev->bus, phy_dev->addr, MII_BMCR, 0);
872
873 smsc911x_reg_write(pdata, TX_CFG, 0);
874 smsc911x_reg_write(pdata, RX_CFG, 0);
875
876 return result;
877}
878#endif /* USE_PHY_WORK_AROUND */
879
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000880static void smsc911x_phy_update_flowcontrol(struct smsc911x_data *pdata)
881{
882 struct phy_device *phy_dev = pdata->phy_dev;
883 u32 afc = smsc911x_reg_read(pdata, AFC_CFG);
884 u32 flow;
885 unsigned long flags;
886
887 if (phy_dev->duplex == DUPLEX_FULL) {
888 u16 lcladv = phy_read(phy_dev, MII_ADVERTISE);
889 u16 rmtadv = phy_read(phy_dev, MII_LPA);
Steve Glendinningbc02ff92008-12-16 02:00:48 -0800890 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000891
892 if (cap & FLOW_CTRL_RX)
893 flow = 0xFFFF0002;
894 else
895 flow = 0;
896
897 if (cap & FLOW_CTRL_TX)
898 afc |= 0xF;
899 else
900 afc &= ~0xF;
901
Joe Perchesdffc6b22011-03-25 14:21:22 +0000902 SMSC_TRACE(pdata, hw, "rx pause %s, tx pause %s",
903 (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
904 (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000905 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000906 SMSC_TRACE(pdata, hw, "half duplex");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000907 flow = 0;
908 afc |= 0xF;
909 }
910
911 spin_lock_irqsave(&pdata->mac_lock, flags);
912 smsc911x_mac_write(pdata, FLOW, flow);
913 spin_unlock_irqrestore(&pdata->mac_lock, flags);
914
915 smsc911x_reg_write(pdata, AFC_CFG, afc);
916}
917
918/* Update link mode if anything has changed. Called periodically when the
919 * PHY is in polling mode, even if nothing has changed. */
920static void smsc911x_phy_adjust_link(struct net_device *dev)
921{
922 struct smsc911x_data *pdata = netdev_priv(dev);
923 struct phy_device *phy_dev = pdata->phy_dev;
924 unsigned long flags;
925 int carrier;
926
927 if (phy_dev->duplex != pdata->last_duplex) {
928 unsigned int mac_cr;
Joe Perchesdffc6b22011-03-25 14:21:22 +0000929 SMSC_TRACE(pdata, hw, "duplex state has changed");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000930
931 spin_lock_irqsave(&pdata->mac_lock, flags);
932 mac_cr = smsc911x_mac_read(pdata, MAC_CR);
933 if (phy_dev->duplex) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000934 SMSC_TRACE(pdata, hw,
935 "configuring for full duplex mode");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000936 mac_cr |= MAC_CR_FDPX_;
937 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000938 SMSC_TRACE(pdata, hw,
939 "configuring for half duplex mode");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000940 mac_cr &= ~MAC_CR_FDPX_;
941 }
942 smsc911x_mac_write(pdata, MAC_CR, mac_cr);
943 spin_unlock_irqrestore(&pdata->mac_lock, flags);
944
945 smsc911x_phy_update_flowcontrol(pdata);
946 pdata->last_duplex = phy_dev->duplex;
947 }
948
949 carrier = netif_carrier_ok(dev);
950 if (carrier != pdata->last_carrier) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000951 SMSC_TRACE(pdata, hw, "carrier state has changed");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000952 if (carrier) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000953 SMSC_TRACE(pdata, hw, "configuring for carrier OK");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000954 if ((pdata->gpio_orig_setting & GPIO_CFG_LED1_EN_) &&
955 (!pdata->using_extphy)) {
Thomas Weber88393162010-03-16 11:47:56 +0100956 /* Restore original GPIO configuration */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000957 pdata->gpio_setting = pdata->gpio_orig_setting;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700958 smsc911x_reg_write(pdata, SMSC_GPIO_CFG,
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000959 pdata->gpio_setting);
960 }
961 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000962 SMSC_TRACE(pdata, hw, "configuring for no carrier");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000963 /* Check global setting that LED1
964 * usage is 10/100 indicator */
965 pdata->gpio_setting = smsc911x_reg_read(pdata,
Steve Mucklef132c6c2012-06-06 18:30:57 -0700966 SMSC_GPIO_CFG);
Joe Perches8e95a202009-12-03 07:58:21 +0000967 if ((pdata->gpio_setting & GPIO_CFG_LED1_EN_) &&
968 (!pdata->using_extphy)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000969 /* Force 10/100 LED off, after saving
Thomas Weber88393162010-03-16 11:47:56 +0100970 * original GPIO configuration */
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000971 pdata->gpio_orig_setting = pdata->gpio_setting;
972
973 pdata->gpio_setting &= ~GPIO_CFG_LED1_EN_;
974 pdata->gpio_setting |= (GPIO_CFG_GPIOBUF0_
975 | GPIO_CFG_GPIODIR0_
976 | GPIO_CFG_GPIOD0_);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700977 smsc911x_reg_write(pdata, SMSC_GPIO_CFG,
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000978 pdata->gpio_setting);
979 }
980 }
981 pdata->last_carrier = carrier;
982 }
983}
984
985static int smsc911x_mii_probe(struct net_device *dev)
986{
987 struct smsc911x_data *pdata = netdev_priv(dev);
988 struct phy_device *phydev = NULL;
kirjanov@gmail.come4a474f2010-02-16 21:54:58 +0000989 int ret;
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000990
991 /* find the first phy */
kirjanov@gmail.come4a474f2010-02-16 21:54:58 +0000992 phydev = phy_find_first(pdata->mii_bus);
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000993 if (!phydev) {
Joe Perchesdffc6b22011-03-25 14:21:22 +0000994 netdev_err(dev, "no PHY found\n");
Steve Glendinningfd9abb32008-11-05 00:35:37 +0000995 return -ENODEV;
996 }
997
Joe Perchesdffc6b22011-03-25 14:21:22 +0000998 SMSC_TRACE(pdata, probe, "PHY: addr %d, phy_id 0x%08X",
999 phydev->addr, phydev->phy_id);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001000
kirjanov@gmail.come4a474f2010-02-16 21:54:58 +00001001 ret = phy_connect_direct(dev, phydev,
1002 &smsc911x_phy_adjust_link, 0,
1003 pdata->config.phy_interface);
1004
1005 if (ret) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001006 netdev_err(dev, "Could not attach to PHY\n");
kirjanov@gmail.come4a474f2010-02-16 21:54:58 +00001007 return ret;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001008 }
1009
Joe Perchesdffc6b22011-03-25 14:21:22 +00001010 netdev_info(dev,
1011 "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1012 phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001013
1014 /* mask with MAC supported features */
1015 phydev->supported &= (PHY_BASIC_FEATURES | SUPPORTED_Pause |
1016 SUPPORTED_Asym_Pause);
1017 phydev->advertising = phydev->supported;
1018
1019 pdata->phy_dev = phydev;
1020 pdata->last_duplex = -1;
1021 pdata->last_carrier = -1;
1022
1023#ifdef USE_PHY_WORK_AROUND
1024 if (smsc911x_phy_loopbacktest(dev) < 0) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001025 SMSC_WARN(pdata, hw, "Failed Loop Back Test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001026 return -ENODEV;
1027 }
Joe Perchesdffc6b22011-03-25 14:21:22 +00001028 SMSC_TRACE(pdata, hw, "Passed Loop Back Test");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001029#endif /* USE_PHY_WORK_AROUND */
1030
Joe Perchesdffc6b22011-03-25 14:21:22 +00001031 SMSC_TRACE(pdata, hw, "phy initialised successfully");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001032 return 0;
1033}
1034
1035static int __devinit smsc911x_mii_init(struct platform_device *pdev,
1036 struct net_device *dev)
1037{
1038 struct smsc911x_data *pdata = netdev_priv(dev);
1039 int err = -ENXIO, i;
1040
1041 pdata->mii_bus = mdiobus_alloc();
1042 if (!pdata->mii_bus) {
1043 err = -ENOMEM;
1044 goto err_out_1;
1045 }
1046
1047 pdata->mii_bus->name = SMSC_MDIONAME;
Florian Fainelli09ef0782012-01-09 23:59:19 +00001048 snprintf(pdata->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1049 pdev->name, pdev->id);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001050 pdata->mii_bus->priv = pdata;
1051 pdata->mii_bus->read = smsc911x_mii_read;
1052 pdata->mii_bus->write = smsc911x_mii_write;
1053 pdata->mii_bus->irq = pdata->phy_irq;
1054 for (i = 0; i < PHY_MAX_ADDR; ++i)
1055 pdata->mii_bus->irq[i] = PHY_POLL;
1056
1057 pdata->mii_bus->parent = &pdev->dev;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001058
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001059 switch (pdata->idrev & 0xFFFF0000) {
1060 case 0x01170000:
1061 case 0x01150000:
1062 case 0x117A0000:
1063 case 0x115A0000:
1064 /* External PHY supported, try to autodetect */
Steve Glendinningd23f0282009-01-27 06:51:11 +00001065 smsc911x_phy_initialise_external(pdata);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001066 break;
1067 default:
Joe Perchesdffc6b22011-03-25 14:21:22 +00001068 SMSC_TRACE(pdata, hw, "External PHY is not supported, "
1069 "using internal PHY");
Steve Glendinningd23f0282009-01-27 06:51:11 +00001070 pdata->using_extphy = 0;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001071 break;
1072 }
1073
1074 if (!pdata->using_extphy) {
1075 /* Mask all PHYs except ID 1 (internal) */
1076 pdata->mii_bus->phy_mask = ~(1 << 1);
1077 }
1078
1079 if (mdiobus_register(pdata->mii_bus)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001080 SMSC_WARN(pdata, probe, "Error registering mii bus");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001081 goto err_out_free_bus_2;
1082 }
1083
1084 if (smsc911x_mii_probe(dev) < 0) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001085 SMSC_WARN(pdata, probe, "Error registering mii bus");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001086 goto err_out_unregister_bus_3;
1087 }
1088
1089 return 0;
1090
1091err_out_unregister_bus_3:
1092 mdiobus_unregister(pdata->mii_bus);
1093err_out_free_bus_2:
1094 mdiobus_free(pdata->mii_bus);
1095err_out_1:
1096 return err;
1097}
1098
1099/* Gets the number of tx statuses in the fifo */
1100static unsigned int smsc911x_tx_get_txstatcount(struct smsc911x_data *pdata)
1101{
1102 return (smsc911x_reg_read(pdata, TX_FIFO_INF)
1103 & TX_FIFO_INF_TSUSED_) >> 16;
1104}
1105
1106/* Reads tx statuses and increments counters where necessary */
1107static void smsc911x_tx_update_txcounters(struct net_device *dev)
1108{
1109 struct smsc911x_data *pdata = netdev_priv(dev);
1110 unsigned int tx_stat;
1111
1112 while ((tx_stat = smsc911x_tx_get_txstatus(pdata)) != 0) {
1113 if (unlikely(tx_stat & 0x80000000)) {
1114 /* In this driver the packet tag is used as the packet
1115 * length. Since a packet length can never reach the
1116 * size of 0x8000, this bit is reserved. It is worth
1117 * noting that the "reserved bit" in the warning above
1118 * does not reference a hardware defined reserved bit
1119 * but rather a driver defined one.
1120 */
Joe Perchesdffc6b22011-03-25 14:21:22 +00001121 SMSC_WARN(pdata, hw, "Packet tag reserved bit is high");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001122 } else {
Steve Glendinning785b6f92009-03-19 00:24:44 +00001123 if (unlikely(tx_stat & TX_STS_ES_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001124 dev->stats.tx_errors++;
1125 } else {
1126 dev->stats.tx_packets++;
1127 dev->stats.tx_bytes += (tx_stat >> 16);
1128 }
Steve Glendinning785b6f92009-03-19 00:24:44 +00001129 if (unlikely(tx_stat & TX_STS_EXCESS_COL_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001130 dev->stats.collisions += 16;
1131 dev->stats.tx_aborted_errors += 1;
1132 } else {
1133 dev->stats.collisions +=
1134 ((tx_stat >> 3) & 0xF);
1135 }
Steve Glendinning785b6f92009-03-19 00:24:44 +00001136 if (unlikely(tx_stat & TX_STS_LOST_CARRIER_))
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001137 dev->stats.tx_carrier_errors += 1;
Steve Glendinning785b6f92009-03-19 00:24:44 +00001138 if (unlikely(tx_stat & TX_STS_LATE_COL_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001139 dev->stats.collisions++;
1140 dev->stats.tx_aborted_errors++;
1141 }
1142 }
1143 }
1144}
1145
1146/* Increments the Rx error counters */
1147static void
1148smsc911x_rx_counterrors(struct net_device *dev, unsigned int rxstat)
1149{
1150 int crc_err = 0;
1151
Steve Glendinning785b6f92009-03-19 00:24:44 +00001152 if (unlikely(rxstat & RX_STS_ES_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001153 dev->stats.rx_errors++;
Steve Glendinning785b6f92009-03-19 00:24:44 +00001154 if (unlikely(rxstat & RX_STS_CRC_ERR_)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001155 dev->stats.rx_crc_errors++;
1156 crc_err = 1;
1157 }
1158 }
1159 if (likely(!crc_err)) {
Steve Glendinning785b6f92009-03-19 00:24:44 +00001160 if (unlikely((rxstat & RX_STS_FRAME_TYPE_) &&
1161 (rxstat & RX_STS_LENGTH_ERR_)))
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001162 dev->stats.rx_length_errors++;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001163 if (rxstat & RX_STS_MCAST_)
1164 dev->stats.multicast++;
1165 }
1166}
1167
1168/* Quickly dumps bad packets */
1169static void
Will Deacon3c5e9792012-04-12 05:54:09 +00001170smsc911x_rx_fastforward(struct smsc911x_data *pdata, unsigned int pktwords)
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001171{
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001172 if (likely(pktwords >= 4)) {
1173 unsigned int timeout = 500;
1174 unsigned int val;
1175 smsc911x_reg_write(pdata, RX_DP_CTRL, RX_DP_CTRL_RX_FFWD_);
1176 do {
1177 udelay(1);
1178 val = smsc911x_reg_read(pdata, RX_DP_CTRL);
Steve Glendinning8dacd542009-03-04 07:33:24 +00001179 } while ((val & RX_DP_CTRL_RX_FFWD_) && --timeout);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001180
1181 if (unlikely(timeout == 0))
Joe Perchesdffc6b22011-03-25 14:21:22 +00001182 SMSC_WARN(pdata, hw, "Timed out waiting for "
1183 "RX FFWD to finish, RX_DP_CTRL: 0x%08X", val);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001184 } else {
1185 unsigned int temp;
1186 while (pktwords--)
1187 temp = smsc911x_reg_read(pdata, RX_DATA_FIFO);
1188 }
1189}
1190
1191/* NAPI poll function */
1192static int smsc911x_poll(struct napi_struct *napi, int budget)
1193{
1194 struct smsc911x_data *pdata =
1195 container_of(napi, struct smsc911x_data, napi);
1196 struct net_device *dev = pdata->dev;
1197 int npackets = 0;
1198
Sriramf88c5b92009-11-12 02:14:38 +00001199 while (npackets < budget) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001200 unsigned int pktlength;
1201 unsigned int pktwords;
1202 struct sk_buff *skb;
1203 unsigned int rxstat = smsc911x_rx_get_rxstatus(pdata);
1204
1205 if (!rxstat) {
1206 unsigned int temp;
1207 /* We processed all packets available. Tell NAPI it can
1208 * stop polling then re-enable rx interrupts */
1209 smsc911x_reg_write(pdata, INT_STS, INT_STS_RSFL_);
Ben Hutchings288379f2009-01-19 16:43:59 -08001210 napi_complete(napi);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001211 temp = smsc911x_reg_read(pdata, INT_EN);
1212 temp |= INT_EN_RSFL_EN_;
1213 smsc911x_reg_write(pdata, INT_EN, temp);
1214 break;
1215 }
1216
1217 /* Count packet for NAPI scheduling, even if it has an error.
1218 * Error packets still require cycles to discard */
1219 npackets++;
1220
1221 pktlength = ((rxstat & 0x3FFF0000) >> 16);
1222 pktwords = (pktlength + NET_IP_ALIGN + 3) >> 2;
1223 smsc911x_rx_counterrors(dev, rxstat);
1224
1225 if (unlikely(rxstat & RX_STS_ES_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001226 SMSC_WARN(pdata, rx_err,
1227 "Discarding packet with error bit set");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001228 /* Packet has an error, discard it and continue with
1229 * the next */
1230 smsc911x_rx_fastforward(pdata, pktwords);
1231 dev->stats.rx_dropped++;
1232 continue;
1233 }
1234
Will Deacon3c5e9792012-04-12 05:54:09 +00001235 skb = netdev_alloc_skb(dev, pktwords << 2);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001236 if (unlikely(!skb)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001237 SMSC_WARN(pdata, rx_err,
1238 "Unable to allocate skb for rx packet");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001239 /* Drop the packet and stop this polling iteration */
1240 smsc911x_rx_fastforward(pdata, pktwords);
1241 dev->stats.rx_dropped++;
1242 break;
1243 }
1244
Will Deacon3c5e9792012-04-12 05:54:09 +00001245 pdata->ops->rx_readfifo(pdata,
1246 (unsigned int *)skb->data, pktwords);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001247
1248 /* Align IP on 16B boundary */
1249 skb_reserve(skb, NET_IP_ALIGN);
1250 skb_put(skb, pktlength - 4);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001251 skb->protocol = eth_type_trans(skb, dev);
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001252 skb_checksum_none_assert(skb);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001253 netif_receive_skb(skb);
1254
1255 /* Update counters */
1256 dev->stats.rx_packets++;
1257 dev->stats.rx_bytes += (pktlength - 4);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001258 }
1259
1260 /* Return total received packets */
1261 return npackets;
1262}
1263
1264/* Returns hash bit number for given MAC address
1265 * Example:
1266 * 01 00 5E 00 00 01 -> returns bit number 31 */
1267static unsigned int smsc911x_hash(char addr[ETH_ALEN])
1268{
1269 return (ether_crc(ETH_ALEN, addr) >> 26) & 0x3f;
1270}
1271
1272static void smsc911x_rx_multicast_update(struct smsc911x_data *pdata)
1273{
1274 /* Performs the multicast & mac_cr update. This is called when
1275 * safe on the current hardware, and with the mac_lock held */
1276 unsigned int mac_cr;
1277
1278 SMSC_ASSERT_MAC_LOCK(pdata);
1279
1280 mac_cr = smsc911x_mac_read(pdata, MAC_CR);
1281 mac_cr |= pdata->set_bits_mask;
1282 mac_cr &= ~(pdata->clear_bits_mask);
1283 smsc911x_mac_write(pdata, MAC_CR, mac_cr);
1284 smsc911x_mac_write(pdata, HASHH, pdata->hashhi);
1285 smsc911x_mac_write(pdata, HASHL, pdata->hashlo);
Joe Perchesdffc6b22011-03-25 14:21:22 +00001286 SMSC_TRACE(pdata, hw, "maccr 0x%08X, HASHH 0x%08X, HASHL 0x%08X",
1287 mac_cr, pdata->hashhi, pdata->hashlo);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001288}
1289
1290static void smsc911x_rx_multicast_update_workaround(struct smsc911x_data *pdata)
1291{
1292 unsigned int mac_cr;
1293
1294 /* This function is only called for older LAN911x devices
1295 * (revA or revB), where MAC_CR, HASHH and HASHL should not
1296 * be modified during Rx - newer devices immediately update the
1297 * registers.
1298 *
1299 * This is called from interrupt context */
1300
1301 spin_lock(&pdata->mac_lock);
1302
1303 /* Check Rx has stopped */
1304 if (smsc911x_mac_read(pdata, MAC_CR) & MAC_CR_RXEN_)
Joe Perchesdffc6b22011-03-25 14:21:22 +00001305 SMSC_WARN(pdata, drv, "Rx not stopped");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001306
1307 /* Perform the update - safe to do now Rx has stopped */
1308 smsc911x_rx_multicast_update(pdata);
1309
1310 /* Re-enable Rx */
1311 mac_cr = smsc911x_mac_read(pdata, MAC_CR);
1312 mac_cr |= MAC_CR_RXEN_;
1313 smsc911x_mac_write(pdata, MAC_CR, mac_cr);
1314
1315 pdata->multicast_update_pending = 0;
1316
1317 spin_unlock(&pdata->mac_lock);
1318}
1319
Javier Martinez Canillas63869942012-01-03 13:36:19 +00001320static int smsc911x_phy_disable_energy_detect(struct smsc911x_data *pdata)
1321{
1322 int rc = 0;
1323
1324 if (!pdata->phy_dev)
1325 return rc;
1326
1327 rc = phy_read(pdata->phy_dev, MII_LAN83C185_CTRL_STATUS);
1328
1329 if (rc < 0) {
1330 SMSC_WARN(pdata, drv, "Failed reading PHY control reg");
1331 return rc;
1332 }
1333
1334 /*
1335 * If energy is detected the PHY is already awake so is not necessary
1336 * to disable the energy detect power-down mode.
1337 */
1338 if ((rc & MII_LAN83C185_EDPWRDOWN) &&
1339 !(rc & MII_LAN83C185_ENERGYON)) {
1340 /* Disable energy detect mode for this SMSC Transceivers */
1341 rc = phy_write(pdata->phy_dev, MII_LAN83C185_CTRL_STATUS,
1342 rc & (~MII_LAN83C185_EDPWRDOWN));
1343
1344 if (rc < 0) {
1345 SMSC_WARN(pdata, drv, "Failed writing PHY control reg");
1346 return rc;
1347 }
1348
1349 mdelay(1);
1350 }
1351
1352 return 0;
1353}
1354
1355static int smsc911x_phy_enable_energy_detect(struct smsc911x_data *pdata)
1356{
1357 int rc = 0;
1358
1359 if (!pdata->phy_dev)
1360 return rc;
1361
1362 rc = phy_read(pdata->phy_dev, MII_LAN83C185_CTRL_STATUS);
1363
1364 if (rc < 0) {
1365 SMSC_WARN(pdata, drv, "Failed reading PHY control reg");
1366 return rc;
1367 }
1368
1369 /* Only enable if energy detect mode is already disabled */
1370 if (!(rc & MII_LAN83C185_EDPWRDOWN)) {
1371 mdelay(100);
1372 /* Enable energy detect mode for this SMSC Transceivers */
1373 rc = phy_write(pdata->phy_dev, MII_LAN83C185_CTRL_STATUS,
1374 rc | MII_LAN83C185_EDPWRDOWN);
1375
1376 if (rc < 0) {
1377 SMSC_WARN(pdata, drv, "Failed writing PHY control reg");
1378 return rc;
1379 }
1380
1381 mdelay(1);
1382 }
1383 return 0;
1384}
1385
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001386static int smsc911x_soft_reset(struct smsc911x_data *pdata)
1387{
1388 unsigned int timeout;
1389 unsigned int temp;
Javier Martinez Canillas63869942012-01-03 13:36:19 +00001390 int ret;
1391
1392 /*
1393 * LAN9210/LAN9211/LAN9220/LAN9221 chips have an internal PHY that
1394 * are initialized in a Energy Detect Power-Down mode that prevents
1395 * the MAC chip to be software reseted. So we have to wakeup the PHY
1396 * before.
1397 */
1398 if (pdata->generation == 4) {
1399 ret = smsc911x_phy_disable_energy_detect(pdata);
1400
1401 if (ret) {
1402 SMSC_WARN(pdata, drv, "Failed to wakeup the PHY chip");
1403 return ret;
1404 }
1405 }
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001406
1407 /* Reset the LAN911x */
1408 smsc911x_reg_write(pdata, HW_CFG, HW_CFG_SRST_);
1409 timeout = 10;
1410 do {
1411 udelay(10);
1412 temp = smsc911x_reg_read(pdata, HW_CFG);
1413 } while ((--timeout) && (temp & HW_CFG_SRST_));
1414
1415 if (unlikely(temp & HW_CFG_SRST_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001416 SMSC_WARN(pdata, drv, "Failed to complete reset");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001417 return -EIO;
1418 }
Javier Martinez Canillas63869942012-01-03 13:36:19 +00001419
1420 if (pdata->generation == 4) {
1421 ret = smsc911x_phy_enable_energy_detect(pdata);
1422
1423 if (ret) {
1424 SMSC_WARN(pdata, drv, "Failed to wakeup the PHY chip");
1425 return ret;
1426 }
1427 }
1428
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001429 return 0;
1430}
1431
1432/* Sets the device MAC address to dev_addr, called with mac_lock held */
1433static void
Steve Glendinning225ddf42009-03-19 00:24:46 +00001434smsc911x_set_hw_mac_address(struct smsc911x_data *pdata, u8 dev_addr[6])
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001435{
1436 u32 mac_high16 = (dev_addr[5] << 8) | dev_addr[4];
1437 u32 mac_low32 = (dev_addr[3] << 24) | (dev_addr[2] << 16) |
1438 (dev_addr[1] << 8) | dev_addr[0];
1439
1440 SMSC_ASSERT_MAC_LOCK(pdata);
1441
1442 smsc911x_mac_write(pdata, ADDRH, mac_high16);
1443 smsc911x_mac_write(pdata, ADDRL, mac_low32);
1444}
1445
1446static int smsc911x_open(struct net_device *dev)
1447{
1448 struct smsc911x_data *pdata = netdev_priv(dev);
1449 unsigned int timeout;
1450 unsigned int temp;
1451 unsigned int intcfg;
1452
1453 /* if the phy is not yet registered, retry later*/
1454 if (!pdata->phy_dev) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001455 SMSC_WARN(pdata, hw, "phy_dev is NULL");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001456 return -EAGAIN;
1457 }
1458
1459 if (!is_valid_ether_addr(dev->dev_addr)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001460 SMSC_WARN(pdata, hw, "dev_addr is not a valid MAC address");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001461 return -EADDRNOTAVAIL;
1462 }
1463
1464 /* Reset the LAN911x */
1465 if (smsc911x_soft_reset(pdata)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001466 SMSC_WARN(pdata, hw, "soft reset failed");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001467 return -EIO;
1468 }
1469
1470 smsc911x_reg_write(pdata, HW_CFG, 0x00050000);
1471 smsc911x_reg_write(pdata, AFC_CFG, 0x006E3740);
1472
Göran Weinholtf277e652011-03-02 04:07:21 +00001473 /* Increase the legal frame size of VLAN tagged frames to 1522 bytes */
1474 spin_lock_irq(&pdata->mac_lock);
1475 smsc911x_mac_write(pdata, VLAN1, ETH_P_8021Q);
1476 spin_unlock_irq(&pdata->mac_lock);
1477
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001478 /* Make sure EEPROM has finished loading before setting GPIO_CFG */
1479 timeout = 50;
Steve Glendinningf7efb6c2009-03-04 07:33:25 +00001480 while ((smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) &&
1481 --timeout) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001482 udelay(10);
1483 }
1484
1485 if (unlikely(timeout == 0))
Joe Perchesdffc6b22011-03-25 14:21:22 +00001486 SMSC_WARN(pdata, ifup,
1487 "Timed out waiting for EEPROM busy bit to clear");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001488
Steve Mucklef132c6c2012-06-06 18:30:57 -07001489 smsc911x_reg_write(pdata, SMSC_GPIO_CFG, 0x70070000);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001490
1491 /* The soft reset above cleared the device's MAC address,
1492 * restore it from local copy (set in probe) */
1493 spin_lock_irq(&pdata->mac_lock);
Steve Glendinning225ddf42009-03-19 00:24:46 +00001494 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001495 spin_unlock_irq(&pdata->mac_lock);
1496
1497 /* Initialise irqs, but leave all sources disabled */
1498 smsc911x_reg_write(pdata, INT_EN, 0);
1499 smsc911x_reg_write(pdata, INT_STS, 0xFFFFFFFF);
1500
1501 /* Set interrupt deassertion to 100uS */
1502 intcfg = ((10 << 24) | INT_CFG_IRQ_EN_);
1503
Steve Glendinning2107fb82008-11-05 00:35:38 +00001504 if (pdata->config.irq_polarity) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001505 SMSC_TRACE(pdata, ifup, "irq polarity: active high");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001506 intcfg |= INT_CFG_IRQ_POL_;
1507 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001508 SMSC_TRACE(pdata, ifup, "irq polarity: active low");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001509 }
1510
Steve Glendinning2107fb82008-11-05 00:35:38 +00001511 if (pdata->config.irq_type) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001512 SMSC_TRACE(pdata, ifup, "irq type: push-pull");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001513 intcfg |= INT_CFG_IRQ_TYPE_;
1514 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001515 SMSC_TRACE(pdata, ifup, "irq type: open drain");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001516 }
1517
1518 smsc911x_reg_write(pdata, INT_CFG, intcfg);
1519
Joe Perchesdffc6b22011-03-25 14:21:22 +00001520 SMSC_TRACE(pdata, ifup, "Testing irq handler using IRQ %d", dev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001521 pdata->software_irq_signal = 0;
1522 smp_wmb();
1523
1524 temp = smsc911x_reg_read(pdata, INT_EN);
1525 temp |= INT_EN_SW_INT_EN_;
1526 smsc911x_reg_write(pdata, INT_EN, temp);
1527
1528 timeout = 1000;
1529 while (timeout--) {
1530 if (pdata->software_irq_signal)
1531 break;
1532 msleep(1);
1533 }
1534
1535 if (!pdata->software_irq_signal) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001536 netdev_warn(dev, "ISR failed signaling test (IRQ %d)\n",
1537 dev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001538 return -ENODEV;
1539 }
Joe Perchesdffc6b22011-03-25 14:21:22 +00001540 SMSC_TRACE(pdata, ifup, "IRQ handler passed test using IRQ %d",
1541 dev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001542
Joe Perchesdffc6b22011-03-25 14:21:22 +00001543 netdev_info(dev, "SMSC911x/921x identified at %#08lx, IRQ: %d\n",
1544 (unsigned long)pdata->ioaddr, dev->irq);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001545
Steve Glendinning44c1d6f2009-03-18 23:37:18 -07001546 /* Reset the last known duplex and carrier */
1547 pdata->last_duplex = -1;
1548 pdata->last_carrier = -1;
1549
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001550 /* Bring the PHY up */
1551 phy_start(pdata->phy_dev);
1552
1553 temp = smsc911x_reg_read(pdata, HW_CFG);
1554 /* Preserve TX FIFO size and external PHY configuration */
1555 temp &= (HW_CFG_TX_FIF_SZ_|0x00000FFF);
1556 temp |= HW_CFG_SF_;
1557 smsc911x_reg_write(pdata, HW_CFG, temp);
1558
1559 temp = smsc911x_reg_read(pdata, FIFO_INT);
1560 temp |= FIFO_INT_TX_AVAIL_LEVEL_;
1561 temp &= ~(FIFO_INT_RX_STS_LEVEL_);
1562 smsc911x_reg_write(pdata, FIFO_INT, temp);
1563
1564 /* set RX Data offset to 2 bytes for alignment */
Will Deacon3c5e9792012-04-12 05:54:09 +00001565 smsc911x_reg_write(pdata, RX_CFG, (NET_IP_ALIGN << 8));
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001566
1567 /* enable NAPI polling before enabling RX interrupts */
1568 napi_enable(&pdata->napi);
1569
1570 temp = smsc911x_reg_read(pdata, INT_EN);
Steve Glendinning1373c0f2009-01-26 21:33:16 -08001571 temp |= (INT_EN_TDFA_EN_ | INT_EN_RSFL_EN_ | INT_EN_RXSTOP_INT_EN_);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001572 smsc911x_reg_write(pdata, INT_EN, temp);
1573
1574 spin_lock_irq(&pdata->mac_lock);
1575 temp = smsc911x_mac_read(pdata, MAC_CR);
1576 temp |= (MAC_CR_TXEN_ | MAC_CR_RXEN_ | MAC_CR_HBDIS_);
1577 smsc911x_mac_write(pdata, MAC_CR, temp);
1578 spin_unlock_irq(&pdata->mac_lock);
1579
1580 smsc911x_reg_write(pdata, TX_CFG, TX_CFG_TX_ON_);
1581
1582 netif_start_queue(dev);
1583 return 0;
1584}
1585
1586/* Entry point for stopping the interface */
1587static int smsc911x_stop(struct net_device *dev)
1588{
1589 struct smsc911x_data *pdata = netdev_priv(dev);
1590 unsigned int temp;
1591
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001592 /* Disable all device interrupts */
1593 temp = smsc911x_reg_read(pdata, INT_CFG);
1594 temp &= ~INT_CFG_IRQ_EN_;
1595 smsc911x_reg_write(pdata, INT_CFG, temp);
1596
1597 /* Stop Tx and Rx polling */
1598 netif_stop_queue(dev);
1599 napi_disable(&pdata->napi);
1600
1601 /* At this point all Rx and Tx activity is stopped */
1602 dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
1603 smsc911x_tx_update_txcounters(dev);
1604
1605 /* Bring the PHY down */
Steve Glendinningdd045192008-12-25 16:40:19 -08001606 if (pdata->phy_dev)
1607 phy_stop(pdata->phy_dev);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001608
Joe Perchesdffc6b22011-03-25 14:21:22 +00001609 SMSC_TRACE(pdata, ifdown, "Interface stopped");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001610 return 0;
1611}
1612
1613/* Entry point for transmitting a packet */
1614static int smsc911x_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1615{
1616 struct smsc911x_data *pdata = netdev_priv(dev);
1617 unsigned int freespace;
1618 unsigned int tx_cmd_a;
1619 unsigned int tx_cmd_b;
1620 unsigned int temp;
1621 u32 wrsz;
1622 ulong bufp;
1623
1624 freespace = smsc911x_reg_read(pdata, TX_FIFO_INF) & TX_FIFO_INF_TDFREE_;
1625
1626 if (unlikely(freespace < TX_FIFO_LOW_THRESHOLD))
Joe Perchesdffc6b22011-03-25 14:21:22 +00001627 SMSC_WARN(pdata, tx_err,
1628 "Tx data fifo low, space available: %d", freespace);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001629
1630 /* Word alignment adjustment */
1631 tx_cmd_a = (u32)((ulong)skb->data & 0x03) << 16;
1632 tx_cmd_a |= TX_CMD_A_FIRST_SEG_ | TX_CMD_A_LAST_SEG_;
1633 tx_cmd_a |= (unsigned int)skb->len;
1634
1635 tx_cmd_b = ((unsigned int)skb->len) << 16;
1636 tx_cmd_b |= (unsigned int)skb->len;
1637
1638 smsc911x_reg_write(pdata, TX_DATA_FIFO, tx_cmd_a);
1639 smsc911x_reg_write(pdata, TX_DATA_FIFO, tx_cmd_b);
1640
1641 bufp = (ulong)skb->data & (~0x3);
1642 wrsz = (u32)skb->len + 3;
1643 wrsz += (u32)((ulong)skb->data & 0x3);
1644 wrsz >>= 2;
1645
Mathieu J. Poirierc326de82011-04-13 17:13:00 -07001646 pdata->ops->tx_writefifo(pdata, (unsigned int *)bufp, wrsz);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001647 freespace -= (skb->len + 32);
Richard Cochran8c0069a2011-06-19 21:51:30 +00001648 skb_tx_timestamp(skb);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001649 dev_kfree_skb(skb);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001650
1651 if (unlikely(smsc911x_tx_get_txstatcount(pdata) >= 30))
1652 smsc911x_tx_update_txcounters(dev);
1653
1654 if (freespace < TX_FIFO_LOW_THRESHOLD) {
1655 netif_stop_queue(dev);
1656 temp = smsc911x_reg_read(pdata, FIFO_INT);
1657 temp &= 0x00FFFFFF;
1658 temp |= 0x32000000;
1659 smsc911x_reg_write(pdata, FIFO_INT, temp);
1660 }
1661
1662 return NETDEV_TX_OK;
1663}
1664
1665/* Entry point for getting status counters */
1666static struct net_device_stats *smsc911x_get_stats(struct net_device *dev)
1667{
1668 struct smsc911x_data *pdata = netdev_priv(dev);
1669 smsc911x_tx_update_txcounters(dev);
1670 dev->stats.rx_dropped += smsc911x_reg_read(pdata, RX_DROP);
1671 return &dev->stats;
1672}
1673
1674/* Entry point for setting addressing modes */
1675static void smsc911x_set_multicast_list(struct net_device *dev)
1676{
1677 struct smsc911x_data *pdata = netdev_priv(dev);
1678 unsigned long flags;
1679
1680 if (dev->flags & IFF_PROMISC) {
1681 /* Enabling promiscuous mode */
1682 pdata->set_bits_mask = MAC_CR_PRMS_;
1683 pdata->clear_bits_mask = (MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
1684 pdata->hashhi = 0;
1685 pdata->hashlo = 0;
1686 } else if (dev->flags & IFF_ALLMULTI) {
1687 /* Enabling all multicast mode */
1688 pdata->set_bits_mask = MAC_CR_MCPAS_;
1689 pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_HPFILT_);
1690 pdata->hashhi = 0;
1691 pdata->hashlo = 0;
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00001692 } else if (!netdev_mc_empty(dev)) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001693 /* Enabling specific multicast addresses */
1694 unsigned int hash_high = 0;
1695 unsigned int hash_low = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +00001696 struct netdev_hw_addr *ha;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001697
1698 pdata->set_bits_mask = MAC_CR_HPFILT_;
1699 pdata->clear_bits_mask = (MAC_CR_PRMS_ | MAC_CR_MCPAS_);
1700
Jiri Pirko22bedad2010-04-01 21:22:57 +00001701 netdev_for_each_mc_addr(ha, dev) {
1702 unsigned int bitnum = smsc911x_hash(ha->addr);
Jiri Pirko2a0d18f2010-02-17 23:22:38 +00001703 unsigned int mask = 0x01 << (bitnum & 0x1F);
1704
1705 if (bitnum & 0x20)
1706 hash_high |= mask;
1707 else
1708 hash_low |= mask;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001709 }
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001710
1711 pdata->hashhi = hash_high;
1712 pdata->hashlo = hash_low;
1713 } else {
1714 /* Enabling local MAC address only */
1715 pdata->set_bits_mask = 0;
1716 pdata->clear_bits_mask =
1717 (MAC_CR_PRMS_ | MAC_CR_MCPAS_ | MAC_CR_HPFILT_);
1718 pdata->hashhi = 0;
1719 pdata->hashlo = 0;
1720 }
1721
1722 spin_lock_irqsave(&pdata->mac_lock, flags);
1723
1724 if (pdata->generation <= 1) {
1725 /* Older hardware revision - cannot change these flags while
1726 * receiving data */
1727 if (!pdata->multicast_update_pending) {
1728 unsigned int temp;
Joe Perchesdffc6b22011-03-25 14:21:22 +00001729 SMSC_TRACE(pdata, hw, "scheduling mcast update");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001730 pdata->multicast_update_pending = 1;
1731
1732 /* Request the hardware to stop, then perform the
1733 * update when we get an RX_STOP interrupt */
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001734 temp = smsc911x_mac_read(pdata, MAC_CR);
1735 temp &= ~(MAC_CR_RXEN_);
1736 smsc911x_mac_write(pdata, MAC_CR, temp);
1737 } else {
1738 /* There is another update pending, this should now
1739 * use the newer values */
1740 }
1741 } else {
1742 /* Newer hardware revision - can write immediately */
1743 smsc911x_rx_multicast_update(pdata);
1744 }
1745
1746 spin_unlock_irqrestore(&pdata->mac_lock, flags);
1747}
1748
1749static irqreturn_t smsc911x_irqhandler(int irq, void *dev_id)
1750{
1751 struct net_device *dev = dev_id;
1752 struct smsc911x_data *pdata = netdev_priv(dev);
1753 u32 intsts = smsc911x_reg_read(pdata, INT_STS);
1754 u32 inten = smsc911x_reg_read(pdata, INT_EN);
1755 int serviced = IRQ_NONE;
1756 u32 temp;
1757
1758 if (unlikely(intsts & inten & INT_STS_SW_INT_)) {
1759 temp = smsc911x_reg_read(pdata, INT_EN);
1760 temp &= (~INT_EN_SW_INT_EN_);
1761 smsc911x_reg_write(pdata, INT_EN, temp);
1762 smsc911x_reg_write(pdata, INT_STS, INT_STS_SW_INT_);
1763 pdata->software_irq_signal = 1;
1764 smp_wmb();
1765 serviced = IRQ_HANDLED;
1766 }
1767
1768 if (unlikely(intsts & inten & INT_STS_RXSTOP_INT_)) {
1769 /* Called when there is a multicast update scheduled and
1770 * it is now safe to complete the update */
Joe Perchesdffc6b22011-03-25 14:21:22 +00001771 SMSC_TRACE(pdata, intr, "RX Stop interrupt");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001772 smsc911x_reg_write(pdata, INT_STS, INT_STS_RXSTOP_INT_);
Steve Glendinning1373c0f2009-01-26 21:33:16 -08001773 if (pdata->multicast_update_pending)
1774 smsc911x_rx_multicast_update_workaround(pdata);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001775 serviced = IRQ_HANDLED;
1776 }
1777
1778 if (intsts & inten & INT_STS_TDFA_) {
1779 temp = smsc911x_reg_read(pdata, FIFO_INT);
1780 temp |= FIFO_INT_TX_AVAIL_LEVEL_;
1781 smsc911x_reg_write(pdata, FIFO_INT, temp);
1782 smsc911x_reg_write(pdata, INT_STS, INT_STS_TDFA_);
1783 netif_wake_queue(dev);
1784 serviced = IRQ_HANDLED;
1785 }
1786
1787 if (unlikely(intsts & inten & INT_STS_RXE_)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001788 SMSC_TRACE(pdata, intr, "RX Error interrupt");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001789 smsc911x_reg_write(pdata, INT_STS, INT_STS_RXE_);
1790 serviced = IRQ_HANDLED;
1791 }
1792
1793 if (likely(intsts & inten & INT_STS_RSFL_)) {
Ben Hutchings288379f2009-01-19 16:43:59 -08001794 if (likely(napi_schedule_prep(&pdata->napi))) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001795 /* Disable Rx interrupts */
1796 temp = smsc911x_reg_read(pdata, INT_EN);
1797 temp &= (~INT_EN_RSFL_EN_);
1798 smsc911x_reg_write(pdata, INT_EN, temp);
1799 /* Schedule a NAPI poll */
Ben Hutchings288379f2009-01-19 16:43:59 -08001800 __napi_schedule(&pdata->napi);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001801 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001802 SMSC_WARN(pdata, rx_err, "napi_schedule_prep failed");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001803 }
1804 serviced = IRQ_HANDLED;
1805 }
1806
1807 return serviced;
1808}
1809
1810#ifdef CONFIG_NET_POLL_CONTROLLER
Steve Glendinning1757ab22008-12-12 22:31:16 -08001811static void smsc911x_poll_controller(struct net_device *dev)
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001812{
1813 disable_irq(dev->irq);
1814 smsc911x_irqhandler(0, dev);
1815 enable_irq(dev->irq);
1816}
1817#endif /* CONFIG_NET_POLL_CONTROLLER */
1818
Steve Glendinning225ddf42009-03-19 00:24:46 +00001819static int smsc911x_set_mac_address(struct net_device *dev, void *p)
1820{
1821 struct smsc911x_data *pdata = netdev_priv(dev);
1822 struct sockaddr *addr = p;
1823
1824 /* On older hardware revisions we cannot change the mac address
1825 * registers while receiving data. Newer devices can safely change
1826 * this at any time. */
1827 if (pdata->generation <= 1 && netif_running(dev))
1828 return -EBUSY;
1829
1830 if (!is_valid_ether_addr(addr->sa_data))
1831 return -EADDRNOTAVAIL;
1832
Danny Kukawka7ce5d222012-02-15 06:45:40 +00001833 dev->addr_assign_type &= ~NET_ADDR_RANDOM;
Steve Glendinning225ddf42009-03-19 00:24:46 +00001834 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
1835
1836 spin_lock_irq(&pdata->mac_lock);
1837 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
1838 spin_unlock_irq(&pdata->mac_lock);
1839
Joe Perchesdffc6b22011-03-25 14:21:22 +00001840 netdev_info(dev, "MAC Address: %pM\n", dev->dev_addr);
Steve Glendinning225ddf42009-03-19 00:24:46 +00001841
1842 return 0;
1843}
1844
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001845/* Standard ioctls for mii-tool */
1846static int smsc911x_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1847{
1848 struct smsc911x_data *pdata = netdev_priv(dev);
1849
1850 if (!netif_running(dev) || !pdata->phy_dev)
1851 return -EINVAL;
1852
Richard Cochran28b04112010-07-17 08:48:55 +00001853 return phy_mii_ioctl(pdata->phy_dev, ifr, cmd);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001854}
1855
1856static int
1857smsc911x_ethtool_getsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1858{
1859 struct smsc911x_data *pdata = netdev_priv(dev);
1860
1861 cmd->maxtxpkt = 1;
1862 cmd->maxrxpkt = 1;
1863 return phy_ethtool_gset(pdata->phy_dev, cmd);
1864}
1865
1866static int
1867smsc911x_ethtool_setsettings(struct net_device *dev, struct ethtool_cmd *cmd)
1868{
1869 struct smsc911x_data *pdata = netdev_priv(dev);
1870
1871 return phy_ethtool_sset(pdata->phy_dev, cmd);
1872}
1873
1874static void smsc911x_ethtool_getdrvinfo(struct net_device *dev,
1875 struct ethtool_drvinfo *info)
1876{
1877 strlcpy(info->driver, SMSC_CHIPNAME, sizeof(info->driver));
1878 strlcpy(info->version, SMSC_DRV_VERSION, sizeof(info->version));
Kay Sieversdb1d7bf2009-01-26 21:12:58 -08001879 strlcpy(info->bus_info, dev_name(dev->dev.parent),
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001880 sizeof(info->bus_info));
1881}
1882
1883static int smsc911x_ethtool_nwayreset(struct net_device *dev)
1884{
1885 struct smsc911x_data *pdata = netdev_priv(dev);
1886
1887 return phy_start_aneg(pdata->phy_dev);
1888}
1889
1890static u32 smsc911x_ethtool_getmsglevel(struct net_device *dev)
1891{
1892 struct smsc911x_data *pdata = netdev_priv(dev);
1893 return pdata->msg_enable;
1894}
1895
1896static void smsc911x_ethtool_setmsglevel(struct net_device *dev, u32 level)
1897{
1898 struct smsc911x_data *pdata = netdev_priv(dev);
1899 pdata->msg_enable = level;
1900}
1901
1902static int smsc911x_ethtool_getregslen(struct net_device *dev)
1903{
1904 return (((E2P_DATA - ID_REV) / 4 + 1) + (WUCSR - MAC_CR) + 1 + 32) *
1905 sizeof(u32);
1906}
1907
1908static void
1909smsc911x_ethtool_getregs(struct net_device *dev, struct ethtool_regs *regs,
1910 void *buf)
1911{
1912 struct smsc911x_data *pdata = netdev_priv(dev);
1913 struct phy_device *phy_dev = pdata->phy_dev;
1914 unsigned long flags;
1915 unsigned int i;
1916 unsigned int j = 0;
1917 u32 *data = buf;
1918
1919 regs->version = pdata->idrev;
1920 for (i = ID_REV; i <= E2P_DATA; i += (sizeof(u32)))
1921 data[j++] = smsc911x_reg_read(pdata, i);
1922
1923 for (i = MAC_CR; i <= WUCSR; i++) {
1924 spin_lock_irqsave(&pdata->mac_lock, flags);
1925 data[j++] = smsc911x_mac_read(pdata, i);
1926 spin_unlock_irqrestore(&pdata->mac_lock, flags);
1927 }
1928
1929 for (i = 0; i <= 31; i++)
1930 data[j++] = smsc911x_mii_read(phy_dev->bus, phy_dev->addr, i);
1931}
1932
1933static void smsc911x_eeprom_enable_access(struct smsc911x_data *pdata)
1934{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001935 unsigned int temp = smsc911x_reg_read(pdata, SMSC_GPIO_CFG);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001936 temp &= ~GPIO_CFG_EEPR_EN_;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001937 smsc911x_reg_write(pdata, SMSC_GPIO_CFG, temp);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001938 msleep(1);
1939}
1940
1941static int smsc911x_eeprom_send_cmd(struct smsc911x_data *pdata, u32 op)
1942{
1943 int timeout = 100;
1944 u32 e2cmd;
1945
Joe Perchesdffc6b22011-03-25 14:21:22 +00001946 SMSC_TRACE(pdata, drv, "op 0x%08x", op);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001947 if (smsc911x_reg_read(pdata, E2P_CMD) & E2P_CMD_EPC_BUSY_) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001948 SMSC_WARN(pdata, drv, "Busy at start");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001949 return -EBUSY;
1950 }
1951
1952 e2cmd = op | E2P_CMD_EPC_BUSY_;
1953 smsc911x_reg_write(pdata, E2P_CMD, e2cmd);
1954
1955 do {
1956 msleep(1);
1957 e2cmd = smsc911x_reg_read(pdata, E2P_CMD);
Roel Kluin2cf0dbe2009-02-20 00:52:19 -08001958 } while ((e2cmd & E2P_CMD_EPC_BUSY_) && (--timeout));
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001959
1960 if (!timeout) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00001961 SMSC_TRACE(pdata, drv, "TIMED OUT");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001962 return -EAGAIN;
1963 }
1964
1965 if (e2cmd & E2P_CMD_EPC_TIMEOUT_) {
David S. Miller1c01a802011-04-11 13:44:25 -07001966 SMSC_TRACE(pdata, drv, "Error occurred during eeprom operation");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001967 return -EINVAL;
1968 }
1969
1970 return 0;
1971}
1972
1973static int smsc911x_eeprom_read_location(struct smsc911x_data *pdata,
1974 u8 address, u8 *data)
1975{
1976 u32 op = E2P_CMD_EPC_CMD_READ_ | address;
1977 int ret;
1978
Joe Perchesdffc6b22011-03-25 14:21:22 +00001979 SMSC_TRACE(pdata, drv, "address 0x%x", address);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001980 ret = smsc911x_eeprom_send_cmd(pdata, op);
1981
1982 if (!ret)
1983 data[address] = smsc911x_reg_read(pdata, E2P_DATA);
1984
1985 return ret;
1986}
1987
1988static int smsc911x_eeprom_write_location(struct smsc911x_data *pdata,
1989 u8 address, u8 data)
1990{
1991 u32 op = E2P_CMD_EPC_CMD_ERASE_ | address;
Steve Glendinning58add9f2009-03-26 07:14:36 +00001992 u32 temp;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001993 int ret;
1994
Joe Perchesdffc6b22011-03-25 14:21:22 +00001995 SMSC_TRACE(pdata, drv, "address 0x%x, data 0x%x", address, data);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00001996 ret = smsc911x_eeprom_send_cmd(pdata, op);
1997
1998 if (!ret) {
1999 op = E2P_CMD_EPC_CMD_WRITE_ | address;
2000 smsc911x_reg_write(pdata, E2P_DATA, (u32)data);
Steve Glendinning58add9f2009-03-26 07:14:36 +00002001
2002 /* Workaround for hardware read-after-write restriction */
2003 temp = smsc911x_reg_read(pdata, BYTE_TEST);
2004
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002005 ret = smsc911x_eeprom_send_cmd(pdata, op);
2006 }
2007
2008 return ret;
2009}
2010
2011static int smsc911x_ethtool_get_eeprom_len(struct net_device *dev)
2012{
2013 return SMSC911X_EEPROM_SIZE;
2014}
2015
2016static int smsc911x_ethtool_get_eeprom(struct net_device *dev,
2017 struct ethtool_eeprom *eeprom, u8 *data)
2018{
2019 struct smsc911x_data *pdata = netdev_priv(dev);
2020 u8 eeprom_data[SMSC911X_EEPROM_SIZE];
2021 int len;
2022 int i;
2023
2024 smsc911x_eeprom_enable_access(pdata);
2025
2026 len = min(eeprom->len, SMSC911X_EEPROM_SIZE);
2027 for (i = 0; i < len; i++) {
2028 int ret = smsc911x_eeprom_read_location(pdata, i, eeprom_data);
2029 if (ret < 0) {
2030 eeprom->len = 0;
2031 return ret;
2032 }
2033 }
2034
2035 memcpy(data, &eeprom_data[eeprom->offset], len);
2036 eeprom->len = len;
2037 return 0;
2038}
2039
2040static int smsc911x_ethtool_set_eeprom(struct net_device *dev,
2041 struct ethtool_eeprom *eeprom, u8 *data)
2042{
2043 int ret;
2044 struct smsc911x_data *pdata = netdev_priv(dev);
2045
2046 smsc911x_eeprom_enable_access(pdata);
2047 smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWEN_);
2048 ret = smsc911x_eeprom_write_location(pdata, eeprom->offset, *data);
2049 smsc911x_eeprom_send_cmd(pdata, E2P_CMD_EPC_CMD_EWDS_);
2050
2051 /* Single byte write, according to man page */
2052 eeprom->len = 1;
2053
2054 return ret;
2055}
2056
Steve Glendinningcb5b04f2008-12-25 16:41:09 -08002057static const struct ethtool_ops smsc911x_ethtool_ops = {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002058 .get_settings = smsc911x_ethtool_getsettings,
2059 .set_settings = smsc911x_ethtool_setsettings,
2060 .get_link = ethtool_op_get_link,
2061 .get_drvinfo = smsc911x_ethtool_getdrvinfo,
2062 .nway_reset = smsc911x_ethtool_nwayreset,
2063 .get_msglevel = smsc911x_ethtool_getmsglevel,
2064 .set_msglevel = smsc911x_ethtool_setmsglevel,
2065 .get_regs_len = smsc911x_ethtool_getregslen,
2066 .get_regs = smsc911x_ethtool_getregs,
2067 .get_eeprom_len = smsc911x_ethtool_get_eeprom_len,
2068 .get_eeprom = smsc911x_ethtool_get_eeprom,
2069 .set_eeprom = smsc911x_ethtool_set_eeprom,
2070};
2071
Steve Glendinning631b7562008-12-25 16:40:47 -08002072static const struct net_device_ops smsc911x_netdev_ops = {
2073 .ndo_open = smsc911x_open,
2074 .ndo_stop = smsc911x_stop,
2075 .ndo_start_xmit = smsc911x_hard_start_xmit,
2076 .ndo_get_stats = smsc911x_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00002077 .ndo_set_rx_mode = smsc911x_set_multicast_list,
Steve Glendinning631b7562008-12-25 16:40:47 -08002078 .ndo_do_ioctl = smsc911x_do_ioctl,
Ben Hutchings635ecaa2009-07-09 17:59:01 +00002079 .ndo_change_mtu = eth_change_mtu,
Steve Glendinning631b7562008-12-25 16:40:47 -08002080 .ndo_validate_addr = eth_validate_addr,
Steve Glendinning225ddf42009-03-19 00:24:46 +00002081 .ndo_set_mac_address = smsc911x_set_mac_address,
Steve Glendinning631b7562008-12-25 16:40:47 -08002082#ifdef CONFIG_NET_POLL_CONTROLLER
2083 .ndo_poll_controller = smsc911x_poll_controller,
2084#endif
2085};
2086
Steve Glendinning31f45742009-01-27 06:51:12 +00002087/* copies the current mac address from hardware to dev->dev_addr */
2088static void __devinit smsc911x_read_mac_address(struct net_device *dev)
2089{
2090 struct smsc911x_data *pdata = netdev_priv(dev);
2091 u32 mac_high16 = smsc911x_mac_read(pdata, ADDRH);
2092 u32 mac_low32 = smsc911x_mac_read(pdata, ADDRL);
2093
2094 dev->dev_addr[0] = (u8)(mac_low32);
2095 dev->dev_addr[1] = (u8)(mac_low32 >> 8);
2096 dev->dev_addr[2] = (u8)(mac_low32 >> 16);
2097 dev->dev_addr[3] = (u8)(mac_low32 >> 24);
2098 dev->dev_addr[4] = (u8)(mac_high16);
2099 dev->dev_addr[5] = (u8)(mac_high16 >> 8);
2100}
2101
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002102/* Initializing private device structures, only called from probe */
2103static int __devinit smsc911x_init(struct net_device *dev)
2104{
2105 struct smsc911x_data *pdata = netdev_priv(dev);
2106 unsigned int byte_test;
Robert Marklund3ac35462011-10-25 22:05:43 +00002107 unsigned int to = 100;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002108
Joe Perchesdffc6b22011-03-25 14:21:22 +00002109 SMSC_TRACE(pdata, probe, "Driver Parameters:");
2110 SMSC_TRACE(pdata, probe, "LAN base: 0x%08lX",
2111 (unsigned long)pdata->ioaddr);
2112 SMSC_TRACE(pdata, probe, "IRQ: %d", dev->irq);
2113 SMSC_TRACE(pdata, probe, "PHY will be autodetected.");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002114
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002115 spin_lock_init(&pdata->dev_lock);
Enric Balletbo i Serra35a67ed2011-04-05 06:52:49 +00002116 spin_lock_init(&pdata->mac_lock);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002117
2118 if (pdata->ioaddr == 0) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002119 SMSC_WARN(pdata, probe, "pdata->ioaddr: 0x00000000");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002120 return -ENODEV;
2121 }
2122
Robert Marklund3ac35462011-10-25 22:05:43 +00002123 /*
2124 * poll the READY bit in PMT_CTRL. Any other access to the device is
2125 * forbidden while this bit isn't set. Try for 100ms
2126 */
2127 while (!(smsc911x_reg_read(pdata, PMT_CTRL) & PMT_CTRL_READY_) && --to)
2128 udelay(1000);
2129 if (to == 0) {
2130 pr_err("Device not READY in 100ms aborting\n");
2131 return -ENODEV;
2132 }
2133
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002134 /* Check byte ordering */
2135 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
Joe Perchesdffc6b22011-03-25 14:21:22 +00002136 SMSC_TRACE(pdata, probe, "BYTE_TEST: 0x%08X", byte_test);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002137 if (byte_test == 0x43218765) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002138 SMSC_TRACE(pdata, probe, "BYTE_TEST looks swapped, "
2139 "applying WORD_SWAP");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002140 smsc911x_reg_write(pdata, WORD_SWAP, 0xffffffff);
2141
2142 /* 1 dummy read of BYTE_TEST is needed after a write to
2143 * WORD_SWAP before its contents are valid */
2144 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
2145
2146 byte_test = smsc911x_reg_read(pdata, BYTE_TEST);
2147 }
2148
2149 if (byte_test != 0x87654321) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002150 SMSC_WARN(pdata, drv, "BYTE_TEST: 0x%08X", byte_test);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002151 if (((byte_test >> 16) & 0xFFFF) == (byte_test & 0xFFFF)) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002152 SMSC_WARN(pdata, probe,
2153 "top 16 bits equal to bottom 16 bits");
2154 SMSC_TRACE(pdata, probe,
2155 "This may mean the chip is set "
2156 "for 32 bit while the bus is reading 16 bit");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002157 }
2158 return -ENODEV;
2159 }
2160
2161 /* Default generation to zero (all workarounds apply) */
2162 pdata->generation = 0;
2163
2164 pdata->idrev = smsc911x_reg_read(pdata, ID_REV);
2165 switch (pdata->idrev & 0xFFFF0000) {
2166 case 0x01180000:
2167 case 0x01170000:
2168 case 0x01160000:
2169 case 0x01150000:
David S. Miller1805b2f2011-10-24 18:18:09 -04002170 case 0x218A0000:
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002171 /* LAN911[5678] family */
2172 pdata->generation = pdata->idrev & 0x0000FFFF;
2173 break;
2174
2175 case 0x118A0000:
2176 case 0x117A0000:
2177 case 0x116A0000:
2178 case 0x115A0000:
2179 /* LAN921[5678] family */
2180 pdata->generation = 3;
2181 break;
2182
2183 case 0x92100000:
2184 case 0x92110000:
2185 case 0x92200000:
2186 case 0x92210000:
2187 /* LAN9210/LAN9211/LAN9220/LAN9221 */
2188 pdata->generation = 4;
2189 break;
2190
2191 default:
Joe Perchesdffc6b22011-03-25 14:21:22 +00002192 SMSC_WARN(pdata, probe, "LAN911x not identified, idrev: 0x%08X",
2193 pdata->idrev);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002194 return -ENODEV;
2195 }
2196
Joe Perchesdffc6b22011-03-25 14:21:22 +00002197 SMSC_TRACE(pdata, probe,
2198 "LAN911x identified, idrev: 0x%08X, generation: %d",
2199 pdata->idrev, pdata->generation);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002200
2201 if (pdata->generation == 0)
Joe Perchesdffc6b22011-03-25 14:21:22 +00002202 SMSC_WARN(pdata, probe,
2203 "This driver is not intended for this chip revision");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002204
Steve Glendinning31f45742009-01-27 06:51:12 +00002205 /* workaround for platforms without an eeprom, where the mac address
2206 * is stored elsewhere and set by the bootloader. This saves the
2207 * mac address before resetting the device */
Enric Balletbo i Serra35a67ed2011-04-05 06:52:49 +00002208 if (pdata->config.flags & SMSC911X_SAVE_MAC_ADDRESS) {
2209 spin_lock_irq(&pdata->mac_lock);
Steve Glendinning31f45742009-01-27 06:51:12 +00002210 smsc911x_read_mac_address(dev);
Enric Balletbo i Serra35a67ed2011-04-05 06:52:49 +00002211 spin_unlock_irq(&pdata->mac_lock);
2212 }
Steve Glendinning31f45742009-01-27 06:51:12 +00002213
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002214 /* Reset the LAN911x */
2215 if (smsc911x_soft_reset(pdata))
2216 return -ENODEV;
2217
2218 /* Disable all interrupt sources until we bring the device up */
2219 smsc911x_reg_write(pdata, INT_EN, 0);
2220
2221 ether_setup(dev);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002222 dev->flags |= IFF_MULTICAST;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002223 netif_napi_add(dev, &pdata->napi, smsc911x_poll, SMSC_NAPI_WEIGHT);
Steve Glendinning631b7562008-12-25 16:40:47 -08002224 dev->netdev_ops = &smsc911x_netdev_ops;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002225 dev->ethtool_ops = &smsc911x_ethtool_ops;
2226
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002227 return 0;
2228}
2229
2230static int __devexit smsc911x_drv_remove(struct platform_device *pdev)
2231{
2232 struct net_device *dev;
2233 struct smsc911x_data *pdata;
2234 struct resource *res;
2235
2236 dev = platform_get_drvdata(pdev);
2237 BUG_ON(!dev);
2238 pdata = netdev_priv(dev);
2239 BUG_ON(!pdata);
2240 BUG_ON(!pdata->ioaddr);
2241 BUG_ON(!pdata->phy_dev);
2242
Joe Perchesdffc6b22011-03-25 14:21:22 +00002243 SMSC_TRACE(pdata, ifdown, "Stopping driver");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002244
Steve Mucklef132c6c2012-06-06 18:30:57 -07002245 if (pdata->config.has_reset_gpio) {
2246 gpio_set_value_cansleep(pdata->config.reset_gpio, 0);
2247 gpio_free(pdata->config.reset_gpio);
2248 }
2249
2250
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002251 phy_disconnect(pdata->phy_dev);
2252 pdata->phy_dev = NULL;
2253 mdiobus_unregister(pdata->mii_bus);
2254 mdiobus_free(pdata->mii_bus);
2255
2256 platform_set_drvdata(pdev, NULL);
2257 unregister_netdev(dev);
2258 free_irq(dev->irq, dev);
2259 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2260 "smsc911x-memory");
2261 if (!res)
Steve Glendinningd4522732008-12-25 16:44:01 -08002262 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002263
Julia Lawall39424532009-07-04 11:31:47 +00002264 release_mem_region(res->start, resource_size(res));
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002265
2266 iounmap(pdata->ioaddr);
2267
Robert Marklundc7e963f2011-11-24 01:03:07 +00002268 (void)smsc911x_disable_resources(pdev);
2269 smsc911x_free_resources(pdev);
2270
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002271 free_netdev(dev);
2272
2273 return 0;
2274}
2275
Mathieu J. Poirierc326de82011-04-13 17:13:00 -07002276/* standard register acces */
2277static const struct smsc911x_ops standard_smsc911x_ops = {
2278 .reg_read = __smsc911x_reg_read,
2279 .reg_write = __smsc911x_reg_write,
2280 .rx_readfifo = smsc911x_rx_readfifo,
2281 .tx_writefifo = smsc911x_tx_writefifo,
2282};
2283
2284/* shifted register access */
2285static const struct smsc911x_ops shifted_smsc911x_ops = {
2286 .reg_read = __smsc911x_reg_read_shift,
2287 .reg_write = __smsc911x_reg_write_shift,
2288 .rx_readfifo = smsc911x_rx_readfifo_shift,
2289 .tx_writefifo = smsc911x_tx_writefifo_shift,
2290};
2291
Shawn Guo79f88ee2011-07-30 08:26:00 +00002292#ifdef CONFIG_OF
2293static int __devinit smsc911x_probe_config_dt(
2294 struct smsc911x_platform_config *config,
2295 struct device_node *np)
2296{
2297 const char *mac;
2298 u32 width = 0;
2299
2300 if (!np)
2301 return -ENODEV;
2302
2303 config->phy_interface = of_get_phy_mode(np);
2304
2305 mac = of_get_mac_address(np);
2306 if (mac)
2307 memcpy(config->mac, mac, ETH_ALEN);
2308
2309 of_property_read_u32(np, "reg-shift", &config->shift);
2310
2311 of_property_read_u32(np, "reg-io-width", &width);
2312 if (width == 4)
2313 config->flags |= SMSC911X_USE_32BIT;
Dave Martinf26cd412011-09-13 00:49:29 +00002314 else
2315 config->flags |= SMSC911X_USE_16BIT;
Shawn Guo79f88ee2011-07-30 08:26:00 +00002316
2317 if (of_get_property(np, "smsc,irq-active-high", NULL))
2318 config->irq_polarity = SMSC911X_IRQ_POLARITY_ACTIVE_HIGH;
2319
2320 if (of_get_property(np, "smsc,irq-push-pull", NULL))
2321 config->irq_type = SMSC911X_IRQ_TYPE_PUSH_PULL;
2322
2323 if (of_get_property(np, "smsc,force-internal-phy", NULL))
2324 config->flags |= SMSC911X_FORCE_INTERNAL_PHY;
2325
2326 if (of_get_property(np, "smsc,force-external-phy", NULL))
2327 config->flags |= SMSC911X_FORCE_EXTERNAL_PHY;
2328
2329 if (of_get_property(np, "smsc,save-mac-address", NULL))
2330 config->flags |= SMSC911X_SAVE_MAC_ADDRESS;
2331
2332 return 0;
2333}
2334#else
2335static inline int smsc911x_probe_config_dt(
2336 struct smsc911x_platform_config *config,
2337 struct device_node *np)
2338{
2339 return -ENODEV;
2340}
2341#endif /* CONFIG_OF */
2342
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002343static int __devinit smsc911x_drv_probe(struct platform_device *pdev)
2344{
Shawn Guo79f88ee2011-07-30 08:26:00 +00002345 struct device_node *np = pdev->dev.of_node;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002346 struct net_device *dev;
2347 struct smsc911x_data *pdata;
Steve Glendinning2107fb82008-11-05 00:35:38 +00002348 struct smsc911x_platform_config *config = pdev->dev.platform_data;
Steve Glendinning61307ed2009-01-27 06:51:09 +00002349 struct resource *res, *irq_res;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002350 unsigned int intcfg = 0;
Steve Glendinning61307ed2009-01-27 06:51:09 +00002351 int res_size, irq_flags;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002352 int retval;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002353
Joe Perchesdffc6b22011-03-25 14:21:22 +00002354 pr_info("Driver version %s\n", SMSC_DRV_VERSION);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002355
2356 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
2357 "smsc911x-memory");
2358 if (!res)
2359 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2360 if (!res) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002361 pr_warn("Could not allocate resource\n");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002362 retval = -ENODEV;
2363 goto out_0;
2364 }
Julia Lawall39424532009-07-04 11:31:47 +00002365 res_size = resource_size(res);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002366
Steve Glendinning61307ed2009-01-27 06:51:09 +00002367 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2368 if (!irq_res) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002369 pr_warn("Could not allocate irq resource\n");
Steve Glendinning61307ed2009-01-27 06:51:09 +00002370 retval = -ENODEV;
2371 goto out_0;
2372 }
2373
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002374 if (!request_mem_region(res->start, res_size, SMSC_CHIPNAME)) {
2375 retval = -EBUSY;
2376 goto out_0;
2377 }
2378
2379 dev = alloc_etherdev(sizeof(struct smsc911x_data));
2380 if (!dev) {
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002381 retval = -ENOMEM;
2382 goto out_release_io_1;
2383 }
2384
2385 SET_NETDEV_DEV(dev, &pdev->dev);
2386
2387 pdata = netdev_priv(dev);
Steve Glendinning61307ed2009-01-27 06:51:09 +00002388 dev->irq = irq_res->start;
2389 irq_flags = irq_res->flags & IRQF_TRIGGER_MASK;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002390 pdata->ioaddr = ioremap_nocache(res->start, res_size);
2391
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002392 pdata->dev = dev;
2393 pdata->msg_enable = ((1 << debug) - 1);
2394
Robert Marklundc7e963f2011-11-24 01:03:07 +00002395 platform_set_drvdata(pdev, dev);
2396
2397 retval = smsc911x_request_resources(pdev);
2398 if (retval)
2399 goto out_return_resources;
2400
2401 retval = smsc911x_enable_resources(pdev);
2402 if (retval)
2403 goto out_disable_resources;
2404
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002405 if (pdata->ioaddr == NULL) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002406 SMSC_WARN(pdata, probe, "Error smsc911x base address invalid");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002407 retval = -ENOMEM;
Robert Marklundc7e963f2011-11-24 01:03:07 +00002408 goto out_disable_resources;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002409 }
2410
Shawn Guo79f88ee2011-07-30 08:26:00 +00002411 retval = smsc911x_probe_config_dt(&pdata->config, np);
2412 if (retval && config) {
2413 /* copy config parameters across to pdata */
2414 memcpy(&pdata->config, config, sizeof(pdata->config));
2415 retval = 0;
2416 }
2417
2418 if (retval) {
2419 SMSC_WARN(pdata, probe, "Error smsc911x config not found");
Robert Marklundc7e963f2011-11-24 01:03:07 +00002420 goto out_disable_resources;
Shawn Guo79f88ee2011-07-30 08:26:00 +00002421 }
2422
Mathieu J. Poirierc326de82011-04-13 17:13:00 -07002423 /* assume standard, non-shifted, access to HW registers */
2424 pdata->ops = &standard_smsc911x_ops;
2425 /* apply the right access if shifting is needed */
Shawn Guo79f88ee2011-07-30 08:26:00 +00002426 if (pdata->config.shift)
Mathieu J. Poirierc326de82011-04-13 17:13:00 -07002427 pdata->ops = &shifted_smsc911x_ops;
2428
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002429 retval = smsc911x_init(dev);
2430 if (retval < 0)
Robert Marklundc7e963f2011-11-24 01:03:07 +00002431 goto out_disable_resources;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002432
2433 /* configure irq polarity and type before connecting isr */
Steve Glendinning2107fb82008-11-05 00:35:38 +00002434 if (pdata->config.irq_polarity == SMSC911X_IRQ_POLARITY_ACTIVE_HIGH)
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002435 intcfg |= INT_CFG_IRQ_POL_;
2436
Steve Glendinning2107fb82008-11-05 00:35:38 +00002437 if (pdata->config.irq_type == SMSC911X_IRQ_TYPE_PUSH_PULL)
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002438 intcfg |= INT_CFG_IRQ_TYPE_;
2439
2440 smsc911x_reg_write(pdata, INT_CFG, intcfg);
2441
2442 /* Ensure interrupts are globally disabled before connecting ISR */
2443 smsc911x_reg_write(pdata, INT_EN, 0);
2444 smsc911x_reg_write(pdata, INT_STS, 0xFFFFFFFF);
2445
Steve Mucklef132c6c2012-06-06 18:30:57 -07002446 retval = request_any_context_irq(dev->irq, smsc911x_irqhandler,
2447 irq_flags | IRQF_SHARED, dev->name,
2448 dev);
2449 if (retval < 0) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002450 SMSC_WARN(pdata, probe,
2451 "Unable to claim requested irq: %d", dev->irq);
Lee Jones163faf32012-04-19 10:36:33 +00002452 goto out_disable_resources;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002453 }
2454
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002455 retval = register_netdev(dev);
2456 if (retval) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002457 SMSC_WARN(pdata, probe, "Error %i registering device", retval);
Robert Marklundc7e963f2011-11-24 01:03:07 +00002458 goto out_free_irq;
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002459 } else {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002460 SMSC_TRACE(pdata, probe,
2461 "Network interface: \"%s\"", dev->name);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002462 }
2463
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002464 retval = smsc911x_mii_init(pdev, dev);
2465 if (retval) {
Joe Perchesdffc6b22011-03-25 14:21:22 +00002466 SMSC_WARN(pdata, probe, "Error %i initialising mii", retval);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002467 goto out_unregister_netdev_5;
2468 }
2469
2470 spin_lock_irq(&pdata->mac_lock);
2471
2472 /* Check if mac address has been specified when bringing interface up */
2473 if (is_valid_ether_addr(dev->dev_addr)) {
Steve Glendinning225ddf42009-03-19 00:24:46 +00002474 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
Joe Perchesdffc6b22011-03-25 14:21:22 +00002475 SMSC_TRACE(pdata, probe,
2476 "MAC Address is specified by configuration");
Manuel Laussaace4952009-10-13 07:25:49 +00002477 } else if (is_valid_ether_addr(pdata->config.mac)) {
2478 memcpy(dev->dev_addr, pdata->config.mac, 6);
Joe Perchesdffc6b22011-03-25 14:21:22 +00002479 SMSC_TRACE(pdata, probe,
2480 "MAC Address specified by platform data");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002481 } else {
2482 /* Try reading mac address from device. if EEPROM is present
2483 * it will already have been set */
Akira Takeuchi62747cd2010-10-27 17:28:58 +01002484 smsc_get_mac(dev);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002485
2486 if (is_valid_ether_addr(dev->dev_addr)) {
2487 /* eeprom values are valid so use them */
Joe Perchesdffc6b22011-03-25 14:21:22 +00002488 SMSC_TRACE(pdata, probe,
2489 "Mac Address is read from LAN911x EEPROM");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002490 } else {
2491 /* eeprom values are invalid, generate random MAC */
Danny Kukawka7ce5d222012-02-15 06:45:40 +00002492 eth_hw_addr_random(dev);
Steve Glendinning225ddf42009-03-19 00:24:46 +00002493 smsc911x_set_hw_mac_address(pdata, dev->dev_addr);
Joe Perchesdffc6b22011-03-25 14:21:22 +00002494 SMSC_TRACE(pdata, probe,
2495 "MAC Address is set to random_ether_addr");
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002496 }
2497 }
2498
2499 spin_unlock_irq(&pdata->mac_lock);
2500
Joe Perchesdffc6b22011-03-25 14:21:22 +00002501 netdev_info(dev, "MAC Address: %pM\n", dev->dev_addr);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002502
2503 return 0;
2504
2505out_unregister_netdev_5:
2506 unregister_netdev(dev);
Robert Marklundc7e963f2011-11-24 01:03:07 +00002507out_free_irq:
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002508 free_irq(dev->irq, dev);
Robert Marklundc7e963f2011-11-24 01:03:07 +00002509out_disable_resources:
2510 (void)smsc911x_disable_resources(pdev);
2511out_return_resources:
2512 smsc911x_free_resources(pdev);
2513 platform_set_drvdata(pdev, NULL);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002514 iounmap(pdata->ioaddr);
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002515 free_netdev(dev);
2516out_release_io_1:
Julia Lawall39424532009-07-04 11:31:47 +00002517 release_mem_region(res->start, resource_size(res));
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002518out_0:
2519 return retval;
2520}
2521
Daniel Mackb6907b02009-05-05 12:22:53 -07002522#ifdef CONFIG_PM
2523/* This implementation assumes the devices remains powered on its VDDVARIO
2524 * pins during suspend. */
2525
Daniel Mack6cb87822009-08-05 08:29:31 +00002526/* TODO: implement freeze/thaw callbacks for hibernation.*/
2527
2528static int smsc911x_suspend(struct device *dev)
Daniel Mackb6907b02009-05-05 12:22:53 -07002529{
Daniel Mack6cb87822009-08-05 08:29:31 +00002530 struct net_device *ndev = dev_get_drvdata(dev);
2531 struct smsc911x_data *pdata = netdev_priv(ndev);
Daniel Mackb6907b02009-05-05 12:22:53 -07002532
2533 /* enable wake on LAN, energy detection and the external PME
2534 * signal. */
2535 smsc911x_reg_write(pdata, PMT_CTRL,
2536 PMT_CTRL_PM_MODE_D1_ | PMT_CTRL_WOL_EN_ |
2537 PMT_CTRL_ED_EN_ | PMT_CTRL_PME_EN_);
2538
Steve Mucklef132c6c2012-06-06 18:30:57 -07002539 /* Drive the GPIO Ethernet_Reset Line low to Suspend */
2540 if (pdata->config.has_reset_gpio)
2541 gpio_set_value_cansleep(pdata->config.reset_gpio, 0);
2542
Daniel Mackb6907b02009-05-05 12:22:53 -07002543 return 0;
2544}
2545
Daniel Mack6cb87822009-08-05 08:29:31 +00002546static int smsc911x_resume(struct device *dev)
Daniel Mackb6907b02009-05-05 12:22:53 -07002547{
Daniel Mack6cb87822009-08-05 08:29:31 +00002548 struct net_device *ndev = dev_get_drvdata(dev);
2549 struct smsc911x_data *pdata = netdev_priv(ndev);
Daniel Mackb6907b02009-05-05 12:22:53 -07002550 unsigned int to = 100;
2551
Steve Mucklef132c6c2012-06-06 18:30:57 -07002552 if (pdata->config.has_reset_gpio)
2553 gpio_set_value_cansleep(pdata->config.reset_gpio, 1);
2554
2555
Daniel Mackb6907b02009-05-05 12:22:53 -07002556 /* Note 3.11 from the datasheet:
2557 * "When the LAN9220 is in a power saving state, a write of any
2558 * data to the BYTE_TEST register will wake-up the device."
2559 */
2560 smsc911x_reg_write(pdata, BYTE_TEST, 0);
2561
2562 /* poll the READY bit in PMT_CTRL. Any other access to the device is
2563 * forbidden while this bit isn't set. Try for 100ms and return -EIO
2564 * if it failed. */
2565 while (!(smsc911x_reg_read(pdata, PMT_CTRL) & PMT_CTRL_READY_) && --to)
2566 udelay(1000);
2567
2568 return (to == 0) ? -EIO : 0;
2569}
2570
Alexey Dobriyan47145212009-12-14 18:00:08 -08002571static const struct dev_pm_ops smsc911x_pm_ops = {
Daniel Mack6cb87822009-08-05 08:29:31 +00002572 .suspend = smsc911x_suspend,
2573 .resume = smsc911x_resume,
2574};
2575
2576#define SMSC911X_PM_OPS (&smsc911x_pm_ops)
2577
Daniel Mackb6907b02009-05-05 12:22:53 -07002578#else
Daniel Mack6cb87822009-08-05 08:29:31 +00002579#define SMSC911X_PM_OPS NULL
Daniel Mackb6907b02009-05-05 12:22:53 -07002580#endif
2581
Shawn Guo79f88ee2011-07-30 08:26:00 +00002582static const struct of_device_id smsc911x_dt_ids[] = {
2583 { .compatible = "smsc,lan9115", },
2584 { /* sentinel */ }
2585};
2586MODULE_DEVICE_TABLE(of, smsc911x_dt_ids);
2587
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002588static struct platform_driver smsc911x_driver = {
2589 .probe = smsc911x_drv_probe,
Mike Frysingerdf911e22009-06-05 14:37:20 +00002590 .remove = __devexit_p(smsc911x_drv_remove),
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002591 .driver = {
Daniel Mack6cb87822009-08-05 08:29:31 +00002592 .name = SMSC_CHIPNAME,
2593 .owner = THIS_MODULE,
2594 .pm = SMSC911X_PM_OPS,
Shawn Guo79f88ee2011-07-30 08:26:00 +00002595 .of_match_table = smsc911x_dt_ids,
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002596 },
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002597};
2598
2599/* Entry point for loading the module */
2600static int __init smsc911x_init_module(void)
2601{
Akira Takeuchi62747cd2010-10-27 17:28:58 +01002602 SMSC_INITIALIZE();
Steve Glendinningfd9abb32008-11-05 00:35:37 +00002603 return platform_driver_register(&smsc911x_driver);
2604}
2605
2606/* entry point for unloading the module */
2607static void __exit smsc911x_cleanup_module(void)
2608{
2609 platform_driver_unregister(&smsc911x_driver);
2610}
2611
2612module_init(smsc911x_init_module);
2613module_exit(smsc911x_cleanup_module);