blob: 66ebbacf066f6692ba21d2fa07538535827074b9 [file] [log] [blame]
Steve Glendinningd0cad872010-03-16 08:46:46 +00001 /***************************************************************************
2 *
3 * Copyright (C) 2007-2010 SMSC
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 *****************************************************************************/
20
21#include <linux/module.h>
22#include <linux/kmod.h>
23#include <linux/init.h>
24#include <linux/netdevice.h>
25#include <linux/etherdevice.h>
26#include <linux/ethtool.h>
27#include <linux/mii.h>
28#include <linux/usb.h>
Steve Glendinning899a3912012-10-30 07:46:32 +000029#include <linux/bitrev.h>
30#include <linux/crc16.h>
Steve Glendinningd0cad872010-03-16 08:46:46 +000031#include <linux/crc32.h>
32#include <linux/usb/usbnet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Steve Glendinningd0cad872010-03-16 08:46:46 +000034#include "smsc75xx.h"
35
36#define SMSC_CHIPNAME "smsc75xx"
37#define SMSC_DRIVER_VERSION "1.0.0"
38#define HS_USB_PKT_SIZE (512)
39#define FS_USB_PKT_SIZE (64)
40#define DEFAULT_HS_BURST_CAP_SIZE (16 * 1024 + 5 * HS_USB_PKT_SIZE)
41#define DEFAULT_FS_BURST_CAP_SIZE (6 * 1024 + 33 * FS_USB_PKT_SIZE)
42#define DEFAULT_BULK_IN_DELAY (0x00002000)
43#define MAX_SINGLE_PACKET_SIZE (9000)
44#define LAN75XX_EEPROM_MAGIC (0x7500)
45#define EEPROM_MAC_OFFSET (0x01)
46#define DEFAULT_TX_CSUM_ENABLE (true)
47#define DEFAULT_RX_CSUM_ENABLE (true)
Steve Glendinningd0cad872010-03-16 08:46:46 +000048#define SMSC75XX_INTERNAL_PHY_ID (1)
49#define SMSC75XX_TX_OVERHEAD (8)
50#define MAX_RX_FIFO_SIZE (20 * 1024)
51#define MAX_TX_FIFO_SIZE (12 * 1024)
52#define USB_VENDOR_ID_SMSC (0x0424)
53#define USB_PRODUCT_ID_LAN7500 (0x7500)
54#define USB_PRODUCT_ID_LAN7505 (0x7505)
Nico Erfurthea1649d2011-11-08 07:30:40 +000055#define RXW_PADDING 2
Steve Glendinningf329ccd2012-11-28 05:59:47 +000056#define SUPPORTED_WAKE (WAKE_PHY | WAKE_UCAST | WAKE_BCAST | \
Steve Glendinning899a3912012-10-30 07:46:32 +000057 WAKE_MCAST | WAKE_ARP | WAKE_MAGIC)
Steve Glendinningd0cad872010-03-16 08:46:46 +000058
Steve Glendinningb4cdea92012-11-28 05:59:49 +000059#define SUSPEND_SUSPEND0 (0x01)
60#define SUSPEND_SUSPEND1 (0x02)
61#define SUSPEND_SUSPEND2 (0x04)
62#define SUSPEND_SUSPEND3 (0x08)
Steve Glendinningb4cdea92012-11-28 05:59:49 +000063#define SUSPEND_ALLMODES (SUSPEND_SUSPEND0 | SUSPEND_SUSPEND1 | \
64 SUSPEND_SUSPEND2 | SUSPEND_SUSPEND3)
65
Steve Glendinningd0cad872010-03-16 08:46:46 +000066struct smsc75xx_priv {
67 struct usbnet *dev;
68 u32 rfe_ctl;
Steve Glendinning6c636502012-09-28 00:57:53 +000069 u32 wolopts;
Steve Glendinningd0cad872010-03-16 08:46:46 +000070 u32 multicast_hash_table[DP_SEL_VHF_HASH_LEN];
Steve Glendinningd0cad872010-03-16 08:46:46 +000071 struct mutex dataport_mutex;
72 spinlock_t rfe_ctl_lock;
73 struct work_struct set_multicast;
Steve Glendinningb4cdea92012-11-28 05:59:49 +000074 u8 suspend_flags;
Steve Glendinningd0cad872010-03-16 08:46:46 +000075};
76
77struct usb_context {
78 struct usb_ctrlrequest req;
79 struct usbnet *dev;
80};
81
Rusty Russelleb939922011-12-19 14:08:01 +000082static bool turbo_mode = true;
Steve Glendinningd0cad872010-03-16 08:46:46 +000083module_param(turbo_mode, bool, 0644);
84MODULE_PARM_DESC(turbo_mode, "Enable multiple frames per Rx transaction");
85
Ming Lei47bbea42012-11-06 04:53:05 +000086static int __must_check __smsc75xx_read_reg(struct usbnet *dev, u32 index,
87 u32 *data, int in_pm)
Steve Glendinningd0cad872010-03-16 08:46:46 +000088{
Ming Lei2b2e41e2012-10-24 19:47:03 +000089 u32 buf;
Steve Glendinningd0cad872010-03-16 08:46:46 +000090 int ret;
Ming Lei47bbea42012-11-06 04:53:05 +000091 int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
Steve Glendinningd0cad872010-03-16 08:46:46 +000092
93 BUG_ON(!dev);
94
Ming Lei47bbea42012-11-06 04:53:05 +000095 if (!in_pm)
96 fn = usbnet_read_cmd;
97 else
98 fn = usbnet_read_cmd_nopm;
99
100 ret = fn(dev, USB_VENDOR_REQUEST_READ_REGISTER, USB_DIR_IN
101 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
102 0, index, &buf, 4);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000103 if (unlikely(ret < 0))
Joe Perches1e1d7412012-11-24 01:27:49 +0000104 netdev_warn(dev->net, "Failed to read reg index 0x%08x: %d\n",
105 index, ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000106
Ming Lei2b2e41e2012-10-24 19:47:03 +0000107 le32_to_cpus(&buf);
108 *data = buf;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000109
110 return ret;
111}
112
Ming Lei47bbea42012-11-06 04:53:05 +0000113static int __must_check __smsc75xx_write_reg(struct usbnet *dev, u32 index,
114 u32 data, int in_pm)
Steve Glendinningd0cad872010-03-16 08:46:46 +0000115{
Ming Lei2b2e41e2012-10-24 19:47:03 +0000116 u32 buf;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000117 int ret;
Ming Lei47bbea42012-11-06 04:53:05 +0000118 int (*fn)(struct usbnet *, u8, u8, u16, u16, const void *, u16);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000119
120 BUG_ON(!dev);
121
Ming Lei47bbea42012-11-06 04:53:05 +0000122 if (!in_pm)
123 fn = usbnet_write_cmd;
124 else
125 fn = usbnet_write_cmd_nopm;
126
Ming Lei2b2e41e2012-10-24 19:47:03 +0000127 buf = data;
128 cpu_to_le32s(&buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000129
Ming Lei47bbea42012-11-06 04:53:05 +0000130 ret = fn(dev, USB_VENDOR_REQUEST_WRITE_REGISTER, USB_DIR_OUT
131 | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
132 0, index, &buf, 4);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000133 if (unlikely(ret < 0))
Joe Perches1e1d7412012-11-24 01:27:49 +0000134 netdev_warn(dev->net, "Failed to write reg index 0x%08x: %d\n",
135 index, ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000136
Steve Glendinningd0cad872010-03-16 08:46:46 +0000137 return ret;
138}
139
Ming Lei47bbea42012-11-06 04:53:05 +0000140static int __must_check smsc75xx_read_reg_nopm(struct usbnet *dev, u32 index,
141 u32 *data)
142{
143 return __smsc75xx_read_reg(dev, index, data, 1);
144}
145
146static int __must_check smsc75xx_write_reg_nopm(struct usbnet *dev, u32 index,
147 u32 data)
148{
149 return __smsc75xx_write_reg(dev, index, data, 1);
150}
151
152static int __must_check smsc75xx_read_reg(struct usbnet *dev, u32 index,
153 u32 *data)
154{
155 return __smsc75xx_read_reg(dev, index, data, 0);
156}
157
158static int __must_check smsc75xx_write_reg(struct usbnet *dev, u32 index,
159 u32 data)
160{
161 return __smsc75xx_write_reg(dev, index, data, 0);
162}
163
Steve Glendinningd0cad872010-03-16 08:46:46 +0000164/* Loop until the read is completed with timeout
165 * called with phy_mutex held */
Steve Glendinningf329ccd2012-11-28 05:59:47 +0000166static __must_check int __smsc75xx_phy_wait_not_busy(struct usbnet *dev,
167 int in_pm)
Steve Glendinningd0cad872010-03-16 08:46:46 +0000168{
169 unsigned long start_time = jiffies;
170 u32 val;
171 int ret;
172
173 do {
Steve Glendinningf329ccd2012-11-28 05:59:47 +0000174 ret = __smsc75xx_read_reg(dev, MII_ACCESS, &val, in_pm);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000175 if (ret < 0) {
176 netdev_warn(dev->net, "Error reading MII_ACCESS\n");
177 return ret;
178 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000179
180 if (!(val & MII_ACCESS_BUSY))
181 return 0;
182 } while (!time_after(jiffies, start_time + HZ));
183
184 return -EIO;
185}
186
Steve Glendinningf329ccd2012-11-28 05:59:47 +0000187static int __smsc75xx_mdio_read(struct net_device *netdev, int phy_id, int idx,
188 int in_pm)
Steve Glendinningd0cad872010-03-16 08:46:46 +0000189{
190 struct usbnet *dev = netdev_priv(netdev);
191 u32 val, addr;
192 int ret;
193
194 mutex_lock(&dev->phy_mutex);
195
196 /* confirm MII not busy */
Steve Glendinningf329ccd2012-11-28 05:59:47 +0000197 ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000198 if (ret < 0) {
199 netdev_warn(dev->net, "MII is busy in smsc75xx_mdio_read\n");
200 goto done;
201 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000202
203 /* set the address, index & direction (read from PHY) */
204 phy_id &= dev->mii.phy_id_mask;
205 idx &= dev->mii.reg_num_mask;
206 addr = ((phy_id << MII_ACCESS_PHY_ADDR_SHIFT) & MII_ACCESS_PHY_ADDR)
207 | ((idx << MII_ACCESS_REG_ADDR_SHIFT) & MII_ACCESS_REG_ADDR)
Steve Glendinningcb8722d2012-04-30 07:56:51 +0000208 | MII_ACCESS_READ | MII_ACCESS_BUSY;
Steve Glendinningf329ccd2012-11-28 05:59:47 +0000209 ret = __smsc75xx_write_reg(dev, MII_ACCESS, addr, in_pm);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000210 if (ret < 0) {
211 netdev_warn(dev->net, "Error writing MII_ACCESS\n");
212 goto done;
213 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000214
Steve Glendinningf329ccd2012-11-28 05:59:47 +0000215 ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000216 if (ret < 0) {
217 netdev_warn(dev->net, "Timed out reading MII reg %02X\n", idx);
218 goto done;
219 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000220
Steve Glendinningf329ccd2012-11-28 05:59:47 +0000221 ret = __smsc75xx_read_reg(dev, MII_DATA, &val, in_pm);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000222 if (ret < 0) {
223 netdev_warn(dev->net, "Error reading MII_DATA\n");
224 goto done;
225 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000226
227 ret = (u16)(val & 0xFFFF);
228
229done:
230 mutex_unlock(&dev->phy_mutex);
231 return ret;
232}
233
Steve Glendinningf329ccd2012-11-28 05:59:47 +0000234static void __smsc75xx_mdio_write(struct net_device *netdev, int phy_id,
235 int idx, int regval, int in_pm)
Steve Glendinningd0cad872010-03-16 08:46:46 +0000236{
237 struct usbnet *dev = netdev_priv(netdev);
238 u32 val, addr;
239 int ret;
240
241 mutex_lock(&dev->phy_mutex);
242
243 /* confirm MII not busy */
Steve Glendinningf329ccd2012-11-28 05:59:47 +0000244 ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000245 if (ret < 0) {
246 netdev_warn(dev->net, "MII is busy in smsc75xx_mdio_write\n");
247 goto done;
248 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000249
250 val = regval;
Steve Glendinningf329ccd2012-11-28 05:59:47 +0000251 ret = __smsc75xx_write_reg(dev, MII_DATA, val, in_pm);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000252 if (ret < 0) {
253 netdev_warn(dev->net, "Error writing MII_DATA\n");
254 goto done;
255 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000256
257 /* set the address, index & direction (write to PHY) */
258 phy_id &= dev->mii.phy_id_mask;
259 idx &= dev->mii.reg_num_mask;
260 addr = ((phy_id << MII_ACCESS_PHY_ADDR_SHIFT) & MII_ACCESS_PHY_ADDR)
261 | ((idx << MII_ACCESS_REG_ADDR_SHIFT) & MII_ACCESS_REG_ADDR)
Steve Glendinningcb8722d2012-04-30 07:56:51 +0000262 | MII_ACCESS_WRITE | MII_ACCESS_BUSY;
Steve Glendinningf329ccd2012-11-28 05:59:47 +0000263 ret = __smsc75xx_write_reg(dev, MII_ACCESS, addr, in_pm);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000264 if (ret < 0) {
265 netdev_warn(dev->net, "Error writing MII_ACCESS\n");
266 goto done;
267 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000268
Steve Glendinningf329ccd2012-11-28 05:59:47 +0000269 ret = __smsc75xx_phy_wait_not_busy(dev, in_pm);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000270 if (ret < 0) {
271 netdev_warn(dev->net, "Timed out writing MII reg %02X\n", idx);
272 goto done;
273 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000274
275done:
276 mutex_unlock(&dev->phy_mutex);
277}
278
Steve Glendinningf329ccd2012-11-28 05:59:47 +0000279static int smsc75xx_mdio_read_nopm(struct net_device *netdev, int phy_id,
280 int idx)
281{
282 return __smsc75xx_mdio_read(netdev, phy_id, idx, 1);
283}
284
285static void smsc75xx_mdio_write_nopm(struct net_device *netdev, int phy_id,
286 int idx, int regval)
287{
288 __smsc75xx_mdio_write(netdev, phy_id, idx, regval, 1);
289}
290
291static int smsc75xx_mdio_read(struct net_device *netdev, int phy_id, int idx)
292{
293 return __smsc75xx_mdio_read(netdev, phy_id, idx, 0);
294}
295
296static void smsc75xx_mdio_write(struct net_device *netdev, int phy_id, int idx,
297 int regval)
298{
299 __smsc75xx_mdio_write(netdev, phy_id, idx, regval, 0);
300}
301
Steve Glendinningd0cad872010-03-16 08:46:46 +0000302static int smsc75xx_wait_eeprom(struct usbnet *dev)
303{
304 unsigned long start_time = jiffies;
305 u32 val;
306 int ret;
307
308 do {
309 ret = smsc75xx_read_reg(dev, E2P_CMD, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000310 if (ret < 0) {
311 netdev_warn(dev->net, "Error reading E2P_CMD\n");
312 return ret;
313 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000314
315 if (!(val & E2P_CMD_BUSY) || (val & E2P_CMD_TIMEOUT))
316 break;
317 udelay(40);
318 } while (!time_after(jiffies, start_time + HZ));
319
320 if (val & (E2P_CMD_TIMEOUT | E2P_CMD_BUSY)) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000321 netdev_warn(dev->net, "EEPROM read operation timeout\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000322 return -EIO;
323 }
324
325 return 0;
326}
327
328static int smsc75xx_eeprom_confirm_not_busy(struct usbnet *dev)
329{
330 unsigned long start_time = jiffies;
331 u32 val;
332 int ret;
333
334 do {
335 ret = smsc75xx_read_reg(dev, E2P_CMD, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000336 if (ret < 0) {
337 netdev_warn(dev->net, "Error reading E2P_CMD\n");
338 return ret;
339 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000340
341 if (!(val & E2P_CMD_BUSY))
342 return 0;
343
344 udelay(40);
345 } while (!time_after(jiffies, start_time + HZ));
346
Joe Perches1e1d7412012-11-24 01:27:49 +0000347 netdev_warn(dev->net, "EEPROM is busy\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000348 return -EIO;
349}
350
351static int smsc75xx_read_eeprom(struct usbnet *dev, u32 offset, u32 length,
352 u8 *data)
353{
354 u32 val;
355 int i, ret;
356
357 BUG_ON(!dev);
358 BUG_ON(!data);
359
360 ret = smsc75xx_eeprom_confirm_not_busy(dev);
361 if (ret)
362 return ret;
363
364 for (i = 0; i < length; i++) {
365 val = E2P_CMD_BUSY | E2P_CMD_READ | (offset & E2P_CMD_ADDR);
366 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000367 if (ret < 0) {
368 netdev_warn(dev->net, "Error writing E2P_CMD\n");
369 return ret;
370 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000371
372 ret = smsc75xx_wait_eeprom(dev);
373 if (ret < 0)
374 return ret;
375
376 ret = smsc75xx_read_reg(dev, E2P_DATA, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000377 if (ret < 0) {
378 netdev_warn(dev->net, "Error reading E2P_DATA\n");
379 return ret;
380 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000381
382 data[i] = val & 0xFF;
383 offset++;
384 }
385
386 return 0;
387}
388
389static int smsc75xx_write_eeprom(struct usbnet *dev, u32 offset, u32 length,
390 u8 *data)
391{
392 u32 val;
393 int i, ret;
394
395 BUG_ON(!dev);
396 BUG_ON(!data);
397
398 ret = smsc75xx_eeprom_confirm_not_busy(dev);
399 if (ret)
400 return ret;
401
402 /* Issue write/erase enable command */
403 val = E2P_CMD_BUSY | E2P_CMD_EWEN;
404 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000405 if (ret < 0) {
406 netdev_warn(dev->net, "Error writing E2P_CMD\n");
407 return ret;
408 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000409
410 ret = smsc75xx_wait_eeprom(dev);
411 if (ret < 0)
412 return ret;
413
414 for (i = 0; i < length; i++) {
415
416 /* Fill data register */
417 val = data[i];
418 ret = smsc75xx_write_reg(dev, E2P_DATA, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000419 if (ret < 0) {
420 netdev_warn(dev->net, "Error writing E2P_DATA\n");
421 return ret;
422 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000423
424 /* Send "write" command */
425 val = E2P_CMD_BUSY | E2P_CMD_WRITE | (offset & E2P_CMD_ADDR);
426 ret = smsc75xx_write_reg(dev, E2P_CMD, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000427 if (ret < 0) {
428 netdev_warn(dev->net, "Error writing E2P_CMD\n");
429 return ret;
430 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000431
432 ret = smsc75xx_wait_eeprom(dev);
433 if (ret < 0)
434 return ret;
435
436 offset++;
437 }
438
439 return 0;
440}
441
442static int smsc75xx_dataport_wait_not_busy(struct usbnet *dev)
443{
444 int i, ret;
445
446 for (i = 0; i < 100; i++) {
447 u32 dp_sel;
448 ret = smsc75xx_read_reg(dev, DP_SEL, &dp_sel);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000449 if (ret < 0) {
450 netdev_warn(dev->net, "Error reading DP_SEL\n");
451 return ret;
452 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000453
454 if (dp_sel & DP_SEL_DPRDY)
455 return 0;
456
457 udelay(40);
458 }
459
Joe Perches1e1d7412012-11-24 01:27:49 +0000460 netdev_warn(dev->net, "smsc75xx_dataport_wait_not_busy timed out\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000461
462 return -EIO;
463}
464
465static int smsc75xx_dataport_write(struct usbnet *dev, u32 ram_select, u32 addr,
466 u32 length, u32 *buf)
467{
468 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
469 u32 dp_sel;
470 int i, ret;
471
472 mutex_lock(&pdata->dataport_mutex);
473
474 ret = smsc75xx_dataport_wait_not_busy(dev);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000475 if (ret < 0) {
476 netdev_warn(dev->net, "smsc75xx_dataport_write busy on entry\n");
477 goto done;
478 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000479
480 ret = smsc75xx_read_reg(dev, DP_SEL, &dp_sel);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000481 if (ret < 0) {
482 netdev_warn(dev->net, "Error reading DP_SEL\n");
483 goto done;
484 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000485
486 dp_sel &= ~DP_SEL_RSEL;
487 dp_sel |= ram_select;
488 ret = smsc75xx_write_reg(dev, DP_SEL, dp_sel);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000489 if (ret < 0) {
490 netdev_warn(dev->net, "Error writing DP_SEL\n");
491 goto done;
492 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000493
494 for (i = 0; i < length; i++) {
495 ret = smsc75xx_write_reg(dev, DP_ADDR, addr + i);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000496 if (ret < 0) {
497 netdev_warn(dev->net, "Error writing DP_ADDR\n");
498 goto done;
499 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000500
501 ret = smsc75xx_write_reg(dev, DP_DATA, buf[i]);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000502 if (ret < 0) {
503 netdev_warn(dev->net, "Error writing DP_DATA\n");
504 goto done;
505 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000506
507 ret = smsc75xx_write_reg(dev, DP_CMD, DP_CMD_WRITE);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000508 if (ret < 0) {
509 netdev_warn(dev->net, "Error writing DP_CMD\n");
510 goto done;
511 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000512
513 ret = smsc75xx_dataport_wait_not_busy(dev);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000514 if (ret < 0) {
515 netdev_warn(dev->net, "smsc75xx_dataport_write timeout\n");
516 goto done;
517 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000518 }
519
520done:
521 mutex_unlock(&pdata->dataport_mutex);
522 return ret;
523}
524
525/* returns hash bit number for given MAC address */
526static u32 smsc75xx_hash(char addr[ETH_ALEN])
527{
528 return (ether_crc(ETH_ALEN, addr) >> 23) & 0x1ff;
529}
530
531static void smsc75xx_deferred_multicast_write(struct work_struct *param)
532{
533 struct smsc75xx_priv *pdata =
534 container_of(param, struct smsc75xx_priv, set_multicast);
535 struct usbnet *dev = pdata->dev;
536 int ret;
537
Joe Perches1e1d7412012-11-24 01:27:49 +0000538 netif_dbg(dev, drv, dev->net, "deferred multicast write 0x%08x\n",
539 pdata->rfe_ctl);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000540
541 smsc75xx_dataport_write(dev, DP_SEL_VHF, DP_SEL_VHF_VLAN_LEN,
542 DP_SEL_VHF_HASH_LEN, pdata->multicast_hash_table);
543
544 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000545 if (ret < 0)
546 netdev_warn(dev->net, "Error writing RFE_CRL\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000547}
548
549static void smsc75xx_set_multicast(struct net_device *netdev)
550{
551 struct usbnet *dev = netdev_priv(netdev);
552 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
553 unsigned long flags;
554 int i;
555
556 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
557
558 pdata->rfe_ctl &=
559 ~(RFE_CTL_AU | RFE_CTL_AM | RFE_CTL_DPF | RFE_CTL_MHF);
560 pdata->rfe_ctl |= RFE_CTL_AB;
561
562 for (i = 0; i < DP_SEL_VHF_HASH_LEN; i++)
563 pdata->multicast_hash_table[i] = 0;
564
565 if (dev->net->flags & IFF_PROMISC) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000566 netif_dbg(dev, drv, dev->net, "promiscuous mode enabled\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000567 pdata->rfe_ctl |= RFE_CTL_AM | RFE_CTL_AU;
568 } else if (dev->net->flags & IFF_ALLMULTI) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000569 netif_dbg(dev, drv, dev->net, "receive all multicast enabled\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000570 pdata->rfe_ctl |= RFE_CTL_AM | RFE_CTL_DPF;
571 } else if (!netdev_mc_empty(dev->net)) {
Jiri Pirko22bedad32010-04-01 21:22:57 +0000572 struct netdev_hw_addr *ha;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000573
Joe Perches1e1d7412012-11-24 01:27:49 +0000574 netif_dbg(dev, drv, dev->net, "receive multicast hash filter\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000575
576 pdata->rfe_ctl |= RFE_CTL_MHF | RFE_CTL_DPF;
577
Jiri Pirko22bedad32010-04-01 21:22:57 +0000578 netdev_for_each_mc_addr(ha, netdev) {
579 u32 bitnum = smsc75xx_hash(ha->addr);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000580 pdata->multicast_hash_table[bitnum / 32] |=
581 (1 << (bitnum % 32));
582 }
583 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +0000584 netif_dbg(dev, drv, dev->net, "receive own packets only\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000585 pdata->rfe_ctl |= RFE_CTL_DPF;
586 }
587
588 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
589
590 /* defer register writes to a sleepable context */
591 schedule_work(&pdata->set_multicast);
592}
593
594static int smsc75xx_update_flowcontrol(struct usbnet *dev, u8 duplex,
595 u16 lcladv, u16 rmtadv)
596{
597 u32 flow = 0, fct_flow = 0;
598 int ret;
599
600 if (duplex == DUPLEX_FULL) {
601 u8 cap = mii_resolve_flowctrl_fdx(lcladv, rmtadv);
602
603 if (cap & FLOW_CTRL_TX) {
604 flow = (FLOW_TX_FCEN | 0xFFFF);
605 /* set fct_flow thresholds to 20% and 80% */
606 fct_flow = (8 << 8) | 32;
607 }
608
609 if (cap & FLOW_CTRL_RX)
610 flow |= FLOW_RX_FCEN;
611
Joe Perches1e1d7412012-11-24 01:27:49 +0000612 netif_dbg(dev, link, dev->net, "rx pause %s, tx pause %s\n",
613 (cap & FLOW_CTRL_RX ? "enabled" : "disabled"),
614 (cap & FLOW_CTRL_TX ? "enabled" : "disabled"));
Steve Glendinningd0cad872010-03-16 08:46:46 +0000615 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +0000616 netif_dbg(dev, link, dev->net, "half duplex\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000617 }
618
619 ret = smsc75xx_write_reg(dev, FLOW, flow);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000620 if (ret < 0) {
621 netdev_warn(dev->net, "Error writing FLOW\n");
622 return ret;
623 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000624
625 ret = smsc75xx_write_reg(dev, FCT_FLOW, fct_flow);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000626 if (ret < 0) {
627 netdev_warn(dev->net, "Error writing FCT_FLOW\n");
628 return ret;
629 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000630
631 return 0;
632}
633
634static int smsc75xx_link_reset(struct usbnet *dev)
635{
636 struct mii_if_info *mii = &dev->mii;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000637 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET };
Steve Glendinningd0cad872010-03-16 08:46:46 +0000638 u16 lcladv, rmtadv;
639 int ret;
640
Steve Glendinning4f94a922012-05-04 00:57:12 +0000641 /* write to clear phy interrupt status */
Steve Glendinning77496222012-05-04 00:57:11 +0000642 smsc75xx_mdio_write(dev->net, mii->phy_id, PHY_INT_SRC,
643 PHY_INT_SRC_CLEAR_ALL);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000644
645 ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000646 if (ret < 0) {
647 netdev_warn(dev->net, "Error writing INT_STS\n");
648 return ret;
649 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000650
651 mii_check_media(mii, 1, 1);
652 mii_ethtool_gset(&dev->mii, &ecmd);
653 lcladv = smsc75xx_mdio_read(dev->net, mii->phy_id, MII_ADVERTISE);
654 rmtadv = smsc75xx_mdio_read(dev->net, mii->phy_id, MII_LPA);
655
Joe Perches1e1d7412012-11-24 01:27:49 +0000656 netif_dbg(dev, link, dev->net, "speed: %u duplex: %d lcladv: %04x rmtadv: %04x\n",
657 ethtool_cmd_speed(&ecmd), ecmd.duplex, lcladv, rmtadv);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000658
659 return smsc75xx_update_flowcontrol(dev, ecmd.duplex, lcladv, rmtadv);
660}
661
662static void smsc75xx_status(struct usbnet *dev, struct urb *urb)
663{
664 u32 intdata;
665
666 if (urb->actual_length != 4) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000667 netdev_warn(dev->net, "unexpected urb length %d\n",
668 urb->actual_length);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000669 return;
670 }
671
672 memcpy(&intdata, urb->transfer_buffer, 4);
673 le32_to_cpus(&intdata);
674
Joe Perches1e1d7412012-11-24 01:27:49 +0000675 netif_dbg(dev, link, dev->net, "intdata: 0x%08X\n", intdata);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000676
677 if (intdata & INT_ENP_PHY_INT)
678 usbnet_defer_kevent(dev, EVENT_LINK_RESET);
679 else
Joe Perches1e1d7412012-11-24 01:27:49 +0000680 netdev_warn(dev->net, "unexpected interrupt, intdata=0x%08X\n",
681 intdata);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000682}
683
Steve Glendinningd0cad872010-03-16 08:46:46 +0000684static int smsc75xx_ethtool_get_eeprom_len(struct net_device *net)
685{
686 return MAX_EEPROM_SIZE;
687}
688
689static int smsc75xx_ethtool_get_eeprom(struct net_device *netdev,
690 struct ethtool_eeprom *ee, u8 *data)
691{
692 struct usbnet *dev = netdev_priv(netdev);
693
694 ee->magic = LAN75XX_EEPROM_MAGIC;
695
696 return smsc75xx_read_eeprom(dev, ee->offset, ee->len, data);
697}
698
699static int smsc75xx_ethtool_set_eeprom(struct net_device *netdev,
700 struct ethtool_eeprom *ee, u8 *data)
701{
702 struct usbnet *dev = netdev_priv(netdev);
703
704 if (ee->magic != LAN75XX_EEPROM_MAGIC) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000705 netdev_warn(dev->net, "EEPROM: magic value mismatch: 0x%x\n",
706 ee->magic);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000707 return -EINVAL;
708 }
709
710 return smsc75xx_write_eeprom(dev, ee->offset, ee->len, data);
711}
712
Steve Glendinning6c636502012-09-28 00:57:53 +0000713static void smsc75xx_ethtool_get_wol(struct net_device *net,
714 struct ethtool_wolinfo *wolinfo)
715{
716 struct usbnet *dev = netdev_priv(net);
717 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
718
719 wolinfo->supported = SUPPORTED_WAKE;
720 wolinfo->wolopts = pdata->wolopts;
721}
722
723static int smsc75xx_ethtool_set_wol(struct net_device *net,
724 struct ethtool_wolinfo *wolinfo)
725{
726 struct usbnet *dev = netdev_priv(net);
727 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
Steve Glendinning351f33d2012-11-28 05:59:50 +0000728 int ret;
Steve Glendinning6c636502012-09-28 00:57:53 +0000729
730 pdata->wolopts = wolinfo->wolopts & SUPPORTED_WAKE;
Steve Glendinning351f33d2012-11-28 05:59:50 +0000731
732 ret = device_set_wakeup_enable(&dev->udev->dev, pdata->wolopts);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000733 if (ret < 0)
734 netdev_warn(dev->net, "device_set_wakeup_enable error %d\n", ret);
Steve Glendinning351f33d2012-11-28 05:59:50 +0000735
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000736 return ret;
Steve Glendinning6c636502012-09-28 00:57:53 +0000737}
738
Steve Glendinningd0cad872010-03-16 08:46:46 +0000739static const struct ethtool_ops smsc75xx_ethtool_ops = {
740 .get_link = usbnet_get_link,
741 .nway_reset = usbnet_nway_reset,
742 .get_drvinfo = usbnet_get_drvinfo,
743 .get_msglevel = usbnet_get_msglevel,
744 .set_msglevel = usbnet_set_msglevel,
745 .get_settings = usbnet_get_settings,
746 .set_settings = usbnet_set_settings,
747 .get_eeprom_len = smsc75xx_ethtool_get_eeprom_len,
748 .get_eeprom = smsc75xx_ethtool_get_eeprom,
749 .set_eeprom = smsc75xx_ethtool_set_eeprom,
Steve Glendinning6c636502012-09-28 00:57:53 +0000750 .get_wol = smsc75xx_ethtool_get_wol,
751 .set_wol = smsc75xx_ethtool_set_wol,
Steve Glendinningd0cad872010-03-16 08:46:46 +0000752};
753
754static int smsc75xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
755{
756 struct usbnet *dev = netdev_priv(netdev);
757
758 if (!netif_running(netdev))
759 return -EINVAL;
760
761 return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
762}
763
764static void smsc75xx_init_mac_address(struct usbnet *dev)
765{
766 /* try reading mac address from EEPROM */
767 if (smsc75xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
768 dev->net->dev_addr) == 0) {
769 if (is_valid_ether_addr(dev->net->dev_addr)) {
770 /* eeprom values are valid so use them */
771 netif_dbg(dev, ifup, dev->net,
Joe Perches1e1d7412012-11-24 01:27:49 +0000772 "MAC address read from EEPROM\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000773 return;
774 }
775 }
776
777 /* no eeprom, or eeprom values are invalid. generate random MAC */
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000778 eth_hw_addr_random(dev->net);
Joe Perches1e1d7412012-11-24 01:27:49 +0000779 netif_dbg(dev, ifup, dev->net, "MAC address set to eth_random_addr\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000780}
781
782static int smsc75xx_set_mac_address(struct usbnet *dev)
783{
784 u32 addr_lo = dev->net->dev_addr[0] | dev->net->dev_addr[1] << 8 |
785 dev->net->dev_addr[2] << 16 | dev->net->dev_addr[3] << 24;
786 u32 addr_hi = dev->net->dev_addr[4] | dev->net->dev_addr[5] << 8;
787
788 int ret = smsc75xx_write_reg(dev, RX_ADDRH, addr_hi);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000789 if (ret < 0) {
790 netdev_warn(dev->net, "Failed to write RX_ADDRH: %d\n", ret);
791 return ret;
792 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000793
794 ret = smsc75xx_write_reg(dev, RX_ADDRL, addr_lo);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000795 if (ret < 0) {
796 netdev_warn(dev->net, "Failed to write RX_ADDRL: %d\n", ret);
797 return ret;
798 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000799
800 addr_hi |= ADDR_FILTX_FB_VALID;
801 ret = smsc75xx_write_reg(dev, ADDR_FILTX, addr_hi);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000802 if (ret < 0) {
803 netdev_warn(dev->net, "Failed to write ADDR_FILTX: %d\n", ret);
804 return ret;
805 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000806
807 ret = smsc75xx_write_reg(dev, ADDR_FILTX + 4, addr_lo);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000808 if (ret < 0)
809 netdev_warn(dev->net, "Failed to write ADDR_FILTX+4: %d\n", ret);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000810
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000811 return ret;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000812}
813
814static int smsc75xx_phy_initialize(struct usbnet *dev)
815{
Steve Glendinningb1405042012-04-30 07:56:54 +0000816 int bmcr, ret, timeout = 0;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000817
818 /* Initialize MII structure */
819 dev->mii.dev = dev->net;
820 dev->mii.mdio_read = smsc75xx_mdio_read;
821 dev->mii.mdio_write = smsc75xx_mdio_write;
822 dev->mii.phy_id_mask = 0x1f;
823 dev->mii.reg_num_mask = 0x1f;
Steve Glendinningc0b92e42012-04-30 07:56:55 +0000824 dev->mii.supports_gmii = 1;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000825 dev->mii.phy_id = SMSC75XX_INTERNAL_PHY_ID;
826
827 /* reset phy and wait for reset to complete */
828 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
829
830 do {
831 msleep(10);
832 bmcr = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, MII_BMCR);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000833 if (bmcr < 0) {
834 netdev_warn(dev->net, "Error reading MII_BMCR\n");
835 return bmcr;
836 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000837 timeout++;
Steve Glendinning8a1d59d2012-04-30 07:56:53 +0000838 } while ((bmcr & BMCR_RESET) && (timeout < 100));
Steve Glendinningd0cad872010-03-16 08:46:46 +0000839
840 if (timeout >= 100) {
Joe Perches1e1d7412012-11-24 01:27:49 +0000841 netdev_warn(dev->net, "timeout on PHY Reset\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000842 return -EIO;
843 }
844
845 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
846 ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP |
847 ADVERTISE_PAUSE_ASYM);
Steve Glendinningc0b92e42012-04-30 07:56:55 +0000848 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, MII_CTRL1000,
849 ADVERTISE_1000FULL);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000850
Steve Glendinningb1405042012-04-30 07:56:54 +0000851 /* read and write to clear phy interrupt status */
852 ret = smsc75xx_mdio_read(dev->net, dev->mii.phy_id, PHY_INT_SRC);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000853 if (ret < 0) {
854 netdev_warn(dev->net, "Error reading PHY_INT_SRC\n");
855 return ret;
856 }
857
Steve Glendinningb1405042012-04-30 07:56:54 +0000858 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_SRC, 0xffff);
Steve Glendinningd0cad872010-03-16 08:46:46 +0000859
860 smsc75xx_mdio_write(dev->net, dev->mii.phy_id, PHY_INT_MASK,
861 PHY_INT_MASK_DEFAULT);
862 mii_nway_restart(&dev->mii);
863
Joe Perches1e1d7412012-11-24 01:27:49 +0000864 netif_dbg(dev, ifup, dev->net, "phy initialised successfully\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000865 return 0;
866}
867
868static int smsc75xx_set_rx_max_frame_length(struct usbnet *dev, int size)
869{
870 int ret = 0;
871 u32 buf;
872 bool rxenabled;
873
874 ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000875 if (ret < 0) {
876 netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
877 return ret;
878 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000879
880 rxenabled = ((buf & MAC_RX_RXEN) != 0);
881
882 if (rxenabled) {
883 buf &= ~MAC_RX_RXEN;
884 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000885 if (ret < 0) {
886 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
887 return ret;
888 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000889 }
890
891 /* add 4 to size for FCS */
892 buf &= ~MAC_RX_MAX_SIZE;
893 buf |= (((size + 4) << MAC_RX_MAX_SIZE_SHIFT) & MAC_RX_MAX_SIZE);
894
895 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000896 if (ret < 0) {
897 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
898 return ret;
899 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000900
901 if (rxenabled) {
902 buf |= MAC_RX_RXEN;
903 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000904 if (ret < 0) {
905 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
906 return ret;
907 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000908 }
909
910 return 0;
911}
912
913static int smsc75xx_change_mtu(struct net_device *netdev, int new_mtu)
914{
915 struct usbnet *dev = netdev_priv(netdev);
Steve Glendinning4c51e532013-03-28 02:34:41 +0000916 int ret;
Steve Glendinningd0cad872010-03-16 08:46:46 +0000917
Steve Glendinning4c51e532013-03-28 02:34:41 +0000918 if (new_mtu > MAX_SINGLE_PACKET_SIZE)
919 return -EINVAL;
920
921 ret = smsc75xx_set_rx_max_frame_length(dev, new_mtu + ETH_HLEN);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000922 if (ret < 0) {
923 netdev_warn(dev->net, "Failed to set mac rx frame length\n");
924 return ret;
925 }
Steve Glendinningd0cad872010-03-16 08:46:46 +0000926
927 return usbnet_change_mtu(netdev, new_mtu);
928}
929
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700930/* Enable or disable Rx checksum offload engine */
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000931static int smsc75xx_set_features(struct net_device *netdev,
932 netdev_features_t features)
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700933{
934 struct usbnet *dev = netdev_priv(netdev);
935 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
936 unsigned long flags;
937 int ret;
938
939 spin_lock_irqsave(&pdata->rfe_ctl_lock, flags);
940
941 if (features & NETIF_F_RXCSUM)
942 pdata->rfe_ctl |= RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM;
943 else
944 pdata->rfe_ctl &= ~(RFE_CTL_TCPUDP_CKM | RFE_CTL_IP_CKM);
945
946 spin_unlock_irqrestore(&pdata->rfe_ctl_lock, flags);
947 /* it's racing here! */
948
949 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000950 if (ret < 0)
951 netdev_warn(dev->net, "Error writing RFE_CTL\n");
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700952
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000953 return ret;
Michał Mirosław78e47fe2011-04-01 20:56:23 -0700954}
955
Ming Lei47bbea42012-11-06 04:53:05 +0000956static int smsc75xx_wait_ready(struct usbnet *dev, int in_pm)
Steve Glendinning8762cec2012-09-28 00:57:51 +0000957{
958 int timeout = 0;
959
960 do {
961 u32 buf;
Ming Lei47bbea42012-11-06 04:53:05 +0000962 int ret;
963
964 ret = __smsc75xx_read_reg(dev, PMT_CTL, &buf, in_pm);
965
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000966 if (ret < 0) {
967 netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
968 return ret;
969 }
Steve Glendinning8762cec2012-09-28 00:57:51 +0000970
971 if (buf & PMT_CTL_DEV_RDY)
972 return 0;
973
974 msleep(10);
975 timeout++;
976 } while (timeout < 100);
977
Joe Perches1e1d7412012-11-24 01:27:49 +0000978 netdev_warn(dev->net, "timeout waiting for device ready\n");
Steve Glendinning8762cec2012-09-28 00:57:51 +0000979 return -EIO;
980}
981
Steve Glendinningd0cad872010-03-16 08:46:46 +0000982static int smsc75xx_reset(struct usbnet *dev)
983{
984 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
985 u32 buf;
986 int ret = 0, timeout;
987
Joe Perches1e1d7412012-11-24 01:27:49 +0000988 netif_dbg(dev, ifup, dev->net, "entering smsc75xx_reset\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +0000989
Ming Lei47bbea42012-11-06 04:53:05 +0000990 ret = smsc75xx_wait_ready(dev, 0);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000991 if (ret < 0) {
992 netdev_warn(dev->net, "device not ready in smsc75xx_reset\n");
993 return ret;
994 }
Steve Glendinning8762cec2012-09-28 00:57:51 +0000995
Steve Glendinningd0cad872010-03-16 08:46:46 +0000996 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +0000997 if (ret < 0) {
998 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
999 return ret;
1000 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001001
1002 buf |= HW_CFG_LRST;
1003
1004 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001005 if (ret < 0) {
1006 netdev_warn(dev->net, "Failed to write HW_CFG: %d\n", ret);
1007 return ret;
1008 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001009
1010 timeout = 0;
1011 do {
1012 msleep(10);
1013 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001014 if (ret < 0) {
1015 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1016 return ret;
1017 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001018 timeout++;
1019 } while ((buf & HW_CFG_LRST) && (timeout < 100));
1020
1021 if (timeout >= 100) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001022 netdev_warn(dev->net, "timeout on completion of Lite Reset\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +00001023 return -EIO;
1024 }
1025
Joe Perches1e1d7412012-11-24 01:27:49 +00001026 netif_dbg(dev, ifup, dev->net, "Lite reset complete, resetting PHY\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +00001027
1028 ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001029 if (ret < 0) {
1030 netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
1031 return ret;
1032 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001033
1034 buf |= PMT_CTL_PHY_RST;
1035
1036 ret = smsc75xx_write_reg(dev, PMT_CTL, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001037 if (ret < 0) {
1038 netdev_warn(dev->net, "Failed to write PMT_CTL: %d\n", ret);
1039 return ret;
1040 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001041
1042 timeout = 0;
1043 do {
1044 msleep(10);
1045 ret = smsc75xx_read_reg(dev, PMT_CTL, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001046 if (ret < 0) {
1047 netdev_warn(dev->net, "Failed to read PMT_CTL: %d\n", ret);
1048 return ret;
1049 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001050 timeout++;
1051 } while ((buf & PMT_CTL_PHY_RST) && (timeout < 100));
1052
1053 if (timeout >= 100) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001054 netdev_warn(dev->net, "timeout waiting for PHY Reset\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +00001055 return -EIO;
1056 }
1057
Joe Perches1e1d7412012-11-24 01:27:49 +00001058 netif_dbg(dev, ifup, dev->net, "PHY reset complete\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +00001059
Steve Glendinningd0cad872010-03-16 08:46:46 +00001060 ret = smsc75xx_set_mac_address(dev);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001061 if (ret < 0) {
1062 netdev_warn(dev->net, "Failed to set mac address\n");
1063 return ret;
1064 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001065
Joe Perches1e1d7412012-11-24 01:27:49 +00001066 netif_dbg(dev, ifup, dev->net, "MAC Address: %pM\n",
1067 dev->net->dev_addr);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001068
1069 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001070 if (ret < 0) {
1071 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1072 return ret;
1073 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001074
Joe Perches1e1d7412012-11-24 01:27:49 +00001075 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG : 0x%08x\n",
1076 buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001077
1078 buf |= HW_CFG_BIR;
1079
1080 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001081 if (ret < 0) {
1082 netdev_warn(dev->net, "Failed to write HW_CFG: %d\n", ret);
1083 return ret;
1084 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001085
1086 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001087 if (ret < 0) {
1088 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1089 return ret;
1090 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001091
Joe Perches1e1d7412012-11-24 01:27:49 +00001092 netif_dbg(dev, ifup, dev->net, "Read Value from HW_CFG after writing HW_CFG_BIR: 0x%08x\n",
1093 buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001094
1095 if (!turbo_mode) {
1096 buf = 0;
1097 dev->rx_urb_size = MAX_SINGLE_PACKET_SIZE;
1098 } else if (dev->udev->speed == USB_SPEED_HIGH) {
1099 buf = DEFAULT_HS_BURST_CAP_SIZE / HS_USB_PKT_SIZE;
1100 dev->rx_urb_size = DEFAULT_HS_BURST_CAP_SIZE;
1101 } else {
1102 buf = DEFAULT_FS_BURST_CAP_SIZE / FS_USB_PKT_SIZE;
1103 dev->rx_urb_size = DEFAULT_FS_BURST_CAP_SIZE;
1104 }
1105
Joe Perches1e1d7412012-11-24 01:27:49 +00001106 netif_dbg(dev, ifup, dev->net, "rx_urb_size=%ld\n",
1107 (ulong)dev->rx_urb_size);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001108
1109 ret = smsc75xx_write_reg(dev, BURST_CAP, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001110 if (ret < 0) {
1111 netdev_warn(dev->net, "Failed to write BURST_CAP: %d\n", ret);
1112 return ret;
1113 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001114
1115 ret = smsc75xx_read_reg(dev, BURST_CAP, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001116 if (ret < 0) {
1117 netdev_warn(dev->net, "Failed to read BURST_CAP: %d\n", ret);
1118 return ret;
1119 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001120
1121 netif_dbg(dev, ifup, dev->net,
Joe Perches1e1d7412012-11-24 01:27:49 +00001122 "Read Value from BURST_CAP after writing: 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001123
1124 ret = smsc75xx_write_reg(dev, BULK_IN_DLY, DEFAULT_BULK_IN_DELAY);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001125 if (ret < 0) {
1126 netdev_warn(dev->net, "Failed to write BULK_IN_DLY: %d\n", ret);
1127 return ret;
1128 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001129
1130 ret = smsc75xx_read_reg(dev, BULK_IN_DLY, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001131 if (ret < 0) {
1132 netdev_warn(dev->net, "Failed to read BULK_IN_DLY: %d\n", ret);
1133 return ret;
1134 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001135
1136 netif_dbg(dev, ifup, dev->net,
Joe Perches1e1d7412012-11-24 01:27:49 +00001137 "Read Value from BULK_IN_DLY after writing: 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001138
1139 if (turbo_mode) {
1140 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001141 if (ret < 0) {
1142 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1143 return ret;
1144 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001145
Joe Perches1e1d7412012-11-24 01:27:49 +00001146 netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001147
1148 buf |= (HW_CFG_MEF | HW_CFG_BCE);
1149
1150 ret = smsc75xx_write_reg(dev, HW_CFG, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001151 if (ret < 0) {
1152 netdev_warn(dev->net, "Failed to write HW_CFG: %d\n", ret);
1153 return ret;
1154 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001155
1156 ret = smsc75xx_read_reg(dev, HW_CFG, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001157 if (ret < 0) {
1158 netdev_warn(dev->net, "Failed to read HW_CFG: %d\n", ret);
1159 return ret;
1160 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001161
Joe Perches1e1d7412012-11-24 01:27:49 +00001162 netif_dbg(dev, ifup, dev->net, "HW_CFG: 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001163 }
1164
1165 /* set FIFO sizes */
1166 buf = (MAX_RX_FIFO_SIZE - 512) / 512;
1167 ret = smsc75xx_write_reg(dev, FCT_RX_FIFO_END, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001168 if (ret < 0) {
1169 netdev_warn(dev->net, "Failed to write FCT_RX_FIFO_END: %d\n", ret);
1170 return ret;
1171 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001172
Joe Perches1e1d7412012-11-24 01:27:49 +00001173 netif_dbg(dev, ifup, dev->net, "FCT_RX_FIFO_END set to 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001174
1175 buf = (MAX_TX_FIFO_SIZE - 512) / 512;
1176 ret = smsc75xx_write_reg(dev, FCT_TX_FIFO_END, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001177 if (ret < 0) {
1178 netdev_warn(dev->net, "Failed to write FCT_TX_FIFO_END: %d\n", ret);
1179 return ret;
1180 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001181
Joe Perches1e1d7412012-11-24 01:27:49 +00001182 netif_dbg(dev, ifup, dev->net, "FCT_TX_FIFO_END set to 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001183
1184 ret = smsc75xx_write_reg(dev, INT_STS, INT_STS_CLEAR_ALL);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001185 if (ret < 0) {
1186 netdev_warn(dev->net, "Failed to write INT_STS: %d\n", ret);
1187 return ret;
1188 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001189
1190 ret = smsc75xx_read_reg(dev, ID_REV, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001191 if (ret < 0) {
1192 netdev_warn(dev->net, "Failed to read ID_REV: %d\n", ret);
1193 return ret;
1194 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001195
Joe Perches1e1d7412012-11-24 01:27:49 +00001196 netif_dbg(dev, ifup, dev->net, "ID_REV = 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001197
Steve Glendinning97138a12012-05-04 00:57:13 +00001198 ret = smsc75xx_read_reg(dev, E2P_CMD, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001199 if (ret < 0) {
1200 netdev_warn(dev->net, "Failed to read E2P_CMD: %d\n", ret);
1201 return ret;
1202 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001203
Steve Glendinning97138a12012-05-04 00:57:13 +00001204 /* only set default GPIO/LED settings if no EEPROM is detected */
1205 if (!(buf & E2P_CMD_LOADED)) {
1206 ret = smsc75xx_read_reg(dev, LED_GPIO_CFG, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001207 if (ret < 0) {
1208 netdev_warn(dev->net, "Failed to read LED_GPIO_CFG: %d\n", ret);
1209 return ret;
1210 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001211
Steve Glendinning97138a12012-05-04 00:57:13 +00001212 buf &= ~(LED_GPIO_CFG_LED2_FUN_SEL | LED_GPIO_CFG_LED10_FUN_SEL);
1213 buf |= LED_GPIO_CFG_LEDGPIO_EN | LED_GPIO_CFG_LED2_FUN_SEL;
1214
1215 ret = smsc75xx_write_reg(dev, LED_GPIO_CFG, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001216 if (ret < 0) {
1217 netdev_warn(dev->net, "Failed to write LED_GPIO_CFG: %d\n", ret);
1218 return ret;
1219 }
Steve Glendinning97138a12012-05-04 00:57:13 +00001220 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001221
1222 ret = smsc75xx_write_reg(dev, FLOW, 0);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001223 if (ret < 0) {
1224 netdev_warn(dev->net, "Failed to write FLOW: %d\n", ret);
1225 return ret;
1226 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001227
1228 ret = smsc75xx_write_reg(dev, FCT_FLOW, 0);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001229 if (ret < 0) {
1230 netdev_warn(dev->net, "Failed to write FCT_FLOW: %d\n", ret);
1231 return ret;
1232 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001233
1234 /* Don't need rfe_ctl_lock during initialisation */
1235 ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001236 if (ret < 0) {
1237 netdev_warn(dev->net, "Failed to read RFE_CTL: %d\n", ret);
1238 return ret;
1239 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001240
1241 pdata->rfe_ctl |= RFE_CTL_AB | RFE_CTL_DPF;
1242
1243 ret = smsc75xx_write_reg(dev, RFE_CTL, pdata->rfe_ctl);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001244 if (ret < 0) {
1245 netdev_warn(dev->net, "Failed to write RFE_CTL: %d\n", ret);
1246 return ret;
1247 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001248
1249 ret = smsc75xx_read_reg(dev, RFE_CTL, &pdata->rfe_ctl);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001250 if (ret < 0) {
1251 netdev_warn(dev->net, "Failed to read RFE_CTL: %d\n", ret);
1252 return ret;
1253 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001254
Joe Perches1e1d7412012-11-24 01:27:49 +00001255 netif_dbg(dev, ifup, dev->net, "RFE_CTL set to 0x%08x\n",
1256 pdata->rfe_ctl);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001257
1258 /* Enable or disable checksum offload engines */
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001259 smsc75xx_set_features(dev->net, dev->net->features);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001260
1261 smsc75xx_set_multicast(dev->net);
1262
1263 ret = smsc75xx_phy_initialize(dev);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001264 if (ret < 0) {
1265 netdev_warn(dev->net, "Failed to initialize PHY: %d\n", ret);
1266 return ret;
1267 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001268
1269 ret = smsc75xx_read_reg(dev, INT_EP_CTL, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001270 if (ret < 0) {
1271 netdev_warn(dev->net, "Failed to read INT_EP_CTL: %d\n", ret);
1272 return ret;
1273 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001274
1275 /* enable PHY interrupts */
1276 buf |= INT_ENP_PHY_INT;
1277
1278 ret = smsc75xx_write_reg(dev, INT_EP_CTL, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001279 if (ret < 0) {
1280 netdev_warn(dev->net, "Failed to write INT_EP_CTL: %d\n", ret);
1281 return ret;
1282 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001283
Steve Glendinning2f3a0812012-04-30 07:56:56 +00001284 /* allow mac to detect speed and duplex from phy */
1285 ret = smsc75xx_read_reg(dev, MAC_CR, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001286 if (ret < 0) {
1287 netdev_warn(dev->net, "Failed to read MAC_CR: %d\n", ret);
1288 return ret;
1289 }
Steve Glendinning2f3a0812012-04-30 07:56:56 +00001290
1291 buf |= (MAC_CR_ADD | MAC_CR_ASD);
1292 ret = smsc75xx_write_reg(dev, MAC_CR, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001293 if (ret < 0) {
1294 netdev_warn(dev->net, "Failed to write MAC_CR: %d\n", ret);
1295 return ret;
1296 }
Steve Glendinning2f3a0812012-04-30 07:56:56 +00001297
Steve Glendinningd0cad872010-03-16 08:46:46 +00001298 ret = smsc75xx_read_reg(dev, MAC_TX, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001299 if (ret < 0) {
1300 netdev_warn(dev->net, "Failed to read MAC_TX: %d\n", ret);
1301 return ret;
1302 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001303
1304 buf |= MAC_TX_TXEN;
1305
1306 ret = smsc75xx_write_reg(dev, MAC_TX, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001307 if (ret < 0) {
1308 netdev_warn(dev->net, "Failed to write MAC_TX: %d\n", ret);
1309 return ret;
1310 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001311
Joe Perches1e1d7412012-11-24 01:27:49 +00001312 netif_dbg(dev, ifup, dev->net, "MAC_TX set to 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001313
1314 ret = smsc75xx_read_reg(dev, FCT_TX_CTL, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001315 if (ret < 0) {
1316 netdev_warn(dev->net, "Failed to read FCT_TX_CTL: %d\n", ret);
1317 return ret;
1318 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001319
1320 buf |= FCT_TX_CTL_EN;
1321
1322 ret = smsc75xx_write_reg(dev, FCT_TX_CTL, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001323 if (ret < 0) {
1324 netdev_warn(dev->net, "Failed to write FCT_TX_CTL: %d\n", ret);
1325 return ret;
1326 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001327
Joe Perches1e1d7412012-11-24 01:27:49 +00001328 netif_dbg(dev, ifup, dev->net, "FCT_TX_CTL set to 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001329
Steve Glendinning4c51e532013-03-28 02:34:41 +00001330 ret = smsc75xx_set_rx_max_frame_length(dev, dev->net->mtu + ETH_HLEN);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001331 if (ret < 0) {
1332 netdev_warn(dev->net, "Failed to set max rx frame length\n");
1333 return ret;
1334 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001335
1336 ret = smsc75xx_read_reg(dev, MAC_RX, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001337 if (ret < 0) {
1338 netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
1339 return ret;
1340 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001341
1342 buf |= MAC_RX_RXEN;
1343
1344 ret = smsc75xx_write_reg(dev, MAC_RX, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001345 if (ret < 0) {
1346 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
1347 return ret;
1348 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001349
Joe Perches1e1d7412012-11-24 01:27:49 +00001350 netif_dbg(dev, ifup, dev->net, "MAC_RX set to 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001351
1352 ret = smsc75xx_read_reg(dev, FCT_RX_CTL, &buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001353 if (ret < 0) {
1354 netdev_warn(dev->net, "Failed to read FCT_RX_CTL: %d\n", ret);
1355 return ret;
1356 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001357
1358 buf |= FCT_RX_CTL_EN;
1359
1360 ret = smsc75xx_write_reg(dev, FCT_RX_CTL, buf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001361 if (ret < 0) {
1362 netdev_warn(dev->net, "Failed to write FCT_RX_CTL: %d\n", ret);
1363 return ret;
1364 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001365
Joe Perches1e1d7412012-11-24 01:27:49 +00001366 netif_dbg(dev, ifup, dev->net, "FCT_RX_CTL set to 0x%08x\n", buf);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001367
Joe Perches1e1d7412012-11-24 01:27:49 +00001368 netif_dbg(dev, ifup, dev->net, "smsc75xx_reset, return 0\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +00001369 return 0;
1370}
1371
1372static const struct net_device_ops smsc75xx_netdev_ops = {
1373 .ndo_open = usbnet_open,
1374 .ndo_stop = usbnet_stop,
1375 .ndo_start_xmit = usbnet_start_xmit,
1376 .ndo_tx_timeout = usbnet_tx_timeout,
1377 .ndo_change_mtu = smsc75xx_change_mtu,
1378 .ndo_set_mac_address = eth_mac_addr,
1379 .ndo_validate_addr = eth_validate_addr,
1380 .ndo_do_ioctl = smsc75xx_ioctl,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001381 .ndo_set_rx_mode = smsc75xx_set_multicast,
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001382 .ndo_set_features = smsc75xx_set_features,
Steve Glendinningd0cad872010-03-16 08:46:46 +00001383};
1384
1385static int smsc75xx_bind(struct usbnet *dev, struct usb_interface *intf)
1386{
1387 struct smsc75xx_priv *pdata = NULL;
1388 int ret;
1389
1390 printk(KERN_INFO SMSC_CHIPNAME " v" SMSC_DRIVER_VERSION "\n");
1391
1392 ret = usbnet_get_endpoints(dev, intf);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001393 if (ret < 0) {
1394 netdev_warn(dev->net, "usbnet_get_endpoints failed: %d\n", ret);
1395 return ret;
1396 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001397
1398 dev->data[0] = (unsigned long)kzalloc(sizeof(struct smsc75xx_priv),
Joe Perches38673c82013-02-03 17:28:11 +00001399 GFP_KERNEL);
Steve Glendinningd0cad872010-03-16 08:46:46 +00001400
1401 pdata = (struct smsc75xx_priv *)(dev->data[0]);
Joe Perches38673c82013-02-03 17:28:11 +00001402 if (!pdata)
Steve Glendinningd0cad872010-03-16 08:46:46 +00001403 return -ENOMEM;
Steve Glendinningd0cad872010-03-16 08:46:46 +00001404
1405 pdata->dev = dev;
1406
1407 spin_lock_init(&pdata->rfe_ctl_lock);
1408 mutex_init(&pdata->dataport_mutex);
1409
1410 INIT_WORK(&pdata->set_multicast, smsc75xx_deferred_multicast_write);
1411
Eric Dumazet20f01702013-07-23 17:15:54 -07001412 if (DEFAULT_TX_CSUM_ENABLE)
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001413 dev->net->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
Eric Dumazet20f01702013-07-23 17:15:54 -07001414
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001415 if (DEFAULT_RX_CSUM_ENABLE)
1416 dev->net->features |= NETIF_F_RXCSUM;
Steve Glendinningd0cad872010-03-16 08:46:46 +00001417
Michał Mirosław78e47fe2011-04-01 20:56:23 -07001418 dev->net->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
Eric Dumazet20f01702013-07-23 17:15:54 -07001419 NETIF_F_RXCSUM;
Steve Glendinningd0cad872010-03-16 08:46:46 +00001420
Steve Glendinning481705a2012-12-10 01:01:19 +00001421 ret = smsc75xx_wait_ready(dev, 0);
1422 if (ret < 0) {
1423 netdev_warn(dev->net, "device not ready in smsc75xx_bind\n");
1424 return ret;
1425 }
1426
1427 smsc75xx_init_mac_address(dev);
1428
Steve Glendinningd0cad872010-03-16 08:46:46 +00001429 /* Init all registers */
1430 ret = smsc75xx_reset(dev);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001431 if (ret < 0) {
1432 netdev_warn(dev->net, "smsc75xx_reset error %d\n", ret);
1433 return ret;
1434 }
Steve Glendinningd0cad872010-03-16 08:46:46 +00001435
1436 dev->net->netdev_ops = &smsc75xx_netdev_ops;
1437 dev->net->ethtool_ops = &smsc75xx_ethtool_ops;
1438 dev->net->flags |= IFF_MULTICAST;
1439 dev->net->hard_header_len += SMSC75XX_TX_OVERHEAD;
Stephane Filloda99ff7d2012-04-15 11:38:29 +00001440 dev->hard_mtu = dev->net->mtu + dev->net->hard_header_len;
Steve Glendinningd0cad872010-03-16 08:46:46 +00001441 return 0;
1442}
1443
1444static void smsc75xx_unbind(struct usbnet *dev, struct usb_interface *intf)
1445{
1446 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1447 if (pdata) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001448 netif_dbg(dev, ifdown, dev->net, "free pdata\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +00001449 kfree(pdata);
1450 pdata = NULL;
1451 dev->data[0] = 0;
1452 }
1453}
1454
Steve Glendinning899a3912012-10-30 07:46:32 +00001455static u16 smsc_crc(const u8 *buffer, size_t len)
1456{
1457 return bitrev16(crc16(0xFFFF, buffer, len));
1458}
1459
1460static int smsc75xx_write_wuff(struct usbnet *dev, int filter, u32 wuf_cfg,
1461 u32 wuf_mask1)
1462{
1463 int cfg_base = WUF_CFGX + filter * 4;
1464 int mask_base = WUF_MASKX + filter * 16;
1465 int ret;
1466
1467 ret = smsc75xx_write_reg(dev, cfg_base, wuf_cfg);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001468 if (ret < 0) {
1469 netdev_warn(dev->net, "Error writing WUF_CFGX\n");
1470 return ret;
1471 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001472
1473 ret = smsc75xx_write_reg(dev, mask_base, wuf_mask1);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001474 if (ret < 0) {
1475 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1476 return ret;
1477 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001478
1479 ret = smsc75xx_write_reg(dev, mask_base + 4, 0);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001480 if (ret < 0) {
1481 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1482 return ret;
1483 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001484
1485 ret = smsc75xx_write_reg(dev, mask_base + 8, 0);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001486 if (ret < 0) {
1487 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1488 return ret;
1489 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001490
1491 ret = smsc75xx_write_reg(dev, mask_base + 12, 0);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001492 if (ret < 0) {
1493 netdev_warn(dev->net, "Error writing WUF_MASKX\n");
1494 return ret;
1495 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001496
1497 return 0;
1498}
1499
Steve Glendinning9deb2752012-11-28 05:59:46 +00001500static int smsc75xx_enter_suspend0(struct usbnet *dev)
1501{
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001502 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
Steve Glendinning9deb2752012-11-28 05:59:46 +00001503 u32 val;
1504 int ret;
1505
1506 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001507 if (ret < 0) {
1508 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1509 return ret;
1510 }
Steve Glendinning9deb2752012-11-28 05:59:46 +00001511
1512 val &= (~(PMT_CTL_SUS_MODE | PMT_CTL_PHY_RST));
1513 val |= PMT_CTL_SUS_MODE_0 | PMT_CTL_WOL_EN | PMT_CTL_WUPS;
1514
1515 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001516 if (ret < 0) {
1517 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1518 return ret;
1519 }
Steve Glendinning9deb2752012-11-28 05:59:46 +00001520
Steve Glendinning351f33d2012-11-28 05:59:50 +00001521 pdata->suspend_flags |= SUSPEND_SUSPEND0;
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001522
Steve Glendinning9deb2752012-11-28 05:59:46 +00001523 return 0;
1524}
1525
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001526static int smsc75xx_enter_suspend1(struct usbnet *dev)
1527{
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001528 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001529 u32 val;
1530 int ret;
1531
1532 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001533 if (ret < 0) {
1534 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1535 return ret;
1536 }
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001537
1538 val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1539 val |= PMT_CTL_SUS_MODE_1;
1540
1541 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001542 if (ret < 0) {
1543 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1544 return ret;
1545 }
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001546
1547 /* clear wol status, enable energy detection */
1548 val &= ~PMT_CTL_WUPS;
1549 val |= (PMT_CTL_WUPS_ED | PMT_CTL_ED_EN);
1550
1551 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001552 if (ret < 0) {
1553 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1554 return ret;
1555 }
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001556
Steve Glendinning351f33d2012-11-28 05:59:50 +00001557 pdata->suspend_flags |= SUSPEND_SUSPEND1;
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001558
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001559 return 0;
1560}
1561
Steve Glendinning9deb2752012-11-28 05:59:46 +00001562static int smsc75xx_enter_suspend2(struct usbnet *dev)
1563{
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001564 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
Steve Glendinning9deb2752012-11-28 05:59:46 +00001565 u32 val;
1566 int ret;
1567
1568 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001569 if (ret < 0) {
1570 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1571 return ret;
1572 }
Steve Glendinning9deb2752012-11-28 05:59:46 +00001573
1574 val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1575 val |= PMT_CTL_SUS_MODE_2;
1576
1577 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001578 if (ret < 0) {
1579 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1580 return ret;
1581 }
Steve Glendinning9deb2752012-11-28 05:59:46 +00001582
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001583 pdata->suspend_flags |= SUSPEND_SUSPEND2;
1584
1585 return 0;
1586}
1587
1588static int smsc75xx_enter_suspend3(struct usbnet *dev)
1589{
1590 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
1591 u32 val;
1592 int ret;
1593
1594 ret = smsc75xx_read_reg_nopm(dev, FCT_RX_CTL, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001595 if (ret < 0) {
1596 netdev_warn(dev->net, "Error reading FCT_RX_CTL\n");
1597 return ret;
1598 }
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001599
1600 if (val & FCT_RX_CTL_RXUSED) {
1601 netdev_dbg(dev->net, "rx fifo not empty in autosuspend\n");
1602 return -EBUSY;
1603 }
1604
1605 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001606 if (ret < 0) {
1607 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1608 return ret;
1609 }
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001610
1611 val &= ~(PMT_CTL_SUS_MODE | PMT_CTL_WUPS | PMT_CTL_PHY_RST);
1612 val |= PMT_CTL_SUS_MODE_3 | PMT_CTL_RES_CLR_WKP_EN;
1613
1614 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001615 if (ret < 0) {
1616 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1617 return ret;
1618 }
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001619
1620 /* clear wol status */
1621 val &= ~PMT_CTL_WUPS;
1622 val |= PMT_CTL_WUPS_WOL;
1623
1624 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001625 if (ret < 0) {
1626 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1627 return ret;
1628 }
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001629
Steve Glendinning351f33d2012-11-28 05:59:50 +00001630 pdata->suspend_flags |= SUSPEND_SUSPEND3;
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001631
Steve Glendinning9deb2752012-11-28 05:59:46 +00001632 return 0;
1633}
1634
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001635static int smsc75xx_enable_phy_wakeup_interrupts(struct usbnet *dev, u16 mask)
1636{
1637 struct mii_if_info *mii = &dev->mii;
1638 int ret;
1639
1640 netdev_dbg(dev->net, "enabling PHY wakeup interrupts\n");
1641
1642 /* read to clear */
1643 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_SRC);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001644 if (ret < 0) {
1645 netdev_warn(dev->net, "Error reading PHY_INT_SRC\n");
1646 return ret;
1647 }
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001648
1649 /* enable interrupt source */
1650 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, PHY_INT_MASK);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001651 if (ret < 0) {
1652 netdev_warn(dev->net, "Error reading PHY_INT_MASK\n");
1653 return ret;
1654 }
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001655
1656 ret |= mask;
1657
1658 smsc75xx_mdio_write_nopm(dev->net, mii->phy_id, PHY_INT_MASK, ret);
1659
1660 return 0;
1661}
1662
1663static int smsc75xx_link_ok_nopm(struct usbnet *dev)
1664{
1665 struct mii_if_info *mii = &dev->mii;
1666 int ret;
1667
1668 /* first, a dummy read, needed to latch some MII phys */
1669 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001670 if (ret < 0) {
1671 netdev_warn(dev->net, "Error reading MII_BMSR\n");
1672 return ret;
1673 }
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001674
1675 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id, MII_BMSR);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001676 if (ret < 0) {
1677 netdev_warn(dev->net, "Error reading MII_BMSR\n");
1678 return ret;
1679 }
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001680
1681 return !!(ret & BMSR_LSTATUS);
1682}
1683
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001684static int smsc75xx_autosuspend(struct usbnet *dev, u32 link_up)
1685{
1686 int ret;
1687
1688 if (!netif_running(dev->net)) {
1689 /* interface is ifconfig down so fully power down hw */
1690 netdev_dbg(dev->net, "autosuspend entering SUSPEND2\n");
1691 return smsc75xx_enter_suspend2(dev);
1692 }
1693
1694 if (!link_up) {
1695 /* link is down so enter EDPD mode */
1696 netdev_dbg(dev->net, "autosuspend entering SUSPEND1\n");
1697
1698 /* enable PHY wakeup events for if cable is attached */
1699 ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1700 PHY_INT_MASK_ANEG_COMP);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001701 if (ret < 0) {
1702 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1703 return ret;
1704 }
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001705
1706 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1707 return smsc75xx_enter_suspend1(dev);
1708 }
1709
1710 /* enable PHY wakeup events so we remote wakeup if cable is pulled */
1711 ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1712 PHY_INT_MASK_LINK_DOWN);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001713 if (ret < 0) {
1714 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1715 return ret;
1716 }
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001717
1718 netdev_dbg(dev->net, "autosuspend entering SUSPEND3\n");
1719 return smsc75xx_enter_suspend3(dev);
1720}
1721
Steve Glendinning16c79a02012-09-28 00:57:52 +00001722static int smsc75xx_suspend(struct usb_interface *intf, pm_message_t message)
1723{
1724 struct usbnet *dev = usb_get_intfdata(intf);
Steve Glendinning6c636502012-09-28 00:57:53 +00001725 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001726 u32 val, link_up;
Steve Glendinning16c79a02012-09-28 00:57:52 +00001727 int ret;
Steve Glendinning16c79a02012-09-28 00:57:52 +00001728
Steve Glendinning16c79a02012-09-28 00:57:52 +00001729 ret = usbnet_suspend(intf, message);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001730 if (ret < 0) {
1731 netdev_warn(dev->net, "usbnet_suspend error\n");
1732 return ret;
1733 }
Steve Glendinning16c79a02012-09-28 00:57:52 +00001734
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001735 if (pdata->suspend_flags) {
1736 netdev_warn(dev->net, "error during last resume\n");
1737 pdata->suspend_flags = 0;
1738 }
1739
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001740 /* determine if link is up using only _nopm functions */
1741 link_up = smsc75xx_link_ok_nopm(dev);
1742
Steve Glendinningb4cdea92012-11-28 05:59:49 +00001743 if (message.event == PM_EVENT_AUTO_SUSPEND) {
1744 ret = smsc75xx_autosuspend(dev, link_up);
1745 goto done;
1746 }
1747
1748 /* if we get this far we're not autosuspending */
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001749 /* if no wol options set, or if link is down and we're not waking on
1750 * PHY activity, enter lowest power SUSPEND2 mode
1751 */
1752 if (!(pdata->wolopts & SUPPORTED_WAKE) ||
1753 !(link_up || (pdata->wolopts & WAKE_PHY))) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001754 netdev_info(dev->net, "entering SUSPEND2 mode\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00001755
1756 /* disable energy detect (link up) & wake up events */
Ming Lei47bbea42012-11-06 04:53:05 +00001757 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001758 if (ret < 0) {
1759 netdev_warn(dev->net, "Error reading WUCSR\n");
1760 goto done;
1761 }
Steve Glendinning6c636502012-09-28 00:57:53 +00001762
1763 val &= ~(WUCSR_MPEN | WUCSR_WUEN);
1764
Ming Lei47bbea42012-11-06 04:53:05 +00001765 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001766 if (ret < 0) {
1767 netdev_warn(dev->net, "Error writing WUCSR\n");
1768 goto done;
1769 }
Steve Glendinning6c636502012-09-28 00:57:53 +00001770
Ming Lei47bbea42012-11-06 04:53:05 +00001771 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001772 if (ret < 0) {
1773 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1774 goto done;
1775 }
Steve Glendinning6c636502012-09-28 00:57:53 +00001776
1777 val &= ~(PMT_CTL_ED_EN | PMT_CTL_WOL_EN);
1778
Ming Lei47bbea42012-11-06 04:53:05 +00001779 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001780 if (ret < 0) {
1781 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1782 goto done;
1783 }
Steve Glendinning6c636502012-09-28 00:57:53 +00001784
Steve Glendinningeacdd6c2012-11-28 05:59:48 +00001785 ret = smsc75xx_enter_suspend2(dev);
1786 goto done;
Steve Glendinning6c636502012-09-28 00:57:53 +00001787 }
1788
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001789 if (pdata->wolopts & WAKE_PHY) {
1790 ret = smsc75xx_enable_phy_wakeup_interrupts(dev,
1791 (PHY_INT_MASK_ANEG_COMP | PHY_INT_MASK_LINK_DOWN));
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001792 if (ret < 0) {
1793 netdev_warn(dev->net, "error enabling PHY wakeup ints\n");
1794 goto done;
1795 }
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001796
1797 /* if link is down then configure EDPD and enter SUSPEND1,
1798 * otherwise enter SUSPEND0 below
1799 */
1800 if (!link_up) {
1801 struct mii_if_info *mii = &dev->mii;
1802 netdev_info(dev->net, "entering SUSPEND1 mode\n");
1803
1804 /* enable energy detect power-down mode */
1805 ret = smsc75xx_mdio_read_nopm(dev->net, mii->phy_id,
1806 PHY_MODE_CTRL_STS);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001807 if (ret < 0) {
1808 netdev_warn(dev->net, "Error reading PHY_MODE_CTRL_STS\n");
1809 goto done;
1810 }
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001811
1812 ret |= MODE_CTRL_STS_EDPWRDOWN;
1813
1814 smsc75xx_mdio_write_nopm(dev->net, mii->phy_id,
1815 PHY_MODE_CTRL_STS, ret);
1816
1817 /* enter SUSPEND1 mode */
Steve Glendinningeacdd6c2012-11-28 05:59:48 +00001818 ret = smsc75xx_enter_suspend1(dev);
1819 goto done;
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001820 }
1821 }
1822
Steve Glendinning899a3912012-10-30 07:46:32 +00001823 if (pdata->wolopts & (WAKE_MCAST | WAKE_ARP)) {
1824 int i, filter = 0;
1825
1826 /* disable all filters */
1827 for (i = 0; i < WUF_NUM; i++) {
Ming Lei47bbea42012-11-06 04:53:05 +00001828 ret = smsc75xx_write_reg_nopm(dev, WUF_CFGX + i * 4, 0);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001829 if (ret < 0) {
1830 netdev_warn(dev->net, "Error writing WUF_CFGX\n");
1831 goto done;
1832 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001833 }
1834
1835 if (pdata->wolopts & WAKE_MCAST) {
1836 const u8 mcast[] = {0x01, 0x00, 0x5E};
Joe Perches1e1d7412012-11-24 01:27:49 +00001837 netdev_info(dev->net, "enabling multicast detection\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001838
1839 val = WUF_CFGX_EN | WUF_CFGX_ATYPE_MULTICAST
1840 | smsc_crc(mcast, 3);
1841 ret = smsc75xx_write_wuff(dev, filter++, val, 0x0007);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001842 if (ret < 0) {
1843 netdev_warn(dev->net, "Error writing wakeup filter\n");
1844 goto done;
1845 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001846 }
1847
1848 if (pdata->wolopts & WAKE_ARP) {
1849 const u8 arp[] = {0x08, 0x06};
Joe Perches1e1d7412012-11-24 01:27:49 +00001850 netdev_info(dev->net, "enabling ARP detection\n");
Steve Glendinning899a3912012-10-30 07:46:32 +00001851
1852 val = WUF_CFGX_EN | WUF_CFGX_ATYPE_ALL | (0x0C << 16)
1853 | smsc_crc(arp, 2);
1854 ret = smsc75xx_write_wuff(dev, filter++, val, 0x0003);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001855 if (ret < 0) {
1856 netdev_warn(dev->net, "Error writing wakeup filter\n");
1857 goto done;
1858 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001859 }
1860
1861 /* clear any pending pattern match packet status */
Ming Lei47bbea42012-11-06 04:53:05 +00001862 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001863 if (ret < 0) {
1864 netdev_warn(dev->net, "Error reading WUCSR\n");
1865 goto done;
1866 }
Steve Glendinning6c636502012-09-28 00:57:53 +00001867
Steve Glendinning899a3912012-10-30 07:46:32 +00001868 val |= WUCSR_WUFR;
1869
Ming Lei47bbea42012-11-06 04:53:05 +00001870 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001871 if (ret < 0) {
1872 netdev_warn(dev->net, "Error writing WUCSR\n");
1873 goto done;
1874 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001875
Joe Perches1e1d7412012-11-24 01:27:49 +00001876 netdev_info(dev->net, "enabling packet match detection\n");
Ming Lei47bbea42012-11-06 04:53:05 +00001877 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001878 if (ret < 0) {
1879 netdev_warn(dev->net, "Error reading WUCSR\n");
1880 goto done;
1881 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001882
1883 val |= WUCSR_WUEN;
1884
Ming Lei47bbea42012-11-06 04:53:05 +00001885 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001886 if (ret < 0) {
1887 netdev_warn(dev->net, "Error writing WUCSR\n");
1888 goto done;
1889 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001890 } else {
Joe Perches1e1d7412012-11-24 01:27:49 +00001891 netdev_info(dev->net, "disabling packet match detection\n");
Ming Lei47bbea42012-11-06 04:53:05 +00001892 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001893 if (ret < 0) {
1894 netdev_warn(dev->net, "Error reading WUCSR\n");
1895 goto done;
1896 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001897
1898 val &= ~WUCSR_WUEN;
Steve Glendinning6c636502012-09-28 00:57:53 +00001899
Ming Lei47bbea42012-11-06 04:53:05 +00001900 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001901 if (ret < 0) {
1902 netdev_warn(dev->net, "Error writing WUCSR\n");
1903 goto done;
1904 }
Steve Glendinning6c636502012-09-28 00:57:53 +00001905 }
1906
Steve Glendinning899a3912012-10-30 07:46:32 +00001907 /* disable magic, bcast & unicast wakeup sources */
Ming Lei47bbea42012-11-06 04:53:05 +00001908 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001909 if (ret < 0) {
1910 netdev_warn(dev->net, "Error reading WUCSR\n");
1911 goto done;
1912 }
Steve Glendinning6c636502012-09-28 00:57:53 +00001913
Steve Glendinning899a3912012-10-30 07:46:32 +00001914 val &= ~(WUCSR_MPEN | WUCSR_BCST_EN | WUCSR_PFDA_EN);
Steve Glendinning6c636502012-09-28 00:57:53 +00001915
Ming Lei47bbea42012-11-06 04:53:05 +00001916 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001917 if (ret < 0) {
1918 netdev_warn(dev->net, "Error writing WUCSR\n");
1919 goto done;
1920 }
Steve Glendinning6c636502012-09-28 00:57:53 +00001921
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001922 if (pdata->wolopts & WAKE_PHY) {
1923 netdev_info(dev->net, "enabling PHY wakeup\n");
1924
1925 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001926 if (ret < 0) {
1927 netdev_warn(dev->net, "Error reading PMT_CTL\n");
1928 goto done;
1929 }
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001930
1931 /* clear wol status, enable energy detection */
1932 val &= ~PMT_CTL_WUPS;
1933 val |= (PMT_CTL_WUPS_ED | PMT_CTL_ED_EN);
1934
1935 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001936 if (ret < 0) {
1937 netdev_warn(dev->net, "Error writing PMT_CTL\n");
1938 goto done;
1939 }
Steve Glendinningf329ccd2012-11-28 05:59:47 +00001940 }
1941
Steve Glendinning899a3912012-10-30 07:46:32 +00001942 if (pdata->wolopts & WAKE_MAGIC) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001943 netdev_info(dev->net, "enabling magic packet wakeup\n");
Ming Lei47bbea42012-11-06 04:53:05 +00001944 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001945 if (ret < 0) {
1946 netdev_warn(dev->net, "Error reading WUCSR\n");
1947 goto done;
1948 }
Steve Glendinning6c636502012-09-28 00:57:53 +00001949
Steve Glendinning899a3912012-10-30 07:46:32 +00001950 /* clear any pending magic packet status */
1951 val |= WUCSR_MPR | WUCSR_MPEN;
Steve Glendinning6c636502012-09-28 00:57:53 +00001952
Ming Lei47bbea42012-11-06 04:53:05 +00001953 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001954 if (ret < 0) {
1955 netdev_warn(dev->net, "Error writing WUCSR\n");
1956 goto done;
1957 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001958 }
Steve Glendinning6c636502012-09-28 00:57:53 +00001959
Steve Glendinning899a3912012-10-30 07:46:32 +00001960 if (pdata->wolopts & WAKE_BCAST) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001961 netdev_info(dev->net, "enabling broadcast detection\n");
Ming Lei47bbea42012-11-06 04:53:05 +00001962 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001963 if (ret < 0) {
1964 netdev_warn(dev->net, "Error reading WUCSR\n");
1965 goto done;
1966 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001967
1968 val |= WUCSR_BCAST_FR | WUCSR_BCST_EN;
1969
Ming Lei47bbea42012-11-06 04:53:05 +00001970 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001971 if (ret < 0) {
1972 netdev_warn(dev->net, "Error writing WUCSR\n");
1973 goto done;
1974 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001975 }
1976
1977 if (pdata->wolopts & WAKE_UCAST) {
Joe Perches1e1d7412012-11-24 01:27:49 +00001978 netdev_info(dev->net, "enabling unicast detection\n");
Ming Lei47bbea42012-11-06 04:53:05 +00001979 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001980 if (ret < 0) {
1981 netdev_warn(dev->net, "Error reading WUCSR\n");
1982 goto done;
1983 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001984
1985 val |= WUCSR_WUFR | WUCSR_PFDA_EN;
1986
Ming Lei47bbea42012-11-06 04:53:05 +00001987 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001988 if (ret < 0) {
1989 netdev_warn(dev->net, "Error writing WUCSR\n");
1990 goto done;
1991 }
Steve Glendinning899a3912012-10-30 07:46:32 +00001992 }
1993
1994 /* enable receiver to enable frame reception */
Ming Lei47bbea42012-11-06 04:53:05 +00001995 ret = smsc75xx_read_reg_nopm(dev, MAC_RX, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00001996 if (ret < 0) {
1997 netdev_warn(dev->net, "Failed to read MAC_RX: %d\n", ret);
1998 goto done;
1999 }
Steve Glendinning6c636502012-09-28 00:57:53 +00002000
2001 val |= MAC_RX_RXEN;
2002
Ming Lei47bbea42012-11-06 04:53:05 +00002003 ret = smsc75xx_write_reg_nopm(dev, MAC_RX, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00002004 if (ret < 0) {
2005 netdev_warn(dev->net, "Failed to write MAC_RX: %d\n", ret);
2006 goto done;
2007 }
Steve Glendinning6c636502012-09-28 00:57:53 +00002008
2009 /* some wol options are enabled, so enter SUSPEND0 */
Joe Perches1e1d7412012-11-24 01:27:49 +00002010 netdev_info(dev->net, "entering SUSPEND0 mode\n");
Steve Glendinningeacdd6c2012-11-28 05:59:48 +00002011 ret = smsc75xx_enter_suspend0(dev);
2012
2013done:
Ming Lei5410a472013-03-15 12:08:59 +08002014 /*
2015 * TODO: resume() might need to handle the suspend failure
2016 * in system sleep
2017 */
2018 if (ret && PMSG_IS_AUTO(message))
Steve Glendinningeacdd6c2012-11-28 05:59:48 +00002019 usbnet_resume(intf);
2020 return ret;
Steve Glendinning16c79a02012-09-28 00:57:52 +00002021}
2022
2023static int smsc75xx_resume(struct usb_interface *intf)
2024{
2025 struct usbnet *dev = usb_get_intfdata(intf);
Steve Glendinning6c636502012-09-28 00:57:53 +00002026 struct smsc75xx_priv *pdata = (struct smsc75xx_priv *)(dev->data[0]);
Steve Glendinningb4cdea92012-11-28 05:59:49 +00002027 u8 suspend_flags = pdata->suspend_flags;
Steve Glendinning16c79a02012-09-28 00:57:52 +00002028 int ret;
2029 u32 val;
2030
Steve Glendinningb4cdea92012-11-28 05:59:49 +00002031 netdev_dbg(dev->net, "resume suspend_flags=0x%02x\n", suspend_flags);
Steve Glendinning16c79a02012-09-28 00:57:52 +00002032
Steve Glendinningb4cdea92012-11-28 05:59:49 +00002033 /* do this first to ensure it's cleared even in error case */
2034 pdata->suspend_flags = 0;
Steve Glendinning16c79a02012-09-28 00:57:52 +00002035
Steve Glendinningb4cdea92012-11-28 05:59:49 +00002036 if (suspend_flags & SUSPEND_ALLMODES) {
Steve Glendinning899a3912012-10-30 07:46:32 +00002037 /* Disable wakeup sources */
Ming Lei47bbea42012-11-06 04:53:05 +00002038 ret = smsc75xx_read_reg_nopm(dev, WUCSR, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00002039 if (ret < 0) {
2040 netdev_warn(dev->net, "Error reading WUCSR\n");
2041 return ret;
2042 }
Steve Glendinning16c79a02012-09-28 00:57:52 +00002043
Steve Glendinning899a3912012-10-30 07:46:32 +00002044 val &= ~(WUCSR_WUEN | WUCSR_MPEN | WUCSR_PFDA_EN
2045 | WUCSR_BCST_EN);
Steve Glendinning16c79a02012-09-28 00:57:52 +00002046
Ming Lei47bbea42012-11-06 04:53:05 +00002047 ret = smsc75xx_write_reg_nopm(dev, WUCSR, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00002048 if (ret < 0) {
2049 netdev_warn(dev->net, "Error writing WUCSR\n");
2050 return ret;
2051 }
Steve Glendinning6c636502012-09-28 00:57:53 +00002052
2053 /* clear wake-up status */
Ming Lei47bbea42012-11-06 04:53:05 +00002054 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00002055 if (ret < 0) {
2056 netdev_warn(dev->net, "Error reading PMT_CTL\n");
2057 return ret;
2058 }
Steve Glendinning6c636502012-09-28 00:57:53 +00002059
2060 val &= ~PMT_CTL_WOL_EN;
2061 val |= PMT_CTL_WUPS;
2062
Ming Lei47bbea42012-11-06 04:53:05 +00002063 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00002064 if (ret < 0) {
2065 netdev_warn(dev->net, "Error writing PMT_CTL\n");
2066 return ret;
2067 }
Steve Glendinningb4cdea92012-11-28 05:59:49 +00002068 }
2069
2070 if (suspend_flags & SUSPEND_SUSPEND2) {
Joe Perches1e1d7412012-11-24 01:27:49 +00002071 netdev_info(dev->net, "resuming from SUSPEND2\n");
Steve Glendinning6c636502012-09-28 00:57:53 +00002072
Ming Lei47bbea42012-11-06 04:53:05 +00002073 ret = smsc75xx_read_reg_nopm(dev, PMT_CTL, &val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00002074 if (ret < 0) {
2075 netdev_warn(dev->net, "Error reading PMT_CTL\n");
2076 return ret;
2077 }
Steve Glendinning6c636502012-09-28 00:57:53 +00002078
2079 val |= PMT_CTL_PHY_PWRUP;
2080
Ming Lei47bbea42012-11-06 04:53:05 +00002081 ret = smsc75xx_write_reg_nopm(dev, PMT_CTL, val);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00002082 if (ret < 0) {
2083 netdev_warn(dev->net, "Error writing PMT_CTL\n");
2084 return ret;
2085 }
Steve Glendinning6c636502012-09-28 00:57:53 +00002086 }
Steve Glendinning16c79a02012-09-28 00:57:52 +00002087
Ming Lei47bbea42012-11-06 04:53:05 +00002088 ret = smsc75xx_wait_ready(dev, 1);
Steve Glendinninge3c678e2012-11-30 04:52:44 +00002089 if (ret < 0) {
2090 netdev_warn(dev->net, "device not ready in smsc75xx_resume\n");
2091 return ret;
2092 }
Steve Glendinning16c79a02012-09-28 00:57:52 +00002093
2094 return usbnet_resume(intf);
2095}
2096
Michał Mirosław78e47fe2011-04-01 20:56:23 -07002097static void smsc75xx_rx_csum_offload(struct usbnet *dev, struct sk_buff *skb,
2098 u32 rx_cmd_a, u32 rx_cmd_b)
Steve Glendinningd0cad872010-03-16 08:46:46 +00002099{
Michał Mirosław78e47fe2011-04-01 20:56:23 -07002100 if (!(dev->net->features & NETIF_F_RXCSUM) ||
2101 unlikely(rx_cmd_a & RX_CMD_A_LCSM)) {
Steve Glendinningd0cad872010-03-16 08:46:46 +00002102 skb->ip_summed = CHECKSUM_NONE;
2103 } else {
2104 skb->csum = ntohs((u16)(rx_cmd_b >> RX_CMD_B_CSUM_SHIFT));
2105 skb->ip_summed = CHECKSUM_COMPLETE;
2106 }
2107}
2108
2109static int smsc75xx_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
2110{
Steve Glendinningd0cad872010-03-16 08:46:46 +00002111 while (skb->len > 0) {
2112 u32 rx_cmd_a, rx_cmd_b, align_count, size;
2113 struct sk_buff *ax_skb;
2114 unsigned char *packet;
2115
2116 memcpy(&rx_cmd_a, skb->data, sizeof(rx_cmd_a));
2117 le32_to_cpus(&rx_cmd_a);
2118 skb_pull(skb, 4);
2119
2120 memcpy(&rx_cmd_b, skb->data, sizeof(rx_cmd_b));
2121 le32_to_cpus(&rx_cmd_b);
Nico Erfurthea1649d2011-11-08 07:30:40 +00002122 skb_pull(skb, 4 + RXW_PADDING);
Steve Glendinningd0cad872010-03-16 08:46:46 +00002123
2124 packet = skb->data;
2125
2126 /* get the packet length */
Nico Erfurthea1649d2011-11-08 07:30:40 +00002127 size = (rx_cmd_a & RX_CMD_A_LEN) - RXW_PADDING;
2128 align_count = (4 - ((size + RXW_PADDING) % 4)) % 4;
Steve Glendinningd0cad872010-03-16 08:46:46 +00002129
2130 if (unlikely(rx_cmd_a & RX_CMD_A_RED)) {
2131 netif_dbg(dev, rx_err, dev->net,
Joe Perches1e1d7412012-11-24 01:27:49 +00002132 "Error rx_cmd_a=0x%08x\n", rx_cmd_a);
Steve Glendinningd0cad872010-03-16 08:46:46 +00002133 dev->net->stats.rx_errors++;
2134 dev->net->stats.rx_dropped++;
2135
2136 if (rx_cmd_a & RX_CMD_A_FCS)
2137 dev->net->stats.rx_crc_errors++;
2138 else if (rx_cmd_a & (RX_CMD_A_LONG | RX_CMD_A_RUNT))
2139 dev->net->stats.rx_frame_errors++;
2140 } else {
Steve Glendinning4c51e532013-03-28 02:34:41 +00002141 /* MAX_SINGLE_PACKET_SIZE + 4(CRC) + 2(COE) + 4(Vlan) */
2142 if (unlikely(size > (MAX_SINGLE_PACKET_SIZE + ETH_HLEN + 12))) {
Steve Glendinningd0cad872010-03-16 08:46:46 +00002143 netif_dbg(dev, rx_err, dev->net,
Joe Perches1e1d7412012-11-24 01:27:49 +00002144 "size err rx_cmd_a=0x%08x\n",
2145 rx_cmd_a);
Steve Glendinningd0cad872010-03-16 08:46:46 +00002146 return 0;
2147 }
2148
2149 /* last frame in this batch */
2150 if (skb->len == size) {
Michał Mirosław78e47fe2011-04-01 20:56:23 -07002151 smsc75xx_rx_csum_offload(dev, skb, rx_cmd_a,
2152 rx_cmd_b);
Steve Glendinningd0cad872010-03-16 08:46:46 +00002153
2154 skb_trim(skb, skb->len - 4); /* remove fcs */
2155 skb->truesize = size + sizeof(struct sk_buff);
2156
2157 return 1;
2158 }
2159
2160 ax_skb = skb_clone(skb, GFP_ATOMIC);
2161 if (unlikely(!ax_skb)) {
Joe Perches1e1d7412012-11-24 01:27:49 +00002162 netdev_warn(dev->net, "Error allocating skb\n");
Steve Glendinningd0cad872010-03-16 08:46:46 +00002163 return 0;
2164 }
2165
2166 ax_skb->len = size;
2167 ax_skb->data = packet;
2168 skb_set_tail_pointer(ax_skb, size);
2169
Michał Mirosław78e47fe2011-04-01 20:56:23 -07002170 smsc75xx_rx_csum_offload(dev, ax_skb, rx_cmd_a,
2171 rx_cmd_b);
Steve Glendinningd0cad872010-03-16 08:46:46 +00002172
2173 skb_trim(ax_skb, ax_skb->len - 4); /* remove fcs */
2174 ax_skb->truesize = size + sizeof(struct sk_buff);
2175
2176 usbnet_skb_return(dev, ax_skb);
2177 }
2178
2179 skb_pull(skb, size);
2180
2181 /* padding bytes before the next frame starts */
2182 if (skb->len)
2183 skb_pull(skb, align_count);
2184 }
2185
2186 if (unlikely(skb->len < 0)) {
Joe Perches1e1d7412012-11-24 01:27:49 +00002187 netdev_warn(dev->net, "invalid rx length<0 %d\n", skb->len);
Steve Glendinningd0cad872010-03-16 08:46:46 +00002188 return 0;
2189 }
2190
2191 return 1;
2192}
2193
2194static struct sk_buff *smsc75xx_tx_fixup(struct usbnet *dev,
2195 struct sk_buff *skb, gfp_t flags)
2196{
2197 u32 tx_cmd_a, tx_cmd_b;
2198
Steve Glendinningd0cad872010-03-16 08:46:46 +00002199 if (skb_headroom(skb) < SMSC75XX_TX_OVERHEAD) {
2200 struct sk_buff *skb2 =
2201 skb_copy_expand(skb, SMSC75XX_TX_OVERHEAD, 0, flags);
2202 dev_kfree_skb_any(skb);
2203 skb = skb2;
2204 if (!skb)
2205 return NULL;
2206 }
2207
2208 tx_cmd_a = (u32)(skb->len & TX_CMD_A_LEN) | TX_CMD_A_FCS;
2209
2210 if (skb->ip_summed == CHECKSUM_PARTIAL)
2211 tx_cmd_a |= TX_CMD_A_IPE | TX_CMD_A_TPE;
2212
2213 if (skb_is_gso(skb)) {
2214 u16 mss = max(skb_shinfo(skb)->gso_size, TX_MSS_MIN);
2215 tx_cmd_b = (mss << TX_CMD_B_MSS_SHIFT) & TX_CMD_B_MSS;
2216
2217 tx_cmd_a |= TX_CMD_A_LSO;
2218 } else {
2219 tx_cmd_b = 0;
2220 }
2221
2222 skb_push(skb, 4);
2223 cpu_to_le32s(&tx_cmd_b);
2224 memcpy(skb->data, &tx_cmd_b, 4);
2225
2226 skb_push(skb, 4);
2227 cpu_to_le32s(&tx_cmd_a);
2228 memcpy(skb->data, &tx_cmd_a, 4);
2229
2230 return skb;
2231}
2232
Steve Glendinningb4cdea92012-11-28 05:59:49 +00002233static int smsc75xx_manage_power(struct usbnet *dev, int on)
2234{
2235 dev->intf->needs_remote_wakeup = on;
2236 return 0;
2237}
2238
Steve Glendinningd0cad872010-03-16 08:46:46 +00002239static const struct driver_info smsc75xx_info = {
2240 .description = "smsc75xx USB 2.0 Gigabit Ethernet",
2241 .bind = smsc75xx_bind,
2242 .unbind = smsc75xx_unbind,
2243 .link_reset = smsc75xx_link_reset,
2244 .reset = smsc75xx_reset,
2245 .rx_fixup = smsc75xx_rx_fixup,
2246 .tx_fixup = smsc75xx_tx_fixup,
2247 .status = smsc75xx_status,
Steve Glendinningb4cdea92012-11-28 05:59:49 +00002248 .manage_power = smsc75xx_manage_power,
Steve Glendinning7bdd3052012-04-30 07:56:50 +00002249 .flags = FLAG_ETHER | FLAG_SEND_ZLP | FLAG_LINK_INTR,
Steve Glendinningd0cad872010-03-16 08:46:46 +00002250};
2251
2252static const struct usb_device_id products[] = {
2253 {
2254 /* SMSC7500 USB Gigabit Ethernet Device */
2255 USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7500),
2256 .driver_info = (unsigned long) &smsc75xx_info,
2257 },
2258 {
2259 /* SMSC7500 USB Gigabit Ethernet Device */
2260 USB_DEVICE(USB_VENDOR_ID_SMSC, USB_PRODUCT_ID_LAN7505),
2261 .driver_info = (unsigned long) &smsc75xx_info,
2262 },
2263 { }, /* END */
2264};
2265MODULE_DEVICE_TABLE(usb, products);
2266
2267static struct usb_driver smsc75xx_driver = {
2268 .name = SMSC_CHIPNAME,
2269 .id_table = products,
2270 .probe = usbnet_probe,
Steve Glendinning16c79a02012-09-28 00:57:52 +00002271 .suspend = smsc75xx_suspend,
2272 .resume = smsc75xx_resume,
2273 .reset_resume = smsc75xx_resume,
Steve Glendinningd0cad872010-03-16 08:46:46 +00002274 .disconnect = usbnet_disconnect,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07002275 .disable_hub_initiated_lpm = 1,
Steve Glendinningb4cdea92012-11-28 05:59:49 +00002276 .supports_autosuspend = 1,
Steve Glendinningd0cad872010-03-16 08:46:46 +00002277};
2278
Greg Kroah-Hartmand632eb12011-11-18 09:44:20 -08002279module_usb_driver(smsc75xx_driver);
Steve Glendinningd0cad872010-03-16 08:46:46 +00002280
2281MODULE_AUTHOR("Nancy Lin");
Steve Glendinning90b24cf2012-04-16 12:13:29 +01002282MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
Steve Glendinningd0cad872010-03-16 08:46:46 +00002283MODULE_DESCRIPTION("SMSC75XX USB 2.0 Gigabit Ethernet Devices");
2284MODULE_LICENSE("GPL");