]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/net/ethernet/renesas/sh_eth.c
sh_eth: remove EDMAC_BIG_ENDIAN
[mirror_ubuntu-zesty-kernel.git] / drivers / net / ethernet / renesas / sh_eth.c
CommitLineData
128296fc 1/* SuperH Ethernet device driver
86a74ff2 2 *
966d6dbb 3 * Copyright (C) 2014 Renesas Electronics Corporation
f0e81fec 4 * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
b356e978
SS
5 * Copyright (C) 2008-2014 Renesas Solutions Corp.
6 * Copyright (C) 2013-2014 Cogent Embedded, Inc.
702eca02 7 * Copyright (C) 2014 Codethink Limited
86a74ff2
NI
8 *
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.
86a74ff2
NI
17 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 */
21
0654011d
YS
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/spinlock.h>
6a27cded 25#include <linux/interrupt.h>
86a74ff2
NI
26#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>
b356e978
SS
32#include <linux/of.h>
33#include <linux/of_device.h>
34#include <linux/of_irq.h>
35#include <linux/of_net.h>
86a74ff2
NI
36#include <linux/phy.h>
37#include <linux/cache.h>
38#include <linux/io.h>
bcd5149d 39#include <linux/pm_runtime.h>
5a0e3ad6 40#include <linux/slab.h>
dc19e4e5 41#include <linux/ethtool.h>
fdb37a7f 42#include <linux/if_vlan.h>
f0e81fec 43#include <linux/clk.h>
d4fa0e35 44#include <linux/sh_eth.h>
702eca02 45#include <linux/of_mdio.h>
86a74ff2
NI
46
47#include "sh_eth.h"
48
dc19e4e5
NI
49#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
2274d375
SS
55#define SH_ETH_OFFSET_INVALID ((u16)~0)
56
3365711d
BH
57#define SH_ETH_OFFSET_DEFAULTS \
58 [0 ... SH_ETH_MAX_REGISTER_OFFSET - 1] = SH_ETH_OFFSET_INVALID
59
c0013f6f 60static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
3365711d
BH
61 SH_ETH_OFFSET_DEFAULTS,
62
c0013f6f
SS
63 [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,
c0013f6f
SS
142
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
db893473 157static const u16 sh_eth_offset_fast_rz[SH_ETH_MAX_REGISTER_OFFSET] = {
3365711d
BH
158 SH_ETH_OFFSET_DEFAULTS,
159
db893473
SH
160 [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,
db893473
SH
208
209 [TXNLCR0] = 0x0080,
210 [TXALCR0] = 0x0084,
211 [RXNLCR0] = 0x0088,
212 [RXALCR0] = 0x008C,
213};
214
a3f109bd 215static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
3365711d
BH
216 SH_ETH_OFFSET_DEFAULTS,
217
a3f109bd
SS
218 [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,
55754f19 258 [RMIIMODE] = 0x026c,
a3f109bd
SS
259 [FCFTR] = 0x0270,
260 [TRIMD] = 0x027c,
261};
262
c0013f6f 263static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
3365711d
BH
264 SH_ETH_OFFSET_DEFAULTS,
265
c0013f6f
SS
266 [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] = {
3365711d
BH
318 SH_ETH_OFFSET_DEFAULTS,
319
d8b0426a
SS
320 [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
c0013f6f
SS
341 [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,
c0013f6f
SS
404};
405
740c7f31
BH
406static void sh_eth_rcv_snd_disable(struct net_device *ndev);
407static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev);
408
2274d375
SS
409static 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
504c8ca5 431static bool sh_eth_is_gether(struct sh_eth_private *mdp)
dabdde9e 432{
504c8ca5 433 return mdp->reg_offset == sh_eth_offset_gigabit;
dabdde9e
NI
434}
435
db893473
SH
436static 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
8e994402 441static void sh_eth_select_mii(struct net_device *ndev)
5e7a76be
NI
442{
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:
f75f14ec
SS
457 netdev_warn(ndev,
458 "PHY interface mode was not setup. Set to MII.\n");
5e7a76be
NI
459 value = 0x1;
460 break;
461 }
462
463 sh_eth_write(ndev, value, RMII_MII);
464}
5e7a76be 465
8e994402 466static void sh_eth_set_duplex(struct net_device *ndev)
65ac8851
YS
467{
468 struct sh_eth_private *mdp = netdev_priv(ndev);
65ac8851
YS
469
470 if (mdp->duplex) /* Full */
4a55530f 471 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
65ac8851 472 else /* Half */
4a55530f 473 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
65ac8851
YS
474}
475
99f84be6
GU
476static 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
a0f48be3
GU
485static 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
99f84be6
GU
504#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};
a0f48be3
GU
535
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};
99f84be6 578
04b0ed2a 579/* There is CPU dependent code */
589ebdef 580static void sh_eth_set_rate_r8a777x(struct net_device *ndev)
65ac8851
YS
581{
582 struct sh_eth_private *mdp = netdev_priv(ndev);
d0418bb7 583
a3f109bd
SS
584 switch (mdp->speed) {
585 case 10: /* 10BASE */
586 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
587 break;
588 case 100:/* 100BASE */
589 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
590 break;
591 default:
592 break;
593 }
594}
595
674853b2 596/* R8A7778/9 */
589ebdef 597static struct sh_eth_cpu_data r8a777x_data = {
a3f109bd 598 .set_duplex = sh_eth_set_duplex,
589ebdef 599 .set_rate = sh_eth_set_rate_r8a777x,
a3f109bd 600
a3153d8c
SS
601 .register_type = SH_ETH_REG_FAST_RCAR,
602
a3f109bd
SS
603 .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,
ca8c3585
SS
608 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
609 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
610 EESR_ECI,
d407bc02 611 .fdr_value = 0x00000f0f,
a3f109bd
SS
612
613 .apr = 1,
614 .mpr = 1,
615 .tpauser = 1,
616 .hw_swap = 1,
617};
a3f109bd 618
94a12b15
SS
619/* R8A7790/1 */
620static struct sh_eth_cpu_data r8a779x_data = {
e18dbf7e
SH
621 .set_duplex = sh_eth_set_duplex,
622 .set_rate = sh_eth_set_rate_r8a777x,
623
a3153d8c
SS
624 .register_type = SH_ETH_REG_FAST_RCAR,
625
e18dbf7e
SH
626 .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,
ba361cb3
LP
631 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
632 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
633 EESR_ECI,
d407bc02 634 .fdr_value = 0x00000f0f,
e18dbf7e 635
01fbd3f5
GU
636 .trscer_err_mask = DESC_I_RINT8,
637
e18dbf7e
SH
638 .apr = 1,
639 .mpr = 1,
640 .tpauser = 1,
641 .hw_swap = 1,
642 .rmiimode = 1,
643};
c74a2248 644#endif /* CONFIG_OF */
e18dbf7e 645
9c3beaab 646static void sh_eth_set_rate_sh7724(struct net_device *ndev)
a3f109bd
SS
647{
648 struct sh_eth_private *mdp = netdev_priv(ndev);
65ac8851
YS
649
650 switch (mdp->speed) {
651 case 10: /* 10BASE */
a3f109bd 652 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
65ac8851
YS
653 break;
654 case 100:/* 100BASE */
a3f109bd 655 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
65ac8851
YS
656 break;
657 default:
658 break;
659 }
660}
661
662/* SH7724 */
9c3beaab 663static struct sh_eth_cpu_data sh7724_data = {
65ac8851 664 .set_duplex = sh_eth_set_duplex,
9c3beaab 665 .set_rate = sh_eth_set_rate_sh7724,
65ac8851 666
a3153d8c
SS
667 .register_type = SH_ETH_REG_FAST_SH4,
668
65ac8851
YS
669 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
670 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
a80c3de7 671 .eesipr_value = 0x01ff009f,
65ac8851
YS
672
673 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
ca8c3585
SS
674 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
675 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
676 EESR_ECI,
65ac8851
YS
677
678 .apr = 1,
679 .mpr = 1,
680 .tpauser = 1,
681 .hw_swap = 1,
503914cf
MD
682 .rpadir = 1,
683 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
65ac8851 684};
5cee1d37 685
24549e2a 686static void sh_eth_set_rate_sh7757(struct net_device *ndev)
f29a3d04
YS
687{
688 struct sh_eth_private *mdp = netdev_priv(ndev);
f29a3d04
YS
689
690 switch (mdp->speed) {
691 case 10: /* 10BASE */
4a55530f 692 sh_eth_write(ndev, 0, RTRATE);
f29a3d04
YS
693 break;
694 case 100:/* 100BASE */
4a55530f 695 sh_eth_write(ndev, 1, RTRATE);
f29a3d04
YS
696 break;
697 default:
698 break;
699 }
700}
701
702/* SH7757 */
24549e2a
SS
703static struct sh_eth_cpu_data sh7757_data = {
704 .set_duplex = sh_eth_set_duplex,
705 .set_rate = sh_eth_set_rate_sh7757,
f29a3d04 706
a3153d8c
SS
707 .register_type = SH_ETH_REG_FAST_SH4,
708
f29a3d04 709 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
f29a3d04
YS
710
711 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
ca8c3585
SS
712 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
713 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
714 EESR_ECI,
f29a3d04 715
5b3dfd13 716 .irq_flags = IRQF_SHARED,
f29a3d04
YS
717 .apr = 1,
718 .mpr = 1,
719 .tpauser = 1,
720 .hw_swap = 1,
721 .no_ade = 1,
2e98e797
YS
722 .rpadir = 1,
723 .rpadir_value = 2 << 16,
6b4b4fea 724 .rtrate = 1,
f29a3d04 725};
65ac8851 726
e403d295 727#define SH_GIGA_ETH_BASE 0xfee00000UL
8fcd4961
YS
728#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;
0799c2d6 733 u32 mahr[2], malr[2];
8fcd4961
YS
734
735 /* save MAHR and MALR */
736 for (i = 0; i < 2; i++) {
ae70644d
YS
737 malr[i] = ioread32((void *)GIGA_MALR(i));
738 mahr[i] = ioread32((void *)GIGA_MAHR(i));
8fcd4961
YS
739 }
740
741 /* reset device */
ae70644d 742 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
8fcd4961
YS
743 mdelay(1);
744
745 /* restore MAHR and MALR */
746 for (i = 0; i < 2; i++) {
ae70644d
YS
747 iowrite32(malr[i], (void *)GIGA_MALR(i));
748 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
8fcd4961
YS
749 }
750}
751
8fcd4961
YS
752static 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) */
24549e2a 772static struct sh_eth_cpu_data sh7757_data_giga = {
8fcd4961 773 .chip_reset = sh_eth_chip_reset_giga,
04b0ed2a 774 .set_duplex = sh_eth_set_duplex,
8fcd4961
YS
775 .set_rate = sh_eth_set_rate_giga,
776
a3153d8c
SS
777 .register_type = SH_ETH_REG_GIGABIT,
778
8fcd4961
YS
779 .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,
ca8c3585
SS
784 .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,
8fcd4961 787 .fdr_value = 0x0000072f,
8fcd4961 788
5b3dfd13 789 .irq_flags = IRQF_SHARED,
8fcd4961
YS
790 .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,
3acbc971 799 .tsu = 1,
8fcd4961
YS
800};
801
f5d12767
SS
802/* SH7734 */
803static struct sh_eth_cpu_data sh7734_data = {
380af9e3
YS
804 .chip_reset = sh_eth_chip_reset,
805 .set_duplex = sh_eth_set_duplex,
f5d12767
SS
806 .set_rate = sh_eth_set_rate_gether,
807
a3153d8c
SS
808 .register_type = SH_ETH_REG_GIGABIT,
809
f5d12767
SS
810 .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,
ca8c3585
SS
815 .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,
f5d12767
SS
818
819 .apr = 1,
820 .mpr = 1,
821 .tpauser = 1,
822 .bculr = 1,
823 .hw_swap = 1,
824 .no_trimd = 1,
825 .no_ade = 1,
826 .tsu = 1,
827 .hw_crc = 1,
828 .select_mii = 1,
829};
830
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,
380af9e3 836
a3153d8c
SS
837 .register_type = SH_ETH_REG_GIGABIT,
838
380af9e3
YS
839 .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,
128296fc
SS
844 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
845 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
380af9e3 846 EESR_ECI,
380af9e3
YS
847
848 .apr = 1,
849 .mpr = 1,
850 .tpauser = 1,
851 .bculr = 1,
852 .hw_swap = 1,
380af9e3
YS
853 .no_trimd = 1,
854 .no_ade = 1,
4986b996 855 .tsu = 1,
5b3dfd13 856 .irq_flags = IRQF_SHARED,
380af9e3
YS
857};
858
c18a79ab 859static struct sh_eth_cpu_data sh7619_data = {
a3153d8c
SS
860 .register_type = SH_ETH_REG_FAST_SH3_SH2,
861
380af9e3
YS
862 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
863
864 .apr = 1,
865 .mpr = 1,
866 .tpauser = 1,
867 .hw_swap = 1,
868};
7bbe150d
SS
869
870static struct sh_eth_cpu_data sh771x_data = {
a3153d8c
SS
871 .register_type = SH_ETH_REG_FAST_SH3_SH2,
872
380af9e3 873 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
4986b996 874 .tsu = 1,
380af9e3 875};
380af9e3
YS
876
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)
128296fc 886 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF |
380af9e3
YS
887 DEFAULT_FIFO_F_D_RFD;
888
889 if (!cd->fdr_value)
890 cd->fdr_value = DEFAULT_FDR_INIT;
891
380af9e3
YS
892 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;
b284fbe3
NI
897
898 if (!cd->trscer_err_mask)
899 cd->trscer_err_mask = DEFAULT_TRSCER_ERR_MASK;
380af9e3
YS
900}
901
5cee1d37
NI
902static 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 }
9f8c4265 913 if (cnt <= 0) {
f75f14ec 914 netdev_err(ndev, "Device reset failed\n");
5cee1d37
NI
915 ret = -ETIMEDOUT;
916 }
917 return ret;
380af9e3 918}
dabdde9e
NI
919
920static int sh_eth_reset(struct net_device *ndev)
921{
922 struct sh_eth_private *mdp = netdev_priv(ndev);
923 int ret = 0;
924
db893473 925 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp)) {
dabdde9e
NI
926 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)
f738a13d 932 return ret;
dabdde9e
NI
933
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
dabdde9e
NI
959 return ret;
960}
380af9e3 961
380af9e3
YS
962static void sh_eth_set_receive_align(struct sk_buff *skb)
963{
4d6a949c 964 uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1);
380af9e3 965
380af9e3 966 if (reserve)
4d6a949c 967 skb_reserve(skb, SH_ETH_RX_ALIGN - reserve);
380af9e3 968}
380af9e3
YS
969
970
71557a37
YS
971/* CPU <-> EDMAC endian convert */
972static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
973{
888cc8c2 974 return cpu_to_le32(x);
71557a37
YS
975}
976
977static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
978{
888cc8c2 979 return le32_to_cpu(x);
71557a37
YS
980}
981
128296fc 982/* Program the hardware MAC address from dev->dev_addr. */
86a74ff2
NI
983static void update_mac_address(struct net_device *ndev)
984{
4a55530f 985 sh_eth_write(ndev,
128296fc
SS
986 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
987 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
4a55530f 988 sh_eth_write(ndev,
128296fc 989 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
86a74ff2
NI
990}
991
128296fc 992/* Get MAC address from SuperH MAC address register
86a74ff2
NI
993 *
994 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
995 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
996 * When you want use this device, you must set MAC address in bootloader.
997 *
998 */
748031f9 999static void read_mac_address(struct net_device *ndev, unsigned char *mac)
86a74ff2 1000{
748031f9 1001 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
d458cdf7 1002 memcpy(ndev->dev_addr, mac, ETH_ALEN);
748031f9 1003 } else {
37742f02
SS
1004 u32 mahr = sh_eth_read(ndev, MAHR);
1005 u32 malr = sh_eth_read(ndev, MALR);
1006
1007 ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
1008 ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
1009 ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
1010 ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
1011 ndev->dev_addr[4] = (malr >> 8) & 0xFF;
1012 ndev->dev_addr[5] = (malr >> 0) & 0xFF;
748031f9 1013 }
86a74ff2
NI
1014}
1015
0799c2d6 1016static u32 sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
c5ed5368 1017{
db893473 1018 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp))
c5ed5368
YS
1019 return EDTRR_TRNS_GETHER;
1020 else
1021 return EDTRR_TRNS_ETHER;
1022}
1023
86a74ff2 1024struct bb_info {
ae70644d 1025 void (*set_gate)(void *addr);
86a74ff2 1026 struct mdiobb_ctrl ctrl;
ae70644d 1027 void *addr;
86a74ff2
NI
1028};
1029
39b4b06b 1030static void sh_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
86a74ff2
NI
1031{
1032 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
78fa3c5c 1033 u32 pir;
b3017e6a
YS
1034
1035 if (bitbang->set_gate)
1036 bitbang->set_gate(bitbang->addr);
1037
78fa3c5c 1038 pir = ioread32(bitbang->addr);
39b4b06b 1039 if (set)
78fa3c5c 1040 pir |= mask;
86a74ff2 1041 else
78fa3c5c
SS
1042 pir &= ~mask;
1043 iowrite32(pir, bitbang->addr);
39b4b06b
SS
1044}
1045
1046/* Data I/O pin control */
1047static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1048{
1049 sh_mdio_ctrl(ctrl, PIR_MMD, bit);
86a74ff2
NI
1050}
1051
1052/* Set bit data*/
1053static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
1054{
39b4b06b 1055 sh_mdio_ctrl(ctrl, PIR_MDO, bit);
86a74ff2
NI
1056}
1057
1058/* Get bit data*/
1059static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
1060{
1061 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
b3017e6a
YS
1062
1063 if (bitbang->set_gate)
1064 bitbang->set_gate(bitbang->addr);
1065
78fa3c5c 1066 return (ioread32(bitbang->addr) & PIR_MDI) != 0;
86a74ff2
NI
1067}
1068
1069/* MDC pin control */
1070static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1071{
39b4b06b 1072 sh_mdio_ctrl(ctrl, PIR_MDC, bit);
86a74ff2
NI
1073}
1074
1075/* mdio bus control struct */
1076static struct mdiobb_ops bb_ops = {
1077 .owner = THIS_MODULE,
1078 .set_mdc = sh_mdc_ctrl,
1079 .set_mdio_dir = sh_mmd_ctrl,
1080 .set_mdio_data = sh_set_mdio,
1081 .get_mdio_data = sh_get_mdio,
1082};
1083
86a74ff2
NI
1084/* free skb and descriptor buffer */
1085static void sh_eth_ring_free(struct net_device *ndev)
1086{
1087 struct sh_eth_private *mdp = netdev_priv(ndev);
8e03a5e7 1088 int ringsize, i;
86a74ff2
NI
1089
1090 /* Free Rx skb ringbuffer */
1091 if (mdp->rx_skbuff) {
179d80af
SS
1092 for (i = 0; i < mdp->num_rx_ring; i++)
1093 dev_kfree_skb(mdp->rx_skbuff[i]);
86a74ff2
NI
1094 }
1095 kfree(mdp->rx_skbuff);
91c77550 1096 mdp->rx_skbuff = NULL;
86a74ff2
NI
1097
1098 /* Free Tx skb ringbuffer */
1099 if (mdp->tx_skbuff) {
179d80af
SS
1100 for (i = 0; i < mdp->num_tx_ring; i++)
1101 dev_kfree_skb(mdp->tx_skbuff[i]);
86a74ff2
NI
1102 }
1103 kfree(mdp->tx_skbuff);
91c77550 1104 mdp->tx_skbuff = NULL;
8e03a5e7
SS
1105
1106 if (mdp->rx_ring) {
1107 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
1108 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1109 mdp->rx_desc_dma);
1110 mdp->rx_ring = NULL;
1111 }
1112
1113 if (mdp->tx_ring) {
1114 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
1115 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1116 mdp->tx_desc_dma);
1117 mdp->tx_ring = NULL;
1118 }
86a74ff2
NI
1119}
1120
1121/* format skb and descriptor buffer */
1122static void sh_eth_ring_format(struct net_device *ndev)
1123{
1124 struct sh_eth_private *mdp = netdev_priv(ndev);
1125 int i;
1126 struct sk_buff *skb;
1127 struct sh_eth_rxdesc *rxdesc = NULL;
1128 struct sh_eth_txdesc *txdesc = NULL;
525b8075
YS
1129 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
1130 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
cb368595 1131 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
52b9fa36 1132 dma_addr_t dma_addr;
5cbf20c7 1133 u32 buf_len;
86a74ff2 1134
128296fc
SS
1135 mdp->cur_rx = 0;
1136 mdp->cur_tx = 0;
1137 mdp->dirty_rx = 0;
1138 mdp->dirty_tx = 0;
86a74ff2
NI
1139
1140 memset(mdp->rx_ring, 0, rx_ringsize);
1141
1142 /* build Rx ring buffer */
525b8075 1143 for (i = 0; i < mdp->num_rx_ring; i++) {
86a74ff2
NI
1144 /* skb */
1145 mdp->rx_skbuff[i] = NULL;
4d6a949c 1146 skb = netdev_alloc_skb(ndev, skbuff_size);
86a74ff2
NI
1147 if (skb == NULL)
1148 break;
380af9e3
YS
1149 sh_eth_set_receive_align(skb);
1150
86a74ff2
NI
1151 /* RX descriptor */
1152 rxdesc = &mdp->rx_ring[i];
ab857916 1153 /* The size of the buffer is a multiple of 32 bytes. */
5cbf20c7
SS
1154 buf_len = ALIGN(mdp->rx_buf_sz, 32);
1155 rxdesc->len = cpu_to_edmac(mdp, buf_len << 16);
1156 dma_addr = dma_map_single(&ndev->dev, skb->data, buf_len,
52b9fa36
BH
1157 DMA_FROM_DEVICE);
1158 if (dma_mapping_error(&ndev->dev, dma_addr)) {
1159 kfree_skb(skb);
1160 break;
1161 }
1162 mdp->rx_skbuff[i] = skb;
1299653a 1163 rxdesc->addr = cpu_to_edmac(mdp, dma_addr);
71557a37 1164 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
86a74ff2 1165
b0ca2a21
NI
1166 /* Rx descriptor address set */
1167 if (i == 0) {
4a55530f 1168 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
db893473
SH
1169 if (sh_eth_is_gether(mdp) ||
1170 sh_eth_is_rz_fast_ether(mdp))
c5ed5368 1171 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
b0ca2a21 1172 }
86a74ff2
NI
1173 }
1174
525b8075 1175 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
86a74ff2
NI
1176
1177 /* Mark the last entry as wrapping the ring. */
c238041f 1178 rxdesc->status |= cpu_to_edmac(mdp, RD_RDLE);
86a74ff2
NI
1179
1180 memset(mdp->tx_ring, 0, tx_ringsize);
1181
1182 /* build Tx ring buffer */
525b8075 1183 for (i = 0; i < mdp->num_tx_ring; i++) {
86a74ff2
NI
1184 mdp->tx_skbuff[i] = NULL;
1185 txdesc = &mdp->tx_ring[i];
71557a37 1186 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
5cbf20c7 1187 txdesc->len = cpu_to_edmac(mdp, 0);
b0ca2a21 1188 if (i == 0) {
71557a37 1189 /* Tx descriptor address set */
4a55530f 1190 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
db893473
SH
1191 if (sh_eth_is_gether(mdp) ||
1192 sh_eth_is_rz_fast_ether(mdp))
c5ed5368 1193 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
b0ca2a21 1194 }
86a74ff2
NI
1195 }
1196
71557a37 1197 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
86a74ff2
NI
1198}
1199
1200/* Get skb and descriptor buffer */
1201static int sh_eth_ring_init(struct net_device *ndev)
1202{
1203 struct sh_eth_private *mdp = netdev_priv(ndev);
91d80683 1204 int rx_ringsize, tx_ringsize;
86a74ff2 1205
128296fc 1206 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
86a74ff2
NI
1207 * card needs room to do 8 byte alignment, +2 so we can reserve
1208 * the first 2 bytes, and +16 gets room for the status word from the
1209 * card.
1210 */
1211 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1212 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
503914cf
MD
1213 if (mdp->cd->rpadir)
1214 mdp->rx_buf_sz += NET_IP_ALIGN;
86a74ff2
NI
1215
1216 /* Allocate RX and TX skb rings */
2c94e856
SS
1217 mdp->rx_skbuff = kcalloc(mdp->num_rx_ring, sizeof(*mdp->rx_skbuff),
1218 GFP_KERNEL);
91d80683
SS
1219 if (!mdp->rx_skbuff)
1220 return -ENOMEM;
86a74ff2 1221
2c94e856
SS
1222 mdp->tx_skbuff = kcalloc(mdp->num_tx_ring, sizeof(*mdp->tx_skbuff),
1223 GFP_KERNEL);
91d80683 1224 if (!mdp->tx_skbuff)
8e03a5e7 1225 goto ring_free;
86a74ff2
NI
1226
1227 /* Allocate all Rx descriptors. */
525b8075 1228 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
86a74ff2 1229 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
d0320f75 1230 GFP_KERNEL);
91d80683 1231 if (!mdp->rx_ring)
8e03a5e7 1232 goto ring_free;
86a74ff2
NI
1233
1234 mdp->dirty_rx = 0;
1235
1236 /* Allocate all Tx descriptors. */
525b8075 1237 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
86a74ff2 1238 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
d0320f75 1239 GFP_KERNEL);
91d80683 1240 if (!mdp->tx_ring)
8e03a5e7 1241 goto ring_free;
91d80683 1242 return 0;
86a74ff2 1243
8e03a5e7
SS
1244ring_free:
1245 /* Free Rx and Tx skb ring buffer and DMA buffer */
86a74ff2
NI
1246 sh_eth_ring_free(ndev);
1247
91d80683 1248 return -ENOMEM;
86a74ff2
NI
1249}
1250
525b8075 1251static int sh_eth_dev_init(struct net_device *ndev, bool start)
86a74ff2
NI
1252{
1253 int ret = 0;
1254 struct sh_eth_private *mdp = netdev_priv(ndev);
86a74ff2
NI
1255 u32 val;
1256
1257 /* Soft Reset */
5cee1d37
NI
1258 ret = sh_eth_reset(ndev);
1259 if (ret)
f738a13d 1260 return ret;
86a74ff2 1261
55754f19
SH
1262 if (mdp->cd->rmiimode)
1263 sh_eth_write(ndev, 0x1, RMIIMODE);
1264
b0ca2a21
NI
1265 /* Descriptor format */
1266 sh_eth_ring_format(ndev);
380af9e3 1267 if (mdp->cd->rpadir)
4a55530f 1268 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
86a74ff2
NI
1269
1270 /* all sh_eth int mask */
4a55530f 1271 sh_eth_write(ndev, 0, EESIPR);
86a74ff2 1272
10b9194f 1273#if defined(__LITTLE_ENDIAN)
380af9e3 1274 if (mdp->cd->hw_swap)
4a55530f 1275 sh_eth_write(ndev, EDMR_EL, EDMR);
380af9e3 1276 else
b0ca2a21 1277#endif
4a55530f 1278 sh_eth_write(ndev, 0, EDMR);
86a74ff2 1279
b0ca2a21 1280 /* FIFO size set */
4a55530f
YS
1281 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1282 sh_eth_write(ndev, 0, TFTR);
86a74ff2 1283
530aa2d0
BD
1284 /* Frame recv control (enable multiple-packets per rx irq) */
1285 sh_eth_write(ndev, RMCR_RNC, RMCR);
86a74ff2 1286
b284fbe3 1287 sh_eth_write(ndev, mdp->cd->trscer_err_mask, TRSCER);
86a74ff2 1288
380af9e3 1289 if (mdp->cd->bculr)
4a55530f 1290 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
b0ca2a21 1291
4a55530f 1292 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
86a74ff2 1293
380af9e3 1294 if (!mdp->cd->no_trimd)
4a55530f 1295 sh_eth_write(ndev, 0, TRIMD);
86a74ff2 1296
b0ca2a21 1297 /* Recv frame limit set register */
fdb37a7f
YS
1298 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1299 RFLR);
86a74ff2 1300
4a55530f 1301 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
283e38db
BH
1302 if (start) {
1303 mdp->irq_enabled = true;
525b8075 1304 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
283e38db 1305 }
86a74ff2
NI
1306
1307 /* PAUSE Prohibition */
4a55530f 1308 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
86a74ff2
NI
1309 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
1310
4a55530f 1311 sh_eth_write(ndev, val, ECMR);
b0ca2a21 1312
380af9e3
YS
1313 if (mdp->cd->set_rate)
1314 mdp->cd->set_rate(ndev);
1315
b0ca2a21 1316 /* E-MAC Status Register clear */
4a55530f 1317 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
b0ca2a21
NI
1318
1319 /* E-MAC Interrupt Enable register */
525b8075
YS
1320 if (start)
1321 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
86a74ff2
NI
1322
1323 /* Set MAC address */
1324 update_mac_address(ndev);
1325
1326 /* mask reset */
380af9e3 1327 if (mdp->cd->apr)
4a55530f 1328 sh_eth_write(ndev, APR_AP, APR);
380af9e3 1329 if (mdp->cd->mpr)
4a55530f 1330 sh_eth_write(ndev, MPR_MP, MPR);
380af9e3 1331 if (mdp->cd->tpauser)
4a55530f 1332 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
b0ca2a21 1333
525b8075
YS
1334 if (start) {
1335 /* Setting the Rx mode will start the Rx process. */
1336 sh_eth_write(ndev, EDRRR_R, EDRRR);
86a74ff2 1337
525b8075
YS
1338 netif_start_queue(ndev);
1339 }
86a74ff2
NI
1340
1341 return ret;
1342}
1343
740c7f31
BH
1344static void sh_eth_dev_exit(struct net_device *ndev)
1345{
1346 struct sh_eth_private *mdp = netdev_priv(ndev);
1347 int i;
1348
1349 /* Deactivate all TX descriptors, so DMA should stop at next
1350 * packet boundary if it's currently running
1351 */
1352 for (i = 0; i < mdp->num_tx_ring; i++)
1353 mdp->tx_ring[i].status &= ~cpu_to_edmac(mdp, TD_TACT);
1354
1355 /* Disable TX FIFO egress to MAC */
1356 sh_eth_rcv_snd_disable(ndev);
1357
1358 /* Stop RX DMA at next packet boundary */
1359 sh_eth_write(ndev, 0, EDRRR);
1360
1361 /* Aside from TX DMA, we can't tell when the hardware is
1362 * really stopped, so we need to reset to make sure.
1363 * Before doing that, wait for long enough to *probably*
1364 * finish transmitting the last packet and poll stats.
1365 */
1366 msleep(2); /* max frame time at 10 Mbps < 1250 us */
1367 sh_eth_get_stats(ndev);
1368 sh_eth_reset(ndev);
a14c7d15
GU
1369
1370 /* Set MAC address again */
1371 update_mac_address(ndev);
740c7f31
BH
1372}
1373
86a74ff2
NI
1374/* free Tx skb function */
1375static int sh_eth_txfree(struct net_device *ndev)
1376{
1377 struct sh_eth_private *mdp = netdev_priv(ndev);
1378 struct sh_eth_txdesc *txdesc;
128296fc 1379 int free_num = 0;
86a74ff2
NI
1380 int entry = 0;
1381
1382 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
525b8075 1383 entry = mdp->dirty_tx % mdp->num_tx_ring;
86a74ff2 1384 txdesc = &mdp->tx_ring[entry];
71557a37 1385 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
86a74ff2 1386 break;
7d7355f5 1387 /* TACT bit must be checked before all the following reads */
f32bfb9a 1388 dma_rmb();
e5fd13f4
BH
1389 netif_info(mdp, tx_done, ndev,
1390 "tx entry %d status 0x%08x\n",
1391 entry, edmac_to_cpu(mdp, txdesc->status));
86a74ff2
NI
1392 /* Free the original skb. */
1393 if (mdp->tx_skbuff[entry]) {
1299653a
SS
1394 dma_unmap_single(&ndev->dev,
1395 edmac_to_cpu(mdp, txdesc->addr),
5cbf20c7
SS
1396 edmac_to_cpu(mdp, txdesc->len) >> 16,
1397 DMA_TO_DEVICE);
86a74ff2
NI
1398 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1399 mdp->tx_skbuff[entry] = NULL;
128296fc 1400 free_num++;
86a74ff2 1401 }
71557a37 1402 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
525b8075 1403 if (entry >= mdp->num_tx_ring - 1)
71557a37 1404 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
86a74ff2 1405
bb7d92e3 1406 ndev->stats.tx_packets++;
5cbf20c7 1407 ndev->stats.tx_bytes += edmac_to_cpu(mdp, txdesc->len) >> 16;
86a74ff2 1408 }
128296fc 1409 return free_num;
86a74ff2
NI
1410}
1411
1412/* Packet receive function */
3719109d 1413static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
86a74ff2
NI
1414{
1415 struct sh_eth_private *mdp = netdev_priv(ndev);
1416 struct sh_eth_rxdesc *rxdesc;
1417
525b8075
YS
1418 int entry = mdp->cur_rx % mdp->num_rx_ring;
1419 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
319cd520 1420 int limit;
86a74ff2
NI
1421 struct sk_buff *skb;
1422 u16 pkt_len = 0;
380af9e3 1423 u32 desc_status;
cb368595 1424 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
52b9fa36 1425 dma_addr_t dma_addr;
5cbf20c7 1426 u32 buf_len;
86a74ff2 1427
319cd520
MK
1428 boguscnt = min(boguscnt, *quota);
1429 limit = boguscnt;
86a74ff2 1430 rxdesc = &mdp->rx_ring[entry];
71557a37 1431 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
7d7355f5 1432 /* RACT bit must be checked before all the following reads */
f32bfb9a 1433 dma_rmb();
71557a37 1434 desc_status = edmac_to_cpu(mdp, rxdesc->status);
5cbf20c7 1435 pkt_len = edmac_to_cpu(mdp, rxdesc->len) & RD_RFL;
86a74ff2
NI
1436
1437 if (--boguscnt < 0)
1438 break;
1439
e5fd13f4
BH
1440 netif_info(mdp, rx_status, ndev,
1441 "rx entry %d status 0x%08x len %d\n",
1442 entry, desc_status, pkt_len);
1443
86a74ff2 1444 if (!(desc_status & RDFEND))
bb7d92e3 1445 ndev->stats.rx_length_errors++;
86a74ff2 1446
128296fc 1447 /* In case of almost all GETHER/ETHERs, the Receive Frame State
dd019897 1448 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
9b4a6364
BH
1449 * bit 0. However, in case of the R8A7740 and R7S72100
1450 * the RFS bits are from bit 25 to bit 16. So, the
db893473 1451 * driver needs right shifting by 16.
dd019897 1452 */
ac8025a6
SS
1453 if (mdp->cd->shift_rd0)
1454 desc_status >>= 16;
dd019897 1455
248be83d 1456 skb = mdp->rx_skbuff[entry];
86a74ff2
NI
1457 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1458 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
bb7d92e3 1459 ndev->stats.rx_errors++;
86a74ff2 1460 if (desc_status & RD_RFS1)
bb7d92e3 1461 ndev->stats.rx_crc_errors++;
86a74ff2 1462 if (desc_status & RD_RFS2)
bb7d92e3 1463 ndev->stats.rx_frame_errors++;
86a74ff2 1464 if (desc_status & RD_RFS3)
bb7d92e3 1465 ndev->stats.rx_length_errors++;
86a74ff2 1466 if (desc_status & RD_RFS4)
bb7d92e3 1467 ndev->stats.rx_length_errors++;
86a74ff2 1468 if (desc_status & RD_RFS6)
bb7d92e3 1469 ndev->stats.rx_missed_errors++;
86a74ff2 1470 if (desc_status & RD_RFS10)
bb7d92e3 1471 ndev->stats.rx_over_errors++;
248be83d 1472 } else if (skb) {
1299653a 1473 dma_addr = edmac_to_cpu(mdp, rxdesc->addr);
380af9e3
YS
1474 if (!mdp->cd->hw_swap)
1475 sh_eth_soft_swap(
1299653a 1476 phys_to_virt(ALIGN(dma_addr, 4)),
380af9e3 1477 pkt_len + 2);
86a74ff2 1478 mdp->rx_skbuff[entry] = NULL;
503914cf
MD
1479 if (mdp->cd->rpadir)
1480 skb_reserve(skb, NET_IP_ALIGN);
1299653a 1481 dma_unmap_single(&ndev->dev, dma_addr,
ab857916 1482 ALIGN(mdp->rx_buf_sz, 32),
52b9fa36 1483 DMA_FROM_DEVICE);
86a74ff2
NI
1484 skb_put(skb, pkt_len);
1485 skb->protocol = eth_type_trans(skb, ndev);
a8e9fd0f 1486 netif_receive_skb(skb);
bb7d92e3
ED
1487 ndev->stats.rx_packets++;
1488 ndev->stats.rx_bytes += pkt_len;
25b77ad7
BH
1489 if (desc_status & RD_RFS8)
1490 ndev->stats.multicast++;
86a74ff2 1491 }
525b8075 1492 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
862df497 1493 rxdesc = &mdp->rx_ring[entry];
86a74ff2
NI
1494 }
1495
1496 /* Refill the Rx ring buffers. */
1497 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
525b8075 1498 entry = mdp->dirty_rx % mdp->num_rx_ring;
86a74ff2 1499 rxdesc = &mdp->rx_ring[entry];
ab857916 1500 /* The size of the buffer is 32 byte boundary. */
5cbf20c7
SS
1501 buf_len = ALIGN(mdp->rx_buf_sz, 32);
1502 rxdesc->len = cpu_to_edmac(mdp, buf_len << 16);
b0ca2a21 1503
86a74ff2 1504 if (mdp->rx_skbuff[entry] == NULL) {
4d6a949c 1505 skb = netdev_alloc_skb(ndev, skbuff_size);
86a74ff2
NI
1506 if (skb == NULL)
1507 break; /* Better luck next round. */
380af9e3 1508 sh_eth_set_receive_align(skb);
52b9fa36 1509 dma_addr = dma_map_single(&ndev->dev, skb->data,
5cbf20c7 1510 buf_len, DMA_FROM_DEVICE);
52b9fa36
BH
1511 if (dma_mapping_error(&ndev->dev, dma_addr)) {
1512 kfree_skb(skb);
1513 break;
1514 }
1515 mdp->rx_skbuff[entry] = skb;
380af9e3 1516
bc8acf2c 1517 skb_checksum_none_assert(skb);
1299653a 1518 rxdesc->addr = cpu_to_edmac(mdp, dma_addr);
86a74ff2 1519 }
f32bfb9a 1520 dma_wmb(); /* RACT bit must be set after all the above writes */
525b8075 1521 if (entry >= mdp->num_rx_ring - 1)
86a74ff2 1522 rxdesc->status |=
c238041f 1523 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDLE);
86a74ff2
NI
1524 else
1525 rxdesc->status |=
71557a37 1526 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
86a74ff2
NI
1527 }
1528
1529 /* Restart Rx engine if stopped. */
1530 /* If we don't need to check status, don't. -KDU */
79fba9f5 1531 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
a18e08bd 1532 /* fix the values for the next receiving if RDE is set */
3365711d
BH
1533 if (intr_status & EESR_RDE &&
1534 mdp->reg_offset[RDFAR] != SH_ETH_OFFSET_INVALID) {
128296fc
SS
1535 u32 count = (sh_eth_read(ndev, RDFAR) -
1536 sh_eth_read(ndev, RDLAR)) >> 4;
1537
1538 mdp->cur_rx = count;
1539 mdp->dirty_rx = count;
1540 }
4a55530f 1541 sh_eth_write(ndev, EDRRR_R, EDRRR);
79fba9f5 1542 }
86a74ff2 1543
319cd520
MK
1544 *quota -= limit - boguscnt - 1;
1545
4f809cea 1546 return *quota <= 0;
86a74ff2
NI
1547}
1548
4a55530f 1549static void sh_eth_rcv_snd_disable(struct net_device *ndev)
dc19e4e5
NI
1550{
1551 /* disable tx and rx */
4a55530f
YS
1552 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
1553 ~(ECMR_RE | ECMR_TE), ECMR);
dc19e4e5
NI
1554}
1555
4a55530f 1556static void sh_eth_rcv_snd_enable(struct net_device *ndev)
dc19e4e5
NI
1557{
1558 /* enable tx and rx */
4a55530f
YS
1559 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
1560 (ECMR_RE | ECMR_TE), ECMR);
dc19e4e5
NI
1561}
1562
86a74ff2 1563/* error control function */
0799c2d6 1564static void sh_eth_error(struct net_device *ndev, u32 intr_status)
86a74ff2
NI
1565{
1566 struct sh_eth_private *mdp = netdev_priv(ndev);
86a74ff2 1567 u32 felic_stat;
380af9e3
YS
1568 u32 link_stat;
1569 u32 mask;
86a74ff2
NI
1570
1571 if (intr_status & EESR_ECI) {
4a55530f
YS
1572 felic_stat = sh_eth_read(ndev, ECSR);
1573 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
86a74ff2 1574 if (felic_stat & ECSR_ICD)
bb7d92e3 1575 ndev->stats.tx_carrier_errors++;
86a74ff2
NI
1576 if (felic_stat & ECSR_LCHNG) {
1577 /* Link Changed */
4923576b 1578 if (mdp->cd->no_psr || mdp->no_ether_link) {
1e1b812b 1579 goto ignore_link;
380af9e3 1580 } else {
4a55530f 1581 link_stat = (sh_eth_read(ndev, PSR));
4923576b
YS
1582 if (mdp->ether_link_active_low)
1583 link_stat = ~link_stat;
380af9e3 1584 }
128296fc 1585 if (!(link_stat & PHY_ST_LINK)) {
4a55530f 1586 sh_eth_rcv_snd_disable(ndev);
128296fc 1587 } else {
86a74ff2 1588 /* Link Up */
4a55530f 1589 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
128296fc
SS
1590 ~DMAC_M_ECI, EESIPR);
1591 /* clear int */
4a55530f 1592 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
128296fc 1593 ECSR);
4a55530f 1594 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
128296fc 1595 DMAC_M_ECI, EESIPR);
86a74ff2 1596 /* enable tx and rx */
4a55530f 1597 sh_eth_rcv_snd_enable(ndev);
86a74ff2
NI
1598 }
1599 }
1600 }
1601
1e1b812b 1602ignore_link:
86a74ff2 1603 if (intr_status & EESR_TWB) {
4eb313a7
SS
1604 /* Unused write back interrupt */
1605 if (intr_status & EESR_TABT) { /* Transmit Abort int */
bb7d92e3 1606 ndev->stats.tx_aborted_errors++;
8d5009f6 1607 netif_err(mdp, tx_err, ndev, "Transmit Abort\n");
4eb313a7 1608 }
86a74ff2
NI
1609 }
1610
1611 if (intr_status & EESR_RABT) {
1612 /* Receive Abort int */
1613 if (intr_status & EESR_RFRMER) {
1614 /* Receive Frame Overflow int */
bb7d92e3 1615 ndev->stats.rx_frame_errors++;
86a74ff2
NI
1616 }
1617 }
380af9e3 1618
dc19e4e5
NI
1619 if (intr_status & EESR_TDE) {
1620 /* Transmit Descriptor Empty int */
bb7d92e3 1621 ndev->stats.tx_fifo_errors++;
8d5009f6 1622 netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n");
dc19e4e5
NI
1623 }
1624
1625 if (intr_status & EESR_TFE) {
1626 /* FIFO under flow */
bb7d92e3 1627 ndev->stats.tx_fifo_errors++;
8d5009f6 1628 netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n");
86a74ff2
NI
1629 }
1630
1631 if (intr_status & EESR_RDE) {
1632 /* Receive Descriptor Empty int */
bb7d92e3 1633 ndev->stats.rx_over_errors++;
86a74ff2 1634 }
dc19e4e5 1635
86a74ff2
NI
1636 if (intr_status & EESR_RFE) {
1637 /* Receive FIFO Overflow int */
bb7d92e3 1638 ndev->stats.rx_fifo_errors++;
dc19e4e5
NI
1639 }
1640
1641 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1642 /* Address Error */
bb7d92e3 1643 ndev->stats.tx_fifo_errors++;
8d5009f6 1644 netif_err(mdp, tx_err, ndev, "Address Error\n");
86a74ff2 1645 }
380af9e3
YS
1646
1647 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1648 if (mdp->cd->no_ade)
1649 mask &= ~EESR_ADE;
1650 if (intr_status & mask) {
86a74ff2 1651 /* Tx error */
4a55530f 1652 u32 edtrr = sh_eth_read(ndev, EDTRR);
090d560f 1653
86a74ff2 1654 /* dmesg */
da246855
SS
1655 netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1656 intr_status, mdp->cur_tx, mdp->dirty_tx,
1657 (u32)ndev->state, edtrr);
86a74ff2
NI
1658 /* dirty buffer free */
1659 sh_eth_txfree(ndev);
1660
1661 /* SH7712 BUG */
c5ed5368 1662 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
86a74ff2 1663 /* tx dma start */
c5ed5368 1664 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
86a74ff2
NI
1665 }
1666 /* wakeup */
1667 netif_wake_queue(ndev);
1668 }
1669}
1670
1671static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1672{
1673 struct net_device *ndev = netdev;
1674 struct sh_eth_private *mdp = netdev_priv(ndev);
380af9e3 1675 struct sh_eth_cpu_data *cd = mdp->cd;
0e0fde3c 1676 irqreturn_t ret = IRQ_NONE;
0799c2d6 1677 u32 intr_status, intr_enable;
86a74ff2 1678
86a74ff2
NI
1679 spin_lock(&mdp->lock);
1680
3893b273 1681 /* Get interrupt status */
4a55530f 1682 intr_status = sh_eth_read(ndev, EESR);
3893b273
SS
1683 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1684 * enabled since it's the one that comes thru regardless of the mask,
1685 * and we need to fully handle it in sh_eth_error() in order to quench
1686 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1687 */
3719109d
SS
1688 intr_enable = sh_eth_read(ndev, EESIPR);
1689 intr_status &= intr_enable | DMAC_M_ECI;
1690 if (intr_status & (EESR_RX_CHECK | cd->tx_check | cd->eesr_err_check))
0e0fde3c 1691 ret = IRQ_HANDLED;
3719109d 1692 else
283e38db
BH
1693 goto out;
1694
1695 if (!likely(mdp->irq_enabled)) {
1696 sh_eth_write(ndev, 0, EESIPR);
1697 goto out;
1698 }
86a74ff2 1699
3719109d
SS
1700 if (intr_status & EESR_RX_CHECK) {
1701 if (napi_schedule_prep(&mdp->napi)) {
1702 /* Mask Rx interrupts */
1703 sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
1704 EESIPR);
1705 __napi_schedule(&mdp->napi);
1706 } else {
da246855 1707 netdev_warn(ndev,
0799c2d6 1708 "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
da246855 1709 intr_status, intr_enable);
3719109d
SS
1710 }
1711 }
86a74ff2 1712
b0ca2a21 1713 /* Tx Check */
380af9e3 1714 if (intr_status & cd->tx_check) {
3719109d
SS
1715 /* Clear Tx interrupts */
1716 sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
1717
86a74ff2
NI
1718 sh_eth_txfree(ndev);
1719 netif_wake_queue(ndev);
1720 }
1721
3719109d
SS
1722 if (intr_status & cd->eesr_err_check) {
1723 /* Clear error interrupts */
1724 sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
1725
86a74ff2 1726 sh_eth_error(ndev, intr_status);
3719109d 1727 }
86a74ff2 1728
283e38db 1729out:
86a74ff2
NI
1730 spin_unlock(&mdp->lock);
1731
0e0fde3c 1732 return ret;
86a74ff2
NI
1733}
1734
3719109d
SS
1735static int sh_eth_poll(struct napi_struct *napi, int budget)
1736{
1737 struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
1738 napi);
1739 struct net_device *ndev = napi->dev;
1740 int quota = budget;
0799c2d6 1741 u32 intr_status;
3719109d
SS
1742
1743 for (;;) {
1744 intr_status = sh_eth_read(ndev, EESR);
1745 if (!(intr_status & EESR_RX_CHECK))
1746 break;
1747 /* Clear Rx interrupts */
1748 sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
1749
1750 if (sh_eth_rx(ndev, intr_status, &quota))
1751 goto out;
1752 }
1753
1754 napi_complete(napi);
1755
1756 /* Reenable Rx interrupts */
283e38db
BH
1757 if (mdp->irq_enabled)
1758 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
3719109d
SS
1759out:
1760 return budget - quota;
1761}
1762
86a74ff2
NI
1763/* PHY state control function */
1764static void sh_eth_adjust_link(struct net_device *ndev)
1765{
1766 struct sh_eth_private *mdp = netdev_priv(ndev);
1767 struct phy_device *phydev = mdp->phydev;
86a74ff2
NI
1768 int new_state = 0;
1769
3340d2aa 1770 if (phydev->link) {
86a74ff2
NI
1771 if (phydev->duplex != mdp->duplex) {
1772 new_state = 1;
1773 mdp->duplex = phydev->duplex;
380af9e3
YS
1774 if (mdp->cd->set_duplex)
1775 mdp->cd->set_duplex(ndev);
86a74ff2
NI
1776 }
1777
1778 if (phydev->speed != mdp->speed) {
1779 new_state = 1;
1780 mdp->speed = phydev->speed;
380af9e3
YS
1781 if (mdp->cd->set_rate)
1782 mdp->cd->set_rate(ndev);
86a74ff2 1783 }
3340d2aa 1784 if (!mdp->link) {
91a56152 1785 sh_eth_write(ndev,
128296fc
SS
1786 sh_eth_read(ndev, ECMR) & ~ECMR_TXF,
1787 ECMR);
86a74ff2
NI
1788 new_state = 1;
1789 mdp->link = phydev->link;
1e1b812b
SS
1790 if (mdp->cd->no_psr || mdp->no_ether_link)
1791 sh_eth_rcv_snd_enable(ndev);
86a74ff2
NI
1792 }
1793 } else if (mdp->link) {
1794 new_state = 1;
3340d2aa 1795 mdp->link = 0;
86a74ff2
NI
1796 mdp->speed = 0;
1797 mdp->duplex = -1;
1e1b812b
SS
1798 if (mdp->cd->no_psr || mdp->no_ether_link)
1799 sh_eth_rcv_snd_disable(ndev);
86a74ff2
NI
1800 }
1801
dc19e4e5 1802 if (new_state && netif_msg_link(mdp))
86a74ff2
NI
1803 phy_print_status(phydev);
1804}
1805
1806/* PHY init function */
1807static int sh_eth_phy_init(struct net_device *ndev)
1808{
702eca02 1809 struct device_node *np = ndev->dev.parent->of_node;
86a74ff2 1810 struct sh_eth_private *mdp = netdev_priv(ndev);
86a74ff2
NI
1811 struct phy_device *phydev = NULL;
1812
3340d2aa 1813 mdp->link = 0;
86a74ff2
NI
1814 mdp->speed = 0;
1815 mdp->duplex = -1;
1816
1817 /* Try connect to PHY */
702eca02
BD
1818 if (np) {
1819 struct device_node *pn;
1820
1821 pn = of_parse_phandle(np, "phy-handle", 0);
1822 phydev = of_phy_connect(ndev, pn,
1823 sh_eth_adjust_link, 0,
1824 mdp->phy_interface);
1825
1826 if (!phydev)
1827 phydev = ERR_PTR(-ENOENT);
1828 } else {
1829 char phy_id[MII_BUS_ID_SIZE + 3];
1830
1831 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
1832 mdp->mii_bus->id, mdp->phy_id);
1833
1834 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
1835 mdp->phy_interface);
1836 }
1837
86a74ff2 1838 if (IS_ERR(phydev)) {
da246855 1839 netdev_err(ndev, "failed to connect PHY\n");
86a74ff2
NI
1840 return PTR_ERR(phydev);
1841 }
380af9e3 1842
da246855
SS
1843 netdev_info(ndev, "attached PHY %d (IRQ %d) to driver %s\n",
1844 phydev->addr, phydev->irq, phydev->drv->name);
86a74ff2
NI
1845
1846 mdp->phydev = phydev;
1847
1848 return 0;
1849}
1850
1851/* PHY control start function */
1852static int sh_eth_phy_start(struct net_device *ndev)
1853{
1854 struct sh_eth_private *mdp = netdev_priv(ndev);
1855 int ret;
1856
1857 ret = sh_eth_phy_init(ndev);
1858 if (ret)
1859 return ret;
1860
86a74ff2
NI
1861 phy_start(mdp->phydev);
1862
1863 return 0;
1864}
1865
dc19e4e5 1866static int sh_eth_get_settings(struct net_device *ndev,
128296fc 1867 struct ethtool_cmd *ecmd)
dc19e4e5
NI
1868{
1869 struct sh_eth_private *mdp = netdev_priv(ndev);
1870 unsigned long flags;
1871 int ret;
1872
4f9dce23
BH
1873 if (!mdp->phydev)
1874 return -ENODEV;
1875
dc19e4e5
NI
1876 spin_lock_irqsave(&mdp->lock, flags);
1877 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1878 spin_unlock_irqrestore(&mdp->lock, flags);
1879
1880 return ret;
1881}
1882
1883static int sh_eth_set_settings(struct net_device *ndev,
128296fc 1884 struct ethtool_cmd *ecmd)
dc19e4e5
NI
1885{
1886 struct sh_eth_private *mdp = netdev_priv(ndev);
1887 unsigned long flags;
1888 int ret;
dc19e4e5 1889
4f9dce23
BH
1890 if (!mdp->phydev)
1891 return -ENODEV;
1892
dc19e4e5
NI
1893 spin_lock_irqsave(&mdp->lock, flags);
1894
1895 /* disable tx and rx */
4a55530f 1896 sh_eth_rcv_snd_disable(ndev);
dc19e4e5
NI
1897
1898 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1899 if (ret)
1900 goto error_exit;
1901
1902 if (ecmd->duplex == DUPLEX_FULL)
1903 mdp->duplex = 1;
1904 else
1905 mdp->duplex = 0;
1906
1907 if (mdp->cd->set_duplex)
1908 mdp->cd->set_duplex(ndev);
1909
1910error_exit:
1911 mdelay(1);
1912
1913 /* enable tx and rx */
4a55530f 1914 sh_eth_rcv_snd_enable(ndev);
dc19e4e5
NI
1915
1916 spin_unlock_irqrestore(&mdp->lock, flags);
1917
1918 return ret;
1919}
1920
6b4b4fea
BH
1921/* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
1922 * version must be bumped as well. Just adding registers up to that
1923 * limit is fine, as long as the existing register indices don't
1924 * change.
1925 */
1926#define SH_ETH_REG_DUMP_VERSION 1
1927#define SH_ETH_REG_DUMP_MAX_REGS 256
1928
1929static size_t __sh_eth_get_regs(struct net_device *ndev, u32 *buf)
1930{
1931 struct sh_eth_private *mdp = netdev_priv(ndev);
1932 struct sh_eth_cpu_data *cd = mdp->cd;
1933 u32 *valid_map;
1934 size_t len;
1935
1936 BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET > SH_ETH_REG_DUMP_MAX_REGS);
1937
1938 /* Dump starts with a bitmap that tells ethtool which
1939 * registers are defined for this chip.
1940 */
1941 len = DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS, 32);
1942 if (buf) {
1943 valid_map = buf;
1944 buf += len;
1945 } else {
1946 valid_map = NULL;
1947 }
1948
1949 /* Add a register to the dump, if it has a defined offset.
1950 * This automatically skips most undefined registers, but for
1951 * some it is also necessary to check a capability flag in
1952 * struct sh_eth_cpu_data.
1953 */
1954#define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
1955#define add_reg_from(reg, read_expr) do { \
1956 if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
1957 if (buf) { \
1958 mark_reg_valid(reg); \
1959 *buf++ = read_expr; \
1960 } \
1961 ++len; \
1962 } \
1963 } while (0)
1964#define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
1965#define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
1966
1967 add_reg(EDSR);
1968 add_reg(EDMR);
1969 add_reg(EDTRR);
1970 add_reg(EDRRR);
1971 add_reg(EESR);
1972 add_reg(EESIPR);
1973 add_reg(TDLAR);
1974 add_reg(TDFAR);
1975 add_reg(TDFXR);
1976 add_reg(TDFFR);
1977 add_reg(RDLAR);
1978 add_reg(RDFAR);
1979 add_reg(RDFXR);
1980 add_reg(RDFFR);
1981 add_reg(TRSCER);
1982 add_reg(RMFCR);
1983 add_reg(TFTR);
1984 add_reg(FDR);
1985 add_reg(RMCR);
1986 add_reg(TFUCR);
1987 add_reg(RFOCR);
1988 if (cd->rmiimode)
1989 add_reg(RMIIMODE);
1990 add_reg(FCFTR);
1991 if (cd->rpadir)
1992 add_reg(RPADIR);
1993 if (!cd->no_trimd)
1994 add_reg(TRIMD);
1995 add_reg(ECMR);
1996 add_reg(ECSR);
1997 add_reg(ECSIPR);
1998 add_reg(PIR);
1999 if (!cd->no_psr)
2000 add_reg(PSR);
2001 add_reg(RDMLR);
2002 add_reg(RFLR);
2003 add_reg(IPGR);
2004 if (cd->apr)
2005 add_reg(APR);
2006 if (cd->mpr)
2007 add_reg(MPR);
2008 add_reg(RFCR);
2009 add_reg(RFCF);
2010 if (cd->tpauser)
2011 add_reg(TPAUSER);
2012 add_reg(TPAUSECR);
2013 add_reg(GECMR);
2014 if (cd->bculr)
2015 add_reg(BCULR);
2016 add_reg(MAHR);
2017 add_reg(MALR);
2018 add_reg(TROCR);
2019 add_reg(CDCR);
2020 add_reg(LCCR);
2021 add_reg(CNDCR);
2022 add_reg(CEFCR);
2023 add_reg(FRECR);
2024 add_reg(TSFRCR);
2025 add_reg(TLFRCR);
2026 add_reg(CERCR);
2027 add_reg(CEECR);
2028 add_reg(MAFCR);
2029 if (cd->rtrate)
2030 add_reg(RTRATE);
2031 if (cd->hw_crc)
2032 add_reg(CSMR);
2033 if (cd->select_mii)
2034 add_reg(RMII_MII);
2035 add_reg(ARSTR);
2036 if (cd->tsu) {
2037 add_tsu_reg(TSU_CTRST);
2038 add_tsu_reg(TSU_FWEN0);
2039 add_tsu_reg(TSU_FWEN1);
2040 add_tsu_reg(TSU_FCM);
2041 add_tsu_reg(TSU_BSYSL0);
2042 add_tsu_reg(TSU_BSYSL1);
2043 add_tsu_reg(TSU_PRISL0);
2044 add_tsu_reg(TSU_PRISL1);
2045 add_tsu_reg(TSU_FWSL0);
2046 add_tsu_reg(TSU_FWSL1);
2047 add_tsu_reg(TSU_FWSLC);
2048 add_tsu_reg(TSU_QTAG0);
2049 add_tsu_reg(TSU_QTAG1);
2050 add_tsu_reg(TSU_QTAGM0);
2051 add_tsu_reg(TSU_QTAGM1);
2052 add_tsu_reg(TSU_FWSR);
2053 add_tsu_reg(TSU_FWINMK);
2054 add_tsu_reg(TSU_ADQT0);
2055 add_tsu_reg(TSU_ADQT1);
2056 add_tsu_reg(TSU_VTAG0);
2057 add_tsu_reg(TSU_VTAG1);
2058 add_tsu_reg(TSU_ADSBSY);
2059 add_tsu_reg(TSU_TEN);
2060 add_tsu_reg(TSU_POST1);
2061 add_tsu_reg(TSU_POST2);
2062 add_tsu_reg(TSU_POST3);
2063 add_tsu_reg(TSU_POST4);
2064 if (mdp->reg_offset[TSU_ADRH0] != SH_ETH_OFFSET_INVALID) {
2065 /* This is the start of a table, not just a single
2066 * register.
2067 */
2068 if (buf) {
2069 unsigned int i;
2070
2071 mark_reg_valid(TSU_ADRH0);
2072 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES * 2; i++)
2073 *buf++ = ioread32(
2074 mdp->tsu_addr +
2075 mdp->reg_offset[TSU_ADRH0] +
2076 i * 4);
2077 }
2078 len += SH_ETH_TSU_CAM_ENTRIES * 2;
2079 }
2080 }
2081
2082#undef mark_reg_valid
2083#undef add_reg_from
2084#undef add_reg
2085#undef add_tsu_reg
2086
2087 return len * 4;
2088}
2089
2090static int sh_eth_get_regs_len(struct net_device *ndev)
2091{
2092 return __sh_eth_get_regs(ndev, NULL);
2093}
2094
2095static void sh_eth_get_regs(struct net_device *ndev, struct ethtool_regs *regs,
2096 void *buf)
2097{
2098 struct sh_eth_private *mdp = netdev_priv(ndev);
2099
2100 regs->version = SH_ETH_REG_DUMP_VERSION;
2101
2102 pm_runtime_get_sync(&mdp->pdev->dev);
2103 __sh_eth_get_regs(ndev, buf);
2104 pm_runtime_put_sync(&mdp->pdev->dev);
2105}
2106
dc19e4e5
NI
2107static int sh_eth_nway_reset(struct net_device *ndev)
2108{
2109 struct sh_eth_private *mdp = netdev_priv(ndev);
2110 unsigned long flags;
2111 int ret;
2112
4f9dce23
BH
2113 if (!mdp->phydev)
2114 return -ENODEV;
2115
dc19e4e5
NI
2116 spin_lock_irqsave(&mdp->lock, flags);
2117 ret = phy_start_aneg(mdp->phydev);
2118 spin_unlock_irqrestore(&mdp->lock, flags);
2119
2120 return ret;
2121}
2122
2123static u32 sh_eth_get_msglevel(struct net_device *ndev)
2124{
2125 struct sh_eth_private *mdp = netdev_priv(ndev);
2126 return mdp->msg_enable;
2127}
2128
2129static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
2130{
2131 struct sh_eth_private *mdp = netdev_priv(ndev);
2132 mdp->msg_enable = value;
2133}
2134
2135static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
2136 "rx_current", "tx_current",
2137 "rx_dirty", "tx_dirty",
2138};
2139#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
2140
2141static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
2142{
2143 switch (sset) {
2144 case ETH_SS_STATS:
2145 return SH_ETH_STATS_LEN;
2146 default:
2147 return -EOPNOTSUPP;
2148 }
2149}
2150
2151static void sh_eth_get_ethtool_stats(struct net_device *ndev,
128296fc 2152 struct ethtool_stats *stats, u64 *data)
dc19e4e5
NI
2153{
2154 struct sh_eth_private *mdp = netdev_priv(ndev);
2155 int i = 0;
2156
2157 /* device-specific stats */
2158 data[i++] = mdp->cur_rx;
2159 data[i++] = mdp->cur_tx;
2160 data[i++] = mdp->dirty_rx;
2161 data[i++] = mdp->dirty_tx;
2162}
2163
2164static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
2165{
2166 switch (stringset) {
2167 case ETH_SS_STATS:
2168 memcpy(data, *sh_eth_gstrings_stats,
128296fc 2169 sizeof(sh_eth_gstrings_stats));
dc19e4e5
NI
2170 break;
2171 }
2172}
2173
525b8075
YS
2174static void sh_eth_get_ringparam(struct net_device *ndev,
2175 struct ethtool_ringparam *ring)
2176{
2177 struct sh_eth_private *mdp = netdev_priv(ndev);
2178
2179 ring->rx_max_pending = RX_RING_MAX;
2180 ring->tx_max_pending = TX_RING_MAX;
2181 ring->rx_pending = mdp->num_rx_ring;
2182 ring->tx_pending = mdp->num_tx_ring;
2183}
2184
2185static int sh_eth_set_ringparam(struct net_device *ndev,
2186 struct ethtool_ringparam *ring)
2187{
2188 struct sh_eth_private *mdp = netdev_priv(ndev);
2189 int ret;
2190
2191 if (ring->tx_pending > TX_RING_MAX ||
2192 ring->rx_pending > RX_RING_MAX ||
2193 ring->tx_pending < TX_RING_MIN ||
2194 ring->rx_pending < RX_RING_MIN)
2195 return -EINVAL;
2196 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2197 return -EINVAL;
2198
2199 if (netif_running(ndev)) {
bd888916 2200 netif_device_detach(ndev);
525b8075 2201 netif_tx_disable(ndev);
283e38db
BH
2202
2203 /* Serialise with the interrupt handler and NAPI, then
2204 * disable interrupts. We have to clear the
2205 * irq_enabled flag first to ensure that interrupts
2206 * won't be re-enabled.
2207 */
2208 mdp->irq_enabled = false;
525b8075 2209 synchronize_irq(ndev->irq);
283e38db 2210 napi_synchronize(&mdp->napi);
525b8075 2211 sh_eth_write(ndev, 0x0000, EESIPR);
525b8075 2212
740c7f31 2213 sh_eth_dev_exit(ndev);
525b8075 2214
8e03a5e7 2215 /* Free all the skbuffs in the Rx queue and the DMA buffers. */
084236d8 2216 sh_eth_ring_free(ndev);
084236d8 2217 }
525b8075
YS
2218
2219 /* Set new parameters */
2220 mdp->num_rx_ring = ring->rx_pending;
2221 mdp->num_tx_ring = ring->tx_pending;
2222
525b8075 2223 if (netif_running(ndev)) {
084236d8
BH
2224 ret = sh_eth_ring_init(ndev);
2225 if (ret < 0) {
2226 netdev_err(ndev, "%s: sh_eth_ring_init failed.\n",
2227 __func__);
2228 return ret;
2229 }
2230 ret = sh_eth_dev_init(ndev, false);
2231 if (ret < 0) {
2232 netdev_err(ndev, "%s: sh_eth_dev_init failed.\n",
2233 __func__);
2234 return ret;
2235 }
2236
283e38db 2237 mdp->irq_enabled = true;
525b8075
YS
2238 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
2239 /* Setting the Rx mode will start the Rx process. */
2240 sh_eth_write(ndev, EDRRR_R, EDRRR);
bd888916 2241 netif_device_attach(ndev);
525b8075
YS
2242 }
2243
2244 return 0;
2245}
2246
9b07be4b 2247static const struct ethtool_ops sh_eth_ethtool_ops = {
dc19e4e5
NI
2248 .get_settings = sh_eth_get_settings,
2249 .set_settings = sh_eth_set_settings,
6b4b4fea
BH
2250 .get_regs_len = sh_eth_get_regs_len,
2251 .get_regs = sh_eth_get_regs,
9b07be4b 2252 .nway_reset = sh_eth_nway_reset,
dc19e4e5
NI
2253 .get_msglevel = sh_eth_get_msglevel,
2254 .set_msglevel = sh_eth_set_msglevel,
9b07be4b 2255 .get_link = ethtool_op_get_link,
dc19e4e5
NI
2256 .get_strings = sh_eth_get_strings,
2257 .get_ethtool_stats = sh_eth_get_ethtool_stats,
2258 .get_sset_count = sh_eth_get_sset_count,
525b8075
YS
2259 .get_ringparam = sh_eth_get_ringparam,
2260 .set_ringparam = sh_eth_set_ringparam,
dc19e4e5
NI
2261};
2262
86a74ff2
NI
2263/* network device open function */
2264static int sh_eth_open(struct net_device *ndev)
2265{
2266 int ret = 0;
2267 struct sh_eth_private *mdp = netdev_priv(ndev);
2268
bcd5149d
MD
2269 pm_runtime_get_sync(&mdp->pdev->dev);
2270
d2779e99
SS
2271 napi_enable(&mdp->napi);
2272
a0607fd3 2273 ret = request_irq(ndev->irq, sh_eth_interrupt,
5b3dfd13 2274 mdp->cd->irq_flags, ndev->name, ndev);
86a74ff2 2275 if (ret) {
da246855 2276 netdev_err(ndev, "Can not assign IRQ number\n");
d2779e99 2277 goto out_napi_off;
86a74ff2
NI
2278 }
2279
2280 /* Descriptor set */
2281 ret = sh_eth_ring_init(ndev);
2282 if (ret)
2283 goto out_free_irq;
2284
2285 /* device init */
525b8075 2286 ret = sh_eth_dev_init(ndev, true);
86a74ff2
NI
2287 if (ret)
2288 goto out_free_irq;
2289
2290 /* PHY control start*/
2291 ret = sh_eth_phy_start(ndev);
2292 if (ret)
2293 goto out_free_irq;
2294
7fa2955f
MK
2295 mdp->is_opened = 1;
2296
86a74ff2
NI
2297 return ret;
2298
2299out_free_irq:
2300 free_irq(ndev->irq, ndev);
d2779e99
SS
2301out_napi_off:
2302 napi_disable(&mdp->napi);
bcd5149d 2303 pm_runtime_put_sync(&mdp->pdev->dev);
86a74ff2
NI
2304 return ret;
2305}
2306
2307/* Timeout function */
2308static void sh_eth_tx_timeout(struct net_device *ndev)
2309{
2310 struct sh_eth_private *mdp = netdev_priv(ndev);
86a74ff2
NI
2311 struct sh_eth_rxdesc *rxdesc;
2312 int i;
2313
2314 netif_stop_queue(ndev);
2315
8d5009f6
SS
2316 netif_err(mdp, timer, ndev,
2317 "transmit timed out, status %8.8x, resetting...\n",
0799c2d6 2318 sh_eth_read(ndev, EESR));
86a74ff2
NI
2319
2320 /* tx_errors count up */
bb7d92e3 2321 ndev->stats.tx_errors++;
86a74ff2 2322
86a74ff2 2323 /* Free all the skbuffs in the Rx queue. */
525b8075 2324 for (i = 0; i < mdp->num_rx_ring; i++) {
86a74ff2 2325 rxdesc = &mdp->rx_ring[i];
1299653a
SS
2326 rxdesc->status = cpu_to_edmac(mdp, 0);
2327 rxdesc->addr = cpu_to_edmac(mdp, 0xBADF00D0);
179d80af 2328 dev_kfree_skb(mdp->rx_skbuff[i]);
86a74ff2
NI
2329 mdp->rx_skbuff[i] = NULL;
2330 }
525b8075 2331 for (i = 0; i < mdp->num_tx_ring; i++) {
179d80af 2332 dev_kfree_skb(mdp->tx_skbuff[i]);
86a74ff2
NI
2333 mdp->tx_skbuff[i] = NULL;
2334 }
2335
2336 /* device init */
525b8075 2337 sh_eth_dev_init(ndev, true);
86a74ff2
NI
2338}
2339
2340/* Packet transmit function */
2341static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
2342{
2343 struct sh_eth_private *mdp = netdev_priv(ndev);
2344 struct sh_eth_txdesc *txdesc;
1299653a 2345 dma_addr_t dma_addr;
86a74ff2 2346 u32 entry;
fb5e2f9b 2347 unsigned long flags;
86a74ff2
NI
2348
2349 spin_lock_irqsave(&mdp->lock, flags);
525b8075 2350 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
86a74ff2 2351 if (!sh_eth_txfree(ndev)) {
8d5009f6 2352 netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n");
86a74ff2
NI
2353 netif_stop_queue(ndev);
2354 spin_unlock_irqrestore(&mdp->lock, flags);
5b548140 2355 return NETDEV_TX_BUSY;
86a74ff2
NI
2356 }
2357 }
2358 spin_unlock_irqrestore(&mdp->lock, flags);
2359
dacc73e0 2360 if (skb_put_padto(skb, ETH_ZLEN))
eebfb643
BH
2361 return NETDEV_TX_OK;
2362
525b8075 2363 entry = mdp->cur_tx % mdp->num_tx_ring;
86a74ff2
NI
2364 mdp->tx_skbuff[entry] = skb;
2365 txdesc = &mdp->tx_ring[entry];
86a74ff2 2366 /* soft swap. */
380af9e3 2367 if (!mdp->cd->hw_swap)
3e230993 2368 sh_eth_soft_swap(PTR_ALIGN(skb->data, 4), skb->len + 2);
1299653a
SS
2369 dma_addr = dma_map_single(&ndev->dev, skb->data, skb->len,
2370 DMA_TO_DEVICE);
2371 if (dma_mapping_error(&ndev->dev, dma_addr)) {
aa3933b8
BH
2372 kfree_skb(skb);
2373 return NETDEV_TX_OK;
2374 }
1299653a 2375 txdesc->addr = cpu_to_edmac(mdp, dma_addr);
5cbf20c7 2376 txdesc->len = cpu_to_edmac(mdp, skb->len << 16);
86a74ff2 2377
f32bfb9a 2378 dma_wmb(); /* TACT bit must be set after all the above writes */
525b8075 2379 if (entry >= mdp->num_tx_ring - 1)
71557a37 2380 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
86a74ff2 2381 else
71557a37 2382 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
86a74ff2
NI
2383
2384 mdp->cur_tx++;
2385
c5ed5368
YS
2386 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
2387 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
b0ca2a21 2388
6ed10654 2389 return NETDEV_TX_OK;
86a74ff2
NI
2390}
2391
4398f9c8
BH
2392/* The statistics registers have write-clear behaviour, which means we
2393 * will lose any increment between the read and write. We mitigate
2394 * this by only clearing when we read a non-zero value, so we will
2395 * never falsely report a total of zero.
2396 */
2397static void
2398sh_eth_update_stat(struct net_device *ndev, unsigned long *stat, int reg)
2399{
2400 u32 delta = sh_eth_read(ndev, reg);
2401
2402 if (delta) {
2403 *stat += delta;
2404 sh_eth_write(ndev, 0, reg);
2405 }
2406}
2407
7fa2955f
MK
2408static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
2409{
2410 struct sh_eth_private *mdp = netdev_priv(ndev);
2411
2412 if (sh_eth_is_rz_fast_ether(mdp))
2413 return &ndev->stats;
2414
2415 if (!mdp->is_opened)
2416 return &ndev->stats;
2417
4398f9c8
BH
2418 sh_eth_update_stat(ndev, &ndev->stats.tx_dropped, TROCR);
2419 sh_eth_update_stat(ndev, &ndev->stats.collisions, CDCR);
2420 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, LCCR);
7fa2955f
MK
2421
2422 if (sh_eth_is_gether(mdp)) {
4398f9c8
BH
2423 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2424 CERCR);
2425 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2426 CEECR);
7fa2955f 2427 } else {
4398f9c8
BH
2428 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2429 CNDCR);
7fa2955f
MK
2430 }
2431
2432 return &ndev->stats;
2433}
2434
86a74ff2
NI
2435/* device close function */
2436static int sh_eth_close(struct net_device *ndev)
2437{
2438 struct sh_eth_private *mdp = netdev_priv(ndev);
86a74ff2
NI
2439
2440 netif_stop_queue(ndev);
2441
283e38db
BH
2442 /* Serialise with the interrupt handler and NAPI, then disable
2443 * interrupts. We have to clear the irq_enabled flag first to
2444 * ensure that interrupts won't be re-enabled.
2445 */
2446 mdp->irq_enabled = false;
2447 synchronize_irq(ndev->irq);
2448 napi_disable(&mdp->napi);
4a55530f 2449 sh_eth_write(ndev, 0x0000, EESIPR);
86a74ff2 2450
740c7f31 2451 sh_eth_dev_exit(ndev);
86a74ff2
NI
2452
2453 /* PHY Disconnect */
2454 if (mdp->phydev) {
2455 phy_stop(mdp->phydev);
2456 phy_disconnect(mdp->phydev);
4f9dce23 2457 mdp->phydev = NULL;
86a74ff2
NI
2458 }
2459
2460 free_irq(ndev->irq, ndev);
2461
8e03a5e7 2462 /* Free all the skbuffs in the Rx queue and the DMA buffer. */
86a74ff2
NI
2463 sh_eth_ring_free(ndev);
2464
bcd5149d
MD
2465 pm_runtime_put_sync(&mdp->pdev->dev);
2466
7fa2955f 2467 mdp->is_opened = 0;
bcd5149d 2468
7fa2955f 2469 return 0;
86a74ff2
NI
2470}
2471
bb7d92e3 2472/* ioctl to device function */
128296fc 2473static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
86a74ff2
NI
2474{
2475 struct sh_eth_private *mdp = netdev_priv(ndev);
2476 struct phy_device *phydev = mdp->phydev;
2477
2478 if (!netif_running(ndev))
2479 return -EINVAL;
2480
2481 if (!phydev)
2482 return -ENODEV;
2483
28b04113 2484 return phy_mii_ioctl(phydev, rq, cmd);
86a74ff2
NI
2485}
2486
6743fe6d
YS
2487/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2488static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2489 int entry)
2490{
2491 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2492}
2493
2494static u32 sh_eth_tsu_get_post_mask(int entry)
2495{
2496 return 0x0f << (28 - ((entry % 8) * 4));
2497}
2498
2499static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2500{
2501 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2502}
2503
2504static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2505 int entry)
2506{
2507 struct sh_eth_private *mdp = netdev_priv(ndev);
2508 u32 tmp;
2509 void *reg_offset;
2510
2511 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2512 tmp = ioread32(reg_offset);
2513 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2514}
2515
2516static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2517 int entry)
2518{
2519 struct sh_eth_private *mdp = netdev_priv(ndev);
2520 u32 post_mask, ref_mask, tmp;
2521 void *reg_offset;
2522
2523 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2524 post_mask = sh_eth_tsu_get_post_mask(entry);
2525 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2526
2527 tmp = ioread32(reg_offset);
2528 iowrite32(tmp & ~post_mask, reg_offset);
2529
2530 /* If other port enables, the function returns "true" */
2531 return tmp & ref_mask;
2532}
2533
2534static int sh_eth_tsu_busy(struct net_device *ndev)
2535{
2536 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2537 struct sh_eth_private *mdp = netdev_priv(ndev);
2538
2539 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2540 udelay(10);
2541 timeout--;
2542 if (timeout <= 0) {
da246855 2543 netdev_err(ndev, "%s: timeout\n", __func__);
6743fe6d
YS
2544 return -ETIMEDOUT;
2545 }
2546 }
2547
2548 return 0;
2549}
2550
2551static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2552 const u8 *addr)
2553{
2554 u32 val;
2555
2556 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2557 iowrite32(val, reg);
2558 if (sh_eth_tsu_busy(ndev) < 0)
2559 return -EBUSY;
2560
2561 val = addr[4] << 8 | addr[5];
2562 iowrite32(val, reg + 4);
2563 if (sh_eth_tsu_busy(ndev) < 0)
2564 return -EBUSY;
2565
2566 return 0;
2567}
2568
2569static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2570{
2571 u32 val;
2572
2573 val = ioread32(reg);
2574 addr[0] = (val >> 24) & 0xff;
2575 addr[1] = (val >> 16) & 0xff;
2576 addr[2] = (val >> 8) & 0xff;
2577 addr[3] = val & 0xff;
2578 val = ioread32(reg + 4);
2579 addr[4] = (val >> 8) & 0xff;
2580 addr[5] = val & 0xff;
2581}
2582
2583
2584static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2585{
2586 struct sh_eth_private *mdp = netdev_priv(ndev);
2587 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2588 int i;
2589 u8 c_addr[ETH_ALEN];
2590
2591 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2592 sh_eth_tsu_read_entry(reg_offset, c_addr);
c4bde29c 2593 if (ether_addr_equal(addr, c_addr))
6743fe6d
YS
2594 return i;
2595 }
2596
2597 return -ENOENT;
2598}
2599
2600static int sh_eth_tsu_find_empty(struct net_device *ndev)
2601{
2602 u8 blank[ETH_ALEN];
2603 int entry;
2604
2605 memset(blank, 0, sizeof(blank));
2606 entry = sh_eth_tsu_find_entry(ndev, blank);
2607 return (entry < 0) ? -ENOMEM : entry;
2608}
2609
2610static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2611 int entry)
2612{
2613 struct sh_eth_private *mdp = netdev_priv(ndev);
2614 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2615 int ret;
2616 u8 blank[ETH_ALEN];
2617
2618 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2619 ~(1 << (31 - entry)), TSU_TEN);
2620
2621 memset(blank, 0, sizeof(blank));
2622 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2623 if (ret < 0)
2624 return ret;
2625 return 0;
2626}
2627
2628static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2629{
2630 struct sh_eth_private *mdp = netdev_priv(ndev);
2631 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2632 int i, ret;
2633
2634 if (!mdp->cd->tsu)
2635 return 0;
2636
2637 i = sh_eth_tsu_find_entry(ndev, addr);
2638 if (i < 0) {
2639 /* No entry found, create one */
2640 i = sh_eth_tsu_find_empty(ndev);
2641 if (i < 0)
2642 return -ENOMEM;
2643 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2644 if (ret < 0)
2645 return ret;
2646
2647 /* Enable the entry */
2648 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2649 (1 << (31 - i)), TSU_TEN);
2650 }
2651
2652 /* Entry found or created, enable POST */
2653 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2654
2655 return 0;
2656}
2657
2658static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2659{
2660 struct sh_eth_private *mdp = netdev_priv(ndev);
2661 int i, ret;
2662
2663 if (!mdp->cd->tsu)
2664 return 0;
2665
2666 i = sh_eth_tsu_find_entry(ndev, addr);
2667 if (i) {
2668 /* Entry found */
2669 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2670 goto done;
2671
2672 /* Disable the entry if both ports was disabled */
2673 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2674 if (ret < 0)
2675 return ret;
2676 }
2677done:
2678 return 0;
2679}
2680
2681static int sh_eth_tsu_purge_all(struct net_device *ndev)
2682{
2683 struct sh_eth_private *mdp = netdev_priv(ndev);
2684 int i, ret;
2685
b37feed7 2686 if (!mdp->cd->tsu)
6743fe6d
YS
2687 return 0;
2688
2689 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2690 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2691 continue;
2692
2693 /* Disable the entry if both ports was disabled */
2694 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2695 if (ret < 0)
2696 return ret;
2697 }
2698
2699 return 0;
2700}
2701
2702static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2703{
2704 struct sh_eth_private *mdp = netdev_priv(ndev);
2705 u8 addr[ETH_ALEN];
2706 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2707 int i;
2708
b37feed7 2709 if (!mdp->cd->tsu)
6743fe6d
YS
2710 return;
2711
2712 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2713 sh_eth_tsu_read_entry(reg_offset, addr);
2714 if (is_multicast_ether_addr(addr))
2715 sh_eth_tsu_del_entry(ndev, addr);
2716 }
2717}
2718
b37feed7
BH
2719/* Update promiscuous flag and multicast filter */
2720static void sh_eth_set_rx_mode(struct net_device *ndev)
86a74ff2 2721{
6743fe6d
YS
2722 struct sh_eth_private *mdp = netdev_priv(ndev);
2723 u32 ecmr_bits;
2724 int mcast_all = 0;
2725 unsigned long flags;
2726
2727 spin_lock_irqsave(&mdp->lock, flags);
128296fc 2728 /* Initial condition is MCT = 1, PRM = 0.
6743fe6d
YS
2729 * Depending on ndev->flags, set PRM or clear MCT
2730 */
b37feed7
BH
2731 ecmr_bits = sh_eth_read(ndev, ECMR) & ~ECMR_PRM;
2732 if (mdp->cd->tsu)
2733 ecmr_bits |= ECMR_MCT;
6743fe6d
YS
2734
2735 if (!(ndev->flags & IFF_MULTICAST)) {
2736 sh_eth_tsu_purge_mcast(ndev);
2737 mcast_all = 1;
2738 }
2739 if (ndev->flags & IFF_ALLMULTI) {
2740 sh_eth_tsu_purge_mcast(ndev);
2741 ecmr_bits &= ~ECMR_MCT;
2742 mcast_all = 1;
2743 }
2744
86a74ff2 2745 if (ndev->flags & IFF_PROMISC) {
6743fe6d
YS
2746 sh_eth_tsu_purge_all(ndev);
2747 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2748 } else if (mdp->cd->tsu) {
2749 struct netdev_hw_addr *ha;
2750 netdev_for_each_mc_addr(ha, ndev) {
2751 if (mcast_all && is_multicast_ether_addr(ha->addr))
2752 continue;
2753
2754 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2755 if (!mcast_all) {
2756 sh_eth_tsu_purge_mcast(ndev);
2757 ecmr_bits &= ~ECMR_MCT;
2758 mcast_all = 1;
2759 }
2760 }
2761 }
86a74ff2 2762 }
6743fe6d
YS
2763
2764 /* update the ethernet mode */
2765 sh_eth_write(ndev, ecmr_bits, ECMR);
2766
2767 spin_unlock_irqrestore(&mdp->lock, flags);
86a74ff2 2768}
71cc7c37
YS
2769
2770static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2771{
2772 if (!mdp->port)
2773 return TSU_VTAG0;
2774 else
2775 return TSU_VTAG1;
2776}
2777
80d5c368
PM
2778static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2779 __be16 proto, u16 vid)
71cc7c37
YS
2780{
2781 struct sh_eth_private *mdp = netdev_priv(ndev);
2782 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2783
2784 if (unlikely(!mdp->cd->tsu))
2785 return -EPERM;
2786
2787 /* No filtering if vid = 0 */
2788 if (!vid)
2789 return 0;
2790
2791 mdp->vlan_num_ids++;
2792
128296fc 2793 /* The controller has one VLAN tag HW filter. So, if the filter is
71cc7c37
YS
2794 * already enabled, the driver disables it and the filte
2795 */
2796 if (mdp->vlan_num_ids > 1) {
2797 /* disable VLAN filter */
2798 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2799 return 0;
2800 }
2801
2802 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2803 vtag_reg_index);
2804
2805 return 0;
2806}
2807
80d5c368
PM
2808static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2809 __be16 proto, u16 vid)
71cc7c37
YS
2810{
2811 struct sh_eth_private *mdp = netdev_priv(ndev);
2812 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2813
2814 if (unlikely(!mdp->cd->tsu))
2815 return -EPERM;
2816
2817 /* No filtering if vid = 0 */
2818 if (!vid)
2819 return 0;
2820
2821 mdp->vlan_num_ids--;
2822 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2823
2824 return 0;
2825}
86a74ff2
NI
2826
2827/* SuperH's TSU register init function */
4a55530f 2828static void sh_eth_tsu_init(struct sh_eth_private *mdp)
86a74ff2 2829{
db893473
SH
2830 if (sh_eth_is_rz_fast_ether(mdp)) {
2831 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2832 return;
2833 }
2834
4a55530f
YS
2835 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2836 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2837 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2838 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2839 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2840 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2841 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2842 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2843 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2844 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
c5ed5368
YS
2845 if (sh_eth_is_gether(mdp)) {
2846 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2847 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2848 } else {
2849 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2850 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2851 }
4a55530f
YS
2852 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2853 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2854 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2855 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2856 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2857 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2858 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
86a74ff2
NI
2859}
2860
2861/* MDIO bus release function */
bd920ff5 2862static int sh_mdio_release(struct sh_eth_private *mdp)
86a74ff2 2863{
86a74ff2 2864 /* unregister mdio bus */
bd920ff5 2865 mdiobus_unregister(mdp->mii_bus);
86a74ff2
NI
2866
2867 /* free bitbang info */
bd920ff5 2868 free_mdio_bitbang(mdp->mii_bus);
86a74ff2
NI
2869
2870 return 0;
2871}
2872
2873/* MDIO bus init function */
bd920ff5 2874static int sh_mdio_init(struct sh_eth_private *mdp,
b3017e6a 2875 struct sh_eth_plat_data *pd)
86a74ff2
NI
2876{
2877 int ret, i;
2878 struct bb_info *bitbang;
bd920ff5 2879 struct platform_device *pdev = mdp->pdev;
aa8d4225 2880 struct device *dev = &mdp->pdev->dev;
86a74ff2
NI
2881
2882 /* create bit control struct for PHY */
aa8d4225 2883 bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL);
f738a13d
LP
2884 if (!bitbang)
2885 return -ENOMEM;
86a74ff2
NI
2886
2887 /* bitbang init */
ae70644d 2888 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
b3017e6a 2889 bitbang->set_gate = pd->set_mdio_gate;
86a74ff2
NI
2890 bitbang->ctrl.ops = &bb_ops;
2891
c2e07b3a 2892 /* MII controller setting */
86a74ff2 2893 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
f738a13d
LP
2894 if (!mdp->mii_bus)
2895 return -ENOMEM;
86a74ff2
NI
2896
2897 /* Hook up MII support for ethtool */
2898 mdp->mii_bus->name = "sh_mii";
a5bd6060 2899 mdp->mii_bus->parent = dev;
5278fb54 2900 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
bd920ff5 2901 pdev->name, pdev->id);
86a74ff2
NI
2902
2903 /* PHY IRQ */
86b5d251
SS
2904 mdp->mii_bus->irq = devm_kmalloc_array(dev, PHY_MAX_ADDR, sizeof(int),
2905 GFP_KERNEL);
86a74ff2
NI
2906 if (!mdp->mii_bus->irq) {
2907 ret = -ENOMEM;
2908 goto out_free_bus;
2909 }
2910
bd920ff5
LP
2911 /* register MDIO bus */
2912 if (dev->of_node) {
2913 ret = of_mdiobus_register(mdp->mii_bus, dev->of_node);
702eca02
BD
2914 } else {
2915 for (i = 0; i < PHY_MAX_ADDR; i++)
2916 mdp->mii_bus->irq[i] = PHY_POLL;
2917 if (pd->phy_irq > 0)
2918 mdp->mii_bus->irq[pd->phy] = pd->phy_irq;
2919
2920 ret = mdiobus_register(mdp->mii_bus);
2921 }
2922
86a74ff2 2923 if (ret)
d5e07e69 2924 goto out_free_bus;
86a74ff2 2925
86a74ff2
NI
2926 return 0;
2927
86a74ff2 2928out_free_bus:
298cf9be 2929 free_mdio_bitbang(mdp->mii_bus);
86a74ff2
NI
2930 return ret;
2931}
2932
4a55530f
YS
2933static const u16 *sh_eth_get_register_offset(int register_type)
2934{
2935 const u16 *reg_offset = NULL;
2936
2937 switch (register_type) {
2938 case SH_ETH_REG_GIGABIT:
2939 reg_offset = sh_eth_offset_gigabit;
2940 break;
db893473
SH
2941 case SH_ETH_REG_FAST_RZ:
2942 reg_offset = sh_eth_offset_fast_rz;
2943 break;
a3f109bd
SS
2944 case SH_ETH_REG_FAST_RCAR:
2945 reg_offset = sh_eth_offset_fast_rcar;
2946 break;
4a55530f
YS
2947 case SH_ETH_REG_FAST_SH4:
2948 reg_offset = sh_eth_offset_fast_sh4;
2949 break;
2950 case SH_ETH_REG_FAST_SH3_SH2:
2951 reg_offset = sh_eth_offset_fast_sh3_sh2;
2952 break;
2953 default:
4a55530f
YS
2954 break;
2955 }
2956
2957 return reg_offset;
2958}
2959
8f728d79 2960static const struct net_device_ops sh_eth_netdev_ops = {
ebf84eaa
AB
2961 .ndo_open = sh_eth_open,
2962 .ndo_stop = sh_eth_close,
2963 .ndo_start_xmit = sh_eth_start_xmit,
2964 .ndo_get_stats = sh_eth_get_stats,
b37feed7 2965 .ndo_set_rx_mode = sh_eth_set_rx_mode,
ebf84eaa
AB
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
8f728d79
SS
2973static const struct net_device_ops sh_eth_netdev_ops_tsu = {
2974 .ndo_open = sh_eth_open,
2975 .ndo_stop = sh_eth_close,
2976 .ndo_start_xmit = sh_eth_start_xmit,
2977 .ndo_get_stats = sh_eth_get_stats,
b37feed7 2978 .ndo_set_rx_mode = sh_eth_set_rx_mode,
8f728d79
SS
2979 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
2980 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
2981 .ndo_tx_timeout = sh_eth_tx_timeout,
2982 .ndo_do_ioctl = sh_eth_do_ioctl,
2983 .ndo_validate_addr = eth_validate_addr,
2984 .ndo_set_mac_address = eth_mac_addr,
2985 .ndo_change_mtu = eth_change_mtu,
2986};
2987
b356e978
SS
2988#ifdef CONFIG_OF
2989static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
2990{
2991 struct device_node *np = dev->of_node;
2992 struct sh_eth_plat_data *pdata;
b356e978
SS
2993 const char *mac_addr;
2994
2995 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2996 if (!pdata)
2997 return NULL;
2998
2999 pdata->phy_interface = of_get_phy_mode(np);
3000
b356e978
SS
3001 mac_addr = of_get_mac_address(np);
3002 if (mac_addr)
3003 memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
3004
3005 pdata->no_ether_link =
3006 of_property_read_bool(np, "renesas,no-ether-link");
3007 pdata->ether_link_active_low =
3008 of_property_read_bool(np, "renesas,ether-link-active-low");
3009
3010 return pdata;
3011}
3012
3013static const struct of_device_id sh_eth_match_table[] = {
3014 { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data },
3015 { .compatible = "renesas,ether-r8a7778", .data = &r8a777x_data },
3016 { .compatible = "renesas,ether-r8a7779", .data = &r8a777x_data },
3017 { .compatible = "renesas,ether-r8a7790", .data = &r8a779x_data },
3018 { .compatible = "renesas,ether-r8a7791", .data = &r8a779x_data },
9488e1e5 3019 { .compatible = "renesas,ether-r8a7793", .data = &r8a779x_data },
0f76b9d8 3020 { .compatible = "renesas,ether-r8a7794", .data = &r8a779x_data },
b356e978
SS
3021 { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data },
3022 { }
3023};
3024MODULE_DEVICE_TABLE(of, sh_eth_match_table);
3025#else
3026static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
3027{
3028 return NULL;
3029}
3030#endif
3031
86a74ff2
NI
3032static int sh_eth_drv_probe(struct platform_device *pdev)
3033{
9c38657c 3034 int ret, devno = 0;
86a74ff2
NI
3035 struct resource *res;
3036 struct net_device *ndev = NULL;
ec0d7551 3037 struct sh_eth_private *mdp = NULL;
0b76b862 3038 struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev);
afe391ad 3039 const struct platform_device_id *id = platform_get_device_id(pdev);
86a74ff2
NI
3040
3041 /* get base addr */
3042 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
86a74ff2
NI
3043
3044 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
f738a13d
LP
3045 if (!ndev)
3046 return -ENOMEM;
86a74ff2 3047
b5893a08
BD
3048 pm_runtime_enable(&pdev->dev);
3049 pm_runtime_get_sync(&pdev->dev);
3050
86a74ff2
NI
3051 devno = pdev->id;
3052 if (devno < 0)
3053 devno = 0;
3054
3055 ndev->dma = -1;
cc3c080d 3056 ret = platform_get_irq(pdev, 0);
7a468ac6 3057 if (ret < 0)
86a74ff2 3058 goto out_release;
cc3c080d 3059 ndev->irq = ret;
86a74ff2
NI
3060
3061 SET_NETDEV_DEV(ndev, &pdev->dev);
3062
86a74ff2 3063 mdp = netdev_priv(ndev);
525b8075
YS
3064 mdp->num_tx_ring = TX_RING_SIZE;
3065 mdp->num_rx_ring = RX_RING_SIZE;
d5e07e69
SS
3066 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
3067 if (IS_ERR(mdp->addr)) {
3068 ret = PTR_ERR(mdp->addr);
ae70644d
YS
3069 goto out_release;
3070 }
3071
c960804f
VB
3072 ndev->base_addr = res->start;
3073
86a74ff2 3074 spin_lock_init(&mdp->lock);
bcd5149d 3075 mdp->pdev = pdev;
86a74ff2 3076
b356e978
SS
3077 if (pdev->dev.of_node)
3078 pd = sh_eth_parse_dt(&pdev->dev);
3b4c5cbf
SS
3079 if (!pd) {
3080 dev_err(&pdev->dev, "no platform data\n");
3081 ret = -EINVAL;
3082 goto out_release;
3083 }
3084
86a74ff2 3085 /* get PHY ID */
71557a37 3086 mdp->phy_id = pd->phy;
e47c9052 3087 mdp->phy_interface = pd->phy_interface;
4923576b
YS
3088 mdp->no_ether_link = pd->no_ether_link;
3089 mdp->ether_link_active_low = pd->ether_link_active_low;
86a74ff2 3090
380af9e3 3091 /* set cpu data */
b356e978
SS
3092 if (id) {
3093 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
3094 } else {
3095 const struct of_device_id *match;
3096
3097 match = of_match_device(of_match_ptr(sh_eth_match_table),
3098 &pdev->dev);
3099 mdp->cd = (struct sh_eth_cpu_data *)match->data;
3100 }
a3153d8c 3101 mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type);
264be2f5
SS
3102 if (!mdp->reg_offset) {
3103 dev_err(&pdev->dev, "Unknown register type (%d)\n",
3104 mdp->cd->register_type);
3105 ret = -EINVAL;
3106 goto out_release;
3107 }
380af9e3
YS
3108 sh_eth_set_default_cpu_data(mdp->cd);
3109
86a74ff2 3110 /* set function */
8f728d79
SS
3111 if (mdp->cd->tsu)
3112 ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
3113 else
3114 ndev->netdev_ops = &sh_eth_netdev_ops;
7ad24ea4 3115 ndev->ethtool_ops = &sh_eth_ethtool_ops;
86a74ff2
NI
3116 ndev->watchdog_timeo = TX_TIMEOUT;
3117
dc19e4e5
NI
3118 /* debug message level */
3119 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
86a74ff2
NI
3120
3121 /* read and set MAC address */
748031f9 3122 read_mac_address(ndev, pd->mac_addr);
ff6e7228
SS
3123 if (!is_valid_ether_addr(ndev->dev_addr)) {
3124 dev_warn(&pdev->dev,
3125 "no valid MAC address supplied, using a random one.\n");
3126 eth_hw_addr_random(ndev);
3127 }
86a74ff2 3128
6ba88021
YS
3129 /* ioremap the TSU registers */
3130 if (mdp->cd->tsu) {
3131 struct resource *rtsu;
3132 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
d5e07e69
SS
3133 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
3134 if (IS_ERR(mdp->tsu_addr)) {
3135 ret = PTR_ERR(mdp->tsu_addr);
fc0c0900
SS
3136 goto out_release;
3137 }
6743fe6d 3138 mdp->port = devno % 2;
f646968f 3139 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
6ba88021
YS
3140 }
3141
150647fb
YS
3142 /* initialize first or needed device */
3143 if (!devno || pd->needs_init) {
380af9e3
YS
3144 if (mdp->cd->chip_reset)
3145 mdp->cd->chip_reset(ndev);
86a74ff2 3146
4986b996
YS
3147 if (mdp->cd->tsu) {
3148 /* TSU init (Init only)*/
3149 sh_eth_tsu_init(mdp);
3150 }
86a74ff2
NI
3151 }
3152
966d6dbb
HN
3153 if (mdp->cd->rmiimode)
3154 sh_eth_write(ndev, 0x1, RMIIMODE);
3155
daacf03f
LP
3156 /* MDIO bus init */
3157 ret = sh_mdio_init(mdp, pd);
3158 if (ret) {
3159 dev_err(&ndev->dev, "failed to initialise MDIO\n");
3160 goto out_release;
3161 }
3162
3719109d
SS
3163 netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
3164
86a74ff2
NI
3165 /* network device register */
3166 ret = register_netdev(ndev);
3167 if (ret)
3719109d 3168 goto out_napi_del;
86a74ff2 3169
25985edc 3170 /* print device information */
f75f14ec
SS
3171 netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n",
3172 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
86a74ff2 3173
b5893a08 3174 pm_runtime_put(&pdev->dev);
86a74ff2
NI
3175 platform_set_drvdata(pdev, ndev);
3176
3177 return ret;
3178
3719109d
SS
3179out_napi_del:
3180 netif_napi_del(&mdp->napi);
daacf03f 3181 sh_mdio_release(mdp);
3719109d 3182
86a74ff2
NI
3183out_release:
3184 /* net_dev free */
3185 if (ndev)
3186 free_netdev(ndev);
3187
b5893a08
BD
3188 pm_runtime_put(&pdev->dev);
3189 pm_runtime_disable(&pdev->dev);
86a74ff2
NI
3190 return ret;
3191}
3192
3193static int sh_eth_drv_remove(struct platform_device *pdev)
3194{
3195 struct net_device *ndev = platform_get_drvdata(pdev);
3719109d 3196 struct sh_eth_private *mdp = netdev_priv(ndev);
86a74ff2 3197
86a74ff2 3198 unregister_netdev(ndev);
3719109d 3199 netif_napi_del(&mdp->napi);
daacf03f 3200 sh_mdio_release(mdp);
bcd5149d 3201 pm_runtime_disable(&pdev->dev);
86a74ff2 3202 free_netdev(ndev);
86a74ff2
NI
3203
3204 return 0;
3205}
3206
540ad1b8 3207#ifdef CONFIG_PM
b71af046
MU
3208#ifdef CONFIG_PM_SLEEP
3209static int sh_eth_suspend(struct device *dev)
3210{
3211 struct net_device *ndev = dev_get_drvdata(dev);
3212 int ret = 0;
3213
3214 if (netif_running(ndev)) {
3215 netif_device_detach(ndev);
3216 ret = sh_eth_close(ndev);
3217 }
3218
3219 return ret;
3220}
3221
3222static int sh_eth_resume(struct device *dev)
3223{
3224 struct net_device *ndev = dev_get_drvdata(dev);
3225 int ret = 0;
3226
3227 if (netif_running(ndev)) {
3228 ret = sh_eth_open(ndev);
3229 if (ret < 0)
3230 return ret;
3231 netif_device_attach(ndev);
3232 }
3233
3234 return ret;
3235}
3236#endif
3237
bcd5149d
MD
3238static int sh_eth_runtime_nop(struct device *dev)
3239{
128296fc 3240 /* Runtime PM callback shared between ->runtime_suspend()
bcd5149d
MD
3241 * and ->runtime_resume(). Simply returns success.
3242 *
3243 * This driver re-initializes all registers after
3244 * pm_runtime_get_sync() anyway so there is no need
3245 * to save and restore registers here.
3246 */
3247 return 0;
3248}
3249
540ad1b8 3250static const struct dev_pm_ops sh_eth_dev_pm_ops = {
b71af046 3251 SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend, sh_eth_resume)
e7d7e898 3252 SET_RUNTIME_PM_OPS(sh_eth_runtime_nop, sh_eth_runtime_nop, NULL)
bcd5149d 3253};
540ad1b8
NI
3254#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3255#else
3256#define SH_ETH_PM_OPS NULL
3257#endif
bcd5149d 3258
afe391ad 3259static struct platform_device_id sh_eth_id_table[] = {
c18a79ab 3260 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
7bbe150d 3261 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
9c3beaab 3262 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
f5d12767 3263 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
24549e2a
SS
3264 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
3265 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
f5d12767 3266 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
afe391ad
SS
3267 { }
3268};
3269MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
3270
86a74ff2
NI
3271static struct platform_driver sh_eth_driver = {
3272 .probe = sh_eth_drv_probe,
3273 .remove = sh_eth_drv_remove,
afe391ad 3274 .id_table = sh_eth_id_table,
86a74ff2
NI
3275 .driver = {
3276 .name = CARDNAME,
540ad1b8 3277 .pm = SH_ETH_PM_OPS,
b356e978 3278 .of_match_table = of_match_ptr(sh_eth_match_table),
86a74ff2
NI
3279 },
3280};
3281
db62f684 3282module_platform_driver(sh_eth_driver);
86a74ff2
NI
3283
3284MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3285MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3286MODULE_LICENSE("GPL v2");