blob: 94581be64d6511706925434e173d124c38d7539d [file] [log] [blame]
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001/* SuperH Ethernet device driver
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002 *
Hisashi Nakamura966d6db2014-11-13 15:54:05 +09003 * Copyright (C) 2014 Renesas Electronics Corporation
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +00004 * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
Sergei Shtylyovb356e972014-02-18 03:12:43 +03005 * Copyright (C) 2008-2014 Renesas Solutions Corp.
6 * Copyright (C) 2013-2014 Cogent Embedded, Inc.
Ben Dooks702eca02014-03-12 17:47:40 +00007 * Copyright (C) 2014 Codethink Limited
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 */
21
Yoshihiro Shimoda06540112011-09-29 17:16:57 +000022#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/spinlock.h>
David S. Miller823dcd22011-08-20 10:39:12 -070025#include <linux/interrupt.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070026#include <linux/dma-mapping.h>
27#include <linux/etherdevice.h>
28#include <linux/delay.h>
29#include <linux/platform_device.h>
30#include <linux/mdio-bitbang.h>
31#include <linux/netdevice.h>
Sergei Shtylyovb356e972014-02-18 03:12:43 +030032#include <linux/of.h>
33#include <linux/of_device.h>
34#include <linux/of_irq.h>
35#include <linux/of_net.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070036#include <linux/phy.h>
37#include <linux/cache.h>
38#include <linux/io.h>
Magnus Dammbcd51492009-10-09 00:20:04 +000039#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000041#include <linux/ethtool.h>
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +000042#include <linux/if_vlan.h>
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +000043#include <linux/clk.h>
Yoshihiro Shimodad4fa0e32011-09-27 21:49:12 +000044#include <linux/sh_eth.h>
Ben Dooks702eca02014-03-12 17:47:40 +000045#include <linux/of_mdio.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070046
47#include "sh_eth.h"
48
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000049#define SH_ETH_DEF_MSG_ENABLE \
50 (NETIF_MSG_LINK | \
51 NETIF_MSG_TIMER | \
52 NETIF_MSG_RX_ERR| \
53 NETIF_MSG_TX_ERR)
54
Sergei Shtylyov2274d372015-12-13 01:44:50 +030055#define SH_ETH_OFFSET_INVALID ((u16)~0)
56
Ben Hutchings33657112015-02-26 20:34:14 +000057#define SH_ETH_OFFSET_DEFAULTS \
58 [0 ... SH_ETH_MAX_REGISTER_OFFSET - 1] = SH_ETH_OFFSET_INVALID
59
Sergei Shtylyovc0013f62013-03-28 11:48:26 +000060static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +000061 SH_ETH_OFFSET_DEFAULTS,
62
Sergei Shtylyovc0013f62013-03-28 11:48:26 +000063 [EDSR] = 0x0000,
64 [EDMR] = 0x0400,
65 [EDTRR] = 0x0408,
66 [EDRRR] = 0x0410,
67 [EESR] = 0x0428,
68 [EESIPR] = 0x0430,
69 [TDLAR] = 0x0010,
70 [TDFAR] = 0x0014,
71 [TDFXR] = 0x0018,
72 [TDFFR] = 0x001c,
73 [RDLAR] = 0x0030,
74 [RDFAR] = 0x0034,
75 [RDFXR] = 0x0038,
76 [RDFFR] = 0x003c,
77 [TRSCER] = 0x0438,
78 [RMFCR] = 0x0440,
79 [TFTR] = 0x0448,
80 [FDR] = 0x0450,
81 [RMCR] = 0x0458,
82 [RPADIR] = 0x0460,
83 [FCFTR] = 0x0468,
84 [CSMR] = 0x04E4,
85
86 [ECMR] = 0x0500,
87 [ECSR] = 0x0510,
88 [ECSIPR] = 0x0518,
89 [PIR] = 0x0520,
90 [PSR] = 0x0528,
91 [PIPR] = 0x052c,
92 [RFLR] = 0x0508,
93 [APR] = 0x0554,
94 [MPR] = 0x0558,
95 [PFTCR] = 0x055c,
96 [PFRCR] = 0x0560,
97 [TPAUSER] = 0x0564,
98 [GECMR] = 0x05b0,
99 [BCULR] = 0x05b4,
100 [MAHR] = 0x05c0,
101 [MALR] = 0x05c8,
102 [TROCR] = 0x0700,
103 [CDCR] = 0x0708,
104 [LCCR] = 0x0710,
105 [CEFCR] = 0x0740,
106 [FRECR] = 0x0748,
107 [TSFRCR] = 0x0750,
108 [TLFRCR] = 0x0758,
109 [RFCR] = 0x0760,
110 [CERCR] = 0x0768,
111 [CEECR] = 0x0770,
112 [MAFCR] = 0x0778,
113 [RMII_MII] = 0x0790,
114
115 [ARSTR] = 0x0000,
116 [TSU_CTRST] = 0x0004,
117 [TSU_FWEN0] = 0x0010,
118 [TSU_FWEN1] = 0x0014,
119 [TSU_FCM] = 0x0018,
120 [TSU_BSYSL0] = 0x0020,
121 [TSU_BSYSL1] = 0x0024,
122 [TSU_PRISL0] = 0x0028,
123 [TSU_PRISL1] = 0x002c,
124 [TSU_FWSL0] = 0x0030,
125 [TSU_FWSL1] = 0x0034,
126 [TSU_FWSLC] = 0x0038,
127 [TSU_QTAG0] = 0x0040,
128 [TSU_QTAG1] = 0x0044,
129 [TSU_FWSR] = 0x0050,
130 [TSU_FWINMK] = 0x0054,
131 [TSU_ADQT0] = 0x0048,
132 [TSU_ADQT1] = 0x004c,
133 [TSU_VTAG0] = 0x0058,
134 [TSU_VTAG1] = 0x005c,
135 [TSU_ADSBSY] = 0x0060,
136 [TSU_TEN] = 0x0064,
137 [TSU_POST1] = 0x0070,
138 [TSU_POST2] = 0x0074,
139 [TSU_POST3] = 0x0078,
140 [TSU_POST4] = 0x007c,
141 [TSU_ADRH0] = 0x0100,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000142
143 [TXNLCR0] = 0x0080,
144 [TXALCR0] = 0x0084,
145 [RXNLCR0] = 0x0088,
146 [RXALCR0] = 0x008c,
147 [FWNLCR0] = 0x0090,
148 [FWALCR0] = 0x0094,
149 [TXNLCR1] = 0x00a0,
150 [TXALCR1] = 0x00a0,
151 [RXNLCR1] = 0x00a8,
152 [RXALCR1] = 0x00ac,
153 [FWNLCR1] = 0x00b0,
154 [FWALCR1] = 0x00b4,
155};
156
Simon Hormandb893472014-01-17 09:22:28 +0900157static const u16 sh_eth_offset_fast_rz[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000158 SH_ETH_OFFSET_DEFAULTS,
159
Simon Hormandb893472014-01-17 09:22:28 +0900160 [EDSR] = 0x0000,
161 [EDMR] = 0x0400,
162 [EDTRR] = 0x0408,
163 [EDRRR] = 0x0410,
164 [EESR] = 0x0428,
165 [EESIPR] = 0x0430,
166 [TDLAR] = 0x0010,
167 [TDFAR] = 0x0014,
168 [TDFXR] = 0x0018,
169 [TDFFR] = 0x001c,
170 [RDLAR] = 0x0030,
171 [RDFAR] = 0x0034,
172 [RDFXR] = 0x0038,
173 [RDFFR] = 0x003c,
174 [TRSCER] = 0x0438,
175 [RMFCR] = 0x0440,
176 [TFTR] = 0x0448,
177 [FDR] = 0x0450,
178 [RMCR] = 0x0458,
179 [RPADIR] = 0x0460,
180 [FCFTR] = 0x0468,
181 [CSMR] = 0x04E4,
182
183 [ECMR] = 0x0500,
184 [RFLR] = 0x0508,
185 [ECSR] = 0x0510,
186 [ECSIPR] = 0x0518,
187 [PIR] = 0x0520,
188 [APR] = 0x0554,
189 [MPR] = 0x0558,
190 [PFTCR] = 0x055c,
191 [PFRCR] = 0x0560,
192 [TPAUSER] = 0x0564,
193 [MAHR] = 0x05c0,
194 [MALR] = 0x05c8,
195 [CEFCR] = 0x0740,
196 [FRECR] = 0x0748,
197 [TSFRCR] = 0x0750,
198 [TLFRCR] = 0x0758,
199 [RFCR] = 0x0760,
200 [MAFCR] = 0x0778,
201
202 [ARSTR] = 0x0000,
203 [TSU_CTRST] = 0x0004,
204 [TSU_VTAG0] = 0x0058,
205 [TSU_ADSBSY] = 0x0060,
206 [TSU_TEN] = 0x0064,
207 [TSU_ADRH0] = 0x0100,
Simon Hormandb893472014-01-17 09:22:28 +0900208
209 [TXNLCR0] = 0x0080,
210 [TXALCR0] = 0x0084,
211 [RXNLCR0] = 0x0088,
212 [RXALCR0] = 0x008C,
213};
214
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000215static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000216 SH_ETH_OFFSET_DEFAULTS,
217
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000218 [ECMR] = 0x0300,
219 [RFLR] = 0x0308,
220 [ECSR] = 0x0310,
221 [ECSIPR] = 0x0318,
222 [PIR] = 0x0320,
223 [PSR] = 0x0328,
224 [RDMLR] = 0x0340,
225 [IPGR] = 0x0350,
226 [APR] = 0x0354,
227 [MPR] = 0x0358,
228 [RFCF] = 0x0360,
229 [TPAUSER] = 0x0364,
230 [TPAUSECR] = 0x0368,
231 [MAHR] = 0x03c0,
232 [MALR] = 0x03c8,
233 [TROCR] = 0x03d0,
234 [CDCR] = 0x03d4,
235 [LCCR] = 0x03d8,
236 [CNDCR] = 0x03dc,
237 [CEFCR] = 0x03e4,
238 [FRECR] = 0x03e8,
239 [TSFRCR] = 0x03ec,
240 [TLFRCR] = 0x03f0,
241 [RFCR] = 0x03f4,
242 [MAFCR] = 0x03f8,
243
244 [EDMR] = 0x0200,
245 [EDTRR] = 0x0208,
246 [EDRRR] = 0x0210,
247 [TDLAR] = 0x0218,
248 [RDLAR] = 0x0220,
249 [EESR] = 0x0228,
250 [EESIPR] = 0x0230,
251 [TRSCER] = 0x0238,
252 [RMFCR] = 0x0240,
253 [TFTR] = 0x0248,
254 [FDR] = 0x0250,
255 [RMCR] = 0x0258,
256 [TFUCR] = 0x0264,
257 [RFOCR] = 0x0268,
Simon Horman55754f12013-07-23 10:18:04 +0900258 [RMIIMODE] = 0x026c,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000259 [FCFTR] = 0x0270,
260 [TRIMD] = 0x027c,
261};
262
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000263static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000264 SH_ETH_OFFSET_DEFAULTS,
265
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000266 [ECMR] = 0x0100,
267 [RFLR] = 0x0108,
268 [ECSR] = 0x0110,
269 [ECSIPR] = 0x0118,
270 [PIR] = 0x0120,
271 [PSR] = 0x0128,
272 [RDMLR] = 0x0140,
273 [IPGR] = 0x0150,
274 [APR] = 0x0154,
275 [MPR] = 0x0158,
276 [TPAUSER] = 0x0164,
277 [RFCF] = 0x0160,
278 [TPAUSECR] = 0x0168,
279 [BCFRR] = 0x016c,
280 [MAHR] = 0x01c0,
281 [MALR] = 0x01c8,
282 [TROCR] = 0x01d0,
283 [CDCR] = 0x01d4,
284 [LCCR] = 0x01d8,
285 [CNDCR] = 0x01dc,
286 [CEFCR] = 0x01e4,
287 [FRECR] = 0x01e8,
288 [TSFRCR] = 0x01ec,
289 [TLFRCR] = 0x01f0,
290 [RFCR] = 0x01f4,
291 [MAFCR] = 0x01f8,
292 [RTRATE] = 0x01fc,
293
294 [EDMR] = 0x0000,
295 [EDTRR] = 0x0008,
296 [EDRRR] = 0x0010,
297 [TDLAR] = 0x0018,
298 [RDLAR] = 0x0020,
299 [EESR] = 0x0028,
300 [EESIPR] = 0x0030,
301 [TRSCER] = 0x0038,
302 [RMFCR] = 0x0040,
303 [TFTR] = 0x0048,
304 [FDR] = 0x0050,
305 [RMCR] = 0x0058,
306 [TFUCR] = 0x0064,
307 [RFOCR] = 0x0068,
308 [FCFTR] = 0x0070,
309 [RPADIR] = 0x0078,
310 [TRIMD] = 0x007c,
311 [RBWAR] = 0x00c8,
312 [RDFAR] = 0x00cc,
313 [TBRAR] = 0x00d4,
314 [TDFAR] = 0x00d8,
315};
316
317static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000318 SH_ETH_OFFSET_DEFAULTS,
319
Sergei Shtylyovd8b04262014-06-03 23:42:26 +0400320 [EDMR] = 0x0000,
321 [EDTRR] = 0x0004,
322 [EDRRR] = 0x0008,
323 [TDLAR] = 0x000c,
324 [RDLAR] = 0x0010,
325 [EESR] = 0x0014,
326 [EESIPR] = 0x0018,
327 [TRSCER] = 0x001c,
328 [RMFCR] = 0x0020,
329 [TFTR] = 0x0024,
330 [FDR] = 0x0028,
331 [RMCR] = 0x002c,
332 [EDOCR] = 0x0030,
333 [FCFTR] = 0x0034,
334 [RPADIR] = 0x0038,
335 [TRIMD] = 0x003c,
336 [RBWAR] = 0x0040,
337 [RDFAR] = 0x0044,
338 [TBRAR] = 0x004c,
339 [TDFAR] = 0x0050,
340
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000341 [ECMR] = 0x0160,
342 [ECSR] = 0x0164,
343 [ECSIPR] = 0x0168,
344 [PIR] = 0x016c,
345 [MAHR] = 0x0170,
346 [MALR] = 0x0174,
347 [RFLR] = 0x0178,
348 [PSR] = 0x017c,
349 [TROCR] = 0x0180,
350 [CDCR] = 0x0184,
351 [LCCR] = 0x0188,
352 [CNDCR] = 0x018c,
353 [CEFCR] = 0x0194,
354 [FRECR] = 0x0198,
355 [TSFRCR] = 0x019c,
356 [TLFRCR] = 0x01a0,
357 [RFCR] = 0x01a4,
358 [MAFCR] = 0x01a8,
359 [IPGR] = 0x01b4,
360 [APR] = 0x01b8,
361 [MPR] = 0x01bc,
362 [TPAUSER] = 0x01c4,
363 [BCFR] = 0x01cc,
364
365 [ARSTR] = 0x0000,
366 [TSU_CTRST] = 0x0004,
367 [TSU_FWEN0] = 0x0010,
368 [TSU_FWEN1] = 0x0014,
369 [TSU_FCM] = 0x0018,
370 [TSU_BSYSL0] = 0x0020,
371 [TSU_BSYSL1] = 0x0024,
372 [TSU_PRISL0] = 0x0028,
373 [TSU_PRISL1] = 0x002c,
374 [TSU_FWSL0] = 0x0030,
375 [TSU_FWSL1] = 0x0034,
376 [TSU_FWSLC] = 0x0038,
377 [TSU_QTAGM0] = 0x0040,
378 [TSU_QTAGM1] = 0x0044,
379 [TSU_ADQT0] = 0x0048,
380 [TSU_ADQT1] = 0x004c,
381 [TSU_FWSR] = 0x0050,
382 [TSU_FWINMK] = 0x0054,
383 [TSU_ADSBSY] = 0x0060,
384 [TSU_TEN] = 0x0064,
385 [TSU_POST1] = 0x0070,
386 [TSU_POST2] = 0x0074,
387 [TSU_POST3] = 0x0078,
388 [TSU_POST4] = 0x007c,
389
390 [TXNLCR0] = 0x0080,
391 [TXALCR0] = 0x0084,
392 [RXNLCR0] = 0x0088,
393 [RXALCR0] = 0x008c,
394 [FWNLCR0] = 0x0090,
395 [FWALCR0] = 0x0094,
396 [TXNLCR1] = 0x00a0,
397 [TXALCR1] = 0x00a0,
398 [RXNLCR1] = 0x00a8,
399 [RXALCR1] = 0x00ac,
400 [FWNLCR1] = 0x00b0,
401 [FWALCR1] = 0x00b4,
402
403 [TSU_ADRH0] = 0x0100,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000404};
405
Ben Hutchings740c7f32015-01-27 00:49:32 +0000406static void sh_eth_rcv_snd_disable(struct net_device *ndev);
407static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev);
408
Sergei Shtylyov2274d372015-12-13 01:44:50 +0300409static void sh_eth_write(struct net_device *ndev, u32 data, int enum_index)
410{
411 struct sh_eth_private *mdp = netdev_priv(ndev);
412 u16 offset = mdp->reg_offset[enum_index];
413
414 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
415 return;
416
417 iowrite32(data, mdp->addr + offset);
418}
419
420static u32 sh_eth_read(struct net_device *ndev, int enum_index)
421{
422 struct sh_eth_private *mdp = netdev_priv(ndev);
423 u16 offset = mdp->reg_offset[enum_index];
424
425 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
426 return ~0U;
427
428 return ioread32(mdp->addr + offset);
429}
430
Simon Horman504c8ca2014-01-17 09:22:27 +0900431static bool sh_eth_is_gether(struct sh_eth_private *mdp)
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000432{
Simon Horman504c8ca2014-01-17 09:22:27 +0900433 return mdp->reg_offset == sh_eth_offset_gigabit;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000434}
435
Simon Hormandb893472014-01-17 09:22:28 +0900436static bool sh_eth_is_rz_fast_ether(struct sh_eth_private *mdp)
437{
438 return mdp->reg_offset == sh_eth_offset_fast_rz;
439}
440
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400441static void sh_eth_select_mii(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000442{
443 u32 value = 0x0;
444 struct sh_eth_private *mdp = netdev_priv(ndev);
445
446 switch (mdp->phy_interface) {
447 case PHY_INTERFACE_MODE_GMII:
448 value = 0x2;
449 break;
450 case PHY_INTERFACE_MODE_MII:
451 value = 0x1;
452 break;
453 case PHY_INTERFACE_MODE_RMII:
454 value = 0x0;
455 break;
456 default:
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300457 netdev_warn(ndev,
458 "PHY interface mode was not setup. Set to MII.\n");
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000459 value = 0x1;
460 break;
461 }
462
463 sh_eth_write(ndev, value, RMII_MII);
464}
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000465
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400466static void sh_eth_set_duplex(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000467{
468 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000469
470 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000471 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000472 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000473 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000474}
475
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100476static void sh_eth_chip_reset(struct net_device *ndev)
477{
478 struct sh_eth_private *mdp = netdev_priv(ndev);
479
480 /* reset device */
481 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
482 mdelay(1);
483}
484
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100485static void sh_eth_set_rate_gether(struct net_device *ndev)
486{
487 struct sh_eth_private *mdp = netdev_priv(ndev);
488
489 switch (mdp->speed) {
490 case 10: /* 10BASE */
491 sh_eth_write(ndev, GECMR_10, GECMR);
492 break;
493 case 100:/* 100BASE */
494 sh_eth_write(ndev, GECMR_100, GECMR);
495 break;
496 case 1000: /* 1000BASE */
497 sh_eth_write(ndev, GECMR_1000, GECMR);
498 break;
499 default:
500 break;
501 }
502}
503
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100504#ifdef CONFIG_OF
505/* R7S72100 */
506static struct sh_eth_cpu_data r7s72100_data = {
507 .chip_reset = sh_eth_chip_reset,
508 .set_duplex = sh_eth_set_duplex,
509
510 .register_type = SH_ETH_REG_FAST_RZ,
511
512 .ecsr_value = ECSR_ICD,
513 .ecsipr_value = ECSIPR_ICDIP,
514 .eesipr_value = 0xff7f009f,
515
516 .tx_check = EESR_TC1 | EESR_FTC,
517 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
518 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
519 EESR_TDE | EESR_ECI,
520 .fdr_value = 0x0000070f,
521
522 .no_psr = 1,
523 .apr = 1,
524 .mpr = 1,
525 .tpauser = 1,
526 .hw_swap = 1,
527 .rpadir = 1,
528 .rpadir_value = 2 << 16,
529 .no_trimd = 1,
530 .no_ade = 1,
531 .hw_crc = 1,
532 .tsu = 1,
533 .shift_rd0 = 1,
534};
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100535
536static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
537{
538 struct sh_eth_private *mdp = netdev_priv(ndev);
539
540 /* reset device */
541 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
542 mdelay(1);
543
544 sh_eth_select_mii(ndev);
545}
546
547/* R8A7740 */
548static struct sh_eth_cpu_data r8a7740_data = {
549 .chip_reset = sh_eth_chip_reset_r8a7740,
550 .set_duplex = sh_eth_set_duplex,
551 .set_rate = sh_eth_set_rate_gether,
552
553 .register_type = SH_ETH_REG_GIGABIT,
554
555 .ecsr_value = ECSR_ICD | ECSR_MPD,
556 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
557 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
558
559 .tx_check = EESR_TC1 | EESR_FTC,
560 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
561 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
562 EESR_TDE | EESR_ECI,
563 .fdr_value = 0x0000070f,
564
565 .apr = 1,
566 .mpr = 1,
567 .tpauser = 1,
568 .bculr = 1,
569 .hw_swap = 1,
570 .rpadir = 1,
571 .rpadir_value = 2 << 16,
572 .no_trimd = 1,
573 .no_ade = 1,
574 .tsu = 1,
575 .select_mii = 1,
576 .shift_rd0 = 1,
577};
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100578
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000579/* There is CPU dependent code */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000580static void sh_eth_set_rate_r8a777x(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000581{
582 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000583
584 switch (mdp->speed) {
585 case 10: /* 10BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000586 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000587 break;
588 case 100:/* 100BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000589 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
590 break;
591 default:
592 break;
593 }
594}
595
Sergei Shtylyov674853b2013-04-27 10:44:24 +0000596/* R8A7778/9 */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000597static struct sh_eth_cpu_data r8a777x_data = {
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000598 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000599 .set_rate = sh_eth_set_rate_r8a777x,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000600
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400601 .register_type = SH_ETH_REG_FAST_RCAR,
602
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000603 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
604 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
605 .eesipr_value = 0x01ff009f,
606
607 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400608 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
609 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
610 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900611 .fdr_value = 0x00000f0f,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000612
613 .apr = 1,
614 .mpr = 1,
615 .tpauser = 1,
616 .hw_swap = 1,
617};
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000618
Sergei Shtylyov94a12b12013-12-08 02:59:18 +0300619/* R8A7790/1 */
620static struct sh_eth_cpu_data r8a779x_data = {
Simon Hormane18dbf72013-07-23 10:18:05 +0900621 .set_duplex = sh_eth_set_duplex,
622 .set_rate = sh_eth_set_rate_r8a777x,
623
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400624 .register_type = SH_ETH_REG_FAST_RCAR,
625
Simon Hormane18dbf72013-07-23 10:18:05 +0900626 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
627 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
628 .eesipr_value = 0x01ff009f,
629
630 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Laurent Pinchartba361cb2013-07-31 16:42:11 +0900631 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
632 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
633 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900634 .fdr_value = 0x00000f0f,
Simon Hormane18dbf72013-07-23 10:18:05 +0900635
Geert Uytterhoeven01fbd3f2015-01-15 11:52:19 +0100636 .trscer_err_mask = DESC_I_RINT8,
637
Simon Hormane18dbf72013-07-23 10:18:05 +0900638 .apr = 1,
639 .mpr = 1,
640 .tpauser = 1,
641 .hw_swap = 1,
642 .rmiimode = 1,
643};
Geert Uytterhoevenc74a2242015-11-24 15:40:58 +0100644#endif /* CONFIG_OF */
Simon Hormane18dbf72013-07-23 10:18:05 +0900645
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000646static void sh_eth_set_rate_sh7724(struct net_device *ndev)
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000647{
648 struct sh_eth_private *mdp = netdev_priv(ndev);
649
650 switch (mdp->speed) {
651 case 10: /* 10BASE */
652 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
653 break;
654 case 100:/* 100BASE */
655 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000656 break;
657 default:
658 break;
659 }
660}
661
662/* SH7724 */
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000663static struct sh_eth_cpu_data sh7724_data = {
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000664 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000665 .set_rate = sh_eth_set_rate_sh7724,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000666
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400667 .register_type = SH_ETH_REG_FAST_SH4,
668
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000669 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
670 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
Sergei Shtylyova80c3de2013-06-20 02:24:54 +0400671 .eesipr_value = 0x01ff009f,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000672
673 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400674 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
675 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
676 EESR_ECI,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000677
678 .apr = 1,
679 .mpr = 1,
680 .tpauser = 1,
681 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800682 .rpadir = 1,
683 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000684};
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000685
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000686static void sh_eth_set_rate_sh7757(struct net_device *ndev)
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000687{
688 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000689
690 switch (mdp->speed) {
691 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000692 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000693 break;
694 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000695 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000696 break;
697 default:
698 break;
699 }
700}
701
702/* SH7757 */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000703static struct sh_eth_cpu_data sh7757_data = {
704 .set_duplex = sh_eth_set_duplex,
705 .set_rate = sh_eth_set_rate_sh7757,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000706
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400707 .register_type = SH_ETH_REG_FAST_SH4,
708
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000709 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000710
711 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400712 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
713 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
714 EESR_ECI,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000715
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000716 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000717 .apr = 1,
718 .mpr = 1,
719 .tpauser = 1,
720 .hw_swap = 1,
721 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000722 .rpadir = 1,
723 .rpadir_value = 2 << 16,
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +0000724 .rtrate = 1,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000725};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000726
David S. Millere403d292013-06-07 23:40:41 -0700727#define SH_GIGA_ETH_BASE 0xfee00000UL
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000728#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
729#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
730static void sh_eth_chip_reset_giga(struct net_device *ndev)
731{
732 int i;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +0100733 u32 mahr[2], malr[2];
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000734
735 /* save MAHR and MALR */
736 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000737 malr[i] = ioread32((void *)GIGA_MALR(i));
738 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000739 }
740
741 /* reset device */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000742 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000743 mdelay(1);
744
745 /* restore MAHR and MALR */
746 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000747 iowrite32(malr[i], (void *)GIGA_MALR(i));
748 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000749 }
750}
751
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000752static void sh_eth_set_rate_giga(struct net_device *ndev)
753{
754 struct sh_eth_private *mdp = netdev_priv(ndev);
755
756 switch (mdp->speed) {
757 case 10: /* 10BASE */
758 sh_eth_write(ndev, 0x00000000, GECMR);
759 break;
760 case 100:/* 100BASE */
761 sh_eth_write(ndev, 0x00000010, GECMR);
762 break;
763 case 1000: /* 1000BASE */
764 sh_eth_write(ndev, 0x00000020, GECMR);
765 break;
766 default:
767 break;
768 }
769}
770
771/* SH7757(GETHERC) */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000772static struct sh_eth_cpu_data sh7757_data_giga = {
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000773 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000774 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000775 .set_rate = sh_eth_set_rate_giga,
776
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400777 .register_type = SH_ETH_REG_GIGABIT,
778
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000779 .ecsr_value = ECSR_ICD | ECSR_MPD,
780 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
781 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
782
783 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400784 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
785 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
786 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000787 .fdr_value = 0x0000072f,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000788
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000789 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000790 .apr = 1,
791 .mpr = 1,
792 .tpauser = 1,
793 .bculr = 1,
794 .hw_swap = 1,
795 .rpadir = 1,
796 .rpadir_value = 2 << 16,
797 .no_trimd = 1,
798 .no_ade = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000799 .tsu = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000800};
801
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000802/* SH7734 */
803static struct sh_eth_cpu_data sh7734_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000804 .chip_reset = sh_eth_chip_reset,
805 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000806 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000807
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400808 .register_type = SH_ETH_REG_GIGABIT,
809
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000810 .ecsr_value = ECSR_ICD | ECSR_MPD,
811 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
812 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
813
814 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400815 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
816 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
817 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000818
819 .apr = 1,
820 .mpr = 1,
821 .tpauser = 1,
822 .bculr = 1,
823 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000824 .no_trimd = 1,
825 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000826 .tsu = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000827 .hw_crc = 1,
828 .select_mii = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000829};
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000830
831/* SH7763 */
832static struct sh_eth_cpu_data sh7763_data = {
833 .chip_reset = sh_eth_chip_reset,
834 .set_duplex = sh_eth_set_duplex,
835 .set_rate = sh_eth_set_rate_gether,
836
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400837 .register_type = SH_ETH_REG_GIGABIT,
838
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000839 .ecsr_value = ECSR_ICD | ECSR_MPD,
840 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
841 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
842
843 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300844 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
845 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000846 EESR_ECI,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000847
848 .apr = 1,
849 .mpr = 1,
850 .tpauser = 1,
851 .bculr = 1,
852 .hw_swap = 1,
853 .no_trimd = 1,
854 .no_ade = 1,
855 .tsu = 1,
856 .irq_flags = IRQF_SHARED,
857};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000858
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +0000859static struct sh_eth_cpu_data sh7619_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400860 .register_type = SH_ETH_REG_FAST_SH3_SH2,
861
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000862 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
863
864 .apr = 1,
865 .mpr = 1,
866 .tpauser = 1,
867 .hw_swap = 1,
868};
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +0000869
870static struct sh_eth_cpu_data sh771x_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400871 .register_type = SH_ETH_REG_FAST_SH3_SH2,
872
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000873 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000874 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000875};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000876
877static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
878{
879 if (!cd->ecsr_value)
880 cd->ecsr_value = DEFAULT_ECSR_INIT;
881
882 if (!cd->ecsipr_value)
883 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
884
885 if (!cd->fcftr_value)
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300886 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000887 DEFAULT_FIFO_F_D_RFD;
888
889 if (!cd->fdr_value)
890 cd->fdr_value = DEFAULT_FDR_INIT;
891
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000892 if (!cd->tx_check)
893 cd->tx_check = DEFAULT_TX_CHECK;
894
895 if (!cd->eesr_err_check)
896 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +0900897
898 if (!cd->trscer_err_mask)
899 cd->trscer_err_mask = DEFAULT_TRSCER_ERR_MASK;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000900}
901
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000902static int sh_eth_check_reset(struct net_device *ndev)
903{
904 int ret = 0;
905 int cnt = 100;
906
907 while (cnt > 0) {
908 if (!(sh_eth_read(ndev, EDMR) & 0x3))
909 break;
910 mdelay(1);
911 cnt--;
912 }
Sergei Shtylyov9f8c4262013-06-05 23:54:01 +0400913 if (cnt <= 0) {
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300914 netdev_err(ndev, "Device reset failed\n");
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000915 ret = -ETIMEDOUT;
916 }
917 return ret;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000918}
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000919
920static int sh_eth_reset(struct net_device *ndev)
921{
922 struct sh_eth_private *mdp = netdev_priv(ndev);
923 int ret = 0;
924
Simon Hormandb893472014-01-17 09:22:28 +0900925 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp)) {
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000926 sh_eth_write(ndev, EDSR_ENALL, EDSR);
927 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
928 EDMR);
929
930 ret = sh_eth_check_reset(ndev);
931 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +0100932 return ret;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000933
934 /* Table Init */
935 sh_eth_write(ndev, 0x0, TDLAR);
936 sh_eth_write(ndev, 0x0, TDFAR);
937 sh_eth_write(ndev, 0x0, TDFXR);
938 sh_eth_write(ndev, 0x0, TDFFR);
939 sh_eth_write(ndev, 0x0, RDLAR);
940 sh_eth_write(ndev, 0x0, RDFAR);
941 sh_eth_write(ndev, 0x0, RDFXR);
942 sh_eth_write(ndev, 0x0, RDFFR);
943
944 /* Reset HW CRC register */
945 if (mdp->cd->hw_crc)
946 sh_eth_write(ndev, 0x0, CSMR);
947
948 /* Select MII mode */
949 if (mdp->cd->select_mii)
950 sh_eth_select_mii(ndev);
951 } else {
952 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
953 EDMR);
954 mdelay(3);
955 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
956 EDMR);
957 }
958
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000959 return ret;
960}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000961
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000962static void sh_eth_set_receive_align(struct sk_buff *skb)
963{
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900964 uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000965
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000966 if (reserve)
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900967 skb_reserve(skb, SH_ETH_RX_ALIGN - reserve);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000968}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000969
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300970/* Program the hardware MAC address from dev->dev_addr. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700971static void update_mac_address(struct net_device *ndev)
972{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000973 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300974 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
975 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000976 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300977 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700978}
979
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300980/* Get MAC address from SuperH MAC address register
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700981 *
982 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
983 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
984 * When you want use this device, you must set MAC address in bootloader.
985 *
986 */
Magnus Damm748031f2009-10-09 00:17:14 +0000987static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700988{
Magnus Damm748031f2009-10-09 00:17:14 +0000989 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
Joe Perchesd458cdf2013-10-01 19:04:40 -0700990 memcpy(ndev->dev_addr, mac, ETH_ALEN);
Magnus Damm748031f2009-10-09 00:17:14 +0000991 } else {
Sergei Shtylyov37742f02015-12-05 00:58:57 +0300992 u32 mahr = sh_eth_read(ndev, MAHR);
993 u32 malr = sh_eth_read(ndev, MALR);
994
995 ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
996 ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
997 ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
998 ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
999 ndev->dev_addr[4] = (malr >> 8) & 0xFF;
1000 ndev->dev_addr[5] = (malr >> 0) & 0xFF;
Magnus Damm748031f2009-10-09 00:17:14 +00001001 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001002}
1003
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001004static u32 sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001005{
Simon Hormandb893472014-01-17 09:22:28 +09001006 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001007 return EDTRR_TRNS_GETHER;
1008 else
1009 return EDTRR_TRNS_ETHER;
1010}
1011
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001012struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001013 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001014 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001015 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001016};
1017
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001018static void sh_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001019{
1020 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001021 u32 pir;
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001022
1023 if (bitbang->set_gate)
1024 bitbang->set_gate(bitbang->addr);
1025
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001026 pir = ioread32(bitbang->addr);
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001027 if (set)
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001028 pir |= mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001029 else
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001030 pir &= ~mask;
1031 iowrite32(pir, bitbang->addr);
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001032}
1033
1034/* Data I/O pin control */
1035static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1036{
1037 sh_mdio_ctrl(ctrl, PIR_MMD, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001038}
1039
1040/* Set bit data*/
1041static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
1042{
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001043 sh_mdio_ctrl(ctrl, PIR_MDO, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001044}
1045
1046/* Get bit data*/
1047static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
1048{
1049 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001050
1051 if (bitbang->set_gate)
1052 bitbang->set_gate(bitbang->addr);
1053
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001054 return (ioread32(bitbang->addr) & PIR_MDI) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001055}
1056
1057/* MDC pin control */
1058static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1059{
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001060 sh_mdio_ctrl(ctrl, PIR_MDC, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001061}
1062
1063/* mdio bus control struct */
1064static struct mdiobb_ops bb_ops = {
1065 .owner = THIS_MODULE,
1066 .set_mdc = sh_mdc_ctrl,
1067 .set_mdio_dir = sh_mmd_ctrl,
1068 .set_mdio_data = sh_set_mdio,
1069 .get_mdio_data = sh_get_mdio,
1070};
1071
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001072/* free skb and descriptor buffer */
1073static void sh_eth_ring_free(struct net_device *ndev)
1074{
1075 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001076 int ringsize, i;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001077
1078 /* Free Rx skb ringbuffer */
1079 if (mdp->rx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001080 for (i = 0; i < mdp->num_rx_ring; i++)
1081 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001082 }
1083 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001084 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001085
1086 /* Free Tx skb ringbuffer */
1087 if (mdp->tx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001088 for (i = 0; i < mdp->num_tx_ring; i++)
1089 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001090 }
1091 kfree(mdp->tx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001092 mdp->tx_skbuff = NULL;
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001093
1094 if (mdp->rx_ring) {
1095 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
1096 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1097 mdp->rx_desc_dma);
1098 mdp->rx_ring = NULL;
1099 }
1100
1101 if (mdp->tx_ring) {
1102 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
1103 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1104 mdp->tx_desc_dma);
1105 mdp->tx_ring = NULL;
1106 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001107}
1108
1109/* format skb and descriptor buffer */
1110static void sh_eth_ring_format(struct net_device *ndev)
1111{
1112 struct sh_eth_private *mdp = netdev_priv(ndev);
1113 int i;
1114 struct sk_buff *skb;
1115 struct sh_eth_rxdesc *rxdesc = NULL;
1116 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001117 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
1118 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Sergei Shtylyovcb368592015-10-24 00:46:40 +03001119 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001120 dma_addr_t dma_addr;
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001121 u32 buf_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001122
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001123 mdp->cur_rx = 0;
1124 mdp->cur_tx = 0;
1125 mdp->dirty_rx = 0;
1126 mdp->dirty_tx = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001127
1128 memset(mdp->rx_ring, 0, rx_ringsize);
1129
1130 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001131 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001132 /* skb */
1133 mdp->rx_skbuff[i] = NULL;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001134 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001135 if (skb == NULL)
1136 break;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001137 sh_eth_set_receive_align(skb);
1138
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001139 /* RX descriptor */
1140 rxdesc = &mdp->rx_ring[i];
Sergei Shtylyovab857912015-10-24 00:46:03 +03001141 /* The size of the buffer is a multiple of 32 bytes. */
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001142 buf_len = ALIGN(mdp->rx_buf_sz, 32);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001143 rxdesc->len = cpu_to_le32(buf_len << 16);
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001144 dma_addr = dma_map_single(&ndev->dev, skb->data, buf_len,
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001145 DMA_FROM_DEVICE);
1146 if (dma_mapping_error(&ndev->dev, dma_addr)) {
1147 kfree_skb(skb);
1148 break;
1149 }
1150 mdp->rx_skbuff[i] = skb;
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001151 rxdesc->addr = cpu_to_le32(dma_addr);
1152 rxdesc->status = cpu_to_le32(RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001153
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001154 /* Rx descriptor address set */
1155 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001156 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001157 if (sh_eth_is_gether(mdp) ||
1158 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001159 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001160 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001161 }
1162
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001163 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001164
1165 /* Mark the last entry as wrapping the ring. */
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001166 rxdesc->status |= cpu_to_le32(RD_RDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001167
1168 memset(mdp->tx_ring, 0, tx_ringsize);
1169
1170 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001171 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001172 mdp->tx_skbuff[i] = NULL;
1173 txdesc = &mdp->tx_ring[i];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001174 txdesc->status = cpu_to_le32(TD_TFP);
1175 txdesc->len = cpu_to_le32(0);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001176 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001177 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001178 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001179 if (sh_eth_is_gether(mdp) ||
1180 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001181 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001182 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001183 }
1184
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001185 txdesc->status |= cpu_to_le32(TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001186}
1187
1188/* Get skb and descriptor buffer */
1189static int sh_eth_ring_init(struct net_device *ndev)
1190{
1191 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001192 int rx_ringsize, tx_ringsize;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001193
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001194 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001195 * card needs room to do 8 byte alignment, +2 so we can reserve
1196 * the first 2 bytes, and +16 gets room for the status word from the
1197 * card.
1198 */
1199 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1200 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001201 if (mdp->cd->rpadir)
1202 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001203
1204 /* Allocate RX and TX skb rings */
Sergei Shtylyov2c94e852015-10-31 02:05:56 +03001205 mdp->rx_skbuff = kcalloc(mdp->num_rx_ring, sizeof(*mdp->rx_skbuff),
1206 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001207 if (!mdp->rx_skbuff)
1208 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001209
Sergei Shtylyov2c94e852015-10-31 02:05:56 +03001210 mdp->tx_skbuff = kcalloc(mdp->num_tx_ring, sizeof(*mdp->tx_skbuff),
1211 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001212 if (!mdp->tx_skbuff)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001213 goto ring_free;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001214
1215 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001216 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001217 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001218 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001219 if (!mdp->rx_ring)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001220 goto ring_free;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001221
1222 mdp->dirty_rx = 0;
1223
1224 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001225 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001226 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001227 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001228 if (!mdp->tx_ring)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001229 goto ring_free;
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001230 return 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001231
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001232ring_free:
1233 /* Free Rx and Tx skb ring buffer and DMA buffer */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001234 sh_eth_ring_free(ndev);
1235
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001236 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001237}
1238
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001239static int sh_eth_dev_init(struct net_device *ndev, bool start)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001240{
1241 int ret = 0;
1242 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001243 u32 val;
1244
1245 /* Soft Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001246 ret = sh_eth_reset(ndev);
1247 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +01001248 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001249
Simon Horman55754f12013-07-23 10:18:04 +09001250 if (mdp->cd->rmiimode)
1251 sh_eth_write(ndev, 0x1, RMIIMODE);
1252
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001253 /* Descriptor format */
1254 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001255 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001256 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001257
1258 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001259 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001260
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001261#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001262 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001263 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001264 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001265#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001266 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001267
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001268 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001269 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1270 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001271
Ben Dooks530aa2d2014-06-03 12:21:13 +01001272 /* Frame recv control (enable multiple-packets per rx irq) */
1273 sh_eth_write(ndev, RMCR_RNC, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001274
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +09001275 sh_eth_write(ndev, mdp->cd->trscer_err_mask, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001276
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001277 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001278 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001279
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001280 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001281
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001282 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001283 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001284
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001285 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001286 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1287 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001288
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001289 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00001290 if (start) {
1291 mdp->irq_enabled = true;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001292 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00001293 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001294
1295 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001296 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001297 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
1298
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001299 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001300
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001301 if (mdp->cd->set_rate)
1302 mdp->cd->set_rate(ndev);
1303
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001304 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001305 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001306
1307 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001308 if (start)
1309 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001310
1311 /* Set MAC address */
1312 update_mac_address(ndev);
1313
1314 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001315 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001316 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001317 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001318 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001319 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001320 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001321
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001322 if (start) {
1323 /* Setting the Rx mode will start the Rx process. */
1324 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001325
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001326 netif_start_queue(ndev);
1327 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001328
1329 return ret;
1330}
1331
Ben Hutchings740c7f32015-01-27 00:49:32 +00001332static void sh_eth_dev_exit(struct net_device *ndev)
1333{
1334 struct sh_eth_private *mdp = netdev_priv(ndev);
1335 int i;
1336
1337 /* Deactivate all TX descriptors, so DMA should stop at next
1338 * packet boundary if it's currently running
1339 */
1340 for (i = 0; i < mdp->num_tx_ring; i++)
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001341 mdp->tx_ring[i].status &= ~cpu_to_le32(TD_TACT);
Ben Hutchings740c7f32015-01-27 00:49:32 +00001342
1343 /* Disable TX FIFO egress to MAC */
1344 sh_eth_rcv_snd_disable(ndev);
1345
1346 /* Stop RX DMA at next packet boundary */
1347 sh_eth_write(ndev, 0, EDRRR);
1348
1349 /* Aside from TX DMA, we can't tell when the hardware is
1350 * really stopped, so we need to reset to make sure.
1351 * Before doing that, wait for long enough to *probably*
1352 * finish transmitting the last packet and poll stats.
1353 */
1354 msleep(2); /* max frame time at 10 Mbps < 1250 us */
1355 sh_eth_get_stats(ndev);
1356 sh_eth_reset(ndev);
Geert Uytterhoevena14c7d12015-02-27 17:16:26 +01001357
1358 /* Set MAC address again */
1359 update_mac_address(ndev);
Ben Hutchings740c7f32015-01-27 00:49:32 +00001360}
1361
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001362/* free Tx skb function */
1363static int sh_eth_txfree(struct net_device *ndev)
1364{
1365 struct sh_eth_private *mdp = netdev_priv(ndev);
1366 struct sh_eth_txdesc *txdesc;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001367 int free_num = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001368 int entry = 0;
1369
1370 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001371 entry = mdp->dirty_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001372 txdesc = &mdp->tx_ring[entry];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001373 if (txdesc->status & cpu_to_le32(TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001374 break;
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001375 /* TACT bit must be checked before all the following reads */
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001376 dma_rmb();
Ben Hutchingse5fd13f2015-02-26 20:34:46 +00001377 netif_info(mdp, tx_done, ndev,
1378 "tx entry %d status 0x%08x\n",
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001379 entry, le32_to_cpu(txdesc->status));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001380 /* Free the original skb. */
1381 if (mdp->tx_skbuff[entry]) {
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001382 dma_unmap_single(&ndev->dev, le32_to_cpu(txdesc->addr),
1383 le32_to_cpu(txdesc->len) >> 16,
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001384 DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001385 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1386 mdp->tx_skbuff[entry] = NULL;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001387 free_num++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001388 }
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001389 txdesc->status = cpu_to_le32(TD_TFP);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001390 if (entry >= mdp->num_tx_ring - 1)
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001391 txdesc->status |= cpu_to_le32(TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001392
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001393 ndev->stats.tx_packets++;
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001394 ndev->stats.tx_bytes += le32_to_cpu(txdesc->len) >> 16;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001395 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001396 return free_num;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001397}
1398
1399/* Packet receive function */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001400static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001401{
1402 struct sh_eth_private *mdp = netdev_priv(ndev);
1403 struct sh_eth_rxdesc *rxdesc;
1404
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001405 int entry = mdp->cur_rx % mdp->num_rx_ring;
1406 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001407 int limit;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001408 struct sk_buff *skb;
1409 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001410 u32 desc_status;
Sergei Shtylyovcb368592015-10-24 00:46:40 +03001411 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001412 dma_addr_t dma_addr;
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001413 u32 buf_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001414
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001415 boguscnt = min(boguscnt, *quota);
1416 limit = boguscnt;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001417 rxdesc = &mdp->rx_ring[entry];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001418 while (!(rxdesc->status & cpu_to_le32(RD_RACT))) {
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001419 /* RACT bit must be checked before all the following reads */
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001420 dma_rmb();
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001421 desc_status = le32_to_cpu(rxdesc->status);
1422 pkt_len = le32_to_cpu(rxdesc->len) & RD_RFL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001423
1424 if (--boguscnt < 0)
1425 break;
1426
Ben Hutchingse5fd13f2015-02-26 20:34:46 +00001427 netif_info(mdp, rx_status, ndev,
1428 "rx entry %d status 0x%08x len %d\n",
1429 entry, desc_status, pkt_len);
1430
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001431 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001432 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001433
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001434 /* In case of almost all GETHER/ETHERs, the Receive Frame State
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001435 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
Ben Hutchings9b4a6362015-03-03 00:52:39 +00001436 * bit 0. However, in case of the R8A7740 and R7S72100
1437 * the RFS bits are from bit 25 to bit 16. So, the
Simon Hormandb893472014-01-17 09:22:28 +09001438 * driver needs right shifting by 16.
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001439 */
Sergei Shtylyovac8025a2013-06-13 22:12:45 +04001440 if (mdp->cd->shift_rd0)
1441 desc_status >>= 16;
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001442
Sergei Shtylyov248be832015-12-04 01:45:40 +03001443 skb = mdp->rx_skbuff[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001444 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1445 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001446 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001447 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001448 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001449 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001450 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001451 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001452 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001453 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001454 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001455 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001456 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001457 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001458 ndev->stats.rx_over_errors++;
Sergei Shtylyov248be832015-12-04 01:45:40 +03001459 } else if (skb) {
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001460 dma_addr = le32_to_cpu(rxdesc->addr);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001461 if (!mdp->cd->hw_swap)
1462 sh_eth_soft_swap(
Sergei Shtylyov12996532015-12-13 23:05:07 +03001463 phys_to_virt(ALIGN(dma_addr, 4)),
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001464 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001465 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001466 if (mdp->cd->rpadir)
1467 skb_reserve(skb, NET_IP_ALIGN);
Sergei Shtylyov12996532015-12-13 23:05:07 +03001468 dma_unmap_single(&ndev->dev, dma_addr,
Sergei Shtylyovab857912015-10-24 00:46:03 +03001469 ALIGN(mdp->rx_buf_sz, 32),
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001470 DMA_FROM_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001471 skb_put(skb, pkt_len);
1472 skb->protocol = eth_type_trans(skb, ndev);
Sergei Shtylyova8e9fd02013-09-03 03:03:10 +04001473 netif_receive_skb(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001474 ndev->stats.rx_packets++;
1475 ndev->stats.rx_bytes += pkt_len;
Ben Hutchings25b77ad2015-02-26 20:33:30 +00001476 if (desc_status & RD_RFS8)
1477 ndev->stats.multicast++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001478 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001479 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001480 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001481 }
1482
1483 /* Refill the Rx ring buffers. */
1484 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001485 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001486 rxdesc = &mdp->rx_ring[entry];
Sergei Shtylyovab857912015-10-24 00:46:03 +03001487 /* The size of the buffer is 32 byte boundary. */
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001488 buf_len = ALIGN(mdp->rx_buf_sz, 32);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001489 rxdesc->len = cpu_to_le32(buf_len << 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001490
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001491 if (mdp->rx_skbuff[entry] == NULL) {
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001492 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001493 if (skb == NULL)
1494 break; /* Better luck next round. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001495 sh_eth_set_receive_align(skb);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001496 dma_addr = dma_map_single(&ndev->dev, skb->data,
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001497 buf_len, DMA_FROM_DEVICE);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001498 if (dma_mapping_error(&ndev->dev, dma_addr)) {
1499 kfree_skb(skb);
1500 break;
1501 }
1502 mdp->rx_skbuff[entry] = skb;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001503
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001504 skb_checksum_none_assert(skb);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001505 rxdesc->addr = cpu_to_le32(dma_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001506 }
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001507 dma_wmb(); /* RACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001508 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001509 rxdesc->status |=
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001510 cpu_to_le32(RD_RACT | RD_RFP | RD_RDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001511 else
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001512 rxdesc->status |= cpu_to_le32(RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001513 }
1514
1515 /* Restart Rx engine if stopped. */
1516 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001517 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001518 /* fix the values for the next receiving if RDE is set */
Ben Hutchings33657112015-02-26 20:34:14 +00001519 if (intr_status & EESR_RDE &&
1520 mdp->reg_offset[RDFAR] != SH_ETH_OFFSET_INVALID) {
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001521 u32 count = (sh_eth_read(ndev, RDFAR) -
1522 sh_eth_read(ndev, RDLAR)) >> 4;
1523
1524 mdp->cur_rx = count;
1525 mdp->dirty_rx = count;
1526 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001527 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001528 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001529
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001530 *quota -= limit - boguscnt - 1;
1531
Yoshihiro Shimoda4f809ce2014-06-10 09:40:14 +09001532 return *quota <= 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001533}
1534
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001535static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001536{
1537 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001538 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
1539 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001540}
1541
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001542static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001543{
1544 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001545 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
1546 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001547}
1548
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001549/* error control function */
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001550static void sh_eth_error(struct net_device *ndev, u32 intr_status)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001551{
1552 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001553 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001554 u32 link_stat;
1555 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001556
1557 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001558 felic_stat = sh_eth_read(ndev, ECSR);
1559 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001560 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001561 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001562 if (felic_stat & ECSR_LCHNG) {
1563 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001564 if (mdp->cd->no_psr || mdp->no_ether_link) {
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001565 goto ignore_link;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001566 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001567 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001568 if (mdp->ether_link_active_low)
1569 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001570 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001571 if (!(link_stat & PHY_ST_LINK)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001572 sh_eth_rcv_snd_disable(ndev);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001573 } else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001574 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001575 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001576 ~DMAC_M_ECI, EESIPR);
1577 /* clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001578 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001579 ECSR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001580 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001581 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001582 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001583 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001584 }
1585 }
1586 }
1587
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001588ignore_link:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001589 if (intr_status & EESR_TWB) {
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001590 /* Unused write back interrupt */
1591 if (intr_status & EESR_TABT) { /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001592 ndev->stats.tx_aborted_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001593 netif_err(mdp, tx_err, ndev, "Transmit Abort\n");
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001594 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001595 }
1596
1597 if (intr_status & EESR_RABT) {
1598 /* Receive Abort int */
1599 if (intr_status & EESR_RFRMER) {
1600 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001601 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001602 }
1603 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001604
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001605 if (intr_status & EESR_TDE) {
1606 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001607 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001608 netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n");
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001609 }
1610
1611 if (intr_status & EESR_TFE) {
1612 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001613 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001614 netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001615 }
1616
1617 if (intr_status & EESR_RDE) {
1618 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001619 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001620 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001621
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001622 if (intr_status & EESR_RFE) {
1623 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001624 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001625 }
1626
1627 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1628 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001629 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001630 netif_err(mdp, tx_err, ndev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001631 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001632
1633 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1634 if (mdp->cd->no_ade)
1635 mask &= ~EESR_ADE;
1636 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001637 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001638 u32 edtrr = sh_eth_read(ndev, EDTRR);
Sergei Shtylyov090d5602014-01-11 02:41:49 +03001639
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001640 /* dmesg */
Sergei Shtylyovda246852014-03-15 03:29:14 +03001641 netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1642 intr_status, mdp->cur_tx, mdp->dirty_tx,
1643 (u32)ndev->state, edtrr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001644 /* dirty buffer free */
1645 sh_eth_txfree(ndev);
1646
1647 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001648 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001649 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001650 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001651 }
1652 /* wakeup */
1653 netif_wake_queue(ndev);
1654 }
1655}
1656
1657static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1658{
1659 struct net_device *ndev = netdev;
1660 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001661 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001662 irqreturn_t ret = IRQ_NONE;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001663 u32 intr_status, intr_enable;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001664
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001665 spin_lock(&mdp->lock);
1666
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001667 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001668 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001669 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1670 * enabled since it's the one that comes thru regardless of the mask,
1671 * and we need to fully handle it in sh_eth_error() in order to quench
1672 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1673 */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001674 intr_enable = sh_eth_read(ndev, EESIPR);
1675 intr_status &= intr_enable | DMAC_M_ECI;
1676 if (intr_status & (EESR_RX_CHECK | cd->tx_check | cd->eesr_err_check))
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001677 ret = IRQ_HANDLED;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001678 else
Ben Hutchings283e38d2015-01-22 12:44:08 +00001679 goto out;
1680
1681 if (!likely(mdp->irq_enabled)) {
1682 sh_eth_write(ndev, 0, EESIPR);
1683 goto out;
1684 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001685
Sergei Shtylyov37191092013-06-19 23:30:23 +04001686 if (intr_status & EESR_RX_CHECK) {
1687 if (napi_schedule_prep(&mdp->napi)) {
1688 /* Mask Rx interrupts */
1689 sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
1690 EESIPR);
1691 __napi_schedule(&mdp->napi);
1692 } else {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001693 netdev_warn(ndev,
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001694 "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
Sergei Shtylyovda246852014-03-15 03:29:14 +03001695 intr_status, intr_enable);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001696 }
1697 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001698
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001699 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001700 if (intr_status & cd->tx_check) {
Sergei Shtylyov37191092013-06-19 23:30:23 +04001701 /* Clear Tx interrupts */
1702 sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
1703
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001704 sh_eth_txfree(ndev);
1705 netif_wake_queue(ndev);
1706 }
1707
Sergei Shtylyov37191092013-06-19 23:30:23 +04001708 if (intr_status & cd->eesr_err_check) {
1709 /* Clear error interrupts */
1710 sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
1711
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001712 sh_eth_error(ndev, intr_status);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001713 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001714
Ben Hutchings283e38d2015-01-22 12:44:08 +00001715out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001716 spin_unlock(&mdp->lock);
1717
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001718 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001719}
1720
Sergei Shtylyov37191092013-06-19 23:30:23 +04001721static int sh_eth_poll(struct napi_struct *napi, int budget)
1722{
1723 struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
1724 napi);
1725 struct net_device *ndev = napi->dev;
1726 int quota = budget;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001727 u32 intr_status;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001728
1729 for (;;) {
1730 intr_status = sh_eth_read(ndev, EESR);
1731 if (!(intr_status & EESR_RX_CHECK))
1732 break;
1733 /* Clear Rx interrupts */
1734 sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
1735
1736 if (sh_eth_rx(ndev, intr_status, &quota))
1737 goto out;
1738 }
1739
1740 napi_complete(napi);
1741
1742 /* Reenable Rx interrupts */
Ben Hutchings283e38d2015-01-22 12:44:08 +00001743 if (mdp->irq_enabled)
1744 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001745out:
1746 return budget - quota;
1747}
1748
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001749/* PHY state control function */
1750static void sh_eth_adjust_link(struct net_device *ndev)
1751{
1752 struct sh_eth_private *mdp = netdev_priv(ndev);
1753 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001754 int new_state = 0;
1755
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001756 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001757 if (phydev->duplex != mdp->duplex) {
1758 new_state = 1;
1759 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001760 if (mdp->cd->set_duplex)
1761 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001762 }
1763
1764 if (phydev->speed != mdp->speed) {
1765 new_state = 1;
1766 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001767 if (mdp->cd->set_rate)
1768 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001769 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001770 if (!mdp->link) {
Yoshihiro Shimoda91a56152011-07-05 20:33:51 +00001771 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001772 sh_eth_read(ndev, ECMR) & ~ECMR_TXF,
1773 ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001774 new_state = 1;
1775 mdp->link = phydev->link;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001776 if (mdp->cd->no_psr || mdp->no_ether_link)
1777 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001778 }
1779 } else if (mdp->link) {
1780 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001781 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001782 mdp->speed = 0;
1783 mdp->duplex = -1;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001784 if (mdp->cd->no_psr || mdp->no_ether_link)
1785 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001786 }
1787
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001788 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001789 phy_print_status(phydev);
1790}
1791
1792/* PHY init function */
1793static int sh_eth_phy_init(struct net_device *ndev)
1794{
Ben Dooks702eca02014-03-12 17:47:40 +00001795 struct device_node *np = ndev->dev.parent->of_node;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001796 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001797 struct phy_device *phydev = NULL;
1798
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001799 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001800 mdp->speed = 0;
1801 mdp->duplex = -1;
1802
1803 /* Try connect to PHY */
Ben Dooks702eca02014-03-12 17:47:40 +00001804 if (np) {
1805 struct device_node *pn;
1806
1807 pn = of_parse_phandle(np, "phy-handle", 0);
1808 phydev = of_phy_connect(ndev, pn,
1809 sh_eth_adjust_link, 0,
1810 mdp->phy_interface);
1811
1812 if (!phydev)
1813 phydev = ERR_PTR(-ENOENT);
1814 } else {
1815 char phy_id[MII_BUS_ID_SIZE + 3];
1816
1817 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
1818 mdp->mii_bus->id, mdp->phy_id);
1819
1820 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
1821 mdp->phy_interface);
1822 }
1823
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001824 if (IS_ERR(phydev)) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001825 netdev_err(ndev, "failed to connect PHY\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001826 return PTR_ERR(phydev);
1827 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001828
Andrew Lunn22209432016-01-06 20:11:13 +01001829 phy_attached_info(phydev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001830
1831 mdp->phydev = phydev;
1832
1833 return 0;
1834}
1835
1836/* PHY control start function */
1837static int sh_eth_phy_start(struct net_device *ndev)
1838{
1839 struct sh_eth_private *mdp = netdev_priv(ndev);
1840 int ret;
1841
1842 ret = sh_eth_phy_init(ndev);
1843 if (ret)
1844 return ret;
1845
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001846 phy_start(mdp->phydev);
1847
1848 return 0;
1849}
1850
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001851static int sh_eth_get_settings(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001852 struct ethtool_cmd *ecmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001853{
1854 struct sh_eth_private *mdp = netdev_priv(ndev);
1855 unsigned long flags;
1856 int ret;
1857
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001858 if (!mdp->phydev)
1859 return -ENODEV;
1860
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001861 spin_lock_irqsave(&mdp->lock, flags);
1862 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1863 spin_unlock_irqrestore(&mdp->lock, flags);
1864
1865 return ret;
1866}
1867
1868static int sh_eth_set_settings(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001869 struct ethtool_cmd *ecmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001870{
1871 struct sh_eth_private *mdp = netdev_priv(ndev);
1872 unsigned long flags;
1873 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001874
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001875 if (!mdp->phydev)
1876 return -ENODEV;
1877
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001878 spin_lock_irqsave(&mdp->lock, flags);
1879
1880 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001881 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001882
1883 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1884 if (ret)
1885 goto error_exit;
1886
1887 if (ecmd->duplex == DUPLEX_FULL)
1888 mdp->duplex = 1;
1889 else
1890 mdp->duplex = 0;
1891
1892 if (mdp->cd->set_duplex)
1893 mdp->cd->set_duplex(ndev);
1894
1895error_exit:
1896 mdelay(1);
1897
1898 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001899 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001900
1901 spin_unlock_irqrestore(&mdp->lock, flags);
1902
1903 return ret;
1904}
1905
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00001906/* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
1907 * version must be bumped as well. Just adding registers up to that
1908 * limit is fine, as long as the existing register indices don't
1909 * change.
1910 */
1911#define SH_ETH_REG_DUMP_VERSION 1
1912#define SH_ETH_REG_DUMP_MAX_REGS 256
1913
1914static size_t __sh_eth_get_regs(struct net_device *ndev, u32 *buf)
1915{
1916 struct sh_eth_private *mdp = netdev_priv(ndev);
1917 struct sh_eth_cpu_data *cd = mdp->cd;
1918 u32 *valid_map;
1919 size_t len;
1920
1921 BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET > SH_ETH_REG_DUMP_MAX_REGS);
1922
1923 /* Dump starts with a bitmap that tells ethtool which
1924 * registers are defined for this chip.
1925 */
1926 len = DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS, 32);
1927 if (buf) {
1928 valid_map = buf;
1929 buf += len;
1930 } else {
1931 valid_map = NULL;
1932 }
1933
1934 /* Add a register to the dump, if it has a defined offset.
1935 * This automatically skips most undefined registers, but for
1936 * some it is also necessary to check a capability flag in
1937 * struct sh_eth_cpu_data.
1938 */
1939#define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
1940#define add_reg_from(reg, read_expr) do { \
1941 if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
1942 if (buf) { \
1943 mark_reg_valid(reg); \
1944 *buf++ = read_expr; \
1945 } \
1946 ++len; \
1947 } \
1948 } while (0)
1949#define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
1950#define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
1951
1952 add_reg(EDSR);
1953 add_reg(EDMR);
1954 add_reg(EDTRR);
1955 add_reg(EDRRR);
1956 add_reg(EESR);
1957 add_reg(EESIPR);
1958 add_reg(TDLAR);
1959 add_reg(TDFAR);
1960 add_reg(TDFXR);
1961 add_reg(TDFFR);
1962 add_reg(RDLAR);
1963 add_reg(RDFAR);
1964 add_reg(RDFXR);
1965 add_reg(RDFFR);
1966 add_reg(TRSCER);
1967 add_reg(RMFCR);
1968 add_reg(TFTR);
1969 add_reg(FDR);
1970 add_reg(RMCR);
1971 add_reg(TFUCR);
1972 add_reg(RFOCR);
1973 if (cd->rmiimode)
1974 add_reg(RMIIMODE);
1975 add_reg(FCFTR);
1976 if (cd->rpadir)
1977 add_reg(RPADIR);
1978 if (!cd->no_trimd)
1979 add_reg(TRIMD);
1980 add_reg(ECMR);
1981 add_reg(ECSR);
1982 add_reg(ECSIPR);
1983 add_reg(PIR);
1984 if (!cd->no_psr)
1985 add_reg(PSR);
1986 add_reg(RDMLR);
1987 add_reg(RFLR);
1988 add_reg(IPGR);
1989 if (cd->apr)
1990 add_reg(APR);
1991 if (cd->mpr)
1992 add_reg(MPR);
1993 add_reg(RFCR);
1994 add_reg(RFCF);
1995 if (cd->tpauser)
1996 add_reg(TPAUSER);
1997 add_reg(TPAUSECR);
1998 add_reg(GECMR);
1999 if (cd->bculr)
2000 add_reg(BCULR);
2001 add_reg(MAHR);
2002 add_reg(MALR);
2003 add_reg(TROCR);
2004 add_reg(CDCR);
2005 add_reg(LCCR);
2006 add_reg(CNDCR);
2007 add_reg(CEFCR);
2008 add_reg(FRECR);
2009 add_reg(TSFRCR);
2010 add_reg(TLFRCR);
2011 add_reg(CERCR);
2012 add_reg(CEECR);
2013 add_reg(MAFCR);
2014 if (cd->rtrate)
2015 add_reg(RTRATE);
2016 if (cd->hw_crc)
2017 add_reg(CSMR);
2018 if (cd->select_mii)
2019 add_reg(RMII_MII);
2020 add_reg(ARSTR);
2021 if (cd->tsu) {
2022 add_tsu_reg(TSU_CTRST);
2023 add_tsu_reg(TSU_FWEN0);
2024 add_tsu_reg(TSU_FWEN1);
2025 add_tsu_reg(TSU_FCM);
2026 add_tsu_reg(TSU_BSYSL0);
2027 add_tsu_reg(TSU_BSYSL1);
2028 add_tsu_reg(TSU_PRISL0);
2029 add_tsu_reg(TSU_PRISL1);
2030 add_tsu_reg(TSU_FWSL0);
2031 add_tsu_reg(TSU_FWSL1);
2032 add_tsu_reg(TSU_FWSLC);
2033 add_tsu_reg(TSU_QTAG0);
2034 add_tsu_reg(TSU_QTAG1);
2035 add_tsu_reg(TSU_QTAGM0);
2036 add_tsu_reg(TSU_QTAGM1);
2037 add_tsu_reg(TSU_FWSR);
2038 add_tsu_reg(TSU_FWINMK);
2039 add_tsu_reg(TSU_ADQT0);
2040 add_tsu_reg(TSU_ADQT1);
2041 add_tsu_reg(TSU_VTAG0);
2042 add_tsu_reg(TSU_VTAG1);
2043 add_tsu_reg(TSU_ADSBSY);
2044 add_tsu_reg(TSU_TEN);
2045 add_tsu_reg(TSU_POST1);
2046 add_tsu_reg(TSU_POST2);
2047 add_tsu_reg(TSU_POST3);
2048 add_tsu_reg(TSU_POST4);
2049 if (mdp->reg_offset[TSU_ADRH0] != SH_ETH_OFFSET_INVALID) {
2050 /* This is the start of a table, not just a single
2051 * register.
2052 */
2053 if (buf) {
2054 unsigned int i;
2055
2056 mark_reg_valid(TSU_ADRH0);
2057 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES * 2; i++)
2058 *buf++ = ioread32(
2059 mdp->tsu_addr +
2060 mdp->reg_offset[TSU_ADRH0] +
2061 i * 4);
2062 }
2063 len += SH_ETH_TSU_CAM_ENTRIES * 2;
2064 }
2065 }
2066
2067#undef mark_reg_valid
2068#undef add_reg_from
2069#undef add_reg
2070#undef add_tsu_reg
2071
2072 return len * 4;
2073}
2074
2075static int sh_eth_get_regs_len(struct net_device *ndev)
2076{
2077 return __sh_eth_get_regs(ndev, NULL);
2078}
2079
2080static void sh_eth_get_regs(struct net_device *ndev, struct ethtool_regs *regs,
2081 void *buf)
2082{
2083 struct sh_eth_private *mdp = netdev_priv(ndev);
2084
2085 regs->version = SH_ETH_REG_DUMP_VERSION;
2086
2087 pm_runtime_get_sync(&mdp->pdev->dev);
2088 __sh_eth_get_regs(ndev, buf);
2089 pm_runtime_put_sync(&mdp->pdev->dev);
2090}
2091
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002092static int sh_eth_nway_reset(struct net_device *ndev)
2093{
2094 struct sh_eth_private *mdp = netdev_priv(ndev);
2095 unsigned long flags;
2096 int ret;
2097
Ben Hutchings4f9dce232015-01-16 17:51:25 +00002098 if (!mdp->phydev)
2099 return -ENODEV;
2100
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002101 spin_lock_irqsave(&mdp->lock, flags);
2102 ret = phy_start_aneg(mdp->phydev);
2103 spin_unlock_irqrestore(&mdp->lock, flags);
2104
2105 return ret;
2106}
2107
2108static u32 sh_eth_get_msglevel(struct net_device *ndev)
2109{
2110 struct sh_eth_private *mdp = netdev_priv(ndev);
2111 return mdp->msg_enable;
2112}
2113
2114static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
2115{
2116 struct sh_eth_private *mdp = netdev_priv(ndev);
2117 mdp->msg_enable = value;
2118}
2119
2120static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
2121 "rx_current", "tx_current",
2122 "rx_dirty", "tx_dirty",
2123};
2124#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
2125
2126static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
2127{
2128 switch (sset) {
2129 case ETH_SS_STATS:
2130 return SH_ETH_STATS_LEN;
2131 default:
2132 return -EOPNOTSUPP;
2133 }
2134}
2135
2136static void sh_eth_get_ethtool_stats(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002137 struct ethtool_stats *stats, u64 *data)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002138{
2139 struct sh_eth_private *mdp = netdev_priv(ndev);
2140 int i = 0;
2141
2142 /* device-specific stats */
2143 data[i++] = mdp->cur_rx;
2144 data[i++] = mdp->cur_tx;
2145 data[i++] = mdp->dirty_rx;
2146 data[i++] = mdp->dirty_tx;
2147}
2148
2149static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
2150{
2151 switch (stringset) {
2152 case ETH_SS_STATS:
2153 memcpy(data, *sh_eth_gstrings_stats,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002154 sizeof(sh_eth_gstrings_stats));
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002155 break;
2156 }
2157}
2158
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002159static void sh_eth_get_ringparam(struct net_device *ndev,
2160 struct ethtool_ringparam *ring)
2161{
2162 struct sh_eth_private *mdp = netdev_priv(ndev);
2163
2164 ring->rx_max_pending = RX_RING_MAX;
2165 ring->tx_max_pending = TX_RING_MAX;
2166 ring->rx_pending = mdp->num_rx_ring;
2167 ring->tx_pending = mdp->num_tx_ring;
2168}
2169
2170static int sh_eth_set_ringparam(struct net_device *ndev,
2171 struct ethtool_ringparam *ring)
2172{
2173 struct sh_eth_private *mdp = netdev_priv(ndev);
2174 int ret;
2175
2176 if (ring->tx_pending > TX_RING_MAX ||
2177 ring->rx_pending > RX_RING_MAX ||
2178 ring->tx_pending < TX_RING_MIN ||
2179 ring->rx_pending < RX_RING_MIN)
2180 return -EINVAL;
2181 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2182 return -EINVAL;
2183
2184 if (netif_running(ndev)) {
Ben Hutchingsbd888912015-01-22 12:40:25 +00002185 netif_device_detach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002186 netif_tx_disable(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002187
Ben Hutchings283e38d2015-01-22 12:44:08 +00002188 /* Serialise with the interrupt handler and NAPI, then
2189 * disable interrupts. We have to clear the
2190 * irq_enabled flag first to ensure that interrupts
2191 * won't be re-enabled.
2192 */
2193 mdp->irq_enabled = false;
2194 synchronize_irq(ndev->irq);
2195 napi_synchronize(&mdp->napi);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002196 sh_eth_write(ndev, 0x0000, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00002197
Ben Hutchings740c7f32015-01-27 00:49:32 +00002198 sh_eth_dev_exit(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002199
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03002200 /* Free all the skbuffs in the Rx queue and the DMA buffers. */
Ben Hutchings084236d2015-01-22 12:41:34 +00002201 sh_eth_ring_free(ndev);
Ben Hutchings084236d2015-01-22 12:41:34 +00002202 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002203
2204 /* Set new parameters */
2205 mdp->num_rx_ring = ring->rx_pending;
2206 mdp->num_tx_ring = ring->tx_pending;
2207
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002208 if (netif_running(ndev)) {
Ben Hutchings084236d2015-01-22 12:41:34 +00002209 ret = sh_eth_ring_init(ndev);
2210 if (ret < 0) {
2211 netdev_err(ndev, "%s: sh_eth_ring_init failed.\n",
2212 __func__);
2213 return ret;
2214 }
2215 ret = sh_eth_dev_init(ndev, false);
2216 if (ret < 0) {
2217 netdev_err(ndev, "%s: sh_eth_dev_init failed.\n",
2218 __func__);
2219 return ret;
2220 }
2221
Ben Hutchings283e38d2015-01-22 12:44:08 +00002222 mdp->irq_enabled = true;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002223 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
2224 /* Setting the Rx mode will start the Rx process. */
2225 sh_eth_write(ndev, EDRRR_R, EDRRR);
Ben Hutchingsbd888912015-01-22 12:40:25 +00002226 netif_device_attach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002227 }
2228
2229 return 0;
2230}
2231
stephen hemminger9b07be42012-01-04 12:59:49 +00002232static const struct ethtool_ops sh_eth_ethtool_ops = {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002233 .get_settings = sh_eth_get_settings,
2234 .set_settings = sh_eth_set_settings,
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002235 .get_regs_len = sh_eth_get_regs_len,
2236 .get_regs = sh_eth_get_regs,
stephen hemminger9b07be42012-01-04 12:59:49 +00002237 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002238 .get_msglevel = sh_eth_get_msglevel,
2239 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00002240 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002241 .get_strings = sh_eth_get_strings,
2242 .get_ethtool_stats = sh_eth_get_ethtool_stats,
2243 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002244 .get_ringparam = sh_eth_get_ringparam,
2245 .set_ringparam = sh_eth_set_ringparam,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002246};
2247
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002248/* network device open function */
2249static int sh_eth_open(struct net_device *ndev)
2250{
2251 int ret = 0;
2252 struct sh_eth_private *mdp = netdev_priv(ndev);
2253
Magnus Dammbcd51492009-10-09 00:20:04 +00002254 pm_runtime_get_sync(&mdp->pdev->dev);
2255
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002256 napi_enable(&mdp->napi);
2257
Joe Perchesa0607fd2009-11-18 23:29:17 -08002258 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00002259 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002260 if (ret) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002261 netdev_err(ndev, "Can not assign IRQ number\n");
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002262 goto out_napi_off;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002263 }
2264
2265 /* Descriptor set */
2266 ret = sh_eth_ring_init(ndev);
2267 if (ret)
2268 goto out_free_irq;
2269
2270 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002271 ret = sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002272 if (ret)
2273 goto out_free_irq;
2274
2275 /* PHY control start*/
2276 ret = sh_eth_phy_start(ndev);
2277 if (ret)
2278 goto out_free_irq;
2279
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002280 mdp->is_opened = 1;
2281
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002282 return ret;
2283
2284out_free_irq:
2285 free_irq(ndev->irq, ndev);
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002286out_napi_off:
2287 napi_disable(&mdp->napi);
Magnus Dammbcd51492009-10-09 00:20:04 +00002288 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002289 return ret;
2290}
2291
2292/* Timeout function */
2293static void sh_eth_tx_timeout(struct net_device *ndev)
2294{
2295 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002296 struct sh_eth_rxdesc *rxdesc;
2297 int i;
2298
2299 netif_stop_queue(ndev);
2300
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002301 netif_err(mdp, timer, ndev,
2302 "transmit timed out, status %8.8x, resetting...\n",
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01002303 sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002304
2305 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002306 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002307
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002308 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002309 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002310 rxdesc = &mdp->rx_ring[i];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002311 rxdesc->status = cpu_to_le32(0);
2312 rxdesc->addr = cpu_to_le32(0xBADF00D0);
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002313 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002314 mdp->rx_skbuff[i] = NULL;
2315 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002316 for (i = 0; i < mdp->num_tx_ring; i++) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002317 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002318 mdp->tx_skbuff[i] = NULL;
2319 }
2320
2321 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002322 sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002323}
2324
2325/* Packet transmit function */
2326static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
2327{
2328 struct sh_eth_private *mdp = netdev_priv(ndev);
2329 struct sh_eth_txdesc *txdesc;
Sergei Shtylyov12996532015-12-13 23:05:07 +03002330 dma_addr_t dma_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002331 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00002332 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002333
2334 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002335 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002336 if (!sh_eth_txfree(ndev)) {
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002337 netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002338 netif_stop_queue(ndev);
2339 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00002340 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002341 }
2342 }
2343 spin_unlock_irqrestore(&mdp->lock, flags);
2344
Ben Hutchingsdacc73e2015-03-03 00:53:08 +00002345 if (skb_put_padto(skb, ETH_ZLEN))
Ben Hutchingseebfb642015-01-22 12:40:13 +00002346 return NETDEV_TX_OK;
2347
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002348 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002349 mdp->tx_skbuff[entry] = skb;
2350 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002351 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002352 if (!mdp->cd->hw_swap)
Sergei Shtylyov3e230992015-12-13 21:27:04 +03002353 sh_eth_soft_swap(PTR_ALIGN(skb->data, 4), skb->len + 2);
Sergei Shtylyov12996532015-12-13 23:05:07 +03002354 dma_addr = dma_map_single(&ndev->dev, skb->data, skb->len,
2355 DMA_TO_DEVICE);
2356 if (dma_mapping_error(&ndev->dev, dma_addr)) {
Ben Hutchingsaa3933b2015-01-27 00:49:47 +00002357 kfree_skb(skb);
2358 return NETDEV_TX_OK;
2359 }
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002360 txdesc->addr = cpu_to_le32(dma_addr);
2361 txdesc->len = cpu_to_le32(skb->len << 16);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002362
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03002363 dma_wmb(); /* TACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002364 if (entry >= mdp->num_tx_ring - 1)
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002365 txdesc->status |= cpu_to_le32(TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002366 else
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002367 txdesc->status |= cpu_to_le32(TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002368
2369 mdp->cur_tx++;
2370
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002371 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
2372 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09002373
Patrick McHardy6ed10652009-06-23 06:03:08 +00002374 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002375}
2376
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002377/* The statistics registers have write-clear behaviour, which means we
2378 * will lose any increment between the read and write. We mitigate
2379 * this by only clearing when we read a non-zero value, so we will
2380 * never falsely report a total of zero.
2381 */
2382static void
2383sh_eth_update_stat(struct net_device *ndev, unsigned long *stat, int reg)
2384{
2385 u32 delta = sh_eth_read(ndev, reg);
2386
2387 if (delta) {
2388 *stat += delta;
2389 sh_eth_write(ndev, 0, reg);
2390 }
2391}
2392
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002393static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
2394{
2395 struct sh_eth_private *mdp = netdev_priv(ndev);
2396
2397 if (sh_eth_is_rz_fast_ether(mdp))
2398 return &ndev->stats;
2399
2400 if (!mdp->is_opened)
2401 return &ndev->stats;
2402
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002403 sh_eth_update_stat(ndev, &ndev->stats.tx_dropped, TROCR);
2404 sh_eth_update_stat(ndev, &ndev->stats.collisions, CDCR);
2405 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, LCCR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002406
2407 if (sh_eth_is_gether(mdp)) {
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002408 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2409 CERCR);
2410 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2411 CEECR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002412 } else {
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002413 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2414 CNDCR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002415 }
2416
2417 return &ndev->stats;
2418}
2419
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002420/* device close function */
2421static int sh_eth_close(struct net_device *ndev)
2422{
2423 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002424
2425 netif_stop_queue(ndev);
2426
Ben Hutchings283e38d2015-01-22 12:44:08 +00002427 /* Serialise with the interrupt handler and NAPI, then disable
2428 * interrupts. We have to clear the irq_enabled flag first to
2429 * ensure that interrupts won't be re-enabled.
2430 */
2431 mdp->irq_enabled = false;
2432 synchronize_irq(ndev->irq);
2433 napi_disable(&mdp->napi);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002434 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002435
Ben Hutchings740c7f32015-01-27 00:49:32 +00002436 sh_eth_dev_exit(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002437
2438 /* PHY Disconnect */
2439 if (mdp->phydev) {
2440 phy_stop(mdp->phydev);
2441 phy_disconnect(mdp->phydev);
Ben Hutchings4f9dce232015-01-16 17:51:25 +00002442 mdp->phydev = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002443 }
2444
2445 free_irq(ndev->irq, ndev);
2446
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03002447 /* Free all the skbuffs in the Rx queue and the DMA buffer. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002448 sh_eth_ring_free(ndev);
2449
Magnus Dammbcd51492009-10-09 00:20:04 +00002450 pm_runtime_put_sync(&mdp->pdev->dev);
2451
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002452 mdp->is_opened = 0;
2453
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002454 return 0;
2455}
2456
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002457/* ioctl to device function */
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002458static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002459{
2460 struct sh_eth_private *mdp = netdev_priv(ndev);
2461 struct phy_device *phydev = mdp->phydev;
2462
2463 if (!netif_running(ndev))
2464 return -EINVAL;
2465
2466 if (!phydev)
2467 return -ENODEV;
2468
Richard Cochran28b04112010-07-17 08:48:55 +00002469 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002470}
2471
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002472/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2473static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2474 int entry)
2475{
2476 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2477}
2478
2479static u32 sh_eth_tsu_get_post_mask(int entry)
2480{
2481 return 0x0f << (28 - ((entry % 8) * 4));
2482}
2483
2484static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2485{
2486 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2487}
2488
2489static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2490 int entry)
2491{
2492 struct sh_eth_private *mdp = netdev_priv(ndev);
2493 u32 tmp;
2494 void *reg_offset;
2495
2496 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2497 tmp = ioread32(reg_offset);
2498 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2499}
2500
2501static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2502 int entry)
2503{
2504 struct sh_eth_private *mdp = netdev_priv(ndev);
2505 u32 post_mask, ref_mask, tmp;
2506 void *reg_offset;
2507
2508 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2509 post_mask = sh_eth_tsu_get_post_mask(entry);
2510 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2511
2512 tmp = ioread32(reg_offset);
2513 iowrite32(tmp & ~post_mask, reg_offset);
2514
2515 /* If other port enables, the function returns "true" */
2516 return tmp & ref_mask;
2517}
2518
2519static int sh_eth_tsu_busy(struct net_device *ndev)
2520{
2521 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2522 struct sh_eth_private *mdp = netdev_priv(ndev);
2523
2524 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2525 udelay(10);
2526 timeout--;
2527 if (timeout <= 0) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002528 netdev_err(ndev, "%s: timeout\n", __func__);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002529 return -ETIMEDOUT;
2530 }
2531 }
2532
2533 return 0;
2534}
2535
2536static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2537 const u8 *addr)
2538{
2539 u32 val;
2540
2541 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2542 iowrite32(val, reg);
2543 if (sh_eth_tsu_busy(ndev) < 0)
2544 return -EBUSY;
2545
2546 val = addr[4] << 8 | addr[5];
2547 iowrite32(val, reg + 4);
2548 if (sh_eth_tsu_busy(ndev) < 0)
2549 return -EBUSY;
2550
2551 return 0;
2552}
2553
2554static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2555{
2556 u32 val;
2557
2558 val = ioread32(reg);
2559 addr[0] = (val >> 24) & 0xff;
2560 addr[1] = (val >> 16) & 0xff;
2561 addr[2] = (val >> 8) & 0xff;
2562 addr[3] = val & 0xff;
2563 val = ioread32(reg + 4);
2564 addr[4] = (val >> 8) & 0xff;
2565 addr[5] = val & 0xff;
2566}
2567
2568
2569static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2570{
2571 struct sh_eth_private *mdp = netdev_priv(ndev);
2572 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2573 int i;
2574 u8 c_addr[ETH_ALEN];
2575
2576 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2577 sh_eth_tsu_read_entry(reg_offset, c_addr);
dingtianhongc4bde292013-12-30 15:41:17 +08002578 if (ether_addr_equal(addr, c_addr))
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002579 return i;
2580 }
2581
2582 return -ENOENT;
2583}
2584
2585static int sh_eth_tsu_find_empty(struct net_device *ndev)
2586{
2587 u8 blank[ETH_ALEN];
2588 int entry;
2589
2590 memset(blank, 0, sizeof(blank));
2591 entry = sh_eth_tsu_find_entry(ndev, blank);
2592 return (entry < 0) ? -ENOMEM : entry;
2593}
2594
2595static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2596 int entry)
2597{
2598 struct sh_eth_private *mdp = netdev_priv(ndev);
2599 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2600 int ret;
2601 u8 blank[ETH_ALEN];
2602
2603 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2604 ~(1 << (31 - entry)), TSU_TEN);
2605
2606 memset(blank, 0, sizeof(blank));
2607 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2608 if (ret < 0)
2609 return ret;
2610 return 0;
2611}
2612
2613static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2614{
2615 struct sh_eth_private *mdp = netdev_priv(ndev);
2616 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2617 int i, ret;
2618
2619 if (!mdp->cd->tsu)
2620 return 0;
2621
2622 i = sh_eth_tsu_find_entry(ndev, addr);
2623 if (i < 0) {
2624 /* No entry found, create one */
2625 i = sh_eth_tsu_find_empty(ndev);
2626 if (i < 0)
2627 return -ENOMEM;
2628 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2629 if (ret < 0)
2630 return ret;
2631
2632 /* Enable the entry */
2633 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2634 (1 << (31 - i)), TSU_TEN);
2635 }
2636
2637 /* Entry found or created, enable POST */
2638 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2639
2640 return 0;
2641}
2642
2643static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2644{
2645 struct sh_eth_private *mdp = netdev_priv(ndev);
2646 int i, ret;
2647
2648 if (!mdp->cd->tsu)
2649 return 0;
2650
2651 i = sh_eth_tsu_find_entry(ndev, addr);
2652 if (i) {
2653 /* Entry found */
2654 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2655 goto done;
2656
2657 /* Disable the entry if both ports was disabled */
2658 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2659 if (ret < 0)
2660 return ret;
2661 }
2662done:
2663 return 0;
2664}
2665
2666static int sh_eth_tsu_purge_all(struct net_device *ndev)
2667{
2668 struct sh_eth_private *mdp = netdev_priv(ndev);
2669 int i, ret;
2670
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002671 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002672 return 0;
2673
2674 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2675 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2676 continue;
2677
2678 /* Disable the entry if both ports was disabled */
2679 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2680 if (ret < 0)
2681 return ret;
2682 }
2683
2684 return 0;
2685}
2686
2687static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2688{
2689 struct sh_eth_private *mdp = netdev_priv(ndev);
2690 u8 addr[ETH_ALEN];
2691 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2692 int i;
2693
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002694 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002695 return;
2696
2697 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2698 sh_eth_tsu_read_entry(reg_offset, addr);
2699 if (is_multicast_ether_addr(addr))
2700 sh_eth_tsu_del_entry(ndev, addr);
2701 }
2702}
2703
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002704/* Update promiscuous flag and multicast filter */
2705static void sh_eth_set_rx_mode(struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002706{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002707 struct sh_eth_private *mdp = netdev_priv(ndev);
2708 u32 ecmr_bits;
2709 int mcast_all = 0;
2710 unsigned long flags;
2711
2712 spin_lock_irqsave(&mdp->lock, flags);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002713 /* Initial condition is MCT = 1, PRM = 0.
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002714 * Depending on ndev->flags, set PRM or clear MCT
2715 */
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002716 ecmr_bits = sh_eth_read(ndev, ECMR) & ~ECMR_PRM;
2717 if (mdp->cd->tsu)
2718 ecmr_bits |= ECMR_MCT;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002719
2720 if (!(ndev->flags & IFF_MULTICAST)) {
2721 sh_eth_tsu_purge_mcast(ndev);
2722 mcast_all = 1;
2723 }
2724 if (ndev->flags & IFF_ALLMULTI) {
2725 sh_eth_tsu_purge_mcast(ndev);
2726 ecmr_bits &= ~ECMR_MCT;
2727 mcast_all = 1;
2728 }
2729
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002730 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002731 sh_eth_tsu_purge_all(ndev);
2732 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2733 } else if (mdp->cd->tsu) {
2734 struct netdev_hw_addr *ha;
2735 netdev_for_each_mc_addr(ha, ndev) {
2736 if (mcast_all && is_multicast_ether_addr(ha->addr))
2737 continue;
2738
2739 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2740 if (!mcast_all) {
2741 sh_eth_tsu_purge_mcast(ndev);
2742 ecmr_bits &= ~ECMR_MCT;
2743 mcast_all = 1;
2744 }
2745 }
2746 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002747 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002748
2749 /* update the ethernet mode */
2750 sh_eth_write(ndev, ecmr_bits, ECMR);
2751
2752 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002753}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002754
2755static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2756{
2757 if (!mdp->port)
2758 return TSU_VTAG0;
2759 else
2760 return TSU_VTAG1;
2761}
2762
Patrick McHardy80d5c362013-04-19 02:04:28 +00002763static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2764 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002765{
2766 struct sh_eth_private *mdp = netdev_priv(ndev);
2767 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2768
2769 if (unlikely(!mdp->cd->tsu))
2770 return -EPERM;
2771
2772 /* No filtering if vid = 0 */
2773 if (!vid)
2774 return 0;
2775
2776 mdp->vlan_num_ids++;
2777
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002778 /* The controller has one VLAN tag HW filter. So, if the filter is
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002779 * already enabled, the driver disables it and the filte
2780 */
2781 if (mdp->vlan_num_ids > 1) {
2782 /* disable VLAN filter */
2783 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2784 return 0;
2785 }
2786
2787 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2788 vtag_reg_index);
2789
2790 return 0;
2791}
2792
Patrick McHardy80d5c362013-04-19 02:04:28 +00002793static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2794 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002795{
2796 struct sh_eth_private *mdp = netdev_priv(ndev);
2797 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2798
2799 if (unlikely(!mdp->cd->tsu))
2800 return -EPERM;
2801
2802 /* No filtering if vid = 0 */
2803 if (!vid)
2804 return 0;
2805
2806 mdp->vlan_num_ids--;
2807 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2808
2809 return 0;
2810}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002811
2812/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002813static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002814{
Simon Hormandb893472014-01-17 09:22:28 +09002815 if (sh_eth_is_rz_fast_ether(mdp)) {
2816 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2817 return;
2818 }
2819
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002820 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2821 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2822 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2823 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2824 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2825 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2826 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2827 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2828 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2829 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002830 if (sh_eth_is_gether(mdp)) {
2831 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2832 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2833 } else {
2834 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2835 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2836 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002837 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2838 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2839 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2840 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2841 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2842 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2843 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002844}
2845
2846/* MDIO bus release function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002847static int sh_mdio_release(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002848{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002849 /* unregister mdio bus */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002850 mdiobus_unregister(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002851
2852 /* free bitbang info */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002853 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002854
2855 return 0;
2856}
2857
2858/* MDIO bus init function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002859static int sh_mdio_init(struct sh_eth_private *mdp,
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002860 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002861{
2862 int ret, i;
2863 struct bb_info *bitbang;
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002864 struct platform_device *pdev = mdp->pdev;
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002865 struct device *dev = &mdp->pdev->dev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002866
2867 /* create bit control struct for PHY */
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002868 bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002869 if (!bitbang)
2870 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002871
2872 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002873 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002874 bitbang->set_gate = pd->set_mdio_gate;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002875 bitbang->ctrl.ops = &bb_ops;
2876
Stefan Weilc2e07b32010-08-03 19:44:52 +02002877 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002878 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002879 if (!mdp->mii_bus)
2880 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002881
2882 /* Hook up MII support for ethtool */
2883 mdp->mii_bus->name = "sh_mii";
Laurent Pincharta5bd60602014-03-20 15:00:32 +01002884 mdp->mii_bus->parent = dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00002885 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002886 pdev->name, pdev->id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002887
2888 /* PHY IRQ */
Sergei Shtylyov86b5d252014-05-13 02:30:14 +04002889 mdp->mii_bus->irq = devm_kmalloc_array(dev, PHY_MAX_ADDR, sizeof(int),
2890 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002891 if (!mdp->mii_bus->irq) {
2892 ret = -ENOMEM;
2893 goto out_free_bus;
2894 }
2895
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002896 /* register MDIO bus */
2897 if (dev->of_node) {
2898 ret = of_mdiobus_register(mdp->mii_bus, dev->of_node);
Ben Dooks702eca02014-03-12 17:47:40 +00002899 } else {
2900 for (i = 0; i < PHY_MAX_ADDR; i++)
2901 mdp->mii_bus->irq[i] = PHY_POLL;
2902 if (pd->phy_irq > 0)
2903 mdp->mii_bus->irq[pd->phy] = pd->phy_irq;
2904
2905 ret = mdiobus_register(mdp->mii_bus);
2906 }
2907
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002908 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002909 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002910
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002911 return 0;
2912
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002913out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002914 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002915 return ret;
2916}
2917
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002918static const u16 *sh_eth_get_register_offset(int register_type)
2919{
2920 const u16 *reg_offset = NULL;
2921
2922 switch (register_type) {
2923 case SH_ETH_REG_GIGABIT:
2924 reg_offset = sh_eth_offset_gigabit;
2925 break;
Simon Hormandb893472014-01-17 09:22:28 +09002926 case SH_ETH_REG_FAST_RZ:
2927 reg_offset = sh_eth_offset_fast_rz;
2928 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00002929 case SH_ETH_REG_FAST_RCAR:
2930 reg_offset = sh_eth_offset_fast_rcar;
2931 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002932 case SH_ETH_REG_FAST_SH4:
2933 reg_offset = sh_eth_offset_fast_sh4;
2934 break;
2935 case SH_ETH_REG_FAST_SH3_SH2:
2936 reg_offset = sh_eth_offset_fast_sh3_sh2;
2937 break;
2938 default:
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002939 break;
2940 }
2941
2942 return reg_offset;
2943}
2944
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002945static const struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002946 .ndo_open = sh_eth_open,
2947 .ndo_stop = sh_eth_close,
2948 .ndo_start_xmit = sh_eth_start_xmit,
2949 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002950 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002951 .ndo_tx_timeout = sh_eth_tx_timeout,
2952 .ndo_do_ioctl = sh_eth_do_ioctl,
2953 .ndo_validate_addr = eth_validate_addr,
2954 .ndo_set_mac_address = eth_mac_addr,
2955 .ndo_change_mtu = eth_change_mtu,
2956};
2957
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002958static const struct net_device_ops sh_eth_netdev_ops_tsu = {
2959 .ndo_open = sh_eth_open,
2960 .ndo_stop = sh_eth_close,
2961 .ndo_start_xmit = sh_eth_start_xmit,
2962 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002963 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002964 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
2965 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
2966 .ndo_tx_timeout = sh_eth_tx_timeout,
2967 .ndo_do_ioctl = sh_eth_do_ioctl,
2968 .ndo_validate_addr = eth_validate_addr,
2969 .ndo_set_mac_address = eth_mac_addr,
2970 .ndo_change_mtu = eth_change_mtu,
2971};
2972
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002973#ifdef CONFIG_OF
2974static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
2975{
2976 struct device_node *np = dev->of_node;
2977 struct sh_eth_plat_data *pdata;
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002978 const char *mac_addr;
2979
2980 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2981 if (!pdata)
2982 return NULL;
2983
2984 pdata->phy_interface = of_get_phy_mode(np);
2985
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002986 mac_addr = of_get_mac_address(np);
2987 if (mac_addr)
2988 memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
2989
2990 pdata->no_ether_link =
2991 of_property_read_bool(np, "renesas,no-ether-link");
2992 pdata->ether_link_active_low =
2993 of_property_read_bool(np, "renesas,ether-link-active-low");
2994
2995 return pdata;
2996}
2997
2998static const struct of_device_id sh_eth_match_table[] = {
2999 { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data },
3000 { .compatible = "renesas,ether-r8a7778", .data = &r8a777x_data },
3001 { .compatible = "renesas,ether-r8a7779", .data = &r8a777x_data },
3002 { .compatible = "renesas,ether-r8a7790", .data = &r8a779x_data },
3003 { .compatible = "renesas,ether-r8a7791", .data = &r8a779x_data },
Hisashi Nakamura9488e1e2014-11-13 15:59:07 +09003004 { .compatible = "renesas,ether-r8a7793", .data = &r8a779x_data },
Hisashi Nakamura0f76b9d2014-08-01 17:03:00 +02003005 { .compatible = "renesas,ether-r8a7794", .data = &r8a779x_data },
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003006 { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data },
3007 { }
3008};
3009MODULE_DEVICE_TABLE(of, sh_eth_match_table);
3010#else
3011static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
3012{
3013 return NULL;
3014}
3015#endif
3016
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003017static int sh_eth_drv_probe(struct platform_device *pdev)
3018{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07003019 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003020 struct resource *res;
3021 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00003022 struct sh_eth_private *mdp = NULL;
Jingoo Han0b76b862013-08-30 14:00:11 +09003023 struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev);
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003024 const struct platform_device_id *id = platform_get_device_id(pdev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003025
3026 /* get base addr */
3027 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003028
3029 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
Laurent Pinchartf738a132014-03-20 15:00:35 +01003030 if (!ndev)
3031 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003032
Ben Dooksb5893a02014-03-21 12:09:14 +01003033 pm_runtime_enable(&pdev->dev);
3034 pm_runtime_get_sync(&pdev->dev);
3035
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003036 devno = pdev->id;
3037 if (devno < 0)
3038 devno = 0;
3039
3040 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02003041 ret = platform_get_irq(pdev, 0);
Sergei Shtylyov7a468ac2015-08-28 16:56:01 +03003042 if (ret < 0)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003043 goto out_release;
roel kluincc3c0802008-09-10 19:22:44 +02003044 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003045
3046 SET_NETDEV_DEV(ndev, &pdev->dev);
3047
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003048 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00003049 mdp->num_tx_ring = TX_RING_SIZE;
3050 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00003051 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
3052 if (IS_ERR(mdp->addr)) {
3053 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00003054 goto out_release;
3055 }
3056
Varka Bhadramc9608042014-10-24 07:42:09 +05303057 ndev->base_addr = res->start;
3058
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003059 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00003060 mdp->pdev = pdev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003061
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003062 if (pdev->dev.of_node)
3063 pd = sh_eth_parse_dt(&pdev->dev);
Sergei Shtylyov3b4c5cb2013-10-30 23:30:19 +03003064 if (!pd) {
3065 dev_err(&pdev->dev, "no platform data\n");
3066 ret = -EINVAL;
3067 goto out_release;
3068 }
3069
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003070 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04003071 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00003072 mdp->phy_interface = pd->phy_interface;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00003073 mdp->no_ether_link = pd->no_ether_link;
3074 mdp->ether_link_active_low = pd->ether_link_active_low;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003075
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003076 /* set cpu data */
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003077 if (id) {
3078 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
3079 } else {
3080 const struct of_device_id *match;
3081
3082 match = of_match_device(of_match_ptr(sh_eth_match_table),
3083 &pdev->dev);
3084 mdp->cd = (struct sh_eth_cpu_data *)match->data;
3085 }
Sergei Shtylyova3153d82013-08-18 03:11:28 +04003086 mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type);
Sergei Shtylyov264be2f2014-03-15 03:11:24 +03003087 if (!mdp->reg_offset) {
3088 dev_err(&pdev->dev, "Unknown register type (%d)\n",
3089 mdp->cd->register_type);
3090 ret = -EINVAL;
3091 goto out_release;
3092 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003093 sh_eth_set_default_cpu_data(mdp->cd);
3094
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003095 /* set function */
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003096 if (mdp->cd->tsu)
3097 ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
3098 else
3099 ndev->netdev_ops = &sh_eth_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003100 ndev->ethtool_ops = &sh_eth_ethtool_ops;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003101 ndev->watchdog_timeo = TX_TIMEOUT;
3102
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00003103 /* debug message level */
3104 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003105
3106 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00003107 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00003108 if (!is_valid_ether_addr(ndev->dev_addr)) {
3109 dev_warn(&pdev->dev,
3110 "no valid MAC address supplied, using a random one.\n");
3111 eth_hw_addr_random(ndev);
3112 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003113
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003114 /* ioremap the TSU registers */
3115 if (mdp->cd->tsu) {
3116 struct resource *rtsu;
3117 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00003118 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
3119 if (IS_ERR(mdp->tsu_addr)) {
3120 ret = PTR_ERR(mdp->tsu_addr);
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00003121 goto out_release;
3122 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00003123 mdp->port = devno % 2;
Patrick McHardyf6469682013-04-19 02:04:27 +00003124 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003125 }
3126
Yoshihiro Shimoda150647f2012-02-15 17:54:56 +00003127 /* initialize first or needed device */
3128 if (!devno || pd->needs_init) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003129 if (mdp->cd->chip_reset)
3130 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003131
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00003132 if (mdp->cd->tsu) {
3133 /* TSU init (Init only)*/
3134 sh_eth_tsu_init(mdp);
3135 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003136 }
3137
Hisashi Nakamura966d6db2014-11-13 15:54:05 +09003138 if (mdp->cd->rmiimode)
3139 sh_eth_write(ndev, 0x1, RMIIMODE);
3140
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003141 /* MDIO bus init */
3142 ret = sh_mdio_init(mdp, pd);
3143 if (ret) {
3144 dev_err(&ndev->dev, "failed to initialise MDIO\n");
3145 goto out_release;
3146 }
3147
Sergei Shtylyov37191092013-06-19 23:30:23 +04003148 netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
3149
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003150 /* network device register */
3151 ret = register_netdev(ndev);
3152 if (ret)
Sergei Shtylyov37191092013-06-19 23:30:23 +04003153 goto out_napi_del;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003154
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003155 /* print device information */
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +03003156 netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n",
3157 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003158
Ben Dooksb5893a02014-03-21 12:09:14 +01003159 pm_runtime_put(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003160 platform_set_drvdata(pdev, ndev);
3161
3162 return ret;
3163
Sergei Shtylyov37191092013-06-19 23:30:23 +04003164out_napi_del:
3165 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003166 sh_mdio_release(mdp);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003167
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003168out_release:
3169 /* net_dev free */
3170 if (ndev)
3171 free_netdev(ndev);
3172
Ben Dooksb5893a02014-03-21 12:09:14 +01003173 pm_runtime_put(&pdev->dev);
3174 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003175 return ret;
3176}
3177
3178static int sh_eth_drv_remove(struct platform_device *pdev)
3179{
3180 struct net_device *ndev = platform_get_drvdata(pdev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003181 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003182
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003183 unregister_netdev(ndev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003184 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003185 sh_mdio_release(mdp);
Magnus Dammbcd51492009-10-09 00:20:04 +00003186 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003187 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003188
3189 return 0;
3190}
3191
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003192#ifdef CONFIG_PM
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003193#ifdef CONFIG_PM_SLEEP
3194static int sh_eth_suspend(struct device *dev)
3195{
3196 struct net_device *ndev = dev_get_drvdata(dev);
3197 int ret = 0;
3198
3199 if (netif_running(ndev)) {
3200 netif_device_detach(ndev);
3201 ret = sh_eth_close(ndev);
3202 }
3203
3204 return ret;
3205}
3206
3207static int sh_eth_resume(struct device *dev)
3208{
3209 struct net_device *ndev = dev_get_drvdata(dev);
3210 int ret = 0;
3211
3212 if (netif_running(ndev)) {
3213 ret = sh_eth_open(ndev);
3214 if (ret < 0)
3215 return ret;
3216 netif_device_attach(ndev);
3217 }
3218
3219 return ret;
3220}
3221#endif
3222
Magnus Dammbcd51492009-10-09 00:20:04 +00003223static int sh_eth_runtime_nop(struct device *dev)
3224{
Sergei Shtylyov128296f2014-01-03 15:52:22 +03003225 /* Runtime PM callback shared between ->runtime_suspend()
Magnus Dammbcd51492009-10-09 00:20:04 +00003226 * and ->runtime_resume(). Simply returns success.
3227 *
3228 * This driver re-initializes all registers after
3229 * pm_runtime_get_sync() anyway so there is no need
3230 * to save and restore registers here.
3231 */
3232 return 0;
3233}
3234
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003235static const struct dev_pm_ops sh_eth_dev_pm_ops = {
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003236 SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend, sh_eth_resume)
Mikhail Ulyanove7d7e892015-01-22 01:18:44 +03003237 SET_RUNTIME_PM_OPS(sh_eth_runtime_nop, sh_eth_runtime_nop, NULL)
Magnus Dammbcd51492009-10-09 00:20:04 +00003238};
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003239#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3240#else
3241#define SH_ETH_PM_OPS NULL
3242#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00003243
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003244static struct platform_device_id sh_eth_id_table[] = {
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00003245 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00003246 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +00003247 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003248 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
Sergei Shtylyov24549e22013-06-07 13:59:21 +00003249 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
3250 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003251 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003252 { }
3253};
3254MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
3255
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003256static struct platform_driver sh_eth_driver = {
3257 .probe = sh_eth_drv_probe,
3258 .remove = sh_eth_drv_remove,
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003259 .id_table = sh_eth_id_table,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003260 .driver = {
3261 .name = CARDNAME,
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003262 .pm = SH_ETH_PM_OPS,
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003263 .of_match_table = of_match_ptr(sh_eth_match_table),
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003264 },
3265};
3266
Axel Lindb62f682011-11-27 16:44:17 +00003267module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003268
3269MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3270MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3271MODULE_LICENSE("GPL v2");