blob: f45331ed90b01917ae8f256a8ae14b422e8f8bf8 [file] [log] [blame]
Wolfram Sang00e1cae2018-08-22 00:02:19 +02001// SPDX-License-Identifier: GPL-2.0
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002/* SuperH Ethernet device driver
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003 *
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03004 * Copyright (C) 2014 Renesas Electronics Corporation
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +00005 * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
Sergei Shtylyovb356e972014-02-18 03:12:43 +03006 * Copyright (C) 2008-2014 Renesas Solutions Corp.
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03007 * Copyright (C) 2013-2017 Cogent Embedded, Inc.
Ben Dooks702eca02014-03-12 17:47:40 +00008 * Copyright (C) 2014 Codethink Limited
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07009 */
10
Yoshihiro Shimoda06540112011-09-29 17:16:57 +000011#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/spinlock.h>
David S. Miller823dcd22011-08-20 10:39:12 -070014#include <linux/interrupt.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070015#include <linux/dma-mapping.h>
16#include <linux/etherdevice.h>
17#include <linux/delay.h>
18#include <linux/platform_device.h>
19#include <linux/mdio-bitbang.h>
20#include <linux/netdevice.h>
Sergei Shtylyovb356e972014-02-18 03:12:43 +030021#include <linux/of.h>
22#include <linux/of_device.h>
23#include <linux/of_irq.h>
24#include <linux/of_net.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070025#include <linux/phy.h>
26#include <linux/cache.h>
27#include <linux/io.h>
Magnus Dammbcd51492009-10-09 00:20:04 +000028#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000030#include <linux/ethtool.h>
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +000031#include <linux/if_vlan.h>
Yoshihiro Shimodad4fa0e32011-09-27 21:49:12 +000032#include <linux/sh_eth.h>
Ben Dooks702eca02014-03-12 17:47:40 +000033#include <linux/of_mdio.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070034
35#include "sh_eth.h"
36
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000037#define SH_ETH_DEF_MSG_ENABLE \
38 (NETIF_MSG_LINK | \
39 NETIF_MSG_TIMER | \
40 NETIF_MSG_RX_ERR| \
41 NETIF_MSG_TX_ERR)
42
Sergei Shtylyov2274d372015-12-13 01:44:50 +030043#define SH_ETH_OFFSET_INVALID ((u16)~0)
44
Ben Hutchings33657112015-02-26 20:34:14 +000045#define SH_ETH_OFFSET_DEFAULTS \
46 [0 ... SH_ETH_MAX_REGISTER_OFFSET - 1] = SH_ETH_OFFSET_INVALID
47
Sergei Shtylyovc0013f62013-03-28 11:48:26 +000048static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +000049 SH_ETH_OFFSET_DEFAULTS,
50
Sergei Shtylyovc0013f62013-03-28 11:48:26 +000051 [EDSR] = 0x0000,
52 [EDMR] = 0x0400,
53 [EDTRR] = 0x0408,
54 [EDRRR] = 0x0410,
55 [EESR] = 0x0428,
56 [EESIPR] = 0x0430,
57 [TDLAR] = 0x0010,
58 [TDFAR] = 0x0014,
59 [TDFXR] = 0x0018,
60 [TDFFR] = 0x001c,
61 [RDLAR] = 0x0030,
62 [RDFAR] = 0x0034,
63 [RDFXR] = 0x0038,
64 [RDFFR] = 0x003c,
65 [TRSCER] = 0x0438,
66 [RMFCR] = 0x0440,
67 [TFTR] = 0x0448,
68 [FDR] = 0x0450,
69 [RMCR] = 0x0458,
70 [RPADIR] = 0x0460,
71 [FCFTR] = 0x0468,
72 [CSMR] = 0x04E4,
73
74 [ECMR] = 0x0500,
75 [ECSR] = 0x0510,
76 [ECSIPR] = 0x0518,
77 [PIR] = 0x0520,
78 [PSR] = 0x0528,
79 [PIPR] = 0x052c,
80 [RFLR] = 0x0508,
81 [APR] = 0x0554,
82 [MPR] = 0x0558,
83 [PFTCR] = 0x055c,
84 [PFRCR] = 0x0560,
85 [TPAUSER] = 0x0564,
86 [GECMR] = 0x05b0,
87 [BCULR] = 0x05b4,
88 [MAHR] = 0x05c0,
89 [MALR] = 0x05c8,
90 [TROCR] = 0x0700,
91 [CDCR] = 0x0708,
92 [LCCR] = 0x0710,
93 [CEFCR] = 0x0740,
94 [FRECR] = 0x0748,
95 [TSFRCR] = 0x0750,
96 [TLFRCR] = 0x0758,
97 [RFCR] = 0x0760,
98 [CERCR] = 0x0768,
99 [CEECR] = 0x0770,
100 [MAFCR] = 0x0778,
101 [RMII_MII] = 0x0790,
102
103 [ARSTR] = 0x0000,
104 [TSU_CTRST] = 0x0004,
105 [TSU_FWEN0] = 0x0010,
106 [TSU_FWEN1] = 0x0014,
107 [TSU_FCM] = 0x0018,
108 [TSU_BSYSL0] = 0x0020,
109 [TSU_BSYSL1] = 0x0024,
110 [TSU_PRISL0] = 0x0028,
111 [TSU_PRISL1] = 0x002c,
112 [TSU_FWSL0] = 0x0030,
113 [TSU_FWSL1] = 0x0034,
114 [TSU_FWSLC] = 0x0038,
Sergei Shtylyov4869a142018-02-24 20:28:16 +0300115 [TSU_QTAGM0] = 0x0040,
116 [TSU_QTAGM1] = 0x0044,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000117 [TSU_FWSR] = 0x0050,
118 [TSU_FWINMK] = 0x0054,
119 [TSU_ADQT0] = 0x0048,
120 [TSU_ADQT1] = 0x004c,
121 [TSU_VTAG0] = 0x0058,
122 [TSU_VTAG1] = 0x005c,
123 [TSU_ADSBSY] = 0x0060,
124 [TSU_TEN] = 0x0064,
125 [TSU_POST1] = 0x0070,
126 [TSU_POST2] = 0x0074,
127 [TSU_POST3] = 0x0078,
128 [TSU_POST4] = 0x007c,
129 [TSU_ADRH0] = 0x0100,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000130
131 [TXNLCR0] = 0x0080,
132 [TXALCR0] = 0x0084,
133 [RXNLCR0] = 0x0088,
134 [RXALCR0] = 0x008c,
135 [FWNLCR0] = 0x0090,
136 [FWALCR0] = 0x0094,
137 [TXNLCR1] = 0x00a0,
Sergei Shtylyov50f3d742018-01-07 00:26:47 +0300138 [TXALCR1] = 0x00a4,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000139 [RXNLCR1] = 0x00a8,
140 [RXALCR1] = 0x00ac,
141 [FWNLCR1] = 0x00b0,
142 [FWALCR1] = 0x00b4,
143};
144
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000145static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000146 SH_ETH_OFFSET_DEFAULTS,
147
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000148 [ECMR] = 0x0300,
149 [RFLR] = 0x0308,
150 [ECSR] = 0x0310,
151 [ECSIPR] = 0x0318,
152 [PIR] = 0x0320,
153 [PSR] = 0x0328,
154 [RDMLR] = 0x0340,
155 [IPGR] = 0x0350,
156 [APR] = 0x0354,
157 [MPR] = 0x0358,
158 [RFCF] = 0x0360,
159 [TPAUSER] = 0x0364,
160 [TPAUSECR] = 0x0368,
161 [MAHR] = 0x03c0,
162 [MALR] = 0x03c8,
163 [TROCR] = 0x03d0,
164 [CDCR] = 0x03d4,
165 [LCCR] = 0x03d8,
166 [CNDCR] = 0x03dc,
167 [CEFCR] = 0x03e4,
168 [FRECR] = 0x03e8,
169 [TSFRCR] = 0x03ec,
170 [TLFRCR] = 0x03f0,
171 [RFCR] = 0x03f4,
172 [MAFCR] = 0x03f8,
173
174 [EDMR] = 0x0200,
175 [EDTRR] = 0x0208,
176 [EDRRR] = 0x0210,
177 [TDLAR] = 0x0218,
178 [RDLAR] = 0x0220,
179 [EESR] = 0x0228,
180 [EESIPR] = 0x0230,
181 [TRSCER] = 0x0238,
182 [RMFCR] = 0x0240,
183 [TFTR] = 0x0248,
184 [FDR] = 0x0250,
185 [RMCR] = 0x0258,
186 [TFUCR] = 0x0264,
187 [RFOCR] = 0x0268,
Simon Horman55754f12013-07-23 10:18:04 +0900188 [RMIIMODE] = 0x026c,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000189 [FCFTR] = 0x0270,
190 [TRIMD] = 0x027c,
191};
192
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000193static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000194 SH_ETH_OFFSET_DEFAULTS,
195
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000196 [ECMR] = 0x0100,
197 [RFLR] = 0x0108,
198 [ECSR] = 0x0110,
199 [ECSIPR] = 0x0118,
200 [PIR] = 0x0120,
201 [PSR] = 0x0128,
202 [RDMLR] = 0x0140,
203 [IPGR] = 0x0150,
204 [APR] = 0x0154,
205 [MPR] = 0x0158,
206 [TPAUSER] = 0x0164,
207 [RFCF] = 0x0160,
208 [TPAUSECR] = 0x0168,
209 [BCFRR] = 0x016c,
210 [MAHR] = 0x01c0,
211 [MALR] = 0x01c8,
212 [TROCR] = 0x01d0,
213 [CDCR] = 0x01d4,
214 [LCCR] = 0x01d8,
215 [CNDCR] = 0x01dc,
216 [CEFCR] = 0x01e4,
217 [FRECR] = 0x01e8,
218 [TSFRCR] = 0x01ec,
219 [TLFRCR] = 0x01f0,
220 [RFCR] = 0x01f4,
221 [MAFCR] = 0x01f8,
222 [RTRATE] = 0x01fc,
223
224 [EDMR] = 0x0000,
225 [EDTRR] = 0x0008,
226 [EDRRR] = 0x0010,
227 [TDLAR] = 0x0018,
228 [RDLAR] = 0x0020,
229 [EESR] = 0x0028,
230 [EESIPR] = 0x0030,
231 [TRSCER] = 0x0038,
232 [RMFCR] = 0x0040,
233 [TFTR] = 0x0048,
234 [FDR] = 0x0050,
235 [RMCR] = 0x0058,
236 [TFUCR] = 0x0064,
237 [RFOCR] = 0x0068,
238 [FCFTR] = 0x0070,
239 [RPADIR] = 0x0078,
240 [TRIMD] = 0x007c,
241 [RBWAR] = 0x00c8,
242 [RDFAR] = 0x00cc,
243 [TBRAR] = 0x00d4,
244 [TDFAR] = 0x00d8,
245};
246
247static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000248 SH_ETH_OFFSET_DEFAULTS,
249
Sergei Shtylyovd8b04262014-06-03 23:42:26 +0400250 [EDMR] = 0x0000,
251 [EDTRR] = 0x0004,
252 [EDRRR] = 0x0008,
253 [TDLAR] = 0x000c,
254 [RDLAR] = 0x0010,
255 [EESR] = 0x0014,
256 [EESIPR] = 0x0018,
257 [TRSCER] = 0x001c,
258 [RMFCR] = 0x0020,
259 [TFTR] = 0x0024,
260 [FDR] = 0x0028,
261 [RMCR] = 0x002c,
262 [EDOCR] = 0x0030,
263 [FCFTR] = 0x0034,
264 [RPADIR] = 0x0038,
265 [TRIMD] = 0x003c,
266 [RBWAR] = 0x0040,
267 [RDFAR] = 0x0044,
268 [TBRAR] = 0x004c,
269 [TDFAR] = 0x0050,
270
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000271 [ECMR] = 0x0160,
272 [ECSR] = 0x0164,
273 [ECSIPR] = 0x0168,
274 [PIR] = 0x016c,
275 [MAHR] = 0x0170,
276 [MALR] = 0x0174,
277 [RFLR] = 0x0178,
278 [PSR] = 0x017c,
279 [TROCR] = 0x0180,
280 [CDCR] = 0x0184,
281 [LCCR] = 0x0188,
282 [CNDCR] = 0x018c,
283 [CEFCR] = 0x0194,
284 [FRECR] = 0x0198,
285 [TSFRCR] = 0x019c,
286 [TLFRCR] = 0x01a0,
287 [RFCR] = 0x01a4,
288 [MAFCR] = 0x01a8,
289 [IPGR] = 0x01b4,
290 [APR] = 0x01b8,
291 [MPR] = 0x01bc,
292 [TPAUSER] = 0x01c4,
293 [BCFR] = 0x01cc,
294
295 [ARSTR] = 0x0000,
296 [TSU_CTRST] = 0x0004,
297 [TSU_FWEN0] = 0x0010,
298 [TSU_FWEN1] = 0x0014,
299 [TSU_FCM] = 0x0018,
300 [TSU_BSYSL0] = 0x0020,
301 [TSU_BSYSL1] = 0x0024,
302 [TSU_PRISL0] = 0x0028,
303 [TSU_PRISL1] = 0x002c,
304 [TSU_FWSL0] = 0x0030,
305 [TSU_FWSL1] = 0x0034,
306 [TSU_FWSLC] = 0x0038,
307 [TSU_QTAGM0] = 0x0040,
308 [TSU_QTAGM1] = 0x0044,
309 [TSU_ADQT0] = 0x0048,
310 [TSU_ADQT1] = 0x004c,
311 [TSU_FWSR] = 0x0050,
312 [TSU_FWINMK] = 0x0054,
313 [TSU_ADSBSY] = 0x0060,
314 [TSU_TEN] = 0x0064,
315 [TSU_POST1] = 0x0070,
316 [TSU_POST2] = 0x0074,
317 [TSU_POST3] = 0x0078,
318 [TSU_POST4] = 0x007c,
319
320 [TXNLCR0] = 0x0080,
321 [TXALCR0] = 0x0084,
322 [RXNLCR0] = 0x0088,
323 [RXALCR0] = 0x008c,
324 [FWNLCR0] = 0x0090,
325 [FWALCR0] = 0x0094,
326 [TXNLCR1] = 0x00a0,
Sergei Shtylyov50f3d742018-01-07 00:26:47 +0300327 [TXALCR1] = 0x00a4,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000328 [RXNLCR1] = 0x00a8,
329 [RXALCR1] = 0x00ac,
330 [FWNLCR1] = 0x00b0,
331 [FWALCR1] = 0x00b4,
332
333 [TSU_ADRH0] = 0x0100,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000334};
335
Ben Hutchings740c7f32015-01-27 00:49:32 +0000336static void sh_eth_rcv_snd_disable(struct net_device *ndev);
337static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev);
338
Sergei Shtylyov2274d372015-12-13 01:44:50 +0300339static void sh_eth_write(struct net_device *ndev, u32 data, int enum_index)
340{
341 struct sh_eth_private *mdp = netdev_priv(ndev);
342 u16 offset = mdp->reg_offset[enum_index];
343
344 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
345 return;
346
347 iowrite32(data, mdp->addr + offset);
348}
349
350static u32 sh_eth_read(struct net_device *ndev, int enum_index)
351{
352 struct sh_eth_private *mdp = netdev_priv(ndev);
353 u16 offset = mdp->reg_offset[enum_index];
354
355 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
356 return ~0U;
357
358 return ioread32(mdp->addr + offset);
359}
360
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300361static void sh_eth_modify(struct net_device *ndev, int enum_index, u32 clear,
362 u32 set)
363{
364 sh_eth_write(ndev, (sh_eth_read(ndev, enum_index) & ~clear) | set,
365 enum_index);
366}
367
Sergei Shtylyov41414f02018-07-23 21:11:19 +0300368static u16 sh_eth_tsu_get_offset(struct sh_eth_private *mdp, int enum_index)
Sergei Shtylyov388c4bb2018-07-23 21:10:02 +0300369{
Sergei Shtylyov41414f02018-07-23 21:11:19 +0300370 return mdp->reg_offset[enum_index];
Sergei Shtylyov388c4bb2018-07-23 21:10:02 +0300371}
372
Sergei Shtylyov55ea8742018-02-27 14:58:16 +0300373static void sh_eth_tsu_write(struct sh_eth_private *mdp, u32 data,
374 int enum_index)
375{
Sergei Shtylyovecbecb02018-07-23 21:12:38 +0300376 u16 offset = sh_eth_tsu_get_offset(mdp, enum_index);
Sergei Shtylyov627a0d22018-05-02 22:55:52 +0300377
378 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
379 return;
380
381 iowrite32(data, mdp->tsu_addr + offset);
Sergei Shtylyov55ea8742018-02-27 14:58:16 +0300382}
383
384static u32 sh_eth_tsu_read(struct sh_eth_private *mdp, int enum_index)
385{
Sergei Shtylyovecbecb02018-07-23 21:12:38 +0300386 u16 offset = sh_eth_tsu_get_offset(mdp, enum_index);
Sergei Shtylyov627a0d22018-05-02 22:55:52 +0300387
388 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
389 return ~0U;
390
391 return ioread32(mdp->tsu_addr + offset);
Sergei Shtylyov55ea8742018-02-27 14:58:16 +0300392}
393
Sergei Shtylyovbb2fa4e2018-06-02 22:38:56 +0300394static void sh_eth_soft_swap(char *src, int len)
395{
396#ifdef __LITTLE_ENDIAN
397 u32 *p = (u32 *)src;
Sergei Shtylyov11001492018-06-02 22:40:16 +0300398 u32 *maxp = p + DIV_ROUND_UP(len, sizeof(u32));
Sergei Shtylyovbb2fa4e2018-06-02 22:38:56 +0300399
400 for (; p < maxp; p++)
401 *p = swab32(*p);
402#endif
403}
404
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400405static void sh_eth_select_mii(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000406{
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000407 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +0300408 u32 value;
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000409
410 switch (mdp->phy_interface) {
Sergei Shtylyov230c1842018-05-18 21:30:18 +0300411 case PHY_INTERFACE_MODE_RGMII ... PHY_INTERFACE_MODE_RGMII_TXID:
412 value = 0x3;
413 break;
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000414 case PHY_INTERFACE_MODE_GMII:
415 value = 0x2;
416 break;
417 case PHY_INTERFACE_MODE_MII:
418 value = 0x1;
419 break;
420 case PHY_INTERFACE_MODE_RMII:
421 value = 0x0;
422 break;
423 default:
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300424 netdev_warn(ndev,
425 "PHY interface mode was not setup. Set to MII.\n");
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000426 value = 0x1;
427 break;
428 }
429
430 sh_eth_write(ndev, value, RMII_MII);
431}
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000432
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400433static void sh_eth_set_duplex(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000434{
435 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000436
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300437 sh_eth_modify(ndev, ECMR, ECMR_DM, mdp->duplex ? ECMR_DM : 0);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000438}
439
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100440static void sh_eth_chip_reset(struct net_device *ndev)
441{
442 struct sh_eth_private *mdp = netdev_priv(ndev);
443
444 /* reset device */
Sergei Shtylyovec65cfc2016-04-24 23:46:15 +0300445 sh_eth_tsu_write(mdp, ARSTR_ARST, ARSTR);
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100446 mdelay(1);
447}
448
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300449static int sh_eth_soft_reset(struct net_device *ndev)
450{
451 sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, EDMR_SRST_ETHER);
452 mdelay(3);
453 sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, 0);
454
455 return 0;
456}
457
458static int sh_eth_check_soft_reset(struct net_device *ndev)
459{
460 int cnt;
461
462 for (cnt = 100; cnt > 0; cnt--) {
463 if (!(sh_eth_read(ndev, EDMR) & EDMR_SRST_GETHER))
464 return 0;
465 mdelay(1);
466 }
467
468 netdev_err(ndev, "Device reset failed\n");
469 return -ETIMEDOUT;
470}
471
472static int sh_eth_soft_reset_gether(struct net_device *ndev)
473{
474 struct sh_eth_private *mdp = netdev_priv(ndev);
475 int ret;
476
477 sh_eth_write(ndev, EDSR_ENALL, EDSR);
478 sh_eth_modify(ndev, EDMR, EDMR_SRST_GETHER, EDMR_SRST_GETHER);
479
480 ret = sh_eth_check_soft_reset(ndev);
481 if (ret)
482 return ret;
483
484 /* Table Init */
485 sh_eth_write(ndev, 0, TDLAR);
486 sh_eth_write(ndev, 0, TDFAR);
487 sh_eth_write(ndev, 0, TDFXR);
488 sh_eth_write(ndev, 0, TDFFR);
489 sh_eth_write(ndev, 0, RDLAR);
490 sh_eth_write(ndev, 0, RDFAR);
491 sh_eth_write(ndev, 0, RDFXR);
492 sh_eth_write(ndev, 0, RDFFR);
493
494 /* Reset HW CRC register */
Sergei Shtylyov2c2ab5a2019-02-04 21:05:55 +0300495 if (mdp->cd->csmr)
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300496 sh_eth_write(ndev, 0, CSMR);
497
498 /* Select MII mode */
499 if (mdp->cd->select_mii)
500 sh_eth_select_mii(ndev);
501
502 return ret;
503}
504
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100505static void sh_eth_set_rate_gether(struct net_device *ndev)
506{
507 struct sh_eth_private *mdp = netdev_priv(ndev);
508
Sergei Shtylyova6318d52020-02-15 23:13:45 +0300509 if (WARN_ON(!mdp->cd->gecmr))
510 return;
511
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100512 switch (mdp->speed) {
513 case 10: /* 10BASE */
514 sh_eth_write(ndev, GECMR_10, GECMR);
515 break;
516 case 100:/* 100BASE */
517 sh_eth_write(ndev, GECMR_100, GECMR);
518 break;
519 case 1000: /* 1000BASE */
520 sh_eth_write(ndev, GECMR_1000, GECMR);
521 break;
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100522 }
523}
524
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100525#ifdef CONFIG_OF
526/* R7S72100 */
527static struct sh_eth_cpu_data r7s72100_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300528 .soft_reset = sh_eth_soft_reset_gether,
529
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100530 .chip_reset = sh_eth_chip_reset,
531 .set_duplex = sh_eth_set_duplex,
532
Sergei Shtylyovb39b7092020-02-15 23:14:44 +0300533 .register_type = SH_ETH_REG_GIGABIT,
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100534
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300535 .edtrr_trns = EDTRR_TRNS_GETHER,
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100536 .ecsr_value = ECSR_ICD,
537 .ecsipr_value = ECSIPR_ICDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300538 .eesipr_value = EESIPR_TWB1IP | EESIPR_TWBIP | EESIPR_TC1IP |
539 EESIPR_TABTIP | EESIPR_RABTIP | EESIPR_RFCOFIP |
540 EESIPR_ECIIP |
541 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
542 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
543 EESIPR_RMAFIP | EESIPR_RRFIP |
544 EESIPR_RTLFIP | EESIPR_RTSFIP |
545 EESIPR_PREIP | EESIPR_CERFIP,
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100546
547 .tx_check = EESR_TC1 | EESR_FTC,
548 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
549 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300550 EESR_TDE,
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100551 .fdr_value = 0x0000070f,
552
553 .no_psr = 1,
554 .apr = 1,
555 .mpr = 1,
556 .tpauser = 1,
557 .hw_swap = 1,
558 .rpadir = 1,
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100559 .no_trimd = 1,
560 .no_ade = 1,
Sergei Shtylyov246e30c2018-03-24 23:09:55 +0300561 .xdfar_rw = 1,
Sergei Shtylyov2c2ab5a2019-02-04 21:05:55 +0300562 .csmr = 1,
Sergei Shtylyov48132cd2019-02-04 21:07:53 +0300563 .rx_csum = 1,
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100564 .tsu = 1,
Sergei Shtylyovce9134d2018-03-24 23:11:19 +0300565 .no_tx_cntrs = 1,
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100566};
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100567
568static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
569{
Sergei Shtylyovc66b2582016-05-07 14:09:01 -0700570 sh_eth_chip_reset(ndev);
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100571
572 sh_eth_select_mii(ndev);
573}
574
575/* R8A7740 */
576static struct sh_eth_cpu_data r8a7740_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300577 .soft_reset = sh_eth_soft_reset_gether,
578
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100579 .chip_reset = sh_eth_chip_reset_r8a7740,
580 .set_duplex = sh_eth_set_duplex,
581 .set_rate = sh_eth_set_rate_gether,
582
583 .register_type = SH_ETH_REG_GIGABIT,
584
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300585 .edtrr_trns = EDTRR_TRNS_GETHER,
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100586 .ecsr_value = ECSR_ICD | ECSR_MPD,
587 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300588 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
589 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
590 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
591 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
592 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
593 EESIPR_CEEFIP | EESIPR_CELFIP |
594 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
595 EESIPR_PREIP | EESIPR_CERFIP,
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100596
597 .tx_check = EESR_TC1 | EESR_FTC,
598 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
599 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300600 EESR_TDE,
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100601 .fdr_value = 0x0000070f,
602
603 .apr = 1,
604 .mpr = 1,
605 .tpauser = 1,
Sergei Shtylyova6318d52020-02-15 23:13:45 +0300606 .gecmr = 1,
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100607 .bculr = 1,
608 .hw_swap = 1,
609 .rpadir = 1,
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100610 .no_trimd = 1,
611 .no_ade = 1,
Sergei Shtylyov246e30c2018-03-24 23:09:55 +0300612 .xdfar_rw = 1,
Sergei Shtylyov2c2ab5a2019-02-04 21:05:55 +0300613 .csmr = 1,
Sergei Shtylyov040c16f2019-02-04 21:08:54 +0300614 .rx_csum = 1,
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100615 .tsu = 1,
616 .select_mii = 1,
Niklas Söderlund33017e22017-01-09 16:34:07 +0100617 .magic = 1,
Sergei Shtylyov4c1d4582018-03-24 23:12:54 +0300618 .cexcr = 1,
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100619};
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100620
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000621/* There is CPU dependent code */
Simon Horman6c4b2f72017-10-18 09:21:27 +0200622static void sh_eth_set_rate_rcar(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000623{
624 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000625
626 switch (mdp->speed) {
627 case 10: /* 10BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300628 sh_eth_modify(ndev, ECMR, ECMR_ELB, 0);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000629 break;
630 case 100:/* 100BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300631 sh_eth_modify(ndev, ECMR, ECMR_ELB, ECMR_ELB);
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000632 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000633 }
634}
635
Simon Horman6c4b2f72017-10-18 09:21:27 +0200636/* R-Car Gen1 */
637static struct sh_eth_cpu_data rcar_gen1_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300638 .soft_reset = sh_eth_soft_reset,
639
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000640 .set_duplex = sh_eth_set_duplex,
Simon Horman6c4b2f72017-10-18 09:21:27 +0200641 .set_rate = sh_eth_set_rate_rcar,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000642
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400643 .register_type = SH_ETH_REG_FAST_RCAR,
644
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300645 .edtrr_trns = EDTRR_TRNS_ETHER,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000646 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
647 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300648 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ADEIP | EESIPR_ECIIP |
649 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
650 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
651 EESIPR_RMAFIP | EESIPR_RRFIP |
652 EESIPR_RTLFIP | EESIPR_RTSFIP |
653 EESIPR_PREIP | EESIPR_CERFIP,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000654
Sergei Shtylyov27164492018-05-20 00:02:36 +0300655 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400656 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300657 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900658 .fdr_value = 0x00000f0f,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000659
660 .apr = 1,
661 .mpr = 1,
662 .tpauser = 1,
663 .hw_swap = 1,
Sergei Shtylyov6e80e552018-04-01 00:22:08 +0300664 .no_xdfar = 1,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000665};
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000666
Simon Horman6c4b2f72017-10-18 09:21:27 +0200667/* R-Car Gen2 and RZ/G1 */
668static struct sh_eth_cpu_data rcar_gen2_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300669 .soft_reset = sh_eth_soft_reset,
670
Simon Hormane18dbf72013-07-23 10:18:05 +0900671 .set_duplex = sh_eth_set_duplex,
Simon Horman6c4b2f72017-10-18 09:21:27 +0200672 .set_rate = sh_eth_set_rate_rcar,
Simon Hormane18dbf72013-07-23 10:18:05 +0900673
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400674 .register_type = SH_ETH_REG_FAST_RCAR,
675
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300676 .edtrr_trns = EDTRR_TRNS_ETHER,
Niklas Söderlunde410d86d2017-01-09 16:34:06 +0100677 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD | ECSR_MPD,
678 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP |
679 ECSIPR_MPDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300680 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ADEIP | EESIPR_ECIIP |
681 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
682 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
683 EESIPR_RMAFIP | EESIPR_RRFIP |
684 EESIPR_RTLFIP | EESIPR_RTSFIP |
685 EESIPR_PREIP | EESIPR_CERFIP,
Simon Hormane18dbf72013-07-23 10:18:05 +0900686
Sergei Shtylyov27164492018-05-20 00:02:36 +0300687 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
Laurent Pinchartba361cb2013-07-31 16:42:11 +0900688 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300689 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900690 .fdr_value = 0x00000f0f,
Simon Hormane18dbf72013-07-23 10:18:05 +0900691
Geert Uytterhoeven01fbd3f2015-01-15 11:52:19 +0100692 .trscer_err_mask = DESC_I_RINT8,
693
Simon Hormane18dbf72013-07-23 10:18:05 +0900694 .apr = 1,
695 .mpr = 1,
696 .tpauser = 1,
697 .hw_swap = 1,
Sergei Shtylyov6e80e552018-04-01 00:22:08 +0300698 .no_xdfar = 1,
Simon Hormane18dbf72013-07-23 10:18:05 +0900699 .rmiimode = 1,
Niklas Söderlunde410d86d2017-01-09 16:34:06 +0100700 .magic = 1,
Simon Hormane18dbf72013-07-23 10:18:05 +0900701};
Sergei Shtylyov3eb9c2a2018-05-18 21:32:46 +0300702
703/* R8A77980 */
704static struct sh_eth_cpu_data r8a77980_data = {
705 .soft_reset = sh_eth_soft_reset_gether,
706
707 .set_duplex = sh_eth_set_duplex,
708 .set_rate = sh_eth_set_rate_gether,
709
710 .register_type = SH_ETH_REG_GIGABIT,
711
712 .edtrr_trns = EDTRR_TRNS_GETHER,
713 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD | ECSR_MPD,
714 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP |
715 ECSIPR_MPDIP,
716 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
717 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
718 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
719 EESIPR_RMAFIP | EESIPR_RRFIP |
720 EESIPR_RTLFIP | EESIPR_RTSFIP |
721 EESIPR_PREIP | EESIPR_CERFIP,
722
Sergei Shtylyov27164492018-05-20 00:02:36 +0300723 .tx_check = EESR_FTC | EESR_CD | EESR_TRO,
Sergei Shtylyov3eb9c2a2018-05-18 21:32:46 +0300724 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
725 EESR_RFE | EESR_RDE | EESR_RFRMER |
726 EESR_TFE | EESR_TDE | EESR_ECI,
727 .fdr_value = 0x0000070f,
728
729 .apr = 1,
730 .mpr = 1,
731 .tpauser = 1,
Sergei Shtylyova6318d52020-02-15 23:13:45 +0300732 .gecmr = 1,
Sergei Shtylyov3eb9c2a2018-05-18 21:32:46 +0300733 .bculr = 1,
734 .hw_swap = 1,
735 .nbst = 1,
736 .rpadir = 1,
Sergei Shtylyov3eb9c2a2018-05-18 21:32:46 +0300737 .no_trimd = 1,
738 .no_ade = 1,
739 .xdfar_rw = 1,
Sergei Shtylyov2c2ab5a2019-02-04 21:05:55 +0300740 .csmr = 1,
Sergei Shtylyov0da843a2019-02-04 21:10:32 +0300741 .rx_csum = 1,
Sergei Shtylyov3eb9c2a2018-05-18 21:32:46 +0300742 .select_mii = 1,
743 .magic = 1,
744 .cexcr = 1,
745};
Chris Brandt6e0bb042018-08-27 12:42:02 -0500746
747/* R7S9210 */
748static struct sh_eth_cpu_data r7s9210_data = {
749 .soft_reset = sh_eth_soft_reset,
750
751 .set_duplex = sh_eth_set_duplex,
752 .set_rate = sh_eth_set_rate_rcar,
753
754 .register_type = SH_ETH_REG_FAST_SH4,
755
756 .edtrr_trns = EDTRR_TRNS_ETHER,
757 .ecsr_value = ECSR_ICD,
758 .ecsipr_value = ECSIPR_ICDIP,
759 .eesipr_value = EESIPR_TWBIP | EESIPR_TABTIP | EESIPR_RABTIP |
760 EESIPR_RFCOFIP | EESIPR_ECIIP | EESIPR_FTCIP |
761 EESIPR_TDEIP | EESIPR_TFUFIP | EESIPR_FRIP |
762 EESIPR_RDEIP | EESIPR_RFOFIP | EESIPR_CNDIP |
763 EESIPR_DLCIP | EESIPR_CDIP | EESIPR_TROIP |
764 EESIPR_RMAFIP | EESIPR_RRFIP | EESIPR_RTLFIP |
765 EESIPR_RTSFIP | EESIPR_PREIP | EESIPR_CERFIP,
766
767 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
768 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
769 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
770
771 .fdr_value = 0x0000070f,
772
773 .apr = 1,
774 .mpr = 1,
775 .tpauser = 1,
776 .hw_swap = 1,
777 .rpadir = 1,
778 .no_ade = 1,
779 .xdfar_rw = 1,
780};
Geert Uytterhoevenc74a2242015-11-24 15:40:58 +0100781#endif /* CONFIG_OF */
Simon Hormane18dbf72013-07-23 10:18:05 +0900782
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000783static void sh_eth_set_rate_sh7724(struct net_device *ndev)
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000784{
785 struct sh_eth_private *mdp = netdev_priv(ndev);
786
787 switch (mdp->speed) {
788 case 10: /* 10BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300789 sh_eth_modify(ndev, ECMR, ECMR_RTM, 0);
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000790 break;
791 case 100:/* 100BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300792 sh_eth_modify(ndev, ECMR, ECMR_RTM, ECMR_RTM);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000793 break;
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000794 }
795}
796
797/* SH7724 */
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000798static struct sh_eth_cpu_data sh7724_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300799 .soft_reset = sh_eth_soft_reset,
800
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000801 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000802 .set_rate = sh_eth_set_rate_sh7724,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000803
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400804 .register_type = SH_ETH_REG_FAST_SH4,
805
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300806 .edtrr_trns = EDTRR_TRNS_ETHER,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000807 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
808 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300809 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ADEIP | EESIPR_ECIIP |
810 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
811 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
812 EESIPR_RMAFIP | EESIPR_RRFIP |
813 EESIPR_RTLFIP | EESIPR_RTSFIP |
814 EESIPR_PREIP | EESIPR_CERFIP,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000815
Sergei Shtylyov27164492018-05-20 00:02:36 +0300816 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400817 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300818 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000819
820 .apr = 1,
821 .mpr = 1,
822 .tpauser = 1,
823 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800824 .rpadir = 1,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000825};
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000826
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000827static void sh_eth_set_rate_sh7757(struct net_device *ndev)
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000828{
829 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000830
831 switch (mdp->speed) {
832 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000833 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000834 break;
835 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000836 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000837 break;
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000838 }
839}
840
841/* SH7757 */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000842static struct sh_eth_cpu_data sh7757_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300843 .soft_reset = sh_eth_soft_reset,
844
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000845 .set_duplex = sh_eth_set_duplex,
846 .set_rate = sh_eth_set_rate_sh7757,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000847
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400848 .register_type = SH_ETH_REG_FAST_SH4,
849
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300850 .edtrr_trns = EDTRR_TRNS_ETHER,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300851 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
852 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
853 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
854 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
855 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
856 EESIPR_CEEFIP | EESIPR_CELFIP |
857 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
858 EESIPR_PREIP | EESIPR_CERFIP,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000859
Sergei Shtylyov27164492018-05-20 00:02:36 +0300860 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400861 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300862 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000863
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000864 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000865 .apr = 1,
866 .mpr = 1,
867 .tpauser = 1,
868 .hw_swap = 1,
869 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000870 .rpadir = 1,
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +0000871 .rtrate = 1,
Sergei Shtylyova94cf2a2018-02-24 22:41:45 +0300872 .dual_port = 1,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000873};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000874
David S. Millere403d292013-06-07 23:40:41 -0700875#define SH_GIGA_ETH_BASE 0xfee00000UL
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000876#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
877#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
878static void sh_eth_chip_reset_giga(struct net_device *ndev)
879{
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +0100880 u32 mahr[2], malr[2];
Sergei Shtylyov79270922016-05-08 00:08:05 +0300881 int i;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000882
883 /* save MAHR and MALR */
884 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000885 malr[i] = ioread32((void *)GIGA_MALR(i));
886 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000887 }
888
Sergei Shtylyovc66b2582016-05-07 14:09:01 -0700889 sh_eth_chip_reset(ndev);
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000890
891 /* restore MAHR and MALR */
892 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000893 iowrite32(malr[i], (void *)GIGA_MALR(i));
894 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000895 }
896}
897
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000898static void sh_eth_set_rate_giga(struct net_device *ndev)
899{
900 struct sh_eth_private *mdp = netdev_priv(ndev);
901
Sergei Shtylyova6318d52020-02-15 23:13:45 +0300902 if (WARN_ON(!mdp->cd->gecmr))
903 return;
904
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000905 switch (mdp->speed) {
906 case 10: /* 10BASE */
907 sh_eth_write(ndev, 0x00000000, GECMR);
908 break;
909 case 100:/* 100BASE */
910 sh_eth_write(ndev, 0x00000010, GECMR);
911 break;
912 case 1000: /* 1000BASE */
913 sh_eth_write(ndev, 0x00000020, GECMR);
914 break;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000915 }
916}
917
918/* SH7757(GETHERC) */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000919static struct sh_eth_cpu_data sh7757_data_giga = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300920 .soft_reset = sh_eth_soft_reset_gether,
921
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000922 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000923 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000924 .set_rate = sh_eth_set_rate_giga,
925
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400926 .register_type = SH_ETH_REG_GIGABIT,
927
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300928 .edtrr_trns = EDTRR_TRNS_GETHER,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000929 .ecsr_value = ECSR_ICD | ECSR_MPD,
930 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300931 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
932 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
933 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
934 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
935 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
936 EESIPR_CEEFIP | EESIPR_CELFIP |
937 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
938 EESIPR_PREIP | EESIPR_CERFIP,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000939
940 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400941 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
942 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300943 EESR_TDE,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000944 .fdr_value = 0x0000072f,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000945
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000946 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000947 .apr = 1,
948 .mpr = 1,
949 .tpauser = 1,
Sergei Shtylyova6318d52020-02-15 23:13:45 +0300950 .gecmr = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000951 .bculr = 1,
952 .hw_swap = 1,
953 .rpadir = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000954 .no_trimd = 1,
955 .no_ade = 1,
Sergei Shtylyov246e30c2018-03-24 23:09:55 +0300956 .xdfar_rw = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000957 .tsu = 1,
Sergei Shtylyov4c1d4582018-03-24 23:12:54 +0300958 .cexcr = 1,
Sergei Shtylyova94cf2a2018-02-24 22:41:45 +0300959 .dual_port = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000960};
961
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000962/* SH7734 */
963static struct sh_eth_cpu_data sh7734_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300964 .soft_reset = sh_eth_soft_reset_gether,
965
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000966 .chip_reset = sh_eth_chip_reset,
967 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000968 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000969
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400970 .register_type = SH_ETH_REG_GIGABIT,
971
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300972 .edtrr_trns = EDTRR_TRNS_GETHER,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000973 .ecsr_value = ECSR_ICD | ECSR_MPD,
974 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300975 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
976 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
977 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
978 EESIPR_DLCIP | EESIPR_CDIP | EESIPR_TROIP |
979 EESIPR_RMAFIP | EESIPR_CEEFIP | EESIPR_CELFIP |
980 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
981 EESIPR_PREIP | EESIPR_CERFIP,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000982
983 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400984 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
985 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300986 EESR_TDE,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000987
988 .apr = 1,
989 .mpr = 1,
990 .tpauser = 1,
Sergei Shtylyova6318d52020-02-15 23:13:45 +0300991 .gecmr = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000992 .bculr = 1,
993 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000994 .no_trimd = 1,
995 .no_ade = 1,
Sergei Shtylyov246e30c2018-03-24 23:09:55 +0300996 .xdfar_rw = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000997 .tsu = 1,
Sergei Shtylyov2c2ab5a2019-02-04 21:05:55 +0300998 .csmr = 1,
Sergei Shtylyov06240e12019-02-04 21:11:32 +0300999 .rx_csum = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001000 .select_mii = 1,
Niklas Söderlund159c2a92017-01-09 16:34:08 +01001001 .magic = 1,
Sergei Shtylyov4c1d4582018-03-24 23:12:54 +03001002 .cexcr = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001003};
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001004
1005/* SH7763 */
1006static struct sh_eth_cpu_data sh7763_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +03001007 .soft_reset = sh_eth_soft_reset_gether,
1008
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001009 .chip_reset = sh_eth_chip_reset,
1010 .set_duplex = sh_eth_set_duplex,
1011 .set_rate = sh_eth_set_rate_gether,
1012
Sergei Shtylyova3153d82013-08-18 03:11:28 +04001013 .register_type = SH_ETH_REG_GIGABIT,
1014
Sergei Shtylyov3e416992018-03-24 23:08:42 +03001015 .edtrr_trns = EDTRR_TRNS_GETHER,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001016 .ecsr_value = ECSR_ICD | ECSR_MPD,
1017 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +03001018 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
1019 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
1020 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
1021 EESIPR_DLCIP | EESIPR_CDIP | EESIPR_TROIP |
1022 EESIPR_RMAFIP | EESIPR_CEEFIP | EESIPR_CELFIP |
1023 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
1024 EESIPR_PREIP | EESIPR_CERFIP,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001025
1026 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001027 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001028 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001029
1030 .apr = 1,
1031 .mpr = 1,
1032 .tpauser = 1,
Sergei Shtylyova6318d52020-02-15 23:13:45 +03001033 .gecmr = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001034 .bculr = 1,
1035 .hw_swap = 1,
1036 .no_trimd = 1,
1037 .no_ade = 1,
Sergei Shtylyov246e30c2018-03-24 23:09:55 +03001038 .xdfar_rw = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001039 .tsu = 1,
1040 .irq_flags = IRQF_SHARED,
Niklas Söderlund267e1d52017-01-09 16:34:09 +01001041 .magic = 1,
Sergei Shtylyov4c1d4582018-03-24 23:12:54 +03001042 .cexcr = 1,
Sergei Shtylyov997feb12019-02-04 21:12:39 +03001043 .rx_csum = 1,
Sergei Shtylyova94cf2a2018-02-24 22:41:45 +03001044 .dual_port = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001045};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001046
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00001047static struct sh_eth_cpu_data sh7619_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +03001048 .soft_reset = sh_eth_soft_reset,
1049
Sergei Shtylyova3153d82013-08-18 03:11:28 +04001050 .register_type = SH_ETH_REG_FAST_SH3_SH2,
1051
Sergei Shtylyov3e416992018-03-24 23:08:42 +03001052 .edtrr_trns = EDTRR_TRNS_ETHER,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +03001053 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
1054 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
1055 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
1056 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
1057 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
1058 EESIPR_CEEFIP | EESIPR_CELFIP |
1059 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
1060 EESIPR_PREIP | EESIPR_CERFIP,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001061
1062 .apr = 1,
1063 .mpr = 1,
1064 .tpauser = 1,
1065 .hw_swap = 1,
1066};
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00001067
1068static struct sh_eth_cpu_data sh771x_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +03001069 .soft_reset = sh_eth_soft_reset,
1070
Sergei Shtylyova3153d82013-08-18 03:11:28 +04001071 .register_type = SH_ETH_REG_FAST_SH3_SH2,
1072
Sergei Shtylyov3e416992018-03-24 23:08:42 +03001073 .edtrr_trns = EDTRR_TRNS_ETHER,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +03001074 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
1075 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
1076 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
1077 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
1078 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
1079 EESIPR_CEEFIP | EESIPR_CELFIP |
1080 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
1081 EESIPR_PREIP | EESIPR_CERFIP,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001082 .tsu = 1,
Sergei Shtylyova94cf2a2018-02-24 22:41:45 +03001083 .dual_port = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001084};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001085
1086static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
1087{
1088 if (!cd->ecsr_value)
1089 cd->ecsr_value = DEFAULT_ECSR_INIT;
1090
1091 if (!cd->ecsipr_value)
1092 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
1093
1094 if (!cd->fcftr_value)
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001095 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001096 DEFAULT_FIFO_F_D_RFD;
1097
1098 if (!cd->fdr_value)
1099 cd->fdr_value = DEFAULT_FDR_INIT;
1100
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001101 if (!cd->tx_check)
1102 cd->tx_check = DEFAULT_TX_CHECK;
1103
1104 if (!cd->eesr_err_check)
1105 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +09001106
1107 if (!cd->trscer_err_mask)
1108 cd->trscer_err_mask = DEFAULT_TRSCER_ERR_MASK;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001109}
1110
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001111static void sh_eth_set_receive_align(struct sk_buff *skb)
1112{
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001113 uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001114
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001115 if (reserve)
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001116 skb_reserve(skb, SH_ETH_RX_ALIGN - reserve);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001117}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001118
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001119/* Program the hardware MAC address from dev->dev_addr. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001120static void update_mac_address(struct net_device *ndev)
1121{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001122 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001123 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
1124 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001125 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001126 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001127}
1128
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001129/* Get MAC address from SuperH MAC address register
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001130 *
1131 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
1132 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
1133 * When you want use this device, you must set MAC address in bootloader.
1134 *
1135 */
Magnus Damm748031f2009-10-09 00:17:14 +00001136static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001137{
Magnus Damm748031f2009-10-09 00:17:14 +00001138 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
Joe Perchesd458cdf2013-10-01 19:04:40 -07001139 memcpy(ndev->dev_addr, mac, ETH_ALEN);
Magnus Damm748031f2009-10-09 00:17:14 +00001140 } else {
Sergei Shtylyov37742f02015-12-05 00:58:57 +03001141 u32 mahr = sh_eth_read(ndev, MAHR);
1142 u32 malr = sh_eth_read(ndev, MALR);
1143
1144 ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
1145 ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
1146 ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
1147 ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
1148 ndev->dev_addr[4] = (malr >> 8) & 0xFF;
1149 ndev->dev_addr[5] = (malr >> 0) & 0xFF;
Magnus Damm748031f2009-10-09 00:17:14 +00001150 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001151}
1152
1153struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001154 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001155 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001156 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001157};
1158
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001159static void sh_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001160{
1161 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001162 u32 pir;
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001163
1164 if (bitbang->set_gate)
1165 bitbang->set_gate(bitbang->addr);
1166
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001167 pir = ioread32(bitbang->addr);
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001168 if (set)
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001169 pir |= mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001170 else
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001171 pir &= ~mask;
1172 iowrite32(pir, bitbang->addr);
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001173}
1174
1175/* Data I/O pin control */
1176static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1177{
1178 sh_mdio_ctrl(ctrl, PIR_MMD, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001179}
1180
1181/* Set bit data*/
1182static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
1183{
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001184 sh_mdio_ctrl(ctrl, PIR_MDO, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001185}
1186
1187/* Get bit data*/
1188static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
1189{
1190 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001191
1192 if (bitbang->set_gate)
1193 bitbang->set_gate(bitbang->addr);
1194
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001195 return (ioread32(bitbang->addr) & PIR_MDI) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001196}
1197
1198/* MDC pin control */
1199static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1200{
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001201 sh_mdio_ctrl(ctrl, PIR_MDC, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001202}
1203
1204/* mdio bus control struct */
1205static struct mdiobb_ops bb_ops = {
1206 .owner = THIS_MODULE,
1207 .set_mdc = sh_mdc_ctrl,
1208 .set_mdio_dir = sh_mmd_ctrl,
1209 .set_mdio_data = sh_set_mdio,
1210 .get_mdio_data = sh_get_mdio,
1211};
1212
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001213/* free Tx skb function */
1214static int sh_eth_tx_free(struct net_device *ndev, bool sent_only)
1215{
1216 struct sh_eth_private *mdp = netdev_priv(ndev);
1217 struct sh_eth_txdesc *txdesc;
1218 int free_num = 0;
1219 int entry;
1220 bool sent;
1221
1222 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
1223 entry = mdp->dirty_tx % mdp->num_tx_ring;
1224 txdesc = &mdp->tx_ring[entry];
1225 sent = !(txdesc->status & cpu_to_le32(TD_TACT));
1226 if (sent_only && !sent)
1227 break;
1228 /* TACT bit must be checked before all the following reads */
1229 dma_rmb();
1230 netif_info(mdp, tx_done, ndev,
1231 "tx entry %d status 0x%08x\n",
1232 entry, le32_to_cpu(txdesc->status));
1233 /* Free the original skb. */
1234 if (mdp->tx_skbuff[entry]) {
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01001235 dma_unmap_single(&mdp->pdev->dev,
1236 le32_to_cpu(txdesc->addr),
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001237 le32_to_cpu(txdesc->len) >> 16,
1238 DMA_TO_DEVICE);
1239 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1240 mdp->tx_skbuff[entry] = NULL;
1241 free_num++;
1242 }
1243 txdesc->status = cpu_to_le32(TD_TFP);
1244 if (entry >= mdp->num_tx_ring - 1)
1245 txdesc->status |= cpu_to_le32(TD_TDLE);
1246
1247 if (sent) {
1248 ndev->stats.tx_packets++;
1249 ndev->stats.tx_bytes += le32_to_cpu(txdesc->len) >> 16;
1250 }
1251 }
1252 return free_num;
1253}
1254
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001255/* free skb and descriptor buffer */
1256static void sh_eth_ring_free(struct net_device *ndev)
1257{
1258 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001259 int ringsize, i;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001260
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001261 if (mdp->rx_ring) {
1262 for (i = 0; i < mdp->num_rx_ring; i++) {
1263 if (mdp->rx_skbuff[i]) {
1264 struct sh_eth_rxdesc *rxdesc = &mdp->rx_ring[i];
1265
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01001266 dma_unmap_single(&mdp->pdev->dev,
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001267 le32_to_cpu(rxdesc->addr),
1268 ALIGN(mdp->rx_buf_sz, 32),
1269 DMA_FROM_DEVICE);
1270 }
1271 }
1272 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Thomas Petazzoni573500dbf2017-12-04 14:33:27 +01001273 dma_free_coherent(&mdp->pdev->dev, ringsize, mdp->rx_ring,
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001274 mdp->rx_desc_dma);
1275 mdp->rx_ring = NULL;
1276 }
1277
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001278 /* Free Rx skb ringbuffer */
1279 if (mdp->rx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001280 for (i = 0; i < mdp->num_rx_ring; i++)
1281 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001282 }
1283 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c775502012-06-26 20:00:01 +00001284 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001285
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001286 if (mdp->tx_ring) {
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001287 sh_eth_tx_free(ndev, false);
1288
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001289 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Thomas Petazzoni573500dbf2017-12-04 14:33:27 +01001290 dma_free_coherent(&mdp->pdev->dev, ringsize, mdp->tx_ring,
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001291 mdp->tx_desc_dma);
1292 mdp->tx_ring = NULL;
1293 }
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001294
1295 /* Free Tx skb ringbuffer */
1296 kfree(mdp->tx_skbuff);
1297 mdp->tx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001298}
1299
1300/* format skb and descriptor buffer */
1301static void sh_eth_ring_format(struct net_device *ndev)
1302{
1303 struct sh_eth_private *mdp = netdev_priv(ndev);
1304 int i;
1305 struct sk_buff *skb;
1306 struct sh_eth_rxdesc *rxdesc = NULL;
1307 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001308 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
1309 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Sergei Shtylyovcb368592015-10-24 00:46:40 +03001310 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001311 dma_addr_t dma_addr;
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001312 u32 buf_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001313
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001314 mdp->cur_rx = 0;
1315 mdp->cur_tx = 0;
1316 mdp->dirty_rx = 0;
1317 mdp->dirty_tx = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001318
1319 memset(mdp->rx_ring, 0, rx_ringsize);
1320
1321 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001322 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001323 /* skb */
1324 mdp->rx_skbuff[i] = NULL;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001325 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001326 if (skb == NULL)
1327 break;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001328 sh_eth_set_receive_align(skb);
1329
Sergei Shtylyovab857912015-10-24 00:46:03 +03001330 /* The size of the buffer is a multiple of 32 bytes. */
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001331 buf_len = ALIGN(mdp->rx_buf_sz, 32);
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01001332 dma_addr = dma_map_single(&mdp->pdev->dev, skb->data, buf_len,
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001333 DMA_FROM_DEVICE);
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01001334 if (dma_mapping_error(&mdp->pdev->dev, dma_addr)) {
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001335 kfree_skb(skb);
1336 break;
1337 }
1338 mdp->rx_skbuff[i] = skb;
Sergei Shtylyovd0ba9132016-03-08 01:37:09 +03001339
1340 /* RX descriptor */
1341 rxdesc = &mdp->rx_ring[i];
1342 rxdesc->len = cpu_to_le32(buf_len << 16);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001343 rxdesc->addr = cpu_to_le32(dma_addr);
1344 rxdesc->status = cpu_to_le32(RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001345
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001346 /* Rx descriptor address set */
1347 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001348 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Sergei Shtylyov246e30c2018-03-24 23:09:55 +03001349 if (mdp->cd->xdfar_rw)
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001350 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001351 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001352 }
1353
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001354 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001355
1356 /* Mark the last entry as wrapping the ring. */
Sergei Shtylyovc1b7fca2016-03-08 01:36:28 +03001357 if (rxdesc)
1358 rxdesc->status |= cpu_to_le32(RD_RDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001359
1360 memset(mdp->tx_ring, 0, tx_ringsize);
1361
1362 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001363 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001364 mdp->tx_skbuff[i] = NULL;
1365 txdesc = &mdp->tx_ring[i];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001366 txdesc->status = cpu_to_le32(TD_TFP);
1367 txdesc->len = cpu_to_le32(0);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001368 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001369 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001370 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Sergei Shtylyov246e30c2018-03-24 23:09:55 +03001371 if (mdp->cd->xdfar_rw)
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001372 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001373 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001374 }
1375
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001376 txdesc->status |= cpu_to_le32(TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001377}
1378
1379/* Get skb and descriptor buffer */
1380static int sh_eth_ring_init(struct net_device *ndev)
1381{
1382 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001383 int rx_ringsize, tx_ringsize;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001384
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001385 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001386 * card needs room to do 8 byte alignment, +2 so we can reserve
1387 * the first 2 bytes, and +16 gets room for the status word from the
1388 * card.
1389 */
1390 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1391 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001392 if (mdp->cd->rpadir)
1393 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001394
1395 /* Allocate RX and TX skb rings */
Sergei Shtylyov2c94e852015-10-31 02:05:56 +03001396 mdp->rx_skbuff = kcalloc(mdp->num_rx_ring, sizeof(*mdp->rx_skbuff),
1397 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001398 if (!mdp->rx_skbuff)
1399 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001400
Sergei Shtylyov2c94e852015-10-31 02:05:56 +03001401 mdp->tx_skbuff = kcalloc(mdp->num_tx_ring, sizeof(*mdp->tx_skbuff),
1402 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001403 if (!mdp->tx_skbuff)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001404 goto ring_free;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001405
1406 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001407 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Thomas Petazzoni573500dbf2017-12-04 14:33:27 +01001408 mdp->rx_ring = dma_alloc_coherent(&mdp->pdev->dev, rx_ringsize,
1409 &mdp->rx_desc_dma, GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001410 if (!mdp->rx_ring)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001411 goto ring_free;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001412
1413 mdp->dirty_rx = 0;
1414
1415 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001416 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Thomas Petazzoni573500dbf2017-12-04 14:33:27 +01001417 mdp->tx_ring = dma_alloc_coherent(&mdp->pdev->dev, tx_ringsize,
1418 &mdp->tx_desc_dma, GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001419 if (!mdp->tx_ring)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001420 goto ring_free;
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001421 return 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001422
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001423ring_free:
1424 /* Free Rx and Tx skb ring buffer and DMA buffer */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001425 sh_eth_ring_free(ndev);
1426
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001427 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001428}
1429
Sergei Shtylyovf7967212016-04-24 19:11:07 +03001430static int sh_eth_dev_init(struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001431{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001432 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03001433 int ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001434
1435 /* Soft Reset */
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +03001436 ret = mdp->cd->soft_reset(ndev);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001437 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +01001438 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001439
Simon Horman55754f12013-07-23 10:18:04 +09001440 if (mdp->cd->rmiimode)
1441 sh_eth_write(ndev, 0x1, RMIIMODE);
1442
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001443 /* Descriptor format */
1444 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001445 if (mdp->cd->rpadir)
Sergei Shtylyov470103d2018-06-25 23:37:06 +03001446 sh_eth_write(ndev, NET_IP_ALIGN << 16, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001447
1448 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001449 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001450
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001451#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001452 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001453 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001454 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001455#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001456 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001457
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001458 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001459 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1460 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001461
Ben Dooks530aa2d2014-06-03 12:21:13 +01001462 /* Frame recv control (enable multiple-packets per rx irq) */
1463 sh_eth_write(ndev, RMCR_RNC, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001464
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +09001465 sh_eth_write(ndev, mdp->cd->trscer_err_mask, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001466
Sergei Shtylyov93f0fa72018-05-18 21:31:28 +03001467 /* DMA transfer burst mode */
1468 if (mdp->cd->nbst)
1469 sh_eth_modify(ndev, EDMR, EDMR_NBST, EDMR_NBST);
1470
Sergei Shtylyov6b147872018-05-20 00:05:02 +03001471 /* Burst cycle count upper-limit */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001472 if (mdp->cd->bculr)
Sergei Shtylyov6b147872018-05-20 00:05:02 +03001473 sh_eth_write(ndev, 0x800, BCULR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001474
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001475 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001476
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001477 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001478 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001479
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001480 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001481 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1482 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001483
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001484 sh_eth_modify(ndev, EESR, 0, 0);
Sergei Shtylyovf7967212016-04-24 19:11:07 +03001485 mdp->irq_enabled = true;
1486 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001487
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03001488 /* EMAC Mode: PAUSE prohibition; Duplex; RX Checksum; TX; RX */
Sergei Shtylyovbffa7312016-01-11 00:28:14 +03001489 sh_eth_write(ndev, ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) |
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03001490 (ndev->features & NETIF_F_RXCSUM ? ECMR_RCSC : 0) |
Sergei Shtylyovbffa7312016-01-11 00:28:14 +03001491 ECMR_TE | ECMR_RE, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001492
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001493 if (mdp->cd->set_rate)
1494 mdp->cd->set_rate(ndev);
1495
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001496 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001497 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001498
1499 /* E-MAC Interrupt Enable register */
Sergei Shtylyovf7967212016-04-24 19:11:07 +03001500 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001501
1502 /* Set MAC address */
1503 update_mac_address(ndev);
1504
1505 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001506 if (mdp->cd->apr)
Sergei Shtylyov782e85c2018-06-26 18:42:33 +03001507 sh_eth_write(ndev, 1, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001508 if (mdp->cd->mpr)
Sergei Shtylyov782e85c2018-06-26 18:42:33 +03001509 sh_eth_write(ndev, 1, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001510 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001511 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001512
Sergei Shtylyovf7967212016-04-24 19:11:07 +03001513 /* Setting the Rx mode will start the Rx process. */
1514 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001515
1516 return ret;
1517}
1518
Ben Hutchings740c7f32015-01-27 00:49:32 +00001519static void sh_eth_dev_exit(struct net_device *ndev)
1520{
1521 struct sh_eth_private *mdp = netdev_priv(ndev);
1522 int i;
1523
1524 /* Deactivate all TX descriptors, so DMA should stop at next
1525 * packet boundary if it's currently running
1526 */
1527 for (i = 0; i < mdp->num_tx_ring; i++)
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001528 mdp->tx_ring[i].status &= ~cpu_to_le32(TD_TACT);
Ben Hutchings740c7f32015-01-27 00:49:32 +00001529
1530 /* Disable TX FIFO egress to MAC */
1531 sh_eth_rcv_snd_disable(ndev);
1532
1533 /* Stop RX DMA at next packet boundary */
1534 sh_eth_write(ndev, 0, EDRRR);
1535
1536 /* Aside from TX DMA, we can't tell when the hardware is
1537 * really stopped, so we need to reset to make sure.
1538 * Before doing that, wait for long enough to *probably*
1539 * finish transmitting the last packet and poll stats.
1540 */
1541 msleep(2); /* max frame time at 10 Mbps < 1250 us */
1542 sh_eth_get_stats(ndev);
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +03001543 mdp->cd->soft_reset(ndev);
Geert Uytterhoevena14c7d12015-02-27 17:16:26 +01001544
Yoshihiro Shimoda315ca922019-05-28 13:10:46 +09001545 /* Set the RMII mode again if required */
1546 if (mdp->cd->rmiimode)
1547 sh_eth_write(ndev, 0x1, RMIIMODE);
1548
Geert Uytterhoevena14c7d12015-02-27 17:16:26 +01001549 /* Set MAC address again */
1550 update_mac_address(ndev);
Ben Hutchings740c7f32015-01-27 00:49:32 +00001551}
1552
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03001553static void sh_eth_rx_csum(struct sk_buff *skb)
1554{
1555 u8 *hw_csum;
1556
1557 /* The hardware checksum is 2 bytes appended to packet data */
1558 if (unlikely(skb->len < sizeof(__sum16)))
1559 return;
1560 hw_csum = skb_tail_pointer(skb) - sizeof(__sum16);
1561 skb->csum = csum_unfold((__force __sum16)get_unaligned_le16(hw_csum));
1562 skb->ip_summed = CHECKSUM_COMPLETE;
1563 skb_trim(skb, skb->len - sizeof(__sum16));
1564}
1565
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001566/* Packet receive function */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001567static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001568{
1569 struct sh_eth_private *mdp = netdev_priv(ndev);
1570 struct sh_eth_rxdesc *rxdesc;
1571
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001572 int entry = mdp->cur_rx % mdp->num_rx_ring;
1573 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001574 int limit;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001575 struct sk_buff *skb;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001576 u32 desc_status;
Sergei Shtylyovcb368592015-10-24 00:46:40 +03001577 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001578 dma_addr_t dma_addr;
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03001579 u16 pkt_len;
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001580 u32 buf_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001581
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001582 boguscnt = min(boguscnt, *quota);
1583 limit = boguscnt;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001584 rxdesc = &mdp->rx_ring[entry];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001585 while (!(rxdesc->status & cpu_to_le32(RD_RACT))) {
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001586 /* RACT bit must be checked before all the following reads */
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001587 dma_rmb();
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001588 desc_status = le32_to_cpu(rxdesc->status);
1589 pkt_len = le32_to_cpu(rxdesc->len) & RD_RFL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001590
1591 if (--boguscnt < 0)
1592 break;
1593
Ben Hutchingse5fd13f2015-02-26 20:34:46 +00001594 netif_info(mdp, rx_status, ndev,
1595 "rx entry %d status 0x%08x len %d\n",
1596 entry, desc_status, pkt_len);
1597
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001598 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001599 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001600
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001601 /* In case of almost all GETHER/ETHERs, the Receive Frame State
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001602 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
Ben Hutchings9b4a6362015-03-03 00:52:39 +00001603 * bit 0. However, in case of the R8A7740 and R7S72100
1604 * the RFS bits are from bit 25 to bit 16. So, the
Simon Hormandb893472014-01-17 09:22:28 +09001605 * driver needs right shifting by 16.
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001606 */
Sergei Shtylyov2c2ab5a2019-02-04 21:05:55 +03001607 if (mdp->cd->csmr)
Sergei Shtylyovac8025a2013-06-13 22:12:45 +04001608 desc_status >>= 16;
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001609
Sergei Shtylyov248be832015-12-04 01:45:40 +03001610 skb = mdp->rx_skbuff[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001611 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1612 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001613 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001614 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001615 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001616 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001617 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001618 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001619 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001620 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001621 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001622 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001623 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001624 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001625 ndev->stats.rx_over_errors++;
Sergei Shtylyov248be832015-12-04 01:45:40 +03001626 } else if (skb) {
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001627 dma_addr = le32_to_cpu(rxdesc->addr);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001628 if (!mdp->cd->hw_swap)
1629 sh_eth_soft_swap(
Sergei Shtylyov12996532015-12-13 23:05:07 +03001630 phys_to_virt(ALIGN(dma_addr, 4)),
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001631 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001632 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001633 if (mdp->cd->rpadir)
1634 skb_reserve(skb, NET_IP_ALIGN);
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01001635 dma_unmap_single(&mdp->pdev->dev, dma_addr,
Sergei Shtylyovab857912015-10-24 00:46:03 +03001636 ALIGN(mdp->rx_buf_sz, 32),
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001637 DMA_FROM_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001638 skb_put(skb, pkt_len);
1639 skb->protocol = eth_type_trans(skb, ndev);
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03001640 if (ndev->features & NETIF_F_RXCSUM)
1641 sh_eth_rx_csum(skb);
Sergei Shtylyova8e9fd02013-09-03 03:03:10 +04001642 netif_receive_skb(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001643 ndev->stats.rx_packets++;
1644 ndev->stats.rx_bytes += pkt_len;
Ben Hutchings25b77ad2015-02-26 20:33:30 +00001645 if (desc_status & RD_RFS8)
1646 ndev->stats.multicast++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001647 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001648 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001649 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001650 }
1651
1652 /* Refill the Rx ring buffers. */
1653 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001654 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001655 rxdesc = &mdp->rx_ring[entry];
Sergei Shtylyovab857912015-10-24 00:46:03 +03001656 /* The size of the buffer is 32 byte boundary. */
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001657 buf_len = ALIGN(mdp->rx_buf_sz, 32);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001658 rxdesc->len = cpu_to_le32(buf_len << 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001659
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001660 if (mdp->rx_skbuff[entry] == NULL) {
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001661 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001662 if (skb == NULL)
1663 break; /* Better luck next round. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001664 sh_eth_set_receive_align(skb);
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01001665 dma_addr = dma_map_single(&mdp->pdev->dev, skb->data,
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001666 buf_len, DMA_FROM_DEVICE);
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01001667 if (dma_mapping_error(&mdp->pdev->dev, dma_addr)) {
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001668 kfree_skb(skb);
1669 break;
1670 }
1671 mdp->rx_skbuff[entry] = skb;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001672
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001673 skb_checksum_none_assert(skb);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001674 rxdesc->addr = cpu_to_le32(dma_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001675 }
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001676 dma_wmb(); /* RACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001677 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001678 rxdesc->status |=
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001679 cpu_to_le32(RD_RACT | RD_RFP | RD_RDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001680 else
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001681 rxdesc->status |= cpu_to_le32(RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001682 }
1683
1684 /* Restart Rx engine if stopped. */
1685 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001686 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001687 /* fix the values for the next receiving if RDE is set */
Sergei Shtylyov6e80e552018-04-01 00:22:08 +03001688 if (intr_status & EESR_RDE && !mdp->cd->no_xdfar) {
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001689 u32 count = (sh_eth_read(ndev, RDFAR) -
1690 sh_eth_read(ndev, RDLAR)) >> 4;
1691
1692 mdp->cur_rx = count;
1693 mdp->dirty_rx = count;
1694 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001695 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001696 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001697
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001698 *quota -= limit - boguscnt - 1;
1699
Yoshihiro Shimoda4f809ce2014-06-10 09:40:14 +09001700 return *quota <= 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001701}
1702
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001703static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001704{
1705 /* disable tx and rx */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001706 sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001707}
1708
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001709static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001710{
1711 /* enable tx and rx */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001712 sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001713}
1714
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001715/* E-MAC interrupt handler */
1716static void sh_eth_emac_interrupt(struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001717{
1718 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001719 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001720 u32 link_stat;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001721
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001722 felic_stat = sh_eth_read(ndev, ECSR) & sh_eth_read(ndev, ECSIPR);
1723 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
1724 if (felic_stat & ECSR_ICD)
1725 ndev->stats.tx_carrier_errors++;
Niklas Söderlund0cf45a32017-02-01 15:41:55 +01001726 if (felic_stat & ECSR_MPD)
1727 pm_wakeup_event(&mdp->pdev->dev, 0);
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001728 if (felic_stat & ECSR_LCHNG) {
1729 /* Link Changed */
1730 if (mdp->cd->no_psr || mdp->no_ether_link)
1731 return;
1732 link_stat = sh_eth_read(ndev, PSR);
1733 if (mdp->ether_link_active_low)
1734 link_stat = ~link_stat;
1735 if (!(link_stat & PHY_ST_LINK)) {
1736 sh_eth_rcv_snd_disable(ndev);
1737 } else {
1738 /* Link Up */
Sergei Shtylyov1a0bee62017-01-29 15:07:34 +03001739 sh_eth_modify(ndev, EESIPR, EESIPR_ECIIP, 0);
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001740 /* clear int */
1741 sh_eth_modify(ndev, ECSR, 0, 0);
Sergei Shtylyov1a0bee62017-01-29 15:07:34 +03001742 sh_eth_modify(ndev, EESIPR, EESIPR_ECIIP, EESIPR_ECIIP);
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001743 /* enable tx and rx */
1744 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001745 }
1746 }
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001747}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001748
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001749/* error control function */
1750static void sh_eth_error(struct net_device *ndev, u32 intr_status)
1751{
1752 struct sh_eth_private *mdp = netdev_priv(ndev);
1753 u32 mask;
1754
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001755 if (intr_status & EESR_TWB) {
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001756 /* Unused write back interrupt */
1757 if (intr_status & EESR_TABT) { /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001758 ndev->stats.tx_aborted_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001759 netif_err(mdp, tx_err, ndev, "Transmit Abort\n");
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001760 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001761 }
1762
1763 if (intr_status & EESR_RABT) {
1764 /* Receive Abort int */
1765 if (intr_status & EESR_RFRMER) {
1766 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001767 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001768 }
1769 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001770
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001771 if (intr_status & EESR_TDE) {
1772 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001773 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001774 netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n");
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001775 }
1776
1777 if (intr_status & EESR_TFE) {
1778 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001779 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001780 netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001781 }
1782
1783 if (intr_status & EESR_RDE) {
1784 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001785 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001786 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001787
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001788 if (intr_status & EESR_RFE) {
1789 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001790 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001791 }
1792
1793 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1794 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001795 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001796 netif_err(mdp, tx_err, ndev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001797 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001798
1799 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1800 if (mdp->cd->no_ade)
1801 mask &= ~EESR_ADE;
1802 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001803 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001804 u32 edtrr = sh_eth_read(ndev, EDTRR);
Sergei Shtylyov090d5602014-01-11 02:41:49 +03001805
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001806 /* dmesg */
Sergei Shtylyovda246852014-03-15 03:29:14 +03001807 netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1808 intr_status, mdp->cur_tx, mdp->dirty_tx,
1809 (u32)ndev->state, edtrr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001810 /* dirty buffer free */
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001811 sh_eth_tx_free(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001812
1813 /* SH7712 BUG */
Sergei Shtylyov3e416992018-03-24 23:08:42 +03001814 if (edtrr ^ mdp->cd->edtrr_trns) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001815 /* tx dma start */
Sergei Shtylyov3e416992018-03-24 23:08:42 +03001816 sh_eth_write(ndev, mdp->cd->edtrr_trns, EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001817 }
1818 /* wakeup */
1819 netif_wake_queue(ndev);
1820 }
1821}
1822
1823static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1824{
1825 struct net_device *ndev = netdev;
1826 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001827 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001828 irqreturn_t ret = IRQ_NONE;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001829 u32 intr_status, intr_enable;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001830
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001831 spin_lock(&mdp->lock);
1832
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001833 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001834 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001835 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1836 * enabled since it's the one that comes thru regardless of the mask,
1837 * and we need to fully handle it in sh_eth_emac_interrupt() in order
1838 * to quench it as it doesn't get cleared by just writing 1 to the ECI
1839 * bit...
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001840 */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001841 intr_enable = sh_eth_read(ndev, EESIPR);
Sergei Shtylyov1a0bee62017-01-29 15:07:34 +03001842 intr_status &= intr_enable | EESIPR_ECIIP;
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001843 if (intr_status & (EESR_RX_CHECK | cd->tx_check | EESR_ECI |
1844 cd->eesr_err_check))
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001845 ret = IRQ_HANDLED;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001846 else
Ben Hutchings283e38d2015-01-22 12:44:08 +00001847 goto out;
1848
Sergei Shtylyov2344ef32016-12-30 00:07:38 +03001849 if (unlikely(!mdp->irq_enabled)) {
Ben Hutchings283e38d2015-01-22 12:44:08 +00001850 sh_eth_write(ndev, 0, EESIPR);
1851 goto out;
1852 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001853
Sergei Shtylyov37191092013-06-19 23:30:23 +04001854 if (intr_status & EESR_RX_CHECK) {
1855 if (napi_schedule_prep(&mdp->napi)) {
1856 /* Mask Rx interrupts */
1857 sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
1858 EESIPR);
1859 __napi_schedule(&mdp->napi);
1860 } else {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001861 netdev_warn(ndev,
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001862 "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
Sergei Shtylyovda246852014-03-15 03:29:14 +03001863 intr_status, intr_enable);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001864 }
1865 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001866
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001867 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001868 if (intr_status & cd->tx_check) {
Sergei Shtylyov37191092013-06-19 23:30:23 +04001869 /* Clear Tx interrupts */
1870 sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
1871
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001872 sh_eth_tx_free(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001873 netif_wake_queue(ndev);
1874 }
1875
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001876 /* E-MAC interrupt */
1877 if (intr_status & EESR_ECI)
1878 sh_eth_emac_interrupt(ndev);
1879
Sergei Shtylyov37191092013-06-19 23:30:23 +04001880 if (intr_status & cd->eesr_err_check) {
1881 /* Clear error interrupts */
1882 sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
1883
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001884 sh_eth_error(ndev, intr_status);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001885 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001886
Ben Hutchings283e38d2015-01-22 12:44:08 +00001887out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001888 spin_unlock(&mdp->lock);
1889
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001890 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001891}
1892
Sergei Shtylyov37191092013-06-19 23:30:23 +04001893static int sh_eth_poll(struct napi_struct *napi, int budget)
1894{
1895 struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
1896 napi);
1897 struct net_device *ndev = napi->dev;
1898 int quota = budget;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001899 u32 intr_status;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001900
1901 for (;;) {
1902 intr_status = sh_eth_read(ndev, EESR);
1903 if (!(intr_status & EESR_RX_CHECK))
1904 break;
1905 /* Clear Rx interrupts */
1906 sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
1907
1908 if (sh_eth_rx(ndev, intr_status, &quota))
1909 goto out;
1910 }
1911
1912 napi_complete(napi);
1913
1914 /* Reenable Rx interrupts */
Ben Hutchings283e38d2015-01-22 12:44:08 +00001915 if (mdp->irq_enabled)
1916 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001917out:
1918 return budget - quota;
1919}
1920
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001921/* PHY state control function */
1922static void sh_eth_adjust_link(struct net_device *ndev)
1923{
1924 struct sh_eth_private *mdp = netdev_priv(ndev);
Philippe Reynes9fd03752016-08-10 00:04:48 +02001925 struct phy_device *phydev = ndev->phydev;
Vladimir Zapolskiy5cb3f522018-07-04 11:12:40 +03001926 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001927 int new_state = 0;
1928
Vladimir Zapolskiy5cb3f522018-07-04 11:12:40 +03001929 spin_lock_irqsave(&mdp->lock, flags);
1930
1931 /* Disable TX and RX right over here, if E-MAC change is ignored */
1932 if (mdp->cd->no_psr || mdp->no_ether_link)
1933 sh_eth_rcv_snd_disable(ndev);
1934
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001935 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001936 if (phydev->duplex != mdp->duplex) {
1937 new_state = 1;
1938 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001939 if (mdp->cd->set_duplex)
1940 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001941 }
1942
1943 if (phydev->speed != mdp->speed) {
1944 new_state = 1;
1945 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001946 if (mdp->cd->set_rate)
1947 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001948 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001949 if (!mdp->link) {
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001950 sh_eth_modify(ndev, ECMR, ECMR_TXF, 0);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001951 new_state = 1;
1952 mdp->link = phydev->link;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001953 }
1954 } else if (mdp->link) {
1955 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001956 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001957 mdp->speed = 0;
1958 mdp->duplex = -1;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001959 }
1960
Vladimir Zapolskiy5cb3f522018-07-04 11:12:40 +03001961 /* Enable TX and RX right over here, if E-MAC change is ignored */
1962 if ((mdp->cd->no_psr || mdp->no_ether_link) && phydev->link)
1963 sh_eth_rcv_snd_enable(ndev);
1964
Vladimir Zapolskiy5cb3f522018-07-04 11:12:40 +03001965 spin_unlock_irqrestore(&mdp->lock, flags);
1966
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001967 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001968 phy_print_status(phydev);
1969}
1970
1971/* PHY init function */
1972static int sh_eth_phy_init(struct net_device *ndev)
1973{
Ben Dooks702eca02014-03-12 17:47:40 +00001974 struct device_node *np = ndev->dev.parent->of_node;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001975 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03001976 struct phy_device *phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001977
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001978 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001979 mdp->speed = 0;
1980 mdp->duplex = -1;
1981
1982 /* Try connect to PHY */
Ben Dooks702eca02014-03-12 17:47:40 +00001983 if (np) {
1984 struct device_node *pn;
1985
1986 pn = of_parse_phandle(np, "phy-handle", 0);
1987 phydev = of_phy_connect(ndev, pn,
1988 sh_eth_adjust_link, 0,
1989 mdp->phy_interface);
1990
Peter Chen8da703d2016-08-01 15:02:40 +08001991 of_node_put(pn);
Ben Dooks702eca02014-03-12 17:47:40 +00001992 if (!phydev)
1993 phydev = ERR_PTR(-ENOENT);
1994 } else {
1995 char phy_id[MII_BUS_ID_SIZE + 3];
1996
1997 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
1998 mdp->mii_bus->id, mdp->phy_id);
1999
2000 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
2001 mdp->phy_interface);
2002 }
2003
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002004 if (IS_ERR(phydev)) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002005 netdev_err(ndev, "failed to connect PHY\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002006 return PTR_ERR(phydev);
2007 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002008
Thomas Petazzoni2aab6b42017-12-08 16:35:40 +01002009 /* mask with MAC supported features */
2010 if (mdp->cd->register_type != SH_ETH_REG_GIGABIT) {
2011 int err = phy_set_max_speed(phydev, SPEED_100);
2012 if (err) {
2013 netdev_err(ndev, "failed to limit PHY to 100 Mbit/s\n");
2014 phy_disconnect(phydev);
2015 return err;
2016 }
2017 }
2018
Andrew Lunn22209432016-01-06 20:11:13 +01002019 phy_attached_info(phydev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002020
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002021 return 0;
2022}
2023
2024/* PHY control start function */
2025static int sh_eth_phy_start(struct net_device *ndev)
2026{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002027 int ret;
2028
2029 ret = sh_eth_phy_init(ndev);
2030 if (ret)
2031 return ret;
2032
Philippe Reynes9fd03752016-08-10 00:04:48 +02002033 phy_start(ndev->phydev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002034
2035 return 0;
2036}
2037
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002038/* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
2039 * version must be bumped as well. Just adding registers up to that
2040 * limit is fine, as long as the existing register indices don't
2041 * change.
2042 */
2043#define SH_ETH_REG_DUMP_VERSION 1
2044#define SH_ETH_REG_DUMP_MAX_REGS 256
2045
2046static size_t __sh_eth_get_regs(struct net_device *ndev, u32 *buf)
2047{
2048 struct sh_eth_private *mdp = netdev_priv(ndev);
2049 struct sh_eth_cpu_data *cd = mdp->cd;
2050 u32 *valid_map;
2051 size_t len;
2052
2053 BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET > SH_ETH_REG_DUMP_MAX_REGS);
2054
2055 /* Dump starts with a bitmap that tells ethtool which
2056 * registers are defined for this chip.
2057 */
2058 len = DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS, 32);
2059 if (buf) {
2060 valid_map = buf;
2061 buf += len;
2062 } else {
2063 valid_map = NULL;
2064 }
2065
2066 /* Add a register to the dump, if it has a defined offset.
2067 * This automatically skips most undefined registers, but for
2068 * some it is also necessary to check a capability flag in
2069 * struct sh_eth_cpu_data.
2070 */
2071#define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
2072#define add_reg_from(reg, read_expr) do { \
2073 if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
2074 if (buf) { \
2075 mark_reg_valid(reg); \
2076 *buf++ = read_expr; \
2077 } \
2078 ++len; \
2079 } \
2080 } while (0)
2081#define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
2082#define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
2083
2084 add_reg(EDSR);
2085 add_reg(EDMR);
2086 add_reg(EDTRR);
2087 add_reg(EDRRR);
2088 add_reg(EESR);
2089 add_reg(EESIPR);
2090 add_reg(TDLAR);
Sergei Shtylyov7bf47f62020-02-15 23:10:53 +03002091 if (!cd->no_xdfar)
2092 add_reg(TDFAR);
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002093 add_reg(TDFXR);
2094 add_reg(TDFFR);
2095 add_reg(RDLAR);
Sergei Shtylyov7bf47f62020-02-15 23:10:53 +03002096 if (!cd->no_xdfar)
2097 add_reg(RDFAR);
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002098 add_reg(RDFXR);
2099 add_reg(RDFFR);
2100 add_reg(TRSCER);
2101 add_reg(RMFCR);
2102 add_reg(TFTR);
2103 add_reg(FDR);
2104 add_reg(RMCR);
2105 add_reg(TFUCR);
2106 add_reg(RFOCR);
2107 if (cd->rmiimode)
2108 add_reg(RMIIMODE);
2109 add_reg(FCFTR);
2110 if (cd->rpadir)
2111 add_reg(RPADIR);
2112 if (!cd->no_trimd)
2113 add_reg(TRIMD);
2114 add_reg(ECMR);
2115 add_reg(ECSR);
2116 add_reg(ECSIPR);
2117 add_reg(PIR);
2118 if (!cd->no_psr)
2119 add_reg(PSR);
2120 add_reg(RDMLR);
2121 add_reg(RFLR);
2122 add_reg(IPGR);
2123 if (cd->apr)
2124 add_reg(APR);
2125 if (cd->mpr)
2126 add_reg(MPR);
2127 add_reg(RFCR);
2128 add_reg(RFCF);
2129 if (cd->tpauser)
2130 add_reg(TPAUSER);
2131 add_reg(TPAUSECR);
Sergei Shtylyova6318d52020-02-15 23:13:45 +03002132 if (cd->gecmr)
2133 add_reg(GECMR);
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002134 if (cd->bculr)
2135 add_reg(BCULR);
2136 add_reg(MAHR);
2137 add_reg(MALR);
Sergei Shtylyov6eaeedc2020-02-15 23:08:20 +03002138 if (!cd->no_tx_cntrs) {
2139 add_reg(TROCR);
2140 add_reg(CDCR);
2141 add_reg(LCCR);
2142 add_reg(CNDCR);
2143 }
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002144 add_reg(CEFCR);
2145 add_reg(FRECR);
2146 add_reg(TSFRCR);
2147 add_reg(TLFRCR);
Sergei Shtylyovf75ca322020-02-15 23:09:35 +03002148 if (cd->cexcr) {
2149 add_reg(CERCR);
2150 add_reg(CEECR);
2151 }
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002152 add_reg(MAFCR);
2153 if (cd->rtrate)
2154 add_reg(RTRATE);
Sergei Shtylyov2c2ab5a2019-02-04 21:05:55 +03002155 if (cd->csmr)
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002156 add_reg(CSMR);
2157 if (cd->select_mii)
2158 add_reg(RMII_MII);
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002159 if (cd->tsu) {
Sergei Shtylyov17d0fb02018-01-13 20:22:01 +03002160 add_tsu_reg(ARSTR);
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002161 add_tsu_reg(TSU_CTRST);
Sergei Shtylyov3249b1e2020-01-08 23:42:42 +03002162 if (cd->dual_port) {
2163 add_tsu_reg(TSU_FWEN0);
2164 add_tsu_reg(TSU_FWEN1);
2165 add_tsu_reg(TSU_FCM);
2166 add_tsu_reg(TSU_BSYSL0);
2167 add_tsu_reg(TSU_BSYSL1);
2168 add_tsu_reg(TSU_PRISL0);
2169 add_tsu_reg(TSU_PRISL1);
2170 add_tsu_reg(TSU_FWSL0);
2171 add_tsu_reg(TSU_FWSL1);
2172 }
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002173 add_tsu_reg(TSU_FWSLC);
Sergei Shtylyov3249b1e2020-01-08 23:42:42 +03002174 if (cd->dual_port) {
2175 add_tsu_reg(TSU_QTAGM0);
2176 add_tsu_reg(TSU_QTAGM1);
2177 add_tsu_reg(TSU_FWSR);
2178 add_tsu_reg(TSU_FWINMK);
2179 add_tsu_reg(TSU_ADQT0);
2180 add_tsu_reg(TSU_ADQT1);
2181 add_tsu_reg(TSU_VTAG0);
2182 add_tsu_reg(TSU_VTAG1);
2183 }
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002184 add_tsu_reg(TSU_ADSBSY);
2185 add_tsu_reg(TSU_TEN);
2186 add_tsu_reg(TSU_POST1);
2187 add_tsu_reg(TSU_POST2);
2188 add_tsu_reg(TSU_POST3);
2189 add_tsu_reg(TSU_POST4);
Sergei Shtylyove14549a2018-04-01 00:23:51 +03002190 /* This is the start of a table, not just a single register. */
2191 if (buf) {
2192 unsigned int i;
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002193
Sergei Shtylyove14549a2018-04-01 00:23:51 +03002194 mark_reg_valid(TSU_ADRH0);
2195 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES * 2; i++)
2196 *buf++ = ioread32(mdp->tsu_addr +
2197 mdp->reg_offset[TSU_ADRH0] +
2198 i * 4);
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002199 }
Sergei Shtylyove14549a2018-04-01 00:23:51 +03002200 len += SH_ETH_TSU_CAM_ENTRIES * 2;
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002201 }
2202
2203#undef mark_reg_valid
2204#undef add_reg_from
2205#undef add_reg
2206#undef add_tsu_reg
2207
2208 return len * 4;
2209}
2210
2211static int sh_eth_get_regs_len(struct net_device *ndev)
2212{
2213 return __sh_eth_get_regs(ndev, NULL);
2214}
2215
2216static void sh_eth_get_regs(struct net_device *ndev, struct ethtool_regs *regs,
2217 void *buf)
2218{
2219 struct sh_eth_private *mdp = netdev_priv(ndev);
2220
2221 regs->version = SH_ETH_REG_DUMP_VERSION;
2222
2223 pm_runtime_get_sync(&mdp->pdev->dev);
2224 __sh_eth_get_regs(ndev, buf);
2225 pm_runtime_put_sync(&mdp->pdev->dev);
2226}
2227
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002228static u32 sh_eth_get_msglevel(struct net_device *ndev)
2229{
2230 struct sh_eth_private *mdp = netdev_priv(ndev);
2231 return mdp->msg_enable;
2232}
2233
2234static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
2235{
2236 struct sh_eth_private *mdp = netdev_priv(ndev);
2237 mdp->msg_enable = value;
2238}
2239
2240static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
2241 "rx_current", "tx_current",
2242 "rx_dirty", "tx_dirty",
2243};
2244#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
2245
2246static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
2247{
2248 switch (sset) {
2249 case ETH_SS_STATS:
2250 return SH_ETH_STATS_LEN;
2251 default:
2252 return -EOPNOTSUPP;
2253 }
2254}
2255
2256static void sh_eth_get_ethtool_stats(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002257 struct ethtool_stats *stats, u64 *data)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002258{
2259 struct sh_eth_private *mdp = netdev_priv(ndev);
2260 int i = 0;
2261
2262 /* device-specific stats */
2263 data[i++] = mdp->cur_rx;
2264 data[i++] = mdp->cur_tx;
2265 data[i++] = mdp->dirty_rx;
2266 data[i++] = mdp->dirty_tx;
2267}
2268
2269static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
2270{
2271 switch (stringset) {
2272 case ETH_SS_STATS:
2273 memcpy(data, *sh_eth_gstrings_stats,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002274 sizeof(sh_eth_gstrings_stats));
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002275 break;
2276 }
2277}
2278
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002279static void sh_eth_get_ringparam(struct net_device *ndev,
2280 struct ethtool_ringparam *ring)
2281{
2282 struct sh_eth_private *mdp = netdev_priv(ndev);
2283
2284 ring->rx_max_pending = RX_RING_MAX;
2285 ring->tx_max_pending = TX_RING_MAX;
2286 ring->rx_pending = mdp->num_rx_ring;
2287 ring->tx_pending = mdp->num_tx_ring;
2288}
2289
2290static int sh_eth_set_ringparam(struct net_device *ndev,
2291 struct ethtool_ringparam *ring)
2292{
2293 struct sh_eth_private *mdp = netdev_priv(ndev);
2294 int ret;
2295
2296 if (ring->tx_pending > TX_RING_MAX ||
2297 ring->rx_pending > RX_RING_MAX ||
2298 ring->tx_pending < TX_RING_MIN ||
2299 ring->rx_pending < RX_RING_MIN)
2300 return -EINVAL;
2301 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2302 return -EINVAL;
2303
2304 if (netif_running(ndev)) {
Ben Hutchingsbd888912015-01-22 12:40:25 +00002305 netif_device_detach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002306 netif_tx_disable(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002307
Ben Hutchings283e38d2015-01-22 12:44:08 +00002308 /* Serialise with the interrupt handler and NAPI, then
2309 * disable interrupts. We have to clear the
2310 * irq_enabled flag first to ensure that interrupts
2311 * won't be re-enabled.
2312 */
2313 mdp->irq_enabled = false;
2314 synchronize_irq(ndev->irq);
2315 napi_synchronize(&mdp->napi);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002316 sh_eth_write(ndev, 0x0000, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00002317
Ben Hutchings740c7f32015-01-27 00:49:32 +00002318 sh_eth_dev_exit(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002319
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03002320 /* Free all the skbuffs in the Rx queue and the DMA buffers. */
Ben Hutchings084236d2015-01-22 12:41:34 +00002321 sh_eth_ring_free(ndev);
Ben Hutchings084236d2015-01-22 12:41:34 +00002322 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002323
2324 /* Set new parameters */
2325 mdp->num_rx_ring = ring->rx_pending;
2326 mdp->num_tx_ring = ring->tx_pending;
2327
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002328 if (netif_running(ndev)) {
Ben Hutchings084236d2015-01-22 12:41:34 +00002329 ret = sh_eth_ring_init(ndev);
2330 if (ret < 0) {
2331 netdev_err(ndev, "%s: sh_eth_ring_init failed.\n",
2332 __func__);
2333 return ret;
2334 }
Sergei Shtylyovf7967212016-04-24 19:11:07 +03002335 ret = sh_eth_dev_init(ndev);
Ben Hutchings084236d2015-01-22 12:41:34 +00002336 if (ret < 0) {
2337 netdev_err(ndev, "%s: sh_eth_dev_init failed.\n",
2338 __func__);
2339 return ret;
2340 }
2341
Ben Hutchingsbd888912015-01-22 12:40:25 +00002342 netif_device_attach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002343 }
2344
2345 return 0;
2346}
2347
Niklas Söderlundd8981d02017-01-09 16:34:05 +01002348static void sh_eth_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2349{
2350 struct sh_eth_private *mdp = netdev_priv(ndev);
2351
2352 wol->supported = 0;
2353 wol->wolopts = 0;
2354
Geert Uytterhoevenb4580c92018-02-12 14:42:36 +01002355 if (mdp->cd->magic) {
Niklas Söderlundd8981d02017-01-09 16:34:05 +01002356 wol->supported = WAKE_MAGIC;
2357 wol->wolopts = mdp->wol_enabled ? WAKE_MAGIC : 0;
2358 }
2359}
2360
2361static int sh_eth_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2362{
2363 struct sh_eth_private *mdp = netdev_priv(ndev);
2364
Geert Uytterhoevenb4580c92018-02-12 14:42:36 +01002365 if (!mdp->cd->magic || wol->wolopts & ~WAKE_MAGIC)
Niklas Söderlundd8981d02017-01-09 16:34:05 +01002366 return -EOPNOTSUPP;
2367
2368 mdp->wol_enabled = !!(wol->wolopts & WAKE_MAGIC);
2369
2370 device_set_wakeup_enable(&mdp->pdev->dev, mdp->wol_enabled);
2371
2372 return 0;
2373}
2374
stephen hemminger9b07be42012-01-04 12:59:49 +00002375static const struct ethtool_ops sh_eth_ethtool_ops = {
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002376 .get_regs_len = sh_eth_get_regs_len,
2377 .get_regs = sh_eth_get_regs,
Vladimir Zapolskiy4c106282018-07-04 11:12:42 +03002378 .nway_reset = phy_ethtool_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002379 .get_msglevel = sh_eth_get_msglevel,
2380 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00002381 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002382 .get_strings = sh_eth_get_strings,
2383 .get_ethtool_stats = sh_eth_get_ethtool_stats,
2384 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002385 .get_ringparam = sh_eth_get_ringparam,
2386 .set_ringparam = sh_eth_set_ringparam,
Vladimir Zapolskiy45abbd42018-07-04 11:14:48 +03002387 .get_link_ksettings = phy_ethtool_get_link_ksettings,
Vladimir Zapolskiy6783f502018-07-04 11:14:49 +03002388 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Niklas Söderlundd8981d02017-01-09 16:34:05 +01002389 .get_wol = sh_eth_get_wol,
2390 .set_wol = sh_eth_set_wol,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002391};
2392
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002393/* network device open function */
2394static int sh_eth_open(struct net_device *ndev)
2395{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002396 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03002397 int ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002398
Magnus Dammbcd51492009-10-09 00:20:04 +00002399 pm_runtime_get_sync(&mdp->pdev->dev);
2400
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002401 napi_enable(&mdp->napi);
2402
Joe Perchesa0607fd2009-11-18 23:29:17 -08002403 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00002404 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002405 if (ret) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002406 netdev_err(ndev, "Can not assign IRQ number\n");
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002407 goto out_napi_off;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002408 }
2409
2410 /* Descriptor set */
2411 ret = sh_eth_ring_init(ndev);
2412 if (ret)
2413 goto out_free_irq;
2414
2415 /* device init */
Sergei Shtylyovf7967212016-04-24 19:11:07 +03002416 ret = sh_eth_dev_init(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002417 if (ret)
2418 goto out_free_irq;
2419
2420 /* PHY control start*/
2421 ret = sh_eth_phy_start(ndev);
2422 if (ret)
2423 goto out_free_irq;
2424
Sergei Shtylyovad846aa2016-03-14 01:09:53 +03002425 netif_start_queue(ndev);
2426
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002427 mdp->is_opened = 1;
2428
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002429 return ret;
2430
2431out_free_irq:
2432 free_irq(ndev->irq, ndev);
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002433out_napi_off:
2434 napi_disable(&mdp->napi);
Magnus Dammbcd51492009-10-09 00:20:04 +00002435 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002436 return ret;
2437}
2438
2439/* Timeout function */
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002440static void sh_eth_tx_timeout(struct net_device *ndev, unsigned int txqueue)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002441{
2442 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002443 struct sh_eth_rxdesc *rxdesc;
2444 int i;
2445
2446 netif_stop_queue(ndev);
2447
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002448 netif_err(mdp, timer, ndev,
2449 "transmit timed out, status %8.8x, resetting...\n",
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01002450 sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002451
2452 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002453 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002454
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002455 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002456 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002457 rxdesc = &mdp->rx_ring[i];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002458 rxdesc->status = cpu_to_le32(0);
2459 rxdesc->addr = cpu_to_le32(0xBADF00D0);
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002460 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002461 mdp->rx_skbuff[i] = NULL;
2462 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002463 for (i = 0; i < mdp->num_tx_ring; i++) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002464 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002465 mdp->tx_skbuff[i] = NULL;
2466 }
2467
2468 /* device init */
Sergei Shtylyovf7967212016-04-24 19:11:07 +03002469 sh_eth_dev_init(ndev);
Sergei Shtylyovad846aa2016-03-14 01:09:53 +03002470
2471 netif_start_queue(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002472}
2473
2474/* Packet transmit function */
Yunjian Wang1f3e9152020-05-06 17:25:14 +08002475static netdev_tx_t sh_eth_start_xmit(struct sk_buff *skb,
2476 struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002477{
2478 struct sh_eth_private *mdp = netdev_priv(ndev);
2479 struct sh_eth_txdesc *txdesc;
Sergei Shtylyov12996532015-12-13 23:05:07 +03002480 dma_addr_t dma_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002481 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00002482 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002483
2484 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002485 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03002486 if (!sh_eth_tx_free(ndev, true)) {
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002487 netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002488 netif_stop_queue(ndev);
2489 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00002490 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002491 }
2492 }
2493 spin_unlock_irqrestore(&mdp->lock, flags);
2494
Ben Hutchingsdacc73e2015-03-03 00:53:08 +00002495 if (skb_put_padto(skb, ETH_ZLEN))
Ben Hutchingseebfb642015-01-22 12:40:13 +00002496 return NETDEV_TX_OK;
2497
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002498 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002499 mdp->tx_skbuff[entry] = skb;
2500 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002501 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002502 if (!mdp->cd->hw_swap)
Sergei Shtylyov3e230992015-12-13 21:27:04 +03002503 sh_eth_soft_swap(PTR_ALIGN(skb->data, 4), skb->len + 2);
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01002504 dma_addr = dma_map_single(&mdp->pdev->dev, skb->data, skb->len,
Sergei Shtylyov12996532015-12-13 23:05:07 +03002505 DMA_TO_DEVICE);
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01002506 if (dma_mapping_error(&mdp->pdev->dev, dma_addr)) {
Ben Hutchingsaa3933b2015-01-27 00:49:47 +00002507 kfree_skb(skb);
2508 return NETDEV_TX_OK;
2509 }
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002510 txdesc->addr = cpu_to_le32(dma_addr);
2511 txdesc->len = cpu_to_le32(skb->len << 16);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002512
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03002513 dma_wmb(); /* TACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002514 if (entry >= mdp->num_tx_ring - 1)
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002515 txdesc->status |= cpu_to_le32(TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002516 else
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002517 txdesc->status |= cpu_to_le32(TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002518
2519 mdp->cur_tx++;
2520
Sergei Shtylyov3e416992018-03-24 23:08:42 +03002521 if (!(sh_eth_read(ndev, EDTRR) & mdp->cd->edtrr_trns))
2522 sh_eth_write(ndev, mdp->cd->edtrr_trns, EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09002523
Patrick McHardy6ed10652009-06-23 06:03:08 +00002524 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002525}
2526
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002527/* The statistics registers have write-clear behaviour, which means we
2528 * will lose any increment between the read and write. We mitigate
2529 * this by only clearing when we read a non-zero value, so we will
2530 * never falsely report a total of zero.
2531 */
2532static void
2533sh_eth_update_stat(struct net_device *ndev, unsigned long *stat, int reg)
2534{
2535 u32 delta = sh_eth_read(ndev, reg);
2536
2537 if (delta) {
2538 *stat += delta;
2539 sh_eth_write(ndev, 0, reg);
2540 }
2541}
2542
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002543static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
2544{
2545 struct sh_eth_private *mdp = netdev_priv(ndev);
2546
Sergei Shtylyovce9134d2018-03-24 23:11:19 +03002547 if (mdp->cd->no_tx_cntrs)
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002548 return &ndev->stats;
2549
2550 if (!mdp->is_opened)
2551 return &ndev->stats;
2552
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002553 sh_eth_update_stat(ndev, &ndev->stats.tx_dropped, TROCR);
2554 sh_eth_update_stat(ndev, &ndev->stats.collisions, CDCR);
2555 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, LCCR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002556
Sergei Shtylyov4c1d4582018-03-24 23:12:54 +03002557 if (mdp->cd->cexcr) {
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002558 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2559 CERCR);
2560 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2561 CEECR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002562 } else {
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002563 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2564 CNDCR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002565 }
2566
2567 return &ndev->stats;
2568}
2569
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002570/* device close function */
2571static int sh_eth_close(struct net_device *ndev)
2572{
2573 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002574
2575 netif_stop_queue(ndev);
2576
Ben Hutchings283e38d2015-01-22 12:44:08 +00002577 /* Serialise with the interrupt handler and NAPI, then disable
2578 * interrupts. We have to clear the irq_enabled flag first to
2579 * ensure that interrupts won't be re-enabled.
2580 */
2581 mdp->irq_enabled = false;
2582 synchronize_irq(ndev->irq);
2583 napi_disable(&mdp->napi);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002584 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002585
Ben Hutchings740c7f32015-01-27 00:49:32 +00002586 sh_eth_dev_exit(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002587
2588 /* PHY Disconnect */
Philippe Reynes9fd03752016-08-10 00:04:48 +02002589 if (ndev->phydev) {
2590 phy_stop(ndev->phydev);
2591 phy_disconnect(ndev->phydev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002592 }
2593
2594 free_irq(ndev->irq, ndev);
2595
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03002596 /* Free all the skbuffs in the Rx queue and the DMA buffer. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002597 sh_eth_ring_free(ndev);
2598
Magnus Dammbcd51492009-10-09 00:20:04 +00002599 pm_runtime_put_sync(&mdp->pdev->dev);
2600
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002601 mdp->is_opened = 0;
2602
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002603 return 0;
2604}
2605
Niklas Söderlund78d61022017-06-12 10:39:03 +02002606static int sh_eth_change_mtu(struct net_device *ndev, int new_mtu)
2607{
2608 if (netif_running(ndev))
2609 return -EBUSY;
2610
2611 ndev->mtu = new_mtu;
2612 netdev_update_features(ndev);
2613
2614 return 0;
2615}
2616
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002617/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002618static u32 sh_eth_tsu_get_post_mask(int entry)
2619{
2620 return 0x0f << (28 - ((entry % 8) * 4));
2621}
2622
2623static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2624{
2625 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2626}
2627
2628static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2629 int entry)
2630{
2631 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov77cb0652018-05-02 22:54:48 +03002632 int reg = TSU_POST1 + entry / 8;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002633 u32 tmp;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002634
Sergei Shtylyov77cb0652018-05-02 22:54:48 +03002635 tmp = sh_eth_tsu_read(mdp, reg);
2636 sh_eth_tsu_write(mdp, tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002637}
2638
2639static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2640 int entry)
2641{
2642 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov77cb0652018-05-02 22:54:48 +03002643 int reg = TSU_POST1 + entry / 8;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002644 u32 post_mask, ref_mask, tmp;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002645
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002646 post_mask = sh_eth_tsu_get_post_mask(entry);
2647 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2648
Sergei Shtylyov77cb0652018-05-02 22:54:48 +03002649 tmp = sh_eth_tsu_read(mdp, reg);
2650 sh_eth_tsu_write(mdp, tmp & ~post_mask, reg);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002651
2652 /* If other port enables, the function returns "true" */
2653 return tmp & ref_mask;
2654}
2655
2656static int sh_eth_tsu_busy(struct net_device *ndev)
2657{
2658 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2659 struct sh_eth_private *mdp = netdev_priv(ndev);
2660
2661 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2662 udelay(10);
2663 timeout--;
2664 if (timeout <= 0) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002665 netdev_err(ndev, "%s: timeout\n", __func__);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002666 return -ETIMEDOUT;
2667 }
2668 }
2669
2670 return 0;
2671}
2672
Sergei Shtylyov7a54c862018-07-23 21:14:38 +03002673static int sh_eth_tsu_write_entry(struct net_device *ndev, u16 offset,
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002674 const u8 *addr)
2675{
Sergei Shtylyov7a54c862018-07-23 21:14:38 +03002676 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002677 u32 val;
2678
2679 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
Sergei Shtylyov7a54c862018-07-23 21:14:38 +03002680 iowrite32(val, mdp->tsu_addr + offset);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002681 if (sh_eth_tsu_busy(ndev) < 0)
2682 return -EBUSY;
2683
2684 val = addr[4] << 8 | addr[5];
Sergei Shtylyov7a54c862018-07-23 21:14:38 +03002685 iowrite32(val, mdp->tsu_addr + offset + 4);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002686 if (sh_eth_tsu_busy(ndev) < 0)
2687 return -EBUSY;
2688
2689 return 0;
2690}
2691
Sergei Shtylyov51459d42018-07-23 21:15:47 +03002692static void sh_eth_tsu_read_entry(struct net_device *ndev, u16 offset, u8 *addr)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002693{
Sergei Shtylyov51459d42018-07-23 21:15:47 +03002694 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002695 u32 val;
2696
Sergei Shtylyov51459d42018-07-23 21:15:47 +03002697 val = ioread32(mdp->tsu_addr + offset);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002698 addr[0] = (val >> 24) & 0xff;
2699 addr[1] = (val >> 16) & 0xff;
2700 addr[2] = (val >> 8) & 0xff;
2701 addr[3] = val & 0xff;
Sergei Shtylyov51459d42018-07-23 21:15:47 +03002702 val = ioread32(mdp->tsu_addr + offset + 4);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002703 addr[4] = (val >> 8) & 0xff;
2704 addr[5] = val & 0xff;
2705}
2706
2707
2708static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2709{
2710 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov41414f02018-07-23 21:11:19 +03002711 u16 reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002712 int i;
2713 u8 c_addr[ETH_ALEN];
2714
2715 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
Sergei Shtylyov51459d42018-07-23 21:15:47 +03002716 sh_eth_tsu_read_entry(ndev, reg_offset, c_addr);
dingtianhongc4bde292013-12-30 15:41:17 +08002717 if (ether_addr_equal(addr, c_addr))
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002718 return i;
2719 }
2720
2721 return -ENOENT;
2722}
2723
2724static int sh_eth_tsu_find_empty(struct net_device *ndev)
2725{
2726 u8 blank[ETH_ALEN];
2727 int entry;
2728
2729 memset(blank, 0, sizeof(blank));
2730 entry = sh_eth_tsu_find_entry(ndev, blank);
2731 return (entry < 0) ? -ENOMEM : entry;
2732}
2733
2734static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2735 int entry)
2736{
2737 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov41414f02018-07-23 21:11:19 +03002738 u16 reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002739 int ret;
2740 u8 blank[ETH_ALEN];
2741
2742 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2743 ~(1 << (31 - entry)), TSU_TEN);
2744
2745 memset(blank, 0, sizeof(blank));
Sergei Shtylyov7a54c862018-07-23 21:14:38 +03002746 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002747 if (ret < 0)
2748 return ret;
2749 return 0;
2750}
2751
2752static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2753{
2754 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov41414f02018-07-23 21:11:19 +03002755 u16 reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002756 int i, ret;
2757
2758 if (!mdp->cd->tsu)
2759 return 0;
2760
2761 i = sh_eth_tsu_find_entry(ndev, addr);
2762 if (i < 0) {
2763 /* No entry found, create one */
2764 i = sh_eth_tsu_find_empty(ndev);
2765 if (i < 0)
2766 return -ENOMEM;
Sergei Shtylyov7a54c862018-07-23 21:14:38 +03002767 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002768 if (ret < 0)
2769 return ret;
2770
2771 /* Enable the entry */
2772 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2773 (1 << (31 - i)), TSU_TEN);
2774 }
2775
2776 /* Entry found or created, enable POST */
2777 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2778
2779 return 0;
2780}
2781
2782static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2783{
2784 struct sh_eth_private *mdp = netdev_priv(ndev);
2785 int i, ret;
2786
2787 if (!mdp->cd->tsu)
2788 return 0;
2789
2790 i = sh_eth_tsu_find_entry(ndev, addr);
2791 if (i) {
2792 /* Entry found */
2793 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2794 goto done;
2795
2796 /* Disable the entry if both ports was disabled */
2797 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2798 if (ret < 0)
2799 return ret;
2800 }
2801done:
2802 return 0;
2803}
2804
2805static int sh_eth_tsu_purge_all(struct net_device *ndev)
2806{
2807 struct sh_eth_private *mdp = netdev_priv(ndev);
2808 int i, ret;
2809
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002810 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002811 return 0;
2812
2813 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2814 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2815 continue;
2816
2817 /* Disable the entry if both ports was disabled */
2818 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2819 if (ret < 0)
2820 return ret;
2821 }
2822
2823 return 0;
2824}
2825
2826static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2827{
2828 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov41414f02018-07-23 21:11:19 +03002829 u16 reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002830 u8 addr[ETH_ALEN];
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002831 int i;
2832
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002833 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002834 return;
2835
2836 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
Sergei Shtylyov51459d42018-07-23 21:15:47 +03002837 sh_eth_tsu_read_entry(ndev, reg_offset, addr);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002838 if (is_multicast_ether_addr(addr))
2839 sh_eth_tsu_del_entry(ndev, addr);
2840 }
2841}
2842
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002843/* Update promiscuous flag and multicast filter */
2844static void sh_eth_set_rx_mode(struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002845{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002846 struct sh_eth_private *mdp = netdev_priv(ndev);
2847 u32 ecmr_bits;
2848 int mcast_all = 0;
2849 unsigned long flags;
2850
2851 spin_lock_irqsave(&mdp->lock, flags);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002852 /* Initial condition is MCT = 1, PRM = 0.
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002853 * Depending on ndev->flags, set PRM or clear MCT
2854 */
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002855 ecmr_bits = sh_eth_read(ndev, ECMR) & ~ECMR_PRM;
2856 if (mdp->cd->tsu)
2857 ecmr_bits |= ECMR_MCT;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002858
2859 if (!(ndev->flags & IFF_MULTICAST)) {
2860 sh_eth_tsu_purge_mcast(ndev);
2861 mcast_all = 1;
2862 }
2863 if (ndev->flags & IFF_ALLMULTI) {
2864 sh_eth_tsu_purge_mcast(ndev);
2865 ecmr_bits &= ~ECMR_MCT;
2866 mcast_all = 1;
2867 }
2868
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002869 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002870 sh_eth_tsu_purge_all(ndev);
2871 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2872 } else if (mdp->cd->tsu) {
2873 struct netdev_hw_addr *ha;
2874 netdev_for_each_mc_addr(ha, ndev) {
2875 if (mcast_all && is_multicast_ether_addr(ha->addr))
2876 continue;
2877
2878 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2879 if (!mcast_all) {
2880 sh_eth_tsu_purge_mcast(ndev);
2881 ecmr_bits &= ~ECMR_MCT;
2882 mcast_all = 1;
2883 }
2884 }
2885 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002886 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002887
2888 /* update the ethernet mode */
2889 sh_eth_write(ndev, ecmr_bits, ECMR);
2890
2891 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002892}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002893
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03002894static void sh_eth_set_rx_csum(struct net_device *ndev, bool enable)
2895{
2896 struct sh_eth_private *mdp = netdev_priv(ndev);
2897 unsigned long flags;
2898
2899 spin_lock_irqsave(&mdp->lock, flags);
2900
2901 /* Disable TX and RX */
2902 sh_eth_rcv_snd_disable(ndev);
2903
2904 /* Modify RX Checksum setting */
2905 sh_eth_modify(ndev, ECMR, ECMR_RCSC, enable ? ECMR_RCSC : 0);
2906
2907 /* Enable TX and RX */
2908 sh_eth_rcv_snd_enable(ndev);
2909
2910 spin_unlock_irqrestore(&mdp->lock, flags);
2911}
2912
2913static int sh_eth_set_features(struct net_device *ndev,
2914 netdev_features_t features)
2915{
2916 netdev_features_t changed = ndev->features ^ features;
2917 struct sh_eth_private *mdp = netdev_priv(ndev);
2918
2919 if (changed & NETIF_F_RXCSUM && mdp->cd->rx_csum)
2920 sh_eth_set_rx_csum(ndev, features & NETIF_F_RXCSUM);
2921
2922 ndev->features = features;
2923
2924 return 0;
2925}
2926
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002927static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2928{
2929 if (!mdp->port)
2930 return TSU_VTAG0;
2931 else
2932 return TSU_VTAG1;
2933}
2934
Patrick McHardy80d5c362013-04-19 02:04:28 +00002935static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2936 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002937{
2938 struct sh_eth_private *mdp = netdev_priv(ndev);
2939 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2940
2941 if (unlikely(!mdp->cd->tsu))
2942 return -EPERM;
2943
2944 /* No filtering if vid = 0 */
2945 if (!vid)
2946 return 0;
2947
2948 mdp->vlan_num_ids++;
2949
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002950 /* The controller has one VLAN tag HW filter. So, if the filter is
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002951 * already enabled, the driver disables it and the filte
2952 */
2953 if (mdp->vlan_num_ids > 1) {
2954 /* disable VLAN filter */
2955 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2956 return 0;
2957 }
2958
2959 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2960 vtag_reg_index);
2961
2962 return 0;
2963}
2964
Patrick McHardy80d5c362013-04-19 02:04:28 +00002965static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2966 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002967{
2968 struct sh_eth_private *mdp = netdev_priv(ndev);
2969 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2970
2971 if (unlikely(!mdp->cd->tsu))
2972 return -EPERM;
2973
2974 /* No filtering if vid = 0 */
2975 if (!vid)
2976 return 0;
2977
2978 mdp->vlan_num_ids--;
2979 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2980
2981 return 0;
2982}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002983
2984/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002985static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002986{
Sergei Shtylyova94cf2a2018-02-24 22:41:45 +03002987 if (!mdp->cd->dual_port) {
Simon Hormandb893472014-01-17 09:22:28 +09002988 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
Chris Brandte1487882016-09-07 14:57:09 -04002989 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL,
2990 TSU_FWSLC); /* Enable POST registers */
Simon Hormandb893472014-01-17 09:22:28 +09002991 return;
2992 }
2993
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002994 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2995 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2996 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2997 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2998 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2999 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
3000 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
3001 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
3002 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
3003 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Sergei Shtylyov4869a142018-02-24 20:28:16 +03003004 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
3005 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00003006 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
3007 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
3008 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
3009 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
3010 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
3011 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
3012 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003013}
3014
3015/* MDIO bus release function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01003016static int sh_mdio_release(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003017{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003018 /* unregister mdio bus */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01003019 mdiobus_unregister(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003020
3021 /* free bitbang info */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01003022 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003023
3024 return 0;
3025}
3026
3027/* MDIO bus init function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01003028static int sh_mdio_init(struct sh_eth_private *mdp,
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00003029 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003030{
Andrew Lunne7f4dc32016-01-06 20:11:15 +01003031 int ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003032 struct bb_info *bitbang;
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01003033 struct platform_device *pdev = mdp->pdev;
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01003034 struct device *dev = &mdp->pdev->dev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003035
3036 /* create bit control struct for PHY */
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01003037 bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL);
Laurent Pinchartf738a132014-03-20 15:00:35 +01003038 if (!bitbang)
3039 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003040
3041 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00003042 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00003043 bitbang->set_gate = pd->set_mdio_gate;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003044 bitbang->ctrl.ops = &bb_ops;
3045
Stefan Weilc2e07b32010-08-03 19:44:52 +02003046 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003047 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
Laurent Pinchartf738a132014-03-20 15:00:35 +01003048 if (!mdp->mii_bus)
3049 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003050
3051 /* Hook up MII support for ethtool */
3052 mdp->mii_bus->name = "sh_mii";
Laurent Pincharta5bd60602014-03-20 15:00:32 +01003053 mdp->mii_bus->parent = dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00003054 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01003055 pdev->name, pdev->id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003056
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01003057 /* register MDIO bus */
Florian Fainelli00e798c2018-05-15 16:56:19 -07003058 if (pd->phy_irq > 0)
3059 mdp->mii_bus->irq[pd->phy] = pd->phy_irq;
Ben Dooks702eca02014-03-12 17:47:40 +00003060
Florian Fainelli00e798c2018-05-15 16:56:19 -07003061 ret = of_mdiobus_register(mdp->mii_bus, dev->of_node);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003062 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00003063 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003064
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003065 return 0;
3066
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003067out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003068 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003069 return ret;
3070}
3071
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00003072static const u16 *sh_eth_get_register_offset(int register_type)
3073{
3074 const u16 *reg_offset = NULL;
3075
3076 switch (register_type) {
3077 case SH_ETH_REG_GIGABIT:
3078 reg_offset = sh_eth_offset_gigabit;
3079 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00003080 case SH_ETH_REG_FAST_RCAR:
3081 reg_offset = sh_eth_offset_fast_rcar;
3082 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00003083 case SH_ETH_REG_FAST_SH4:
3084 reg_offset = sh_eth_offset_fast_sh4;
3085 break;
3086 case SH_ETH_REG_FAST_SH3_SH2:
3087 reg_offset = sh_eth_offset_fast_sh3_sh2;
3088 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00003089 }
3090
3091 return reg_offset;
3092}
3093
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003094static const struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00003095 .ndo_open = sh_eth_open,
3096 .ndo_stop = sh_eth_close,
3097 .ndo_start_xmit = sh_eth_start_xmit,
3098 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00003099 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00003100 .ndo_tx_timeout = sh_eth_tx_timeout,
Heiner Kallweitfd786fb12020-01-21 22:09:33 +01003101 .ndo_do_ioctl = phy_do_ioctl_running,
Niklas Söderlund78d61022017-06-12 10:39:03 +02003102 .ndo_change_mtu = sh_eth_change_mtu,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00003103 .ndo_validate_addr = eth_validate_addr,
3104 .ndo_set_mac_address = eth_mac_addr,
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03003105 .ndo_set_features = sh_eth_set_features,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00003106};
3107
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003108static const struct net_device_ops sh_eth_netdev_ops_tsu = {
3109 .ndo_open = sh_eth_open,
3110 .ndo_stop = sh_eth_close,
3111 .ndo_start_xmit = sh_eth_start_xmit,
3112 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00003113 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003114 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
3115 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
3116 .ndo_tx_timeout = sh_eth_tx_timeout,
Heiner Kallweitfd786fb12020-01-21 22:09:33 +01003117 .ndo_do_ioctl = phy_do_ioctl_running,
Niklas Söderlund78d61022017-06-12 10:39:03 +02003118 .ndo_change_mtu = sh_eth_change_mtu,
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003119 .ndo_validate_addr = eth_validate_addr,
3120 .ndo_set_mac_address = eth_mac_addr,
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03003121 .ndo_set_features = sh_eth_set_features,
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003122};
3123
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003124#ifdef CONFIG_OF
3125static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
3126{
3127 struct device_node *np = dev->of_node;
3128 struct sh_eth_plat_data *pdata;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01003129 phy_interface_t interface;
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003130 const char *mac_addr;
Kangjie Lu035a14e2019-03-12 02:43:18 -05003131 int ret;
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003132
3133 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
3134 if (!pdata)
3135 return NULL;
3136
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01003137 ret = of_get_phy_mode(np, &interface);
3138 if (ret)
Kangjie Lu035a14e2019-03-12 02:43:18 -05003139 return NULL;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01003140 pdata->phy_interface = interface;
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003141
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003142 mac_addr = of_get_mac_address(np);
Petr Štetiara51645f2019-05-06 23:27:04 +02003143 if (!IS_ERR(mac_addr))
Petr Štetiar2d2924a2019-05-10 11:35:17 +02003144 ether_addr_copy(pdata->mac_addr, mac_addr);
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003145
3146 pdata->no_ether_link =
3147 of_property_read_bool(np, "renesas,no-ether-link");
3148 pdata->ether_link_active_low =
3149 of_property_read_bool(np, "renesas,ether-link-active-low");
3150
3151 return pdata;
3152}
3153
3154static const struct of_device_id sh_eth_match_table[] = {
3155 { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data },
Simon Horman6c4b2f72017-10-18 09:21:27 +02003156 { .compatible = "renesas,ether-r8a7743", .data = &rcar_gen2_data },
3157 { .compatible = "renesas,ether-r8a7745", .data = &rcar_gen2_data },
3158 { .compatible = "renesas,ether-r8a7778", .data = &rcar_gen1_data },
3159 { .compatible = "renesas,ether-r8a7779", .data = &rcar_gen1_data },
3160 { .compatible = "renesas,ether-r8a7790", .data = &rcar_gen2_data },
3161 { .compatible = "renesas,ether-r8a7791", .data = &rcar_gen2_data },
3162 { .compatible = "renesas,ether-r8a7793", .data = &rcar_gen2_data },
3163 { .compatible = "renesas,ether-r8a7794", .data = &rcar_gen2_data },
Sergei Shtylyov3eb9c2a2018-05-18 21:32:46 +03003164 { .compatible = "renesas,gether-r8a77980", .data = &r8a77980_data },
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003165 { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data },
Chris Brandt6e0bb042018-08-27 12:42:02 -05003166 { .compatible = "renesas,ether-r7s9210", .data = &r7s9210_data },
Simon Hormanb4804e02017-10-18 09:21:28 +02003167 { .compatible = "renesas,rcar-gen1-ether", .data = &rcar_gen1_data },
3168 { .compatible = "renesas,rcar-gen2-ether", .data = &rcar_gen2_data },
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003169 { }
3170};
3171MODULE_DEVICE_TABLE(of, sh_eth_match_table);
3172#else
3173static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
3174{
3175 return NULL;
3176}
3177#endif
3178
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003179static int sh_eth_drv_probe(struct platform_device *pdev)
3180{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003181 struct resource *res;
Jingoo Han0b76b862013-08-30 14:00:11 +09003182 struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev);
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003183 const struct platform_device_id *id = platform_get_device_id(pdev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03003184 struct sh_eth_private *mdp;
3185 struct net_device *ndev;
Sergei Shtylyov9662ec12018-01-14 20:47:44 +03003186 int ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003187
3188 /* get base addr */
3189 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003190
3191 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
Laurent Pinchartf738a132014-03-20 15:00:35 +01003192 if (!ndev)
3193 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003194
Ben Dooksb5893a02014-03-21 12:09:14 +01003195 pm_runtime_enable(&pdev->dev);
3196 pm_runtime_get_sync(&pdev->dev);
3197
roel kluincc3c0802008-09-10 19:22:44 +02003198 ret = platform_get_irq(pdev, 0);
Sergei Shtylyov7a468ac2015-08-28 16:56:01 +03003199 if (ret < 0)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003200 goto out_release;
roel kluincc3c0802008-09-10 19:22:44 +02003201 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003202
3203 SET_NETDEV_DEV(ndev, &pdev->dev);
3204
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003205 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00003206 mdp->num_tx_ring = TX_RING_SIZE;
3207 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00003208 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
3209 if (IS_ERR(mdp->addr)) {
3210 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00003211 goto out_release;
3212 }
3213
Varka Bhadramc9608042014-10-24 07:42:09 +05303214 ndev->base_addr = res->start;
3215
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003216 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00003217 mdp->pdev = pdev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003218
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003219 if (pdev->dev.of_node)
3220 pd = sh_eth_parse_dt(&pdev->dev);
Sergei Shtylyov3b4c5cb2013-10-30 23:30:19 +03003221 if (!pd) {
3222 dev_err(&pdev->dev, "no platform data\n");
3223 ret = -EINVAL;
3224 goto out_release;
3225 }
3226
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003227 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04003228 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00003229 mdp->phy_interface = pd->phy_interface;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00003230 mdp->no_ether_link = pd->no_ether_link;
3231 mdp->ether_link_active_low = pd->ether_link_active_low;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003232
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003233 /* set cpu data */
Wolfram Sang42a67c92016-03-01 17:37:59 +01003234 if (id)
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003235 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
Wolfram Sang42a67c92016-03-01 17:37:59 +01003236 else
3237 mdp->cd = (struct sh_eth_cpu_data *)of_device_get_match_data(&pdev->dev);
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003238
Sergei Shtylyova3153d82013-08-18 03:11:28 +04003239 mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type);
Sergei Shtylyov264be2f2014-03-15 03:11:24 +03003240 if (!mdp->reg_offset) {
3241 dev_err(&pdev->dev, "Unknown register type (%d)\n",
3242 mdp->cd->register_type);
3243 ret = -EINVAL;
3244 goto out_release;
3245 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003246 sh_eth_set_default_cpu_data(mdp->cd);
3247
Niklas Söderlund78d61022017-06-12 10:39:03 +02003248 /* User's manual states max MTU should be 2048 but due to the
3249 * alignment calculations in sh_eth_ring_init() the practical
3250 * MTU is a bit less. Maybe this can be optimized some more.
3251 */
3252 ndev->max_mtu = 2000 - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
3253 ndev->min_mtu = ETH_MIN_MTU;
3254
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03003255 if (mdp->cd->rx_csum) {
3256 ndev->features = NETIF_F_RXCSUM;
3257 ndev->hw_features = NETIF_F_RXCSUM;
3258 }
3259
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003260 /* set function */
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003261 if (mdp->cd->tsu)
3262 ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
3263 else
3264 ndev->netdev_ops = &sh_eth_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003265 ndev->ethtool_ops = &sh_eth_ethtool_ops;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003266 ndev->watchdog_timeo = TX_TIMEOUT;
3267
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00003268 /* debug message level */
3269 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003270
3271 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00003272 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00003273 if (!is_valid_ether_addr(ndev->dev_addr)) {
3274 dev_warn(&pdev->dev,
3275 "no valid MAC address supplied, using a random one.\n");
3276 eth_hw_addr_random(ndev);
3277 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003278
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003279 if (mdp->cd->tsu) {
Sergei Shtylyov9662ec12018-01-14 20:47:44 +03003280 int port = pdev->id < 0 ? 0 : pdev->id % 2;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003281 struct resource *rtsu;
Sergei Shtylyovdfe82662018-01-03 20:09:49 +03003282
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003283 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovdfe82662018-01-03 20:09:49 +03003284 if (!rtsu) {
3285 dev_err(&pdev->dev, "no TSU resource\n");
3286 ret = -ENODEV;
3287 goto out_release;
3288 }
3289 /* We can only request the TSU region for the first port
3290 * of the two sharing this TSU for the probe to succeed...
3291 */
Sergei Shtylyov9662ec12018-01-14 20:47:44 +03003292 if (port == 0 &&
Sergei Shtylyovdfe82662018-01-03 20:09:49 +03003293 !devm_request_mem_region(&pdev->dev, rtsu->start,
3294 resource_size(rtsu),
3295 dev_name(&pdev->dev))) {
3296 dev_err(&pdev->dev, "can't request TSU resource.\n");
3297 ret = -EBUSY;
3298 goto out_release;
3299 }
Sergei Shtylyov3e14c962018-01-14 20:47:43 +03003300 /* ioremap the TSU registers */
Sergei Shtylyovdfe82662018-01-03 20:09:49 +03003301 mdp->tsu_addr = devm_ioremap(&pdev->dev, rtsu->start,
3302 resource_size(rtsu));
3303 if (!mdp->tsu_addr) {
3304 dev_err(&pdev->dev, "TSU region ioremap() failed.\n");
3305 ret = -ENOMEM;
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00003306 goto out_release;
3307 }
Sergei Shtylyov9662ec12018-01-14 20:47:44 +03003308 mdp->port = port;
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03003309 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003310
Sergei Shtylyov3e14c962018-01-14 20:47:43 +03003311 /* Need to init only the first port of the two sharing a TSU */
Sergei Shtylyov9662ec12018-01-14 20:47:44 +03003312 if (port == 0) {
Sergei Shtylyov3e14c962018-01-14 20:47:43 +03003313 if (mdp->cd->chip_reset)
3314 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003315
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00003316 /* TSU init (Init only)*/
3317 sh_eth_tsu_init(mdp);
3318 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003319 }
3320
Hisashi Nakamura966d6db2014-11-13 15:54:05 +09003321 if (mdp->cd->rmiimode)
3322 sh_eth_write(ndev, 0x1, RMIIMODE);
3323
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003324 /* MDIO bus init */
3325 ret = sh_mdio_init(mdp, pd);
3326 if (ret) {
Geert Uytterhoevenb7ce5202017-05-18 15:01:35 +02003327 if (ret != -EPROBE_DEFER)
3328 dev_err(&pdev->dev, "MDIO init failed: %d\n", ret);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003329 goto out_release;
3330 }
3331
Sergei Shtylyov37191092013-06-19 23:30:23 +04003332 netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
3333
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003334 /* network device register */
3335 ret = register_netdev(ndev);
3336 if (ret)
Sergei Shtylyov37191092013-06-19 23:30:23 +04003337 goto out_napi_del;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003338
Geert Uytterhoevenb4580c92018-02-12 14:42:36 +01003339 if (mdp->cd->magic)
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003340 device_set_wakeup_capable(&pdev->dev, 1);
3341
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003342 /* print device information */
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +03003343 netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n",
3344 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003345
Ben Dooksb5893a02014-03-21 12:09:14 +01003346 pm_runtime_put(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003347 platform_set_drvdata(pdev, ndev);
3348
3349 return ret;
3350
Sergei Shtylyov37191092013-06-19 23:30:23 +04003351out_napi_del:
3352 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003353 sh_mdio_release(mdp);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003354
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003355out_release:
3356 /* net_dev free */
Sergei Shtylyov4282fc42017-12-31 21:41:36 +03003357 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003358
Ben Dooksb5893a02014-03-21 12:09:14 +01003359 pm_runtime_put(&pdev->dev);
3360 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003361 return ret;
3362}
3363
3364static int sh_eth_drv_remove(struct platform_device *pdev)
3365{
3366 struct net_device *ndev = platform_get_drvdata(pdev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003367 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003368
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003369 unregister_netdev(ndev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003370 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003371 sh_mdio_release(mdp);
Magnus Dammbcd51492009-10-09 00:20:04 +00003372 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003373 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003374
3375 return 0;
3376}
3377
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003378#ifdef CONFIG_PM
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003379#ifdef CONFIG_PM_SLEEP
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003380static int sh_eth_wol_setup(struct net_device *ndev)
3381{
3382 struct sh_eth_private *mdp = netdev_priv(ndev);
3383
3384 /* Only allow ECI interrupts */
3385 synchronize_irq(ndev->irq);
3386 napi_disable(&mdp->napi);
Sergei Shtylyov1a0bee62017-01-29 15:07:34 +03003387 sh_eth_write(ndev, EESIPR_ECIIP, EESIPR);
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003388
3389 /* Enable MagicPacket */
Niklas Söderlund5e2ed132017-02-01 15:41:54 +01003390 sh_eth_modify(ndev, ECMR, ECMR_MPDE, ECMR_MPDE);
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003391
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003392 return enable_irq_wake(ndev->irq);
3393}
3394
3395static int sh_eth_wol_restore(struct net_device *ndev)
3396{
3397 struct sh_eth_private *mdp = netdev_priv(ndev);
3398 int ret;
3399
3400 napi_enable(&mdp->napi);
3401
3402 /* Disable MagicPacket */
3403 sh_eth_modify(ndev, ECMR, ECMR_MPDE, 0);
3404
3405 /* The device needs to be reset to restore MagicPacket logic
3406 * for next wakeup. If we close and open the device it will
3407 * both be reset and all registers restored. This is what
3408 * happens during suspend and resume without WoL enabled.
3409 */
3410 ret = sh_eth_close(ndev);
3411 if (ret < 0)
3412 return ret;
3413 ret = sh_eth_open(ndev);
3414 if (ret < 0)
3415 return ret;
3416
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003417 return disable_irq_wake(ndev->irq);
3418}
3419
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003420static int sh_eth_suspend(struct device *dev)
3421{
3422 struct net_device *ndev = dev_get_drvdata(dev);
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003423 struct sh_eth_private *mdp = netdev_priv(ndev);
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003424 int ret = 0;
3425
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003426 if (!netif_running(ndev))
3427 return 0;
3428
3429 netif_device_detach(ndev);
3430
3431 if (mdp->wol_enabled)
3432 ret = sh_eth_wol_setup(ndev);
3433 else
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003434 ret = sh_eth_close(ndev);
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003435
3436 return ret;
3437}
3438
3439static int sh_eth_resume(struct device *dev)
3440{
3441 struct net_device *ndev = dev_get_drvdata(dev);
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003442 struct sh_eth_private *mdp = netdev_priv(ndev);
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003443 int ret = 0;
3444
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003445 if (!netif_running(ndev))
3446 return 0;
3447
3448 if (mdp->wol_enabled)
3449 ret = sh_eth_wol_restore(ndev);
3450 else
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003451 ret = sh_eth_open(ndev);
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003452
3453 if (ret < 0)
3454 return ret;
3455
3456 netif_device_attach(ndev);
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003457
3458 return ret;
3459}
3460#endif
3461
Magnus Dammbcd51492009-10-09 00:20:04 +00003462static int sh_eth_runtime_nop(struct device *dev)
3463{
Sergei Shtylyov128296f2014-01-03 15:52:22 +03003464 /* Runtime PM callback shared between ->runtime_suspend()
Magnus Dammbcd51492009-10-09 00:20:04 +00003465 * and ->runtime_resume(). Simply returns success.
3466 *
3467 * This driver re-initializes all registers after
3468 * pm_runtime_get_sync() anyway so there is no need
3469 * to save and restore registers here.
3470 */
3471 return 0;
3472}
3473
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003474static const struct dev_pm_ops sh_eth_dev_pm_ops = {
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003475 SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend, sh_eth_resume)
Mikhail Ulyanove7d7e892015-01-22 01:18:44 +03003476 SET_RUNTIME_PM_OPS(sh_eth_runtime_nop, sh_eth_runtime_nop, NULL)
Magnus Dammbcd51492009-10-09 00:20:04 +00003477};
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003478#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3479#else
3480#define SH_ETH_PM_OPS NULL
3481#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00003482
Arvind Yadavef00df82017-08-13 16:42:42 +05303483static const struct platform_device_id sh_eth_id_table[] = {
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00003484 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00003485 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +00003486 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003487 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
Sergei Shtylyov24549e22013-06-07 13:59:21 +00003488 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
3489 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003490 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003491 { }
3492};
3493MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
3494
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003495static struct platform_driver sh_eth_driver = {
3496 .probe = sh_eth_drv_probe,
3497 .remove = sh_eth_drv_remove,
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003498 .id_table = sh_eth_id_table,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003499 .driver = {
3500 .name = CARDNAME,
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003501 .pm = SH_ETH_PM_OPS,
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003502 .of_match_table = of_match_ptr(sh_eth_match_table),
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003503 },
3504};
3505
Axel Lindb62f682011-11-27 16:44:17 +00003506module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003507
3508MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3509MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3510MODULE_LICENSE("GPL v2");