blob: 8733afe764b434ef52c0d5834ea92f2f7744415e [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
Simon Hormandb893472014-01-17 09:22:28 +0900145static const u16 sh_eth_offset_fast_rz[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000146 SH_ETH_OFFSET_DEFAULTS,
147
Simon Hormandb893472014-01-17 09:22:28 +0900148 [EDSR] = 0x0000,
149 [EDMR] = 0x0400,
150 [EDTRR] = 0x0408,
151 [EDRRR] = 0x0410,
152 [EESR] = 0x0428,
153 [EESIPR] = 0x0430,
154 [TDLAR] = 0x0010,
155 [TDFAR] = 0x0014,
156 [TDFXR] = 0x0018,
157 [TDFFR] = 0x001c,
158 [RDLAR] = 0x0030,
159 [RDFAR] = 0x0034,
160 [RDFXR] = 0x0038,
161 [RDFFR] = 0x003c,
162 [TRSCER] = 0x0438,
163 [RMFCR] = 0x0440,
164 [TFTR] = 0x0448,
165 [FDR] = 0x0450,
166 [RMCR] = 0x0458,
167 [RPADIR] = 0x0460,
168 [FCFTR] = 0x0468,
169 [CSMR] = 0x04E4,
170
171 [ECMR] = 0x0500,
172 [RFLR] = 0x0508,
173 [ECSR] = 0x0510,
174 [ECSIPR] = 0x0518,
175 [PIR] = 0x0520,
176 [APR] = 0x0554,
177 [MPR] = 0x0558,
178 [PFTCR] = 0x055c,
179 [PFRCR] = 0x0560,
180 [TPAUSER] = 0x0564,
181 [MAHR] = 0x05c0,
182 [MALR] = 0x05c8,
183 [CEFCR] = 0x0740,
184 [FRECR] = 0x0748,
185 [TSFRCR] = 0x0750,
186 [TLFRCR] = 0x0758,
187 [RFCR] = 0x0760,
188 [MAFCR] = 0x0778,
189
190 [ARSTR] = 0x0000,
191 [TSU_CTRST] = 0x0004,
Chris Brandte1487882016-09-07 14:57:09 -0400192 [TSU_FWSLC] = 0x0038,
Simon Hormandb893472014-01-17 09:22:28 +0900193 [TSU_VTAG0] = 0x0058,
194 [TSU_ADSBSY] = 0x0060,
195 [TSU_TEN] = 0x0064,
Chris Brandte1487882016-09-07 14:57:09 -0400196 [TSU_POST1] = 0x0070,
197 [TSU_POST2] = 0x0074,
198 [TSU_POST3] = 0x0078,
199 [TSU_POST4] = 0x007c,
Simon Hormandb893472014-01-17 09:22:28 +0900200 [TSU_ADRH0] = 0x0100,
Simon Hormandb893472014-01-17 09:22:28 +0900201
202 [TXNLCR0] = 0x0080,
203 [TXALCR0] = 0x0084,
204 [RXNLCR0] = 0x0088,
205 [RXALCR0] = 0x008C,
206};
207
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000208static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000209 SH_ETH_OFFSET_DEFAULTS,
210
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000211 [ECMR] = 0x0300,
212 [RFLR] = 0x0308,
213 [ECSR] = 0x0310,
214 [ECSIPR] = 0x0318,
215 [PIR] = 0x0320,
216 [PSR] = 0x0328,
217 [RDMLR] = 0x0340,
218 [IPGR] = 0x0350,
219 [APR] = 0x0354,
220 [MPR] = 0x0358,
221 [RFCF] = 0x0360,
222 [TPAUSER] = 0x0364,
223 [TPAUSECR] = 0x0368,
224 [MAHR] = 0x03c0,
225 [MALR] = 0x03c8,
226 [TROCR] = 0x03d0,
227 [CDCR] = 0x03d4,
228 [LCCR] = 0x03d8,
229 [CNDCR] = 0x03dc,
230 [CEFCR] = 0x03e4,
231 [FRECR] = 0x03e8,
232 [TSFRCR] = 0x03ec,
233 [TLFRCR] = 0x03f0,
234 [RFCR] = 0x03f4,
235 [MAFCR] = 0x03f8,
236
237 [EDMR] = 0x0200,
238 [EDTRR] = 0x0208,
239 [EDRRR] = 0x0210,
240 [TDLAR] = 0x0218,
241 [RDLAR] = 0x0220,
242 [EESR] = 0x0228,
243 [EESIPR] = 0x0230,
244 [TRSCER] = 0x0238,
245 [RMFCR] = 0x0240,
246 [TFTR] = 0x0248,
247 [FDR] = 0x0250,
248 [RMCR] = 0x0258,
249 [TFUCR] = 0x0264,
250 [RFOCR] = 0x0268,
Simon Horman55754f12013-07-23 10:18:04 +0900251 [RMIIMODE] = 0x026c,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000252 [FCFTR] = 0x0270,
253 [TRIMD] = 0x027c,
254};
255
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000256static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000257 SH_ETH_OFFSET_DEFAULTS,
258
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000259 [ECMR] = 0x0100,
260 [RFLR] = 0x0108,
261 [ECSR] = 0x0110,
262 [ECSIPR] = 0x0118,
263 [PIR] = 0x0120,
264 [PSR] = 0x0128,
265 [RDMLR] = 0x0140,
266 [IPGR] = 0x0150,
267 [APR] = 0x0154,
268 [MPR] = 0x0158,
269 [TPAUSER] = 0x0164,
270 [RFCF] = 0x0160,
271 [TPAUSECR] = 0x0168,
272 [BCFRR] = 0x016c,
273 [MAHR] = 0x01c0,
274 [MALR] = 0x01c8,
275 [TROCR] = 0x01d0,
276 [CDCR] = 0x01d4,
277 [LCCR] = 0x01d8,
278 [CNDCR] = 0x01dc,
279 [CEFCR] = 0x01e4,
280 [FRECR] = 0x01e8,
281 [TSFRCR] = 0x01ec,
282 [TLFRCR] = 0x01f0,
283 [RFCR] = 0x01f4,
284 [MAFCR] = 0x01f8,
285 [RTRATE] = 0x01fc,
286
287 [EDMR] = 0x0000,
288 [EDTRR] = 0x0008,
289 [EDRRR] = 0x0010,
290 [TDLAR] = 0x0018,
291 [RDLAR] = 0x0020,
292 [EESR] = 0x0028,
293 [EESIPR] = 0x0030,
294 [TRSCER] = 0x0038,
295 [RMFCR] = 0x0040,
296 [TFTR] = 0x0048,
297 [FDR] = 0x0050,
298 [RMCR] = 0x0058,
299 [TFUCR] = 0x0064,
300 [RFOCR] = 0x0068,
301 [FCFTR] = 0x0070,
302 [RPADIR] = 0x0078,
303 [TRIMD] = 0x007c,
304 [RBWAR] = 0x00c8,
305 [RDFAR] = 0x00cc,
306 [TBRAR] = 0x00d4,
307 [TDFAR] = 0x00d8,
308};
309
310static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000311 SH_ETH_OFFSET_DEFAULTS,
312
Sergei Shtylyovd8b04262014-06-03 23:42:26 +0400313 [EDMR] = 0x0000,
314 [EDTRR] = 0x0004,
315 [EDRRR] = 0x0008,
316 [TDLAR] = 0x000c,
317 [RDLAR] = 0x0010,
318 [EESR] = 0x0014,
319 [EESIPR] = 0x0018,
320 [TRSCER] = 0x001c,
321 [RMFCR] = 0x0020,
322 [TFTR] = 0x0024,
323 [FDR] = 0x0028,
324 [RMCR] = 0x002c,
325 [EDOCR] = 0x0030,
326 [FCFTR] = 0x0034,
327 [RPADIR] = 0x0038,
328 [TRIMD] = 0x003c,
329 [RBWAR] = 0x0040,
330 [RDFAR] = 0x0044,
331 [TBRAR] = 0x004c,
332 [TDFAR] = 0x0050,
333
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000334 [ECMR] = 0x0160,
335 [ECSR] = 0x0164,
336 [ECSIPR] = 0x0168,
337 [PIR] = 0x016c,
338 [MAHR] = 0x0170,
339 [MALR] = 0x0174,
340 [RFLR] = 0x0178,
341 [PSR] = 0x017c,
342 [TROCR] = 0x0180,
343 [CDCR] = 0x0184,
344 [LCCR] = 0x0188,
345 [CNDCR] = 0x018c,
346 [CEFCR] = 0x0194,
347 [FRECR] = 0x0198,
348 [TSFRCR] = 0x019c,
349 [TLFRCR] = 0x01a0,
350 [RFCR] = 0x01a4,
351 [MAFCR] = 0x01a8,
352 [IPGR] = 0x01b4,
353 [APR] = 0x01b8,
354 [MPR] = 0x01bc,
355 [TPAUSER] = 0x01c4,
356 [BCFR] = 0x01cc,
357
358 [ARSTR] = 0x0000,
359 [TSU_CTRST] = 0x0004,
360 [TSU_FWEN0] = 0x0010,
361 [TSU_FWEN1] = 0x0014,
362 [TSU_FCM] = 0x0018,
363 [TSU_BSYSL0] = 0x0020,
364 [TSU_BSYSL1] = 0x0024,
365 [TSU_PRISL0] = 0x0028,
366 [TSU_PRISL1] = 0x002c,
367 [TSU_FWSL0] = 0x0030,
368 [TSU_FWSL1] = 0x0034,
369 [TSU_FWSLC] = 0x0038,
370 [TSU_QTAGM0] = 0x0040,
371 [TSU_QTAGM1] = 0x0044,
372 [TSU_ADQT0] = 0x0048,
373 [TSU_ADQT1] = 0x004c,
374 [TSU_FWSR] = 0x0050,
375 [TSU_FWINMK] = 0x0054,
376 [TSU_ADSBSY] = 0x0060,
377 [TSU_TEN] = 0x0064,
378 [TSU_POST1] = 0x0070,
379 [TSU_POST2] = 0x0074,
380 [TSU_POST3] = 0x0078,
381 [TSU_POST4] = 0x007c,
382
383 [TXNLCR0] = 0x0080,
384 [TXALCR0] = 0x0084,
385 [RXNLCR0] = 0x0088,
386 [RXALCR0] = 0x008c,
387 [FWNLCR0] = 0x0090,
388 [FWALCR0] = 0x0094,
389 [TXNLCR1] = 0x00a0,
Sergei Shtylyov50f3d742018-01-07 00:26:47 +0300390 [TXALCR1] = 0x00a4,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000391 [RXNLCR1] = 0x00a8,
392 [RXALCR1] = 0x00ac,
393 [FWNLCR1] = 0x00b0,
394 [FWALCR1] = 0x00b4,
395
396 [TSU_ADRH0] = 0x0100,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000397};
398
Ben Hutchings740c7f32015-01-27 00:49:32 +0000399static void sh_eth_rcv_snd_disable(struct net_device *ndev);
400static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev);
401
Sergei Shtylyov2274d372015-12-13 01:44:50 +0300402static void sh_eth_write(struct net_device *ndev, u32 data, int enum_index)
403{
404 struct sh_eth_private *mdp = netdev_priv(ndev);
405 u16 offset = mdp->reg_offset[enum_index];
406
407 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
408 return;
409
410 iowrite32(data, mdp->addr + offset);
411}
412
413static u32 sh_eth_read(struct net_device *ndev, int enum_index)
414{
415 struct sh_eth_private *mdp = netdev_priv(ndev);
416 u16 offset = mdp->reg_offset[enum_index];
417
418 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
419 return ~0U;
420
421 return ioread32(mdp->addr + offset);
422}
423
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300424static void sh_eth_modify(struct net_device *ndev, int enum_index, u32 clear,
425 u32 set)
426{
427 sh_eth_write(ndev, (sh_eth_read(ndev, enum_index) & ~clear) | set,
428 enum_index);
429}
430
Sergei Shtylyov41414f02018-07-23 21:11:19 +0300431static u16 sh_eth_tsu_get_offset(struct sh_eth_private *mdp, int enum_index)
Sergei Shtylyov388c4bb2018-07-23 21:10:02 +0300432{
Sergei Shtylyov41414f02018-07-23 21:11:19 +0300433 return mdp->reg_offset[enum_index];
Sergei Shtylyov388c4bb2018-07-23 21:10:02 +0300434}
435
Sergei Shtylyov55ea8742018-02-27 14:58:16 +0300436static void sh_eth_tsu_write(struct sh_eth_private *mdp, u32 data,
437 int enum_index)
438{
Sergei Shtylyovecbecb02018-07-23 21:12:38 +0300439 u16 offset = sh_eth_tsu_get_offset(mdp, enum_index);
Sergei Shtylyov627a0d22018-05-02 22:55:52 +0300440
441 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
442 return;
443
444 iowrite32(data, mdp->tsu_addr + offset);
Sergei Shtylyov55ea8742018-02-27 14:58:16 +0300445}
446
447static u32 sh_eth_tsu_read(struct sh_eth_private *mdp, int enum_index)
448{
Sergei Shtylyovecbecb02018-07-23 21:12:38 +0300449 u16 offset = sh_eth_tsu_get_offset(mdp, enum_index);
Sergei Shtylyov627a0d22018-05-02 22:55:52 +0300450
451 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
452 return ~0U;
453
454 return ioread32(mdp->tsu_addr + offset);
Sergei Shtylyov55ea8742018-02-27 14:58:16 +0300455}
456
Sergei Shtylyovbb2fa4e2018-06-02 22:38:56 +0300457static void sh_eth_soft_swap(char *src, int len)
458{
459#ifdef __LITTLE_ENDIAN
460 u32 *p = (u32 *)src;
Sergei Shtylyov11001492018-06-02 22:40:16 +0300461 u32 *maxp = p + DIV_ROUND_UP(len, sizeof(u32));
Sergei Shtylyovbb2fa4e2018-06-02 22:38:56 +0300462
463 for (; p < maxp; p++)
464 *p = swab32(*p);
465#endif
466}
467
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400468static void sh_eth_select_mii(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000469{
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000470 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +0300471 u32 value;
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000472
473 switch (mdp->phy_interface) {
Sergei Shtylyov230c1842018-05-18 21:30:18 +0300474 case PHY_INTERFACE_MODE_RGMII ... PHY_INTERFACE_MODE_RGMII_TXID:
475 value = 0x3;
476 break;
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000477 case PHY_INTERFACE_MODE_GMII:
478 value = 0x2;
479 break;
480 case PHY_INTERFACE_MODE_MII:
481 value = 0x1;
482 break;
483 case PHY_INTERFACE_MODE_RMII:
484 value = 0x0;
485 break;
486 default:
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300487 netdev_warn(ndev,
488 "PHY interface mode was not setup. Set to MII.\n");
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000489 value = 0x1;
490 break;
491 }
492
493 sh_eth_write(ndev, value, RMII_MII);
494}
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000495
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400496static void sh_eth_set_duplex(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000497{
498 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000499
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300500 sh_eth_modify(ndev, ECMR, ECMR_DM, mdp->duplex ? ECMR_DM : 0);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000501}
502
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100503static void sh_eth_chip_reset(struct net_device *ndev)
504{
505 struct sh_eth_private *mdp = netdev_priv(ndev);
506
507 /* reset device */
Sergei Shtylyovec65cfc2016-04-24 23:46:15 +0300508 sh_eth_tsu_write(mdp, ARSTR_ARST, ARSTR);
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100509 mdelay(1);
510}
511
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300512static int sh_eth_soft_reset(struct net_device *ndev)
513{
514 sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, EDMR_SRST_ETHER);
515 mdelay(3);
516 sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, 0);
517
518 return 0;
519}
520
521static int sh_eth_check_soft_reset(struct net_device *ndev)
522{
523 int cnt;
524
525 for (cnt = 100; cnt > 0; cnt--) {
526 if (!(sh_eth_read(ndev, EDMR) & EDMR_SRST_GETHER))
527 return 0;
528 mdelay(1);
529 }
530
531 netdev_err(ndev, "Device reset failed\n");
532 return -ETIMEDOUT;
533}
534
535static int sh_eth_soft_reset_gether(struct net_device *ndev)
536{
537 struct sh_eth_private *mdp = netdev_priv(ndev);
538 int ret;
539
540 sh_eth_write(ndev, EDSR_ENALL, EDSR);
541 sh_eth_modify(ndev, EDMR, EDMR_SRST_GETHER, EDMR_SRST_GETHER);
542
543 ret = sh_eth_check_soft_reset(ndev);
544 if (ret)
545 return ret;
546
547 /* Table Init */
548 sh_eth_write(ndev, 0, TDLAR);
549 sh_eth_write(ndev, 0, TDFAR);
550 sh_eth_write(ndev, 0, TDFXR);
551 sh_eth_write(ndev, 0, TDFFR);
552 sh_eth_write(ndev, 0, RDLAR);
553 sh_eth_write(ndev, 0, RDFAR);
554 sh_eth_write(ndev, 0, RDFXR);
555 sh_eth_write(ndev, 0, RDFFR);
556
557 /* Reset HW CRC register */
Sergei Shtylyov2c2ab5a2019-02-04 21:05:55 +0300558 if (mdp->cd->csmr)
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300559 sh_eth_write(ndev, 0, CSMR);
560
561 /* Select MII mode */
562 if (mdp->cd->select_mii)
563 sh_eth_select_mii(ndev);
564
565 return ret;
566}
567
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100568static void sh_eth_set_rate_gether(struct net_device *ndev)
569{
570 struct sh_eth_private *mdp = netdev_priv(ndev);
571
572 switch (mdp->speed) {
573 case 10: /* 10BASE */
574 sh_eth_write(ndev, GECMR_10, GECMR);
575 break;
576 case 100:/* 100BASE */
577 sh_eth_write(ndev, GECMR_100, GECMR);
578 break;
579 case 1000: /* 1000BASE */
580 sh_eth_write(ndev, GECMR_1000, GECMR);
581 break;
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100582 }
583}
584
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100585#ifdef CONFIG_OF
586/* R7S72100 */
587static struct sh_eth_cpu_data r7s72100_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300588 .soft_reset = sh_eth_soft_reset_gether,
589
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100590 .chip_reset = sh_eth_chip_reset,
591 .set_duplex = sh_eth_set_duplex,
592
593 .register_type = SH_ETH_REG_FAST_RZ,
594
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300595 .edtrr_trns = EDTRR_TRNS_GETHER,
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100596 .ecsr_value = ECSR_ICD,
597 .ecsipr_value = ECSIPR_ICDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300598 .eesipr_value = EESIPR_TWB1IP | EESIPR_TWBIP | EESIPR_TC1IP |
599 EESIPR_TABTIP | EESIPR_RABTIP | EESIPR_RFCOFIP |
600 EESIPR_ECIIP |
601 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
602 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
603 EESIPR_RMAFIP | EESIPR_RRFIP |
604 EESIPR_RTLFIP | EESIPR_RTSFIP |
605 EESIPR_PREIP | EESIPR_CERFIP,
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100606
607 .tx_check = EESR_TC1 | EESR_FTC,
608 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
609 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300610 EESR_TDE,
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100611 .fdr_value = 0x0000070f,
612
613 .no_psr = 1,
614 .apr = 1,
615 .mpr = 1,
616 .tpauser = 1,
617 .hw_swap = 1,
618 .rpadir = 1,
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100619 .no_trimd = 1,
620 .no_ade = 1,
Sergei Shtylyov246e30c2018-03-24 23:09:55 +0300621 .xdfar_rw = 1,
Sergei Shtylyov2c2ab5a2019-02-04 21:05:55 +0300622 .csmr = 1,
Sergei Shtylyov48132cd2019-02-04 21:07:53 +0300623 .rx_csum = 1,
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100624 .tsu = 1,
Sergei Shtylyovce9134d2018-03-24 23:11:19 +0300625 .no_tx_cntrs = 1,
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100626};
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100627
628static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
629{
Sergei Shtylyovc66b2582016-05-07 14:09:01 -0700630 sh_eth_chip_reset(ndev);
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100631
632 sh_eth_select_mii(ndev);
633}
634
635/* R8A7740 */
636static struct sh_eth_cpu_data r8a7740_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300637 .soft_reset = sh_eth_soft_reset_gether,
638
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100639 .chip_reset = sh_eth_chip_reset_r8a7740,
640 .set_duplex = sh_eth_set_duplex,
641 .set_rate = sh_eth_set_rate_gether,
642
643 .register_type = SH_ETH_REG_GIGABIT,
644
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300645 .edtrr_trns = EDTRR_TRNS_GETHER,
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100646 .ecsr_value = ECSR_ICD | ECSR_MPD,
647 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300648 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
649 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
650 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
651 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
652 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
653 EESIPR_CEEFIP | EESIPR_CELFIP |
654 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
655 EESIPR_PREIP | EESIPR_CERFIP,
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100656
657 .tx_check = EESR_TC1 | EESR_FTC,
658 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
659 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300660 EESR_TDE,
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100661 .fdr_value = 0x0000070f,
662
663 .apr = 1,
664 .mpr = 1,
665 .tpauser = 1,
666 .bculr = 1,
667 .hw_swap = 1,
668 .rpadir = 1,
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100669 .no_trimd = 1,
670 .no_ade = 1,
Sergei Shtylyov246e30c2018-03-24 23:09:55 +0300671 .xdfar_rw = 1,
Sergei Shtylyov2c2ab5a2019-02-04 21:05:55 +0300672 .csmr = 1,
Sergei Shtylyov040c16f2019-02-04 21:08:54 +0300673 .rx_csum = 1,
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100674 .tsu = 1,
675 .select_mii = 1,
Niklas Söderlund33017e22017-01-09 16:34:07 +0100676 .magic = 1,
Sergei Shtylyov4c1d4582018-03-24 23:12:54 +0300677 .cexcr = 1,
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100678};
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100679
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000680/* There is CPU dependent code */
Simon Horman6c4b2f72017-10-18 09:21:27 +0200681static void sh_eth_set_rate_rcar(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000682{
683 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000684
685 switch (mdp->speed) {
686 case 10: /* 10BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300687 sh_eth_modify(ndev, ECMR, ECMR_ELB, 0);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000688 break;
689 case 100:/* 100BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300690 sh_eth_modify(ndev, ECMR, ECMR_ELB, ECMR_ELB);
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000691 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000692 }
693}
694
Simon Horman6c4b2f72017-10-18 09:21:27 +0200695/* R-Car Gen1 */
696static struct sh_eth_cpu_data rcar_gen1_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300697 .soft_reset = sh_eth_soft_reset,
698
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000699 .set_duplex = sh_eth_set_duplex,
Simon Horman6c4b2f72017-10-18 09:21:27 +0200700 .set_rate = sh_eth_set_rate_rcar,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000701
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400702 .register_type = SH_ETH_REG_FAST_RCAR,
703
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300704 .edtrr_trns = EDTRR_TRNS_ETHER,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000705 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
706 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300707 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ADEIP | EESIPR_ECIIP |
708 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
709 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
710 EESIPR_RMAFIP | EESIPR_RRFIP |
711 EESIPR_RTLFIP | EESIPR_RTSFIP |
712 EESIPR_PREIP | EESIPR_CERFIP,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000713
Sergei Shtylyov27164492018-05-20 00:02:36 +0300714 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400715 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300716 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900717 .fdr_value = 0x00000f0f,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000718
719 .apr = 1,
720 .mpr = 1,
721 .tpauser = 1,
722 .hw_swap = 1,
Sergei Shtylyov6e80e552018-04-01 00:22:08 +0300723 .no_xdfar = 1,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000724};
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000725
Simon Horman6c4b2f72017-10-18 09:21:27 +0200726/* R-Car Gen2 and RZ/G1 */
727static struct sh_eth_cpu_data rcar_gen2_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300728 .soft_reset = sh_eth_soft_reset,
729
Simon Hormane18dbf72013-07-23 10:18:05 +0900730 .set_duplex = sh_eth_set_duplex,
Simon Horman6c4b2f72017-10-18 09:21:27 +0200731 .set_rate = sh_eth_set_rate_rcar,
Simon Hormane18dbf72013-07-23 10:18:05 +0900732
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400733 .register_type = SH_ETH_REG_FAST_RCAR,
734
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300735 .edtrr_trns = EDTRR_TRNS_ETHER,
Niklas Söderlunde410d86d2017-01-09 16:34:06 +0100736 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD | ECSR_MPD,
737 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP |
738 ECSIPR_MPDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300739 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ADEIP | EESIPR_ECIIP |
740 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
741 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
742 EESIPR_RMAFIP | EESIPR_RRFIP |
743 EESIPR_RTLFIP | EESIPR_RTSFIP |
744 EESIPR_PREIP | EESIPR_CERFIP,
Simon Hormane18dbf72013-07-23 10:18:05 +0900745
Sergei Shtylyov27164492018-05-20 00:02:36 +0300746 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
Laurent Pinchartba361cb2013-07-31 16:42:11 +0900747 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300748 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900749 .fdr_value = 0x00000f0f,
Simon Hormane18dbf72013-07-23 10:18:05 +0900750
Geert Uytterhoeven01fbd3f2015-01-15 11:52:19 +0100751 .trscer_err_mask = DESC_I_RINT8,
752
Simon Hormane18dbf72013-07-23 10:18:05 +0900753 .apr = 1,
754 .mpr = 1,
755 .tpauser = 1,
756 .hw_swap = 1,
Sergei Shtylyov6e80e552018-04-01 00:22:08 +0300757 .no_xdfar = 1,
Simon Hormane18dbf72013-07-23 10:18:05 +0900758 .rmiimode = 1,
Niklas Söderlunde410d86d2017-01-09 16:34:06 +0100759 .magic = 1,
Simon Hormane18dbf72013-07-23 10:18:05 +0900760};
Sergei Shtylyov3eb9c2a2018-05-18 21:32:46 +0300761
762/* R8A77980 */
763static struct sh_eth_cpu_data r8a77980_data = {
764 .soft_reset = sh_eth_soft_reset_gether,
765
766 .set_duplex = sh_eth_set_duplex,
767 .set_rate = sh_eth_set_rate_gether,
768
769 .register_type = SH_ETH_REG_GIGABIT,
770
771 .edtrr_trns = EDTRR_TRNS_GETHER,
772 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD | ECSR_MPD,
773 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP |
774 ECSIPR_MPDIP,
775 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
776 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
777 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
778 EESIPR_RMAFIP | EESIPR_RRFIP |
779 EESIPR_RTLFIP | EESIPR_RTSFIP |
780 EESIPR_PREIP | EESIPR_CERFIP,
781
Sergei Shtylyov27164492018-05-20 00:02:36 +0300782 .tx_check = EESR_FTC | EESR_CD | EESR_TRO,
Sergei Shtylyov3eb9c2a2018-05-18 21:32:46 +0300783 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
784 EESR_RFE | EESR_RDE | EESR_RFRMER |
785 EESR_TFE | EESR_TDE | EESR_ECI,
786 .fdr_value = 0x0000070f,
787
788 .apr = 1,
789 .mpr = 1,
790 .tpauser = 1,
791 .bculr = 1,
792 .hw_swap = 1,
793 .nbst = 1,
794 .rpadir = 1,
Sergei Shtylyov3eb9c2a2018-05-18 21:32:46 +0300795 .no_trimd = 1,
796 .no_ade = 1,
797 .xdfar_rw = 1,
Sergei Shtylyov2c2ab5a2019-02-04 21:05:55 +0300798 .csmr = 1,
Sergei Shtylyov0da843a2019-02-04 21:10:32 +0300799 .rx_csum = 1,
Sergei Shtylyov3eb9c2a2018-05-18 21:32:46 +0300800 .select_mii = 1,
801 .magic = 1,
802 .cexcr = 1,
803};
Chris Brandt6e0bb042018-08-27 12:42:02 -0500804
805/* R7S9210 */
806static struct sh_eth_cpu_data r7s9210_data = {
807 .soft_reset = sh_eth_soft_reset,
808
809 .set_duplex = sh_eth_set_duplex,
810 .set_rate = sh_eth_set_rate_rcar,
811
812 .register_type = SH_ETH_REG_FAST_SH4,
813
814 .edtrr_trns = EDTRR_TRNS_ETHER,
815 .ecsr_value = ECSR_ICD,
816 .ecsipr_value = ECSIPR_ICDIP,
817 .eesipr_value = EESIPR_TWBIP | EESIPR_TABTIP | EESIPR_RABTIP |
818 EESIPR_RFCOFIP | EESIPR_ECIIP | EESIPR_FTCIP |
819 EESIPR_TDEIP | EESIPR_TFUFIP | EESIPR_FRIP |
820 EESIPR_RDEIP | EESIPR_RFOFIP | EESIPR_CNDIP |
821 EESIPR_DLCIP | EESIPR_CDIP | EESIPR_TROIP |
822 EESIPR_RMAFIP | EESIPR_RRFIP | EESIPR_RTLFIP |
823 EESIPR_RTSFIP | EESIPR_PREIP | EESIPR_CERFIP,
824
825 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
826 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
827 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
828
829 .fdr_value = 0x0000070f,
830
831 .apr = 1,
832 .mpr = 1,
833 .tpauser = 1,
834 .hw_swap = 1,
835 .rpadir = 1,
836 .no_ade = 1,
837 .xdfar_rw = 1,
838};
Geert Uytterhoevenc74a2242015-11-24 15:40:58 +0100839#endif /* CONFIG_OF */
Simon Hormane18dbf72013-07-23 10:18:05 +0900840
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000841static void sh_eth_set_rate_sh7724(struct net_device *ndev)
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000842{
843 struct sh_eth_private *mdp = netdev_priv(ndev);
844
845 switch (mdp->speed) {
846 case 10: /* 10BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300847 sh_eth_modify(ndev, ECMR, ECMR_RTM, 0);
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000848 break;
849 case 100:/* 100BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300850 sh_eth_modify(ndev, ECMR, ECMR_RTM, ECMR_RTM);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000851 break;
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000852 }
853}
854
855/* SH7724 */
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000856static struct sh_eth_cpu_data sh7724_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300857 .soft_reset = sh_eth_soft_reset,
858
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000859 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000860 .set_rate = sh_eth_set_rate_sh7724,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000861
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400862 .register_type = SH_ETH_REG_FAST_SH4,
863
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300864 .edtrr_trns = EDTRR_TRNS_ETHER,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000865 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
866 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300867 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ADEIP | EESIPR_ECIIP |
868 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
869 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
870 EESIPR_RMAFIP | EESIPR_RRFIP |
871 EESIPR_RTLFIP | EESIPR_RTSFIP |
872 EESIPR_PREIP | EESIPR_CERFIP,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000873
Sergei Shtylyov27164492018-05-20 00:02:36 +0300874 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400875 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300876 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000877
878 .apr = 1,
879 .mpr = 1,
880 .tpauser = 1,
881 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800882 .rpadir = 1,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000883};
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000884
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000885static void sh_eth_set_rate_sh7757(struct net_device *ndev)
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000886{
887 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000888
889 switch (mdp->speed) {
890 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000891 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000892 break;
893 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000894 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000895 break;
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000896 }
897}
898
899/* SH7757 */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000900static struct sh_eth_cpu_data sh7757_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300901 .soft_reset = sh_eth_soft_reset,
902
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000903 .set_duplex = sh_eth_set_duplex,
904 .set_rate = sh_eth_set_rate_sh7757,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000905
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400906 .register_type = SH_ETH_REG_FAST_SH4,
907
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300908 .edtrr_trns = EDTRR_TRNS_ETHER,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300909 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
910 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
911 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
912 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
913 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
914 EESIPR_CEEFIP | EESIPR_CELFIP |
915 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
916 EESIPR_PREIP | EESIPR_CERFIP,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000917
Sergei Shtylyov27164492018-05-20 00:02:36 +0300918 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_TRO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400919 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300920 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000921
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000922 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000923 .apr = 1,
924 .mpr = 1,
925 .tpauser = 1,
926 .hw_swap = 1,
927 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000928 .rpadir = 1,
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +0000929 .rtrate = 1,
Sergei Shtylyova94cf2a2018-02-24 22:41:45 +0300930 .dual_port = 1,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000931};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000932
David S. Millere403d292013-06-07 23:40:41 -0700933#define SH_GIGA_ETH_BASE 0xfee00000UL
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000934#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
935#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
936static void sh_eth_chip_reset_giga(struct net_device *ndev)
937{
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +0100938 u32 mahr[2], malr[2];
Sergei Shtylyov79270922016-05-08 00:08:05 +0300939 int i;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000940
941 /* save MAHR and MALR */
942 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000943 malr[i] = ioread32((void *)GIGA_MALR(i));
944 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000945 }
946
Sergei Shtylyovc66b2582016-05-07 14:09:01 -0700947 sh_eth_chip_reset(ndev);
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000948
949 /* restore MAHR and MALR */
950 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000951 iowrite32(malr[i], (void *)GIGA_MALR(i));
952 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000953 }
954}
955
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000956static void sh_eth_set_rate_giga(struct net_device *ndev)
957{
958 struct sh_eth_private *mdp = netdev_priv(ndev);
959
960 switch (mdp->speed) {
961 case 10: /* 10BASE */
962 sh_eth_write(ndev, 0x00000000, GECMR);
963 break;
964 case 100:/* 100BASE */
965 sh_eth_write(ndev, 0x00000010, GECMR);
966 break;
967 case 1000: /* 1000BASE */
968 sh_eth_write(ndev, 0x00000020, GECMR);
969 break;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000970 }
971}
972
973/* SH7757(GETHERC) */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000974static struct sh_eth_cpu_data sh7757_data_giga = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +0300975 .soft_reset = sh_eth_soft_reset_gether,
976
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000977 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000978 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000979 .set_rate = sh_eth_set_rate_giga,
980
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400981 .register_type = SH_ETH_REG_GIGABIT,
982
Sergei Shtylyov3e416992018-03-24 23:08:42 +0300983 .edtrr_trns = EDTRR_TRNS_GETHER,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000984 .ecsr_value = ECSR_ICD | ECSR_MPD,
985 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +0300986 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
987 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
988 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
989 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
990 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
991 EESIPR_CEEFIP | EESIPR_CELFIP |
992 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
993 EESIPR_PREIP | EESIPR_CERFIP,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000994
995 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400996 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
997 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +0300998 EESR_TDE,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000999 .fdr_value = 0x0000072f,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00001000
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00001001 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00001002 .apr = 1,
1003 .mpr = 1,
1004 .tpauser = 1,
1005 .bculr = 1,
1006 .hw_swap = 1,
1007 .rpadir = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00001008 .no_trimd = 1,
1009 .no_ade = 1,
Sergei Shtylyov246e30c2018-03-24 23:09:55 +03001010 .xdfar_rw = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +00001011 .tsu = 1,
Sergei Shtylyov4c1d4582018-03-24 23:12:54 +03001012 .cexcr = 1,
Sergei Shtylyova94cf2a2018-02-24 22:41:45 +03001013 .dual_port = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +00001014};
1015
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001016/* SH7734 */
1017static struct sh_eth_cpu_data sh7734_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +03001018 .soft_reset = sh_eth_soft_reset_gether,
1019
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001020 .chip_reset = sh_eth_chip_reset,
1021 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001022 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001023
Sergei Shtylyova3153d82013-08-18 03:11:28 +04001024 .register_type = SH_ETH_REG_GIGABIT,
1025
Sergei Shtylyov3e416992018-03-24 23:08:42 +03001026 .edtrr_trns = EDTRR_TRNS_GETHER,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001027 .ecsr_value = ECSR_ICD | ECSR_MPD,
1028 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +03001029 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
1030 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
1031 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
1032 EESIPR_DLCIP | EESIPR_CDIP | EESIPR_TROIP |
1033 EESIPR_RMAFIP | EESIPR_CEEFIP | EESIPR_CELFIP |
1034 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
1035 EESIPR_PREIP | EESIPR_CERFIP,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001036
1037 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +04001038 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
1039 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001040 EESR_TDE,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001041
1042 .apr = 1,
1043 .mpr = 1,
1044 .tpauser = 1,
1045 .bculr = 1,
1046 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001047 .no_trimd = 1,
1048 .no_ade = 1,
Sergei Shtylyov246e30c2018-03-24 23:09:55 +03001049 .xdfar_rw = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001050 .tsu = 1,
Sergei Shtylyov2c2ab5a2019-02-04 21:05:55 +03001051 .csmr = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001052 .select_mii = 1,
Niklas Söderlund159c2a92017-01-09 16:34:08 +01001053 .magic = 1,
Sergei Shtylyov4c1d4582018-03-24 23:12:54 +03001054 .cexcr = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001055};
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001056
1057/* SH7763 */
1058static struct sh_eth_cpu_data sh7763_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +03001059 .soft_reset = sh_eth_soft_reset_gether,
1060
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001061 .chip_reset = sh_eth_chip_reset,
1062 .set_duplex = sh_eth_set_duplex,
1063 .set_rate = sh_eth_set_rate_gether,
1064
Sergei Shtylyova3153d82013-08-18 03:11:28 +04001065 .register_type = SH_ETH_REG_GIGABIT,
1066
Sergei Shtylyov3e416992018-03-24 23:08:42 +03001067 .edtrr_trns = EDTRR_TRNS_GETHER,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001068 .ecsr_value = ECSR_ICD | ECSR_MPD,
1069 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +03001070 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
1071 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
1072 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
1073 EESIPR_DLCIP | EESIPR_CDIP | EESIPR_TROIP |
1074 EESIPR_RMAFIP | EESIPR_CEEFIP | EESIPR_CELFIP |
1075 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
1076 EESIPR_PREIP | EESIPR_CERFIP,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001077
1078 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001079 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001080 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001081
1082 .apr = 1,
1083 .mpr = 1,
1084 .tpauser = 1,
1085 .bculr = 1,
1086 .hw_swap = 1,
1087 .no_trimd = 1,
1088 .no_ade = 1,
Sergei Shtylyov246e30c2018-03-24 23:09:55 +03001089 .xdfar_rw = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001090 .tsu = 1,
1091 .irq_flags = IRQF_SHARED,
Niklas Söderlund267e1d52017-01-09 16:34:09 +01001092 .magic = 1,
Sergei Shtylyov4c1d4582018-03-24 23:12:54 +03001093 .cexcr = 1,
Sergei Shtylyova94cf2a2018-02-24 22:41:45 +03001094 .dual_port = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00001095};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001096
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00001097static struct sh_eth_cpu_data sh7619_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +03001098 .soft_reset = sh_eth_soft_reset,
1099
Sergei Shtylyova3153d82013-08-18 03:11:28 +04001100 .register_type = SH_ETH_REG_FAST_SH3_SH2,
1101
Sergei Shtylyov3e416992018-03-24 23:08:42 +03001102 .edtrr_trns = EDTRR_TRNS_ETHER,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +03001103 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
1104 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
1105 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
1106 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
1107 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
1108 EESIPR_CEEFIP | EESIPR_CELFIP |
1109 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
1110 EESIPR_PREIP | EESIPR_CERFIP,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001111
1112 .apr = 1,
1113 .mpr = 1,
1114 .tpauser = 1,
1115 .hw_swap = 1,
1116};
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00001117
1118static struct sh_eth_cpu_data sh771x_data = {
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +03001119 .soft_reset = sh_eth_soft_reset,
1120
Sergei Shtylyova3153d82013-08-18 03:11:28 +04001121 .register_type = SH_ETH_REG_FAST_SH3_SH2,
1122
Sergei Shtylyov3e416992018-03-24 23:08:42 +03001123 .edtrr_trns = EDTRR_TRNS_ETHER,
Sergei Shtylyov2b2d3eb2017-01-29 15:13:48 +03001124 .eesipr_value = EESIPR_RFCOFIP | EESIPR_ECIIP |
1125 EESIPR_FTCIP | EESIPR_TDEIP | EESIPR_TFUFIP |
1126 EESIPR_FRIP | EESIPR_RDEIP | EESIPR_RFOFIP |
1127 0x0000f000 | EESIPR_CNDIP | EESIPR_DLCIP |
1128 EESIPR_CDIP | EESIPR_TROIP | EESIPR_RMAFIP |
1129 EESIPR_CEEFIP | EESIPR_CELFIP |
1130 EESIPR_RRFIP | EESIPR_RTLFIP | EESIPR_RTSFIP |
1131 EESIPR_PREIP | EESIPR_CERFIP,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00001132 .tsu = 1,
Sergei Shtylyova94cf2a2018-02-24 22:41:45 +03001133 .dual_port = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001134};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001135
1136static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
1137{
1138 if (!cd->ecsr_value)
1139 cd->ecsr_value = DEFAULT_ECSR_INIT;
1140
1141 if (!cd->ecsipr_value)
1142 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
1143
1144 if (!cd->fcftr_value)
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001145 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001146 DEFAULT_FIFO_F_D_RFD;
1147
1148 if (!cd->fdr_value)
1149 cd->fdr_value = DEFAULT_FDR_INIT;
1150
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001151 if (!cd->tx_check)
1152 cd->tx_check = DEFAULT_TX_CHECK;
1153
1154 if (!cd->eesr_err_check)
1155 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +09001156
1157 if (!cd->trscer_err_mask)
1158 cd->trscer_err_mask = DEFAULT_TRSCER_ERR_MASK;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001159}
1160
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001161static void sh_eth_set_receive_align(struct sk_buff *skb)
1162{
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001163 uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001164
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001165 if (reserve)
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001166 skb_reserve(skb, SH_ETH_RX_ALIGN - reserve);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001167}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001168
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001169/* Program the hardware MAC address from dev->dev_addr. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001170static void update_mac_address(struct net_device *ndev)
1171{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001172 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001173 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
1174 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001175 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001176 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001177}
1178
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001179/* Get MAC address from SuperH MAC address register
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001180 *
1181 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
1182 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
1183 * When you want use this device, you must set MAC address in bootloader.
1184 *
1185 */
Magnus Damm748031f2009-10-09 00:17:14 +00001186static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001187{
Magnus Damm748031f2009-10-09 00:17:14 +00001188 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
Joe Perchesd458cdf2013-10-01 19:04:40 -07001189 memcpy(ndev->dev_addr, mac, ETH_ALEN);
Magnus Damm748031f2009-10-09 00:17:14 +00001190 } else {
Sergei Shtylyov37742f02015-12-05 00:58:57 +03001191 u32 mahr = sh_eth_read(ndev, MAHR);
1192 u32 malr = sh_eth_read(ndev, MALR);
1193
1194 ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
1195 ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
1196 ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
1197 ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
1198 ndev->dev_addr[4] = (malr >> 8) & 0xFF;
1199 ndev->dev_addr[5] = (malr >> 0) & 0xFF;
Magnus Damm748031f2009-10-09 00:17:14 +00001200 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001201}
1202
1203struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001204 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001205 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001206 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001207};
1208
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001209static void sh_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001210{
1211 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001212 u32 pir;
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001213
1214 if (bitbang->set_gate)
1215 bitbang->set_gate(bitbang->addr);
1216
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001217 pir = ioread32(bitbang->addr);
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001218 if (set)
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001219 pir |= mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001220 else
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001221 pir &= ~mask;
1222 iowrite32(pir, bitbang->addr);
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001223}
1224
1225/* Data I/O pin control */
1226static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1227{
1228 sh_mdio_ctrl(ctrl, PIR_MMD, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001229}
1230
1231/* Set bit data*/
1232static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
1233{
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001234 sh_mdio_ctrl(ctrl, PIR_MDO, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001235}
1236
1237/* Get bit data*/
1238static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
1239{
1240 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001241
1242 if (bitbang->set_gate)
1243 bitbang->set_gate(bitbang->addr);
1244
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001245 return (ioread32(bitbang->addr) & PIR_MDI) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001246}
1247
1248/* MDC pin control */
1249static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1250{
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001251 sh_mdio_ctrl(ctrl, PIR_MDC, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001252}
1253
1254/* mdio bus control struct */
1255static struct mdiobb_ops bb_ops = {
1256 .owner = THIS_MODULE,
1257 .set_mdc = sh_mdc_ctrl,
1258 .set_mdio_dir = sh_mmd_ctrl,
1259 .set_mdio_data = sh_set_mdio,
1260 .get_mdio_data = sh_get_mdio,
1261};
1262
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001263/* free Tx skb function */
1264static int sh_eth_tx_free(struct net_device *ndev, bool sent_only)
1265{
1266 struct sh_eth_private *mdp = netdev_priv(ndev);
1267 struct sh_eth_txdesc *txdesc;
1268 int free_num = 0;
1269 int entry;
1270 bool sent;
1271
1272 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
1273 entry = mdp->dirty_tx % mdp->num_tx_ring;
1274 txdesc = &mdp->tx_ring[entry];
1275 sent = !(txdesc->status & cpu_to_le32(TD_TACT));
1276 if (sent_only && !sent)
1277 break;
1278 /* TACT bit must be checked before all the following reads */
1279 dma_rmb();
1280 netif_info(mdp, tx_done, ndev,
1281 "tx entry %d status 0x%08x\n",
1282 entry, le32_to_cpu(txdesc->status));
1283 /* Free the original skb. */
1284 if (mdp->tx_skbuff[entry]) {
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01001285 dma_unmap_single(&mdp->pdev->dev,
1286 le32_to_cpu(txdesc->addr),
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001287 le32_to_cpu(txdesc->len) >> 16,
1288 DMA_TO_DEVICE);
1289 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1290 mdp->tx_skbuff[entry] = NULL;
1291 free_num++;
1292 }
1293 txdesc->status = cpu_to_le32(TD_TFP);
1294 if (entry >= mdp->num_tx_ring - 1)
1295 txdesc->status |= cpu_to_le32(TD_TDLE);
1296
1297 if (sent) {
1298 ndev->stats.tx_packets++;
1299 ndev->stats.tx_bytes += le32_to_cpu(txdesc->len) >> 16;
1300 }
1301 }
1302 return free_num;
1303}
1304
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001305/* free skb and descriptor buffer */
1306static void sh_eth_ring_free(struct net_device *ndev)
1307{
1308 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001309 int ringsize, i;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001310
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001311 if (mdp->rx_ring) {
1312 for (i = 0; i < mdp->num_rx_ring; i++) {
1313 if (mdp->rx_skbuff[i]) {
1314 struct sh_eth_rxdesc *rxdesc = &mdp->rx_ring[i];
1315
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01001316 dma_unmap_single(&mdp->pdev->dev,
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001317 le32_to_cpu(rxdesc->addr),
1318 ALIGN(mdp->rx_buf_sz, 32),
1319 DMA_FROM_DEVICE);
1320 }
1321 }
1322 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Thomas Petazzoni573500dbf2017-12-04 14:33:27 +01001323 dma_free_coherent(&mdp->pdev->dev, ringsize, mdp->rx_ring,
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001324 mdp->rx_desc_dma);
1325 mdp->rx_ring = NULL;
1326 }
1327
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001328 /* Free Rx skb ringbuffer */
1329 if (mdp->rx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001330 for (i = 0; i < mdp->num_rx_ring; i++)
1331 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001332 }
1333 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c775502012-06-26 20:00:01 +00001334 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001335
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001336 if (mdp->tx_ring) {
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001337 sh_eth_tx_free(ndev, false);
1338
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001339 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Thomas Petazzoni573500dbf2017-12-04 14:33:27 +01001340 dma_free_coherent(&mdp->pdev->dev, ringsize, mdp->tx_ring,
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001341 mdp->tx_desc_dma);
1342 mdp->tx_ring = NULL;
1343 }
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001344
1345 /* Free Tx skb ringbuffer */
1346 kfree(mdp->tx_skbuff);
1347 mdp->tx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001348}
1349
1350/* format skb and descriptor buffer */
1351static void sh_eth_ring_format(struct net_device *ndev)
1352{
1353 struct sh_eth_private *mdp = netdev_priv(ndev);
1354 int i;
1355 struct sk_buff *skb;
1356 struct sh_eth_rxdesc *rxdesc = NULL;
1357 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001358 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
1359 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Sergei Shtylyovcb368592015-10-24 00:46:40 +03001360 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001361 dma_addr_t dma_addr;
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001362 u32 buf_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001363
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001364 mdp->cur_rx = 0;
1365 mdp->cur_tx = 0;
1366 mdp->dirty_rx = 0;
1367 mdp->dirty_tx = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001368
1369 memset(mdp->rx_ring, 0, rx_ringsize);
1370
1371 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001372 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001373 /* skb */
1374 mdp->rx_skbuff[i] = NULL;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001375 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001376 if (skb == NULL)
1377 break;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001378 sh_eth_set_receive_align(skb);
1379
Sergei Shtylyovab857912015-10-24 00:46:03 +03001380 /* The size of the buffer is a multiple of 32 bytes. */
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001381 buf_len = ALIGN(mdp->rx_buf_sz, 32);
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01001382 dma_addr = dma_map_single(&mdp->pdev->dev, skb->data, buf_len,
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001383 DMA_FROM_DEVICE);
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01001384 if (dma_mapping_error(&mdp->pdev->dev, dma_addr)) {
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001385 kfree_skb(skb);
1386 break;
1387 }
1388 mdp->rx_skbuff[i] = skb;
Sergei Shtylyovd0ba9132016-03-08 01:37:09 +03001389
1390 /* RX descriptor */
1391 rxdesc = &mdp->rx_ring[i];
1392 rxdesc->len = cpu_to_le32(buf_len << 16);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001393 rxdesc->addr = cpu_to_le32(dma_addr);
1394 rxdesc->status = cpu_to_le32(RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001395
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001396 /* Rx descriptor address set */
1397 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001398 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Sergei Shtylyov246e30c2018-03-24 23:09:55 +03001399 if (mdp->cd->xdfar_rw)
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001400 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001401 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001402 }
1403
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001404 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001405
1406 /* Mark the last entry as wrapping the ring. */
Sergei Shtylyovc1b7fca2016-03-08 01:36:28 +03001407 if (rxdesc)
1408 rxdesc->status |= cpu_to_le32(RD_RDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001409
1410 memset(mdp->tx_ring, 0, tx_ringsize);
1411
1412 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001413 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001414 mdp->tx_skbuff[i] = NULL;
1415 txdesc = &mdp->tx_ring[i];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001416 txdesc->status = cpu_to_le32(TD_TFP);
1417 txdesc->len = cpu_to_le32(0);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001418 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001419 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001420 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Sergei Shtylyov246e30c2018-03-24 23:09:55 +03001421 if (mdp->cd->xdfar_rw)
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001422 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001423 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001424 }
1425
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001426 txdesc->status |= cpu_to_le32(TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001427}
1428
1429/* Get skb and descriptor buffer */
1430static int sh_eth_ring_init(struct net_device *ndev)
1431{
1432 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001433 int rx_ringsize, tx_ringsize;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001434
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001435 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001436 * card needs room to do 8 byte alignment, +2 so we can reserve
1437 * the first 2 bytes, and +16 gets room for the status word from the
1438 * card.
1439 */
1440 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1441 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001442 if (mdp->cd->rpadir)
1443 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001444
1445 /* Allocate RX and TX skb rings */
Sergei Shtylyov2c94e852015-10-31 02:05:56 +03001446 mdp->rx_skbuff = kcalloc(mdp->num_rx_ring, sizeof(*mdp->rx_skbuff),
1447 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001448 if (!mdp->rx_skbuff)
1449 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001450
Sergei Shtylyov2c94e852015-10-31 02:05:56 +03001451 mdp->tx_skbuff = kcalloc(mdp->num_tx_ring, sizeof(*mdp->tx_skbuff),
1452 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001453 if (!mdp->tx_skbuff)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001454 goto ring_free;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001455
1456 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001457 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Thomas Petazzoni573500dbf2017-12-04 14:33:27 +01001458 mdp->rx_ring = dma_alloc_coherent(&mdp->pdev->dev, rx_ringsize,
1459 &mdp->rx_desc_dma, GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001460 if (!mdp->rx_ring)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001461 goto ring_free;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001462
1463 mdp->dirty_rx = 0;
1464
1465 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001466 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Thomas Petazzoni573500dbf2017-12-04 14:33:27 +01001467 mdp->tx_ring = dma_alloc_coherent(&mdp->pdev->dev, tx_ringsize,
1468 &mdp->tx_desc_dma, GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001469 if (!mdp->tx_ring)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001470 goto ring_free;
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001471 return 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001472
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001473ring_free:
1474 /* Free Rx and Tx skb ring buffer and DMA buffer */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001475 sh_eth_ring_free(ndev);
1476
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001477 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001478}
1479
Sergei Shtylyovf7967212016-04-24 19:11:07 +03001480static int sh_eth_dev_init(struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001481{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001482 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03001483 int ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001484
1485 /* Soft Reset */
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +03001486 ret = mdp->cd->soft_reset(ndev);
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001487 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +01001488 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001489
Simon Horman55754f12013-07-23 10:18:04 +09001490 if (mdp->cd->rmiimode)
1491 sh_eth_write(ndev, 0x1, RMIIMODE);
1492
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001493 /* Descriptor format */
1494 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001495 if (mdp->cd->rpadir)
Sergei Shtylyov470103d2018-06-25 23:37:06 +03001496 sh_eth_write(ndev, NET_IP_ALIGN << 16, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001497
1498 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001499 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001500
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001501#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001502 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001503 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001504 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001505#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001506 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001507
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001508 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001509 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1510 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001511
Ben Dooks530aa2d2014-06-03 12:21:13 +01001512 /* Frame recv control (enable multiple-packets per rx irq) */
1513 sh_eth_write(ndev, RMCR_RNC, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001514
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +09001515 sh_eth_write(ndev, mdp->cd->trscer_err_mask, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001516
Sergei Shtylyov93f0fa72018-05-18 21:31:28 +03001517 /* DMA transfer burst mode */
1518 if (mdp->cd->nbst)
1519 sh_eth_modify(ndev, EDMR, EDMR_NBST, EDMR_NBST);
1520
Sergei Shtylyov6b147872018-05-20 00:05:02 +03001521 /* Burst cycle count upper-limit */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001522 if (mdp->cd->bculr)
Sergei Shtylyov6b147872018-05-20 00:05:02 +03001523 sh_eth_write(ndev, 0x800, BCULR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001524
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001525 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001526
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001527 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001528 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001529
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001530 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001531 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1532 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001533
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001534 sh_eth_modify(ndev, EESR, 0, 0);
Sergei Shtylyovf7967212016-04-24 19:11:07 +03001535 mdp->irq_enabled = true;
1536 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001537
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03001538 /* EMAC Mode: PAUSE prohibition; Duplex; RX Checksum; TX; RX */
Sergei Shtylyovbffa7312016-01-11 00:28:14 +03001539 sh_eth_write(ndev, ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) |
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03001540 (ndev->features & NETIF_F_RXCSUM ? ECMR_RCSC : 0) |
Sergei Shtylyovbffa7312016-01-11 00:28:14 +03001541 ECMR_TE | ECMR_RE, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001542
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001543 if (mdp->cd->set_rate)
1544 mdp->cd->set_rate(ndev);
1545
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001546 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001547 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001548
1549 /* E-MAC Interrupt Enable register */
Sergei Shtylyovf7967212016-04-24 19:11:07 +03001550 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001551
1552 /* Set MAC address */
1553 update_mac_address(ndev);
1554
1555 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001556 if (mdp->cd->apr)
Sergei Shtylyov782e85c2018-06-26 18:42:33 +03001557 sh_eth_write(ndev, 1, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001558 if (mdp->cd->mpr)
Sergei Shtylyov782e85c2018-06-26 18:42:33 +03001559 sh_eth_write(ndev, 1, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001560 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001561 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001562
Sergei Shtylyovf7967212016-04-24 19:11:07 +03001563 /* Setting the Rx mode will start the Rx process. */
1564 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001565
1566 return ret;
1567}
1568
Ben Hutchings740c7f32015-01-27 00:49:32 +00001569static void sh_eth_dev_exit(struct net_device *ndev)
1570{
1571 struct sh_eth_private *mdp = netdev_priv(ndev);
1572 int i;
1573
1574 /* Deactivate all TX descriptors, so DMA should stop at next
1575 * packet boundary if it's currently running
1576 */
1577 for (i = 0; i < mdp->num_tx_ring; i++)
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001578 mdp->tx_ring[i].status &= ~cpu_to_le32(TD_TACT);
Ben Hutchings740c7f32015-01-27 00:49:32 +00001579
1580 /* Disable TX FIFO egress to MAC */
1581 sh_eth_rcv_snd_disable(ndev);
1582
1583 /* Stop RX DMA at next packet boundary */
1584 sh_eth_write(ndev, 0, EDRRR);
1585
1586 /* Aside from TX DMA, we can't tell when the hardware is
1587 * really stopped, so we need to reset to make sure.
1588 * Before doing that, wait for long enough to *probably*
1589 * finish transmitting the last packet and poll stats.
1590 */
1591 msleep(2); /* max frame time at 10 Mbps < 1250 us */
1592 sh_eth_get_stats(ndev);
Sergei Shtylyov4ceedeb2018-03-24 23:07:41 +03001593 mdp->cd->soft_reset(ndev);
Geert Uytterhoevena14c7d12015-02-27 17:16:26 +01001594
1595 /* Set MAC address again */
1596 update_mac_address(ndev);
Ben Hutchings740c7f32015-01-27 00:49:32 +00001597}
1598
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03001599static void sh_eth_rx_csum(struct sk_buff *skb)
1600{
1601 u8 *hw_csum;
1602
1603 /* The hardware checksum is 2 bytes appended to packet data */
1604 if (unlikely(skb->len < sizeof(__sum16)))
1605 return;
1606 hw_csum = skb_tail_pointer(skb) - sizeof(__sum16);
1607 skb->csum = csum_unfold((__force __sum16)get_unaligned_le16(hw_csum));
1608 skb->ip_summed = CHECKSUM_COMPLETE;
1609 skb_trim(skb, skb->len - sizeof(__sum16));
1610}
1611
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001612/* Packet receive function */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001613static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001614{
1615 struct sh_eth_private *mdp = netdev_priv(ndev);
1616 struct sh_eth_rxdesc *rxdesc;
1617
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001618 int entry = mdp->cur_rx % mdp->num_rx_ring;
1619 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001620 int limit;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001621 struct sk_buff *skb;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001622 u32 desc_status;
Sergei Shtylyovcb368592015-10-24 00:46:40 +03001623 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001624 dma_addr_t dma_addr;
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03001625 u16 pkt_len;
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001626 u32 buf_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001627
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001628 boguscnt = min(boguscnt, *quota);
1629 limit = boguscnt;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001630 rxdesc = &mdp->rx_ring[entry];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001631 while (!(rxdesc->status & cpu_to_le32(RD_RACT))) {
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001632 /* RACT bit must be checked before all the following reads */
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001633 dma_rmb();
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001634 desc_status = le32_to_cpu(rxdesc->status);
1635 pkt_len = le32_to_cpu(rxdesc->len) & RD_RFL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001636
1637 if (--boguscnt < 0)
1638 break;
1639
Ben Hutchingse5fd13f2015-02-26 20:34:46 +00001640 netif_info(mdp, rx_status, ndev,
1641 "rx entry %d status 0x%08x len %d\n",
1642 entry, desc_status, pkt_len);
1643
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001644 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001645 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001646
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001647 /* In case of almost all GETHER/ETHERs, the Receive Frame State
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001648 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
Ben Hutchings9b4a6362015-03-03 00:52:39 +00001649 * bit 0. However, in case of the R8A7740 and R7S72100
1650 * the RFS bits are from bit 25 to bit 16. So, the
Simon Hormandb893472014-01-17 09:22:28 +09001651 * driver needs right shifting by 16.
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001652 */
Sergei Shtylyov2c2ab5a2019-02-04 21:05:55 +03001653 if (mdp->cd->csmr)
Sergei Shtylyovac8025a2013-06-13 22:12:45 +04001654 desc_status >>= 16;
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001655
Sergei Shtylyov248be832015-12-04 01:45:40 +03001656 skb = mdp->rx_skbuff[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001657 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1658 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001659 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001660 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001661 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001662 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001663 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001664 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001665 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001666 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001667 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001668 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001669 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001670 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001671 ndev->stats.rx_over_errors++;
Sergei Shtylyov248be832015-12-04 01:45:40 +03001672 } else if (skb) {
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001673 dma_addr = le32_to_cpu(rxdesc->addr);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001674 if (!mdp->cd->hw_swap)
1675 sh_eth_soft_swap(
Sergei Shtylyov12996532015-12-13 23:05:07 +03001676 phys_to_virt(ALIGN(dma_addr, 4)),
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001677 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001678 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001679 if (mdp->cd->rpadir)
1680 skb_reserve(skb, NET_IP_ALIGN);
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01001681 dma_unmap_single(&mdp->pdev->dev, dma_addr,
Sergei Shtylyovab857912015-10-24 00:46:03 +03001682 ALIGN(mdp->rx_buf_sz, 32),
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001683 DMA_FROM_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001684 skb_put(skb, pkt_len);
1685 skb->protocol = eth_type_trans(skb, ndev);
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03001686 if (ndev->features & NETIF_F_RXCSUM)
1687 sh_eth_rx_csum(skb);
Sergei Shtylyova8e9fd02013-09-03 03:03:10 +04001688 netif_receive_skb(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001689 ndev->stats.rx_packets++;
1690 ndev->stats.rx_bytes += pkt_len;
Ben Hutchings25b77ad2015-02-26 20:33:30 +00001691 if (desc_status & RD_RFS8)
1692 ndev->stats.multicast++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001693 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001694 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001695 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001696 }
1697
1698 /* Refill the Rx ring buffers. */
1699 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001700 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001701 rxdesc = &mdp->rx_ring[entry];
Sergei Shtylyovab857912015-10-24 00:46:03 +03001702 /* The size of the buffer is 32 byte boundary. */
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001703 buf_len = ALIGN(mdp->rx_buf_sz, 32);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001704 rxdesc->len = cpu_to_le32(buf_len << 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001705
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001706 if (mdp->rx_skbuff[entry] == NULL) {
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001707 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001708 if (skb == NULL)
1709 break; /* Better luck next round. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001710 sh_eth_set_receive_align(skb);
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01001711 dma_addr = dma_map_single(&mdp->pdev->dev, skb->data,
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001712 buf_len, DMA_FROM_DEVICE);
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01001713 if (dma_mapping_error(&mdp->pdev->dev, dma_addr)) {
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001714 kfree_skb(skb);
1715 break;
1716 }
1717 mdp->rx_skbuff[entry] = skb;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001718
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001719 skb_checksum_none_assert(skb);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001720 rxdesc->addr = cpu_to_le32(dma_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001721 }
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001722 dma_wmb(); /* RACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001723 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001724 rxdesc->status |=
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001725 cpu_to_le32(RD_RACT | RD_RFP | RD_RDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001726 else
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001727 rxdesc->status |= cpu_to_le32(RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001728 }
1729
1730 /* Restart Rx engine if stopped. */
1731 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001732 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001733 /* fix the values for the next receiving if RDE is set */
Sergei Shtylyov6e80e552018-04-01 00:22:08 +03001734 if (intr_status & EESR_RDE && !mdp->cd->no_xdfar) {
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001735 u32 count = (sh_eth_read(ndev, RDFAR) -
1736 sh_eth_read(ndev, RDLAR)) >> 4;
1737
1738 mdp->cur_rx = count;
1739 mdp->dirty_rx = count;
1740 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001741 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001742 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001743
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001744 *quota -= limit - boguscnt - 1;
1745
Yoshihiro Shimoda4f809ce2014-06-10 09:40:14 +09001746 return *quota <= 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001747}
1748
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001749static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001750{
1751 /* disable tx and rx */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001752 sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001753}
1754
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001755static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001756{
1757 /* enable tx and rx */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001758 sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001759}
1760
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001761/* E-MAC interrupt handler */
1762static void sh_eth_emac_interrupt(struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001763{
1764 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001765 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001766 u32 link_stat;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001767
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001768 felic_stat = sh_eth_read(ndev, ECSR) & sh_eth_read(ndev, ECSIPR);
1769 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
1770 if (felic_stat & ECSR_ICD)
1771 ndev->stats.tx_carrier_errors++;
Niklas Söderlund0cf45a32017-02-01 15:41:55 +01001772 if (felic_stat & ECSR_MPD)
1773 pm_wakeup_event(&mdp->pdev->dev, 0);
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001774 if (felic_stat & ECSR_LCHNG) {
1775 /* Link Changed */
1776 if (mdp->cd->no_psr || mdp->no_ether_link)
1777 return;
1778 link_stat = sh_eth_read(ndev, PSR);
1779 if (mdp->ether_link_active_low)
1780 link_stat = ~link_stat;
1781 if (!(link_stat & PHY_ST_LINK)) {
1782 sh_eth_rcv_snd_disable(ndev);
1783 } else {
1784 /* Link Up */
Sergei Shtylyov1a0bee62017-01-29 15:07:34 +03001785 sh_eth_modify(ndev, EESIPR, EESIPR_ECIIP, 0);
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001786 /* clear int */
1787 sh_eth_modify(ndev, ECSR, 0, 0);
Sergei Shtylyov1a0bee62017-01-29 15:07:34 +03001788 sh_eth_modify(ndev, EESIPR, EESIPR_ECIIP, EESIPR_ECIIP);
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001789 /* enable tx and rx */
1790 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001791 }
1792 }
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001793}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001794
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001795/* error control function */
1796static void sh_eth_error(struct net_device *ndev, u32 intr_status)
1797{
1798 struct sh_eth_private *mdp = netdev_priv(ndev);
1799 u32 mask;
1800
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001801 if (intr_status & EESR_TWB) {
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001802 /* Unused write back interrupt */
1803 if (intr_status & EESR_TABT) { /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001804 ndev->stats.tx_aborted_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001805 netif_err(mdp, tx_err, ndev, "Transmit Abort\n");
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001806 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001807 }
1808
1809 if (intr_status & EESR_RABT) {
1810 /* Receive Abort int */
1811 if (intr_status & EESR_RFRMER) {
1812 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001813 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001814 }
1815 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001816
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001817 if (intr_status & EESR_TDE) {
1818 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001819 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001820 netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n");
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001821 }
1822
1823 if (intr_status & EESR_TFE) {
1824 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001825 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001826 netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001827 }
1828
1829 if (intr_status & EESR_RDE) {
1830 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001831 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001832 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001833
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001834 if (intr_status & EESR_RFE) {
1835 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001836 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001837 }
1838
1839 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1840 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001841 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001842 netif_err(mdp, tx_err, ndev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001843 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001844
1845 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1846 if (mdp->cd->no_ade)
1847 mask &= ~EESR_ADE;
1848 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001849 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001850 u32 edtrr = sh_eth_read(ndev, EDTRR);
Sergei Shtylyov090d5602014-01-11 02:41:49 +03001851
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001852 /* dmesg */
Sergei Shtylyovda246852014-03-15 03:29:14 +03001853 netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1854 intr_status, mdp->cur_tx, mdp->dirty_tx,
1855 (u32)ndev->state, edtrr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001856 /* dirty buffer free */
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001857 sh_eth_tx_free(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001858
1859 /* SH7712 BUG */
Sergei Shtylyov3e416992018-03-24 23:08:42 +03001860 if (edtrr ^ mdp->cd->edtrr_trns) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001861 /* tx dma start */
Sergei Shtylyov3e416992018-03-24 23:08:42 +03001862 sh_eth_write(ndev, mdp->cd->edtrr_trns, EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001863 }
1864 /* wakeup */
1865 netif_wake_queue(ndev);
1866 }
1867}
1868
1869static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1870{
1871 struct net_device *ndev = netdev;
1872 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001873 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001874 irqreturn_t ret = IRQ_NONE;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001875 u32 intr_status, intr_enable;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001876
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001877 spin_lock(&mdp->lock);
1878
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001879 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001880 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001881 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1882 * enabled since it's the one that comes thru regardless of the mask,
1883 * and we need to fully handle it in sh_eth_emac_interrupt() in order
1884 * to quench it as it doesn't get cleared by just writing 1 to the ECI
1885 * bit...
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001886 */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001887 intr_enable = sh_eth_read(ndev, EESIPR);
Sergei Shtylyov1a0bee62017-01-29 15:07:34 +03001888 intr_status &= intr_enable | EESIPR_ECIIP;
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001889 if (intr_status & (EESR_RX_CHECK | cd->tx_check | EESR_ECI |
1890 cd->eesr_err_check))
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001891 ret = IRQ_HANDLED;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001892 else
Ben Hutchings283e38d2015-01-22 12:44:08 +00001893 goto out;
1894
Sergei Shtylyov2344ef32016-12-30 00:07:38 +03001895 if (unlikely(!mdp->irq_enabled)) {
Ben Hutchings283e38d2015-01-22 12:44:08 +00001896 sh_eth_write(ndev, 0, EESIPR);
1897 goto out;
1898 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001899
Sergei Shtylyov37191092013-06-19 23:30:23 +04001900 if (intr_status & EESR_RX_CHECK) {
1901 if (napi_schedule_prep(&mdp->napi)) {
1902 /* Mask Rx interrupts */
1903 sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
1904 EESIPR);
1905 __napi_schedule(&mdp->napi);
1906 } else {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001907 netdev_warn(ndev,
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001908 "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
Sergei Shtylyovda246852014-03-15 03:29:14 +03001909 intr_status, intr_enable);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001910 }
1911 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001912
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001913 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001914 if (intr_status & cd->tx_check) {
Sergei Shtylyov37191092013-06-19 23:30:23 +04001915 /* Clear Tx interrupts */
1916 sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
1917
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03001918 sh_eth_tx_free(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001919 netif_wake_queue(ndev);
1920 }
1921
Sergei Shtylyov9b39f052017-01-04 15:11:21 +03001922 /* E-MAC interrupt */
1923 if (intr_status & EESR_ECI)
1924 sh_eth_emac_interrupt(ndev);
1925
Sergei Shtylyov37191092013-06-19 23:30:23 +04001926 if (intr_status & cd->eesr_err_check) {
1927 /* Clear error interrupts */
1928 sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
1929
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001930 sh_eth_error(ndev, intr_status);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001931 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001932
Ben Hutchings283e38d2015-01-22 12:44:08 +00001933out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001934 spin_unlock(&mdp->lock);
1935
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001936 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001937}
1938
Sergei Shtylyov37191092013-06-19 23:30:23 +04001939static int sh_eth_poll(struct napi_struct *napi, int budget)
1940{
1941 struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
1942 napi);
1943 struct net_device *ndev = napi->dev;
1944 int quota = budget;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001945 u32 intr_status;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001946
1947 for (;;) {
1948 intr_status = sh_eth_read(ndev, EESR);
1949 if (!(intr_status & EESR_RX_CHECK))
1950 break;
1951 /* Clear Rx interrupts */
1952 sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
1953
1954 if (sh_eth_rx(ndev, intr_status, &quota))
1955 goto out;
1956 }
1957
1958 napi_complete(napi);
1959
1960 /* Reenable Rx interrupts */
Ben Hutchings283e38d2015-01-22 12:44:08 +00001961 if (mdp->irq_enabled)
1962 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001963out:
1964 return budget - quota;
1965}
1966
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001967/* PHY state control function */
1968static void sh_eth_adjust_link(struct net_device *ndev)
1969{
1970 struct sh_eth_private *mdp = netdev_priv(ndev);
Philippe Reynes9fd03752016-08-10 00:04:48 +02001971 struct phy_device *phydev = ndev->phydev;
Vladimir Zapolskiy5cb3f522018-07-04 11:12:40 +03001972 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001973 int new_state = 0;
1974
Vladimir Zapolskiy5cb3f522018-07-04 11:12:40 +03001975 spin_lock_irqsave(&mdp->lock, flags);
1976
1977 /* Disable TX and RX right over here, if E-MAC change is ignored */
1978 if (mdp->cd->no_psr || mdp->no_ether_link)
1979 sh_eth_rcv_snd_disable(ndev);
1980
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001981 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001982 if (phydev->duplex != mdp->duplex) {
1983 new_state = 1;
1984 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001985 if (mdp->cd->set_duplex)
1986 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001987 }
1988
1989 if (phydev->speed != mdp->speed) {
1990 new_state = 1;
1991 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001992 if (mdp->cd->set_rate)
1993 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001994 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001995 if (!mdp->link) {
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001996 sh_eth_modify(ndev, ECMR, ECMR_TXF, 0);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001997 new_state = 1;
1998 mdp->link = phydev->link;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001999 }
2000 } else if (mdp->link) {
2001 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00002002 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002003 mdp->speed = 0;
2004 mdp->duplex = -1;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002005 }
2006
Vladimir Zapolskiy5cb3f522018-07-04 11:12:40 +03002007 /* Enable TX and RX right over here, if E-MAC change is ignored */
2008 if ((mdp->cd->no_psr || mdp->no_ether_link) && phydev->link)
2009 sh_eth_rcv_snd_enable(ndev);
2010
2011 mmiowb();
2012 spin_unlock_irqrestore(&mdp->lock, flags);
2013
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002014 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002015 phy_print_status(phydev);
2016}
2017
2018/* PHY init function */
2019static int sh_eth_phy_init(struct net_device *ndev)
2020{
Ben Dooks702eca02014-03-12 17:47:40 +00002021 struct device_node *np = ndev->dev.parent->of_node;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002022 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03002023 struct phy_device *phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002024
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00002025 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002026 mdp->speed = 0;
2027 mdp->duplex = -1;
2028
2029 /* Try connect to PHY */
Ben Dooks702eca02014-03-12 17:47:40 +00002030 if (np) {
2031 struct device_node *pn;
2032
2033 pn = of_parse_phandle(np, "phy-handle", 0);
2034 phydev = of_phy_connect(ndev, pn,
2035 sh_eth_adjust_link, 0,
2036 mdp->phy_interface);
2037
Peter Chen8da703d2016-08-01 15:02:40 +08002038 of_node_put(pn);
Ben Dooks702eca02014-03-12 17:47:40 +00002039 if (!phydev)
2040 phydev = ERR_PTR(-ENOENT);
2041 } else {
2042 char phy_id[MII_BUS_ID_SIZE + 3];
2043
2044 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
2045 mdp->mii_bus->id, mdp->phy_id);
2046
2047 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
2048 mdp->phy_interface);
2049 }
2050
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002051 if (IS_ERR(phydev)) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002052 netdev_err(ndev, "failed to connect PHY\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002053 return PTR_ERR(phydev);
2054 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002055
Thomas Petazzoni2aab6b42017-12-08 16:35:40 +01002056 /* mask with MAC supported features */
2057 if (mdp->cd->register_type != SH_ETH_REG_GIGABIT) {
2058 int err = phy_set_max_speed(phydev, SPEED_100);
2059 if (err) {
2060 netdev_err(ndev, "failed to limit PHY to 100 Mbit/s\n");
2061 phy_disconnect(phydev);
2062 return err;
2063 }
2064 }
2065
Andrew Lunn22209432016-01-06 20:11:13 +01002066 phy_attached_info(phydev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002067
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002068 return 0;
2069}
2070
2071/* PHY control start function */
2072static int sh_eth_phy_start(struct net_device *ndev)
2073{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002074 int ret;
2075
2076 ret = sh_eth_phy_init(ndev);
2077 if (ret)
2078 return ret;
2079
Philippe Reynes9fd03752016-08-10 00:04:48 +02002080 phy_start(ndev->phydev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002081
2082 return 0;
2083}
2084
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002085/* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
2086 * version must be bumped as well. Just adding registers up to that
2087 * limit is fine, as long as the existing register indices don't
2088 * change.
2089 */
2090#define SH_ETH_REG_DUMP_VERSION 1
2091#define SH_ETH_REG_DUMP_MAX_REGS 256
2092
2093static size_t __sh_eth_get_regs(struct net_device *ndev, u32 *buf)
2094{
2095 struct sh_eth_private *mdp = netdev_priv(ndev);
2096 struct sh_eth_cpu_data *cd = mdp->cd;
2097 u32 *valid_map;
2098 size_t len;
2099
2100 BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET > SH_ETH_REG_DUMP_MAX_REGS);
2101
2102 /* Dump starts with a bitmap that tells ethtool which
2103 * registers are defined for this chip.
2104 */
2105 len = DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS, 32);
2106 if (buf) {
2107 valid_map = buf;
2108 buf += len;
2109 } else {
2110 valid_map = NULL;
2111 }
2112
2113 /* Add a register to the dump, if it has a defined offset.
2114 * This automatically skips most undefined registers, but for
2115 * some it is also necessary to check a capability flag in
2116 * struct sh_eth_cpu_data.
2117 */
2118#define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
2119#define add_reg_from(reg, read_expr) do { \
2120 if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
2121 if (buf) { \
2122 mark_reg_valid(reg); \
2123 *buf++ = read_expr; \
2124 } \
2125 ++len; \
2126 } \
2127 } while (0)
2128#define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
2129#define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
2130
2131 add_reg(EDSR);
2132 add_reg(EDMR);
2133 add_reg(EDTRR);
2134 add_reg(EDRRR);
2135 add_reg(EESR);
2136 add_reg(EESIPR);
2137 add_reg(TDLAR);
2138 add_reg(TDFAR);
2139 add_reg(TDFXR);
2140 add_reg(TDFFR);
2141 add_reg(RDLAR);
2142 add_reg(RDFAR);
2143 add_reg(RDFXR);
2144 add_reg(RDFFR);
2145 add_reg(TRSCER);
2146 add_reg(RMFCR);
2147 add_reg(TFTR);
2148 add_reg(FDR);
2149 add_reg(RMCR);
2150 add_reg(TFUCR);
2151 add_reg(RFOCR);
2152 if (cd->rmiimode)
2153 add_reg(RMIIMODE);
2154 add_reg(FCFTR);
2155 if (cd->rpadir)
2156 add_reg(RPADIR);
2157 if (!cd->no_trimd)
2158 add_reg(TRIMD);
2159 add_reg(ECMR);
2160 add_reg(ECSR);
2161 add_reg(ECSIPR);
2162 add_reg(PIR);
2163 if (!cd->no_psr)
2164 add_reg(PSR);
2165 add_reg(RDMLR);
2166 add_reg(RFLR);
2167 add_reg(IPGR);
2168 if (cd->apr)
2169 add_reg(APR);
2170 if (cd->mpr)
2171 add_reg(MPR);
2172 add_reg(RFCR);
2173 add_reg(RFCF);
2174 if (cd->tpauser)
2175 add_reg(TPAUSER);
2176 add_reg(TPAUSECR);
2177 add_reg(GECMR);
2178 if (cd->bculr)
2179 add_reg(BCULR);
2180 add_reg(MAHR);
2181 add_reg(MALR);
2182 add_reg(TROCR);
2183 add_reg(CDCR);
2184 add_reg(LCCR);
2185 add_reg(CNDCR);
2186 add_reg(CEFCR);
2187 add_reg(FRECR);
2188 add_reg(TSFRCR);
2189 add_reg(TLFRCR);
2190 add_reg(CERCR);
2191 add_reg(CEECR);
2192 add_reg(MAFCR);
2193 if (cd->rtrate)
2194 add_reg(RTRATE);
Sergei Shtylyov2c2ab5a2019-02-04 21:05:55 +03002195 if (cd->csmr)
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002196 add_reg(CSMR);
2197 if (cd->select_mii)
2198 add_reg(RMII_MII);
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002199 if (cd->tsu) {
Sergei Shtylyov17d0fb02018-01-13 20:22:01 +03002200 add_tsu_reg(ARSTR);
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002201 add_tsu_reg(TSU_CTRST);
2202 add_tsu_reg(TSU_FWEN0);
2203 add_tsu_reg(TSU_FWEN1);
2204 add_tsu_reg(TSU_FCM);
2205 add_tsu_reg(TSU_BSYSL0);
2206 add_tsu_reg(TSU_BSYSL1);
2207 add_tsu_reg(TSU_PRISL0);
2208 add_tsu_reg(TSU_PRISL1);
2209 add_tsu_reg(TSU_FWSL0);
2210 add_tsu_reg(TSU_FWSL1);
2211 add_tsu_reg(TSU_FWSLC);
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002212 add_tsu_reg(TSU_QTAGM0);
2213 add_tsu_reg(TSU_QTAGM1);
2214 add_tsu_reg(TSU_FWSR);
2215 add_tsu_reg(TSU_FWINMK);
2216 add_tsu_reg(TSU_ADQT0);
2217 add_tsu_reg(TSU_ADQT1);
2218 add_tsu_reg(TSU_VTAG0);
2219 add_tsu_reg(TSU_VTAG1);
2220 add_tsu_reg(TSU_ADSBSY);
2221 add_tsu_reg(TSU_TEN);
2222 add_tsu_reg(TSU_POST1);
2223 add_tsu_reg(TSU_POST2);
2224 add_tsu_reg(TSU_POST3);
2225 add_tsu_reg(TSU_POST4);
Sergei Shtylyove14549a2018-04-01 00:23:51 +03002226 /* This is the start of a table, not just a single register. */
2227 if (buf) {
2228 unsigned int i;
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002229
Sergei Shtylyove14549a2018-04-01 00:23:51 +03002230 mark_reg_valid(TSU_ADRH0);
2231 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES * 2; i++)
2232 *buf++ = ioread32(mdp->tsu_addr +
2233 mdp->reg_offset[TSU_ADRH0] +
2234 i * 4);
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002235 }
Sergei Shtylyove14549a2018-04-01 00:23:51 +03002236 len += SH_ETH_TSU_CAM_ENTRIES * 2;
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002237 }
2238
2239#undef mark_reg_valid
2240#undef add_reg_from
2241#undef add_reg
2242#undef add_tsu_reg
2243
2244 return len * 4;
2245}
2246
2247static int sh_eth_get_regs_len(struct net_device *ndev)
2248{
2249 return __sh_eth_get_regs(ndev, NULL);
2250}
2251
2252static void sh_eth_get_regs(struct net_device *ndev, struct ethtool_regs *regs,
2253 void *buf)
2254{
2255 struct sh_eth_private *mdp = netdev_priv(ndev);
2256
2257 regs->version = SH_ETH_REG_DUMP_VERSION;
2258
2259 pm_runtime_get_sync(&mdp->pdev->dev);
2260 __sh_eth_get_regs(ndev, buf);
2261 pm_runtime_put_sync(&mdp->pdev->dev);
2262}
2263
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002264static u32 sh_eth_get_msglevel(struct net_device *ndev)
2265{
2266 struct sh_eth_private *mdp = netdev_priv(ndev);
2267 return mdp->msg_enable;
2268}
2269
2270static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
2271{
2272 struct sh_eth_private *mdp = netdev_priv(ndev);
2273 mdp->msg_enable = value;
2274}
2275
2276static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
2277 "rx_current", "tx_current",
2278 "rx_dirty", "tx_dirty",
2279};
2280#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
2281
2282static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
2283{
2284 switch (sset) {
2285 case ETH_SS_STATS:
2286 return SH_ETH_STATS_LEN;
2287 default:
2288 return -EOPNOTSUPP;
2289 }
2290}
2291
2292static void sh_eth_get_ethtool_stats(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002293 struct ethtool_stats *stats, u64 *data)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002294{
2295 struct sh_eth_private *mdp = netdev_priv(ndev);
2296 int i = 0;
2297
2298 /* device-specific stats */
2299 data[i++] = mdp->cur_rx;
2300 data[i++] = mdp->cur_tx;
2301 data[i++] = mdp->dirty_rx;
2302 data[i++] = mdp->dirty_tx;
2303}
2304
2305static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
2306{
2307 switch (stringset) {
2308 case ETH_SS_STATS:
2309 memcpy(data, *sh_eth_gstrings_stats,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002310 sizeof(sh_eth_gstrings_stats));
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002311 break;
2312 }
2313}
2314
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002315static void sh_eth_get_ringparam(struct net_device *ndev,
2316 struct ethtool_ringparam *ring)
2317{
2318 struct sh_eth_private *mdp = netdev_priv(ndev);
2319
2320 ring->rx_max_pending = RX_RING_MAX;
2321 ring->tx_max_pending = TX_RING_MAX;
2322 ring->rx_pending = mdp->num_rx_ring;
2323 ring->tx_pending = mdp->num_tx_ring;
2324}
2325
2326static int sh_eth_set_ringparam(struct net_device *ndev,
2327 struct ethtool_ringparam *ring)
2328{
2329 struct sh_eth_private *mdp = netdev_priv(ndev);
2330 int ret;
2331
2332 if (ring->tx_pending > TX_RING_MAX ||
2333 ring->rx_pending > RX_RING_MAX ||
2334 ring->tx_pending < TX_RING_MIN ||
2335 ring->rx_pending < RX_RING_MIN)
2336 return -EINVAL;
2337 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2338 return -EINVAL;
2339
2340 if (netif_running(ndev)) {
Ben Hutchingsbd888912015-01-22 12:40:25 +00002341 netif_device_detach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002342 netif_tx_disable(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002343
Ben Hutchings283e38d2015-01-22 12:44:08 +00002344 /* Serialise with the interrupt handler and NAPI, then
2345 * disable interrupts. We have to clear the
2346 * irq_enabled flag first to ensure that interrupts
2347 * won't be re-enabled.
2348 */
2349 mdp->irq_enabled = false;
2350 synchronize_irq(ndev->irq);
2351 napi_synchronize(&mdp->napi);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002352 sh_eth_write(ndev, 0x0000, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00002353
Ben Hutchings740c7f32015-01-27 00:49:32 +00002354 sh_eth_dev_exit(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002355
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03002356 /* Free all the skbuffs in the Rx queue and the DMA buffers. */
Ben Hutchings084236d2015-01-22 12:41:34 +00002357 sh_eth_ring_free(ndev);
Ben Hutchings084236d2015-01-22 12:41:34 +00002358 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002359
2360 /* Set new parameters */
2361 mdp->num_rx_ring = ring->rx_pending;
2362 mdp->num_tx_ring = ring->tx_pending;
2363
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002364 if (netif_running(ndev)) {
Ben Hutchings084236d2015-01-22 12:41:34 +00002365 ret = sh_eth_ring_init(ndev);
2366 if (ret < 0) {
2367 netdev_err(ndev, "%s: sh_eth_ring_init failed.\n",
2368 __func__);
2369 return ret;
2370 }
Sergei Shtylyovf7967212016-04-24 19:11:07 +03002371 ret = sh_eth_dev_init(ndev);
Ben Hutchings084236d2015-01-22 12:41:34 +00002372 if (ret < 0) {
2373 netdev_err(ndev, "%s: sh_eth_dev_init failed.\n",
2374 __func__);
2375 return ret;
2376 }
2377
Ben Hutchingsbd888912015-01-22 12:40:25 +00002378 netif_device_attach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002379 }
2380
2381 return 0;
2382}
2383
Niklas Söderlundd8981d02017-01-09 16:34:05 +01002384static void sh_eth_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2385{
2386 struct sh_eth_private *mdp = netdev_priv(ndev);
2387
2388 wol->supported = 0;
2389 wol->wolopts = 0;
2390
Geert Uytterhoevenb4580c92018-02-12 14:42:36 +01002391 if (mdp->cd->magic) {
Niklas Söderlundd8981d02017-01-09 16:34:05 +01002392 wol->supported = WAKE_MAGIC;
2393 wol->wolopts = mdp->wol_enabled ? WAKE_MAGIC : 0;
2394 }
2395}
2396
2397static int sh_eth_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2398{
2399 struct sh_eth_private *mdp = netdev_priv(ndev);
2400
Geert Uytterhoevenb4580c92018-02-12 14:42:36 +01002401 if (!mdp->cd->magic || wol->wolopts & ~WAKE_MAGIC)
Niklas Söderlundd8981d02017-01-09 16:34:05 +01002402 return -EOPNOTSUPP;
2403
2404 mdp->wol_enabled = !!(wol->wolopts & WAKE_MAGIC);
2405
2406 device_set_wakeup_enable(&mdp->pdev->dev, mdp->wol_enabled);
2407
2408 return 0;
2409}
2410
stephen hemminger9b07be42012-01-04 12:59:49 +00002411static const struct ethtool_ops sh_eth_ethtool_ops = {
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002412 .get_regs_len = sh_eth_get_regs_len,
2413 .get_regs = sh_eth_get_regs,
Vladimir Zapolskiy4c106282018-07-04 11:12:42 +03002414 .nway_reset = phy_ethtool_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002415 .get_msglevel = sh_eth_get_msglevel,
2416 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00002417 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002418 .get_strings = sh_eth_get_strings,
2419 .get_ethtool_stats = sh_eth_get_ethtool_stats,
2420 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002421 .get_ringparam = sh_eth_get_ringparam,
2422 .set_ringparam = sh_eth_set_ringparam,
Vladimir Zapolskiy45abbd42018-07-04 11:14:48 +03002423 .get_link_ksettings = phy_ethtool_get_link_ksettings,
Vladimir Zapolskiy6783f502018-07-04 11:14:49 +03002424 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Niklas Söderlundd8981d02017-01-09 16:34:05 +01002425 .get_wol = sh_eth_get_wol,
2426 .set_wol = sh_eth_set_wol,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002427};
2428
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002429/* network device open function */
2430static int sh_eth_open(struct net_device *ndev)
2431{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002432 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03002433 int ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002434
Magnus Dammbcd51492009-10-09 00:20:04 +00002435 pm_runtime_get_sync(&mdp->pdev->dev);
2436
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002437 napi_enable(&mdp->napi);
2438
Joe Perchesa0607fd2009-11-18 23:29:17 -08002439 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00002440 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002441 if (ret) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002442 netdev_err(ndev, "Can not assign IRQ number\n");
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002443 goto out_napi_off;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002444 }
2445
2446 /* Descriptor set */
2447 ret = sh_eth_ring_init(ndev);
2448 if (ret)
2449 goto out_free_irq;
2450
2451 /* device init */
Sergei Shtylyovf7967212016-04-24 19:11:07 +03002452 ret = sh_eth_dev_init(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002453 if (ret)
2454 goto out_free_irq;
2455
2456 /* PHY control start*/
2457 ret = sh_eth_phy_start(ndev);
2458 if (ret)
2459 goto out_free_irq;
2460
Sergei Shtylyovad846aa2016-03-14 01:09:53 +03002461 netif_start_queue(ndev);
2462
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002463 mdp->is_opened = 1;
2464
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002465 return ret;
2466
2467out_free_irq:
2468 free_irq(ndev->irq, ndev);
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002469out_napi_off:
2470 napi_disable(&mdp->napi);
Magnus Dammbcd51492009-10-09 00:20:04 +00002471 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002472 return ret;
2473}
2474
2475/* Timeout function */
2476static void sh_eth_tx_timeout(struct net_device *ndev)
2477{
2478 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002479 struct sh_eth_rxdesc *rxdesc;
2480 int i;
2481
2482 netif_stop_queue(ndev);
2483
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002484 netif_err(mdp, timer, ndev,
2485 "transmit timed out, status %8.8x, resetting...\n",
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01002486 sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002487
2488 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002489 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002490
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002491 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002492 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002493 rxdesc = &mdp->rx_ring[i];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002494 rxdesc->status = cpu_to_le32(0);
2495 rxdesc->addr = cpu_to_le32(0xBADF00D0);
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002496 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002497 mdp->rx_skbuff[i] = NULL;
2498 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002499 for (i = 0; i < mdp->num_tx_ring; i++) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002500 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002501 mdp->tx_skbuff[i] = NULL;
2502 }
2503
2504 /* device init */
Sergei Shtylyovf7967212016-04-24 19:11:07 +03002505 sh_eth_dev_init(ndev);
Sergei Shtylyovad846aa2016-03-14 01:09:53 +03002506
2507 netif_start_queue(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002508}
2509
2510/* Packet transmit function */
2511static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
2512{
2513 struct sh_eth_private *mdp = netdev_priv(ndev);
2514 struct sh_eth_txdesc *txdesc;
Sergei Shtylyov12996532015-12-13 23:05:07 +03002515 dma_addr_t dma_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002516 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00002517 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002518
2519 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002520 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Sergei Shtylyov1debdc82017-04-17 15:55:22 +03002521 if (!sh_eth_tx_free(ndev, true)) {
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002522 netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002523 netif_stop_queue(ndev);
2524 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00002525 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002526 }
2527 }
2528 spin_unlock_irqrestore(&mdp->lock, flags);
2529
Ben Hutchingsdacc73e2015-03-03 00:53:08 +00002530 if (skb_put_padto(skb, ETH_ZLEN))
Ben Hutchingseebfb642015-01-22 12:40:13 +00002531 return NETDEV_TX_OK;
2532
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002533 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002534 mdp->tx_skbuff[entry] = skb;
2535 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002536 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002537 if (!mdp->cd->hw_swap)
Sergei Shtylyov3e230992015-12-13 21:27:04 +03002538 sh_eth_soft_swap(PTR_ALIGN(skb->data, 4), skb->len + 2);
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01002539 dma_addr = dma_map_single(&mdp->pdev->dev, skb->data, skb->len,
Sergei Shtylyov12996532015-12-13 23:05:07 +03002540 DMA_TO_DEVICE);
Thomas Petazzoni22c1aed2017-12-04 14:33:26 +01002541 if (dma_mapping_error(&mdp->pdev->dev, dma_addr)) {
Ben Hutchingsaa3933b2015-01-27 00:49:47 +00002542 kfree_skb(skb);
2543 return NETDEV_TX_OK;
2544 }
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002545 txdesc->addr = cpu_to_le32(dma_addr);
2546 txdesc->len = cpu_to_le32(skb->len << 16);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002547
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03002548 dma_wmb(); /* TACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002549 if (entry >= mdp->num_tx_ring - 1)
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002550 txdesc->status |= cpu_to_le32(TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002551 else
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002552 txdesc->status |= cpu_to_le32(TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002553
2554 mdp->cur_tx++;
2555
Sergei Shtylyov3e416992018-03-24 23:08:42 +03002556 if (!(sh_eth_read(ndev, EDTRR) & mdp->cd->edtrr_trns))
2557 sh_eth_write(ndev, mdp->cd->edtrr_trns, EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09002558
Patrick McHardy6ed10652009-06-23 06:03:08 +00002559 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002560}
2561
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002562/* The statistics registers have write-clear behaviour, which means we
2563 * will lose any increment between the read and write. We mitigate
2564 * this by only clearing when we read a non-zero value, so we will
2565 * never falsely report a total of zero.
2566 */
2567static void
2568sh_eth_update_stat(struct net_device *ndev, unsigned long *stat, int reg)
2569{
2570 u32 delta = sh_eth_read(ndev, reg);
2571
2572 if (delta) {
2573 *stat += delta;
2574 sh_eth_write(ndev, 0, reg);
2575 }
2576}
2577
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002578static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
2579{
2580 struct sh_eth_private *mdp = netdev_priv(ndev);
2581
Sergei Shtylyovce9134d2018-03-24 23:11:19 +03002582 if (mdp->cd->no_tx_cntrs)
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002583 return &ndev->stats;
2584
2585 if (!mdp->is_opened)
2586 return &ndev->stats;
2587
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002588 sh_eth_update_stat(ndev, &ndev->stats.tx_dropped, TROCR);
2589 sh_eth_update_stat(ndev, &ndev->stats.collisions, CDCR);
2590 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, LCCR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002591
Sergei Shtylyov4c1d4582018-03-24 23:12:54 +03002592 if (mdp->cd->cexcr) {
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002593 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2594 CERCR);
2595 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2596 CEECR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002597 } else {
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002598 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2599 CNDCR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002600 }
2601
2602 return &ndev->stats;
2603}
2604
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002605/* device close function */
2606static int sh_eth_close(struct net_device *ndev)
2607{
2608 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002609
2610 netif_stop_queue(ndev);
2611
Ben Hutchings283e38d2015-01-22 12:44:08 +00002612 /* Serialise with the interrupt handler and NAPI, then disable
2613 * interrupts. We have to clear the irq_enabled flag first to
2614 * ensure that interrupts won't be re-enabled.
2615 */
2616 mdp->irq_enabled = false;
2617 synchronize_irq(ndev->irq);
2618 napi_disable(&mdp->napi);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002619 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002620
Ben Hutchings740c7f32015-01-27 00:49:32 +00002621 sh_eth_dev_exit(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002622
2623 /* PHY Disconnect */
Philippe Reynes9fd03752016-08-10 00:04:48 +02002624 if (ndev->phydev) {
2625 phy_stop(ndev->phydev);
2626 phy_disconnect(ndev->phydev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002627 }
2628
2629 free_irq(ndev->irq, ndev);
2630
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03002631 /* Free all the skbuffs in the Rx queue and the DMA buffer. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002632 sh_eth_ring_free(ndev);
2633
Magnus Dammbcd51492009-10-09 00:20:04 +00002634 pm_runtime_put_sync(&mdp->pdev->dev);
2635
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002636 mdp->is_opened = 0;
2637
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002638 return 0;
2639}
2640
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002641/* ioctl to device function */
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002642static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002643{
Philippe Reynes9fd03752016-08-10 00:04:48 +02002644 struct phy_device *phydev = ndev->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002645
2646 if (!netif_running(ndev))
2647 return -EINVAL;
2648
2649 if (!phydev)
2650 return -ENODEV;
2651
Richard Cochran28b04112010-07-17 08:48:55 +00002652 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002653}
2654
Niklas Söderlund78d61022017-06-12 10:39:03 +02002655static int sh_eth_change_mtu(struct net_device *ndev, int new_mtu)
2656{
2657 if (netif_running(ndev))
2658 return -EBUSY;
2659
2660 ndev->mtu = new_mtu;
2661 netdev_update_features(ndev);
2662
2663 return 0;
2664}
2665
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002666/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002667static u32 sh_eth_tsu_get_post_mask(int entry)
2668{
2669 return 0x0f << (28 - ((entry % 8) * 4));
2670}
2671
2672static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2673{
2674 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2675}
2676
2677static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2678 int entry)
2679{
2680 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov77cb0652018-05-02 22:54:48 +03002681 int reg = TSU_POST1 + entry / 8;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002682 u32 tmp;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002683
Sergei Shtylyov77cb0652018-05-02 22:54:48 +03002684 tmp = sh_eth_tsu_read(mdp, reg);
2685 sh_eth_tsu_write(mdp, tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002686}
2687
2688static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2689 int entry)
2690{
2691 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov77cb0652018-05-02 22:54:48 +03002692 int reg = TSU_POST1 + entry / 8;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002693 u32 post_mask, ref_mask, tmp;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002694
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002695 post_mask = sh_eth_tsu_get_post_mask(entry);
2696 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2697
Sergei Shtylyov77cb0652018-05-02 22:54:48 +03002698 tmp = sh_eth_tsu_read(mdp, reg);
2699 sh_eth_tsu_write(mdp, tmp & ~post_mask, reg);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002700
2701 /* If other port enables, the function returns "true" */
2702 return tmp & ref_mask;
2703}
2704
2705static int sh_eth_tsu_busy(struct net_device *ndev)
2706{
2707 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2708 struct sh_eth_private *mdp = netdev_priv(ndev);
2709
2710 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2711 udelay(10);
2712 timeout--;
2713 if (timeout <= 0) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002714 netdev_err(ndev, "%s: timeout\n", __func__);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002715 return -ETIMEDOUT;
2716 }
2717 }
2718
2719 return 0;
2720}
2721
Sergei Shtylyov7a54c862018-07-23 21:14:38 +03002722static int sh_eth_tsu_write_entry(struct net_device *ndev, u16 offset,
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002723 const u8 *addr)
2724{
Sergei Shtylyov7a54c862018-07-23 21:14:38 +03002725 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002726 u32 val;
2727
2728 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
Sergei Shtylyov7a54c862018-07-23 21:14:38 +03002729 iowrite32(val, mdp->tsu_addr + offset);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002730 if (sh_eth_tsu_busy(ndev) < 0)
2731 return -EBUSY;
2732
2733 val = addr[4] << 8 | addr[5];
Sergei Shtylyov7a54c862018-07-23 21:14:38 +03002734 iowrite32(val, mdp->tsu_addr + offset + 4);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002735 if (sh_eth_tsu_busy(ndev) < 0)
2736 return -EBUSY;
2737
2738 return 0;
2739}
2740
Sergei Shtylyov51459d42018-07-23 21:15:47 +03002741static void sh_eth_tsu_read_entry(struct net_device *ndev, u16 offset, u8 *addr)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002742{
Sergei Shtylyov51459d42018-07-23 21:15:47 +03002743 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002744 u32 val;
2745
Sergei Shtylyov51459d42018-07-23 21:15:47 +03002746 val = ioread32(mdp->tsu_addr + offset);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002747 addr[0] = (val >> 24) & 0xff;
2748 addr[1] = (val >> 16) & 0xff;
2749 addr[2] = (val >> 8) & 0xff;
2750 addr[3] = val & 0xff;
Sergei Shtylyov51459d42018-07-23 21:15:47 +03002751 val = ioread32(mdp->tsu_addr + offset + 4);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002752 addr[4] = (val >> 8) & 0xff;
2753 addr[5] = val & 0xff;
2754}
2755
2756
2757static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2758{
2759 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov41414f02018-07-23 21:11:19 +03002760 u16 reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002761 int i;
2762 u8 c_addr[ETH_ALEN];
2763
2764 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
Sergei Shtylyov51459d42018-07-23 21:15:47 +03002765 sh_eth_tsu_read_entry(ndev, reg_offset, c_addr);
dingtianhongc4bde292013-12-30 15:41:17 +08002766 if (ether_addr_equal(addr, c_addr))
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002767 return i;
2768 }
2769
2770 return -ENOENT;
2771}
2772
2773static int sh_eth_tsu_find_empty(struct net_device *ndev)
2774{
2775 u8 blank[ETH_ALEN];
2776 int entry;
2777
2778 memset(blank, 0, sizeof(blank));
2779 entry = sh_eth_tsu_find_entry(ndev, blank);
2780 return (entry < 0) ? -ENOMEM : entry;
2781}
2782
2783static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2784 int entry)
2785{
2786 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov41414f02018-07-23 21:11:19 +03002787 u16 reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002788 int ret;
2789 u8 blank[ETH_ALEN];
2790
2791 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2792 ~(1 << (31 - entry)), TSU_TEN);
2793
2794 memset(blank, 0, sizeof(blank));
Sergei Shtylyov7a54c862018-07-23 21:14:38 +03002795 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002796 if (ret < 0)
2797 return ret;
2798 return 0;
2799}
2800
2801static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2802{
2803 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov41414f02018-07-23 21:11:19 +03002804 u16 reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002805 int i, ret;
2806
2807 if (!mdp->cd->tsu)
2808 return 0;
2809
2810 i = sh_eth_tsu_find_entry(ndev, addr);
2811 if (i < 0) {
2812 /* No entry found, create one */
2813 i = sh_eth_tsu_find_empty(ndev);
2814 if (i < 0)
2815 return -ENOMEM;
Sergei Shtylyov7a54c862018-07-23 21:14:38 +03002816 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002817 if (ret < 0)
2818 return ret;
2819
2820 /* Enable the entry */
2821 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2822 (1 << (31 - i)), TSU_TEN);
2823 }
2824
2825 /* Entry found or created, enable POST */
2826 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2827
2828 return 0;
2829}
2830
2831static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2832{
2833 struct sh_eth_private *mdp = netdev_priv(ndev);
2834 int i, ret;
2835
2836 if (!mdp->cd->tsu)
2837 return 0;
2838
2839 i = sh_eth_tsu_find_entry(ndev, addr);
2840 if (i) {
2841 /* Entry found */
2842 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2843 goto done;
2844
2845 /* Disable the entry if both ports was disabled */
2846 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2847 if (ret < 0)
2848 return ret;
2849 }
2850done:
2851 return 0;
2852}
2853
2854static int sh_eth_tsu_purge_all(struct net_device *ndev)
2855{
2856 struct sh_eth_private *mdp = netdev_priv(ndev);
2857 int i, ret;
2858
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002859 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002860 return 0;
2861
2862 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2863 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2864 continue;
2865
2866 /* Disable the entry if both ports was disabled */
2867 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2868 if (ret < 0)
2869 return ret;
2870 }
2871
2872 return 0;
2873}
2874
2875static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2876{
2877 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov41414f02018-07-23 21:11:19 +03002878 u16 reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002879 u8 addr[ETH_ALEN];
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002880 int i;
2881
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002882 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002883 return;
2884
2885 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
Sergei Shtylyov51459d42018-07-23 21:15:47 +03002886 sh_eth_tsu_read_entry(ndev, reg_offset, addr);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002887 if (is_multicast_ether_addr(addr))
2888 sh_eth_tsu_del_entry(ndev, addr);
2889 }
2890}
2891
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002892/* Update promiscuous flag and multicast filter */
2893static void sh_eth_set_rx_mode(struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002894{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002895 struct sh_eth_private *mdp = netdev_priv(ndev);
2896 u32 ecmr_bits;
2897 int mcast_all = 0;
2898 unsigned long flags;
2899
2900 spin_lock_irqsave(&mdp->lock, flags);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002901 /* Initial condition is MCT = 1, PRM = 0.
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002902 * Depending on ndev->flags, set PRM or clear MCT
2903 */
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002904 ecmr_bits = sh_eth_read(ndev, ECMR) & ~ECMR_PRM;
2905 if (mdp->cd->tsu)
2906 ecmr_bits |= ECMR_MCT;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002907
2908 if (!(ndev->flags & IFF_MULTICAST)) {
2909 sh_eth_tsu_purge_mcast(ndev);
2910 mcast_all = 1;
2911 }
2912 if (ndev->flags & IFF_ALLMULTI) {
2913 sh_eth_tsu_purge_mcast(ndev);
2914 ecmr_bits &= ~ECMR_MCT;
2915 mcast_all = 1;
2916 }
2917
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002918 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002919 sh_eth_tsu_purge_all(ndev);
2920 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2921 } else if (mdp->cd->tsu) {
2922 struct netdev_hw_addr *ha;
2923 netdev_for_each_mc_addr(ha, ndev) {
2924 if (mcast_all && is_multicast_ether_addr(ha->addr))
2925 continue;
2926
2927 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2928 if (!mcast_all) {
2929 sh_eth_tsu_purge_mcast(ndev);
2930 ecmr_bits &= ~ECMR_MCT;
2931 mcast_all = 1;
2932 }
2933 }
2934 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002935 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002936
2937 /* update the ethernet mode */
2938 sh_eth_write(ndev, ecmr_bits, ECMR);
2939
2940 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002941}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002942
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03002943static void sh_eth_set_rx_csum(struct net_device *ndev, bool enable)
2944{
2945 struct sh_eth_private *mdp = netdev_priv(ndev);
2946 unsigned long flags;
2947
2948 spin_lock_irqsave(&mdp->lock, flags);
2949
2950 /* Disable TX and RX */
2951 sh_eth_rcv_snd_disable(ndev);
2952
2953 /* Modify RX Checksum setting */
2954 sh_eth_modify(ndev, ECMR, ECMR_RCSC, enable ? ECMR_RCSC : 0);
2955
2956 /* Enable TX and RX */
2957 sh_eth_rcv_snd_enable(ndev);
2958
2959 spin_unlock_irqrestore(&mdp->lock, flags);
2960}
2961
2962static int sh_eth_set_features(struct net_device *ndev,
2963 netdev_features_t features)
2964{
2965 netdev_features_t changed = ndev->features ^ features;
2966 struct sh_eth_private *mdp = netdev_priv(ndev);
2967
2968 if (changed & NETIF_F_RXCSUM && mdp->cd->rx_csum)
2969 sh_eth_set_rx_csum(ndev, features & NETIF_F_RXCSUM);
2970
2971 ndev->features = features;
2972
2973 return 0;
2974}
2975
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002976static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2977{
2978 if (!mdp->port)
2979 return TSU_VTAG0;
2980 else
2981 return TSU_VTAG1;
2982}
2983
Patrick McHardy80d5c362013-04-19 02:04:28 +00002984static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2985 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002986{
2987 struct sh_eth_private *mdp = netdev_priv(ndev);
2988 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2989
2990 if (unlikely(!mdp->cd->tsu))
2991 return -EPERM;
2992
2993 /* No filtering if vid = 0 */
2994 if (!vid)
2995 return 0;
2996
2997 mdp->vlan_num_ids++;
2998
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002999 /* The controller has one VLAN tag HW filter. So, if the filter is
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00003000 * already enabled, the driver disables it and the filte
3001 */
3002 if (mdp->vlan_num_ids > 1) {
3003 /* disable VLAN filter */
3004 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
3005 return 0;
3006 }
3007
3008 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
3009 vtag_reg_index);
3010
3011 return 0;
3012}
3013
Patrick McHardy80d5c362013-04-19 02:04:28 +00003014static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
3015 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00003016{
3017 struct sh_eth_private *mdp = netdev_priv(ndev);
3018 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
3019
3020 if (unlikely(!mdp->cd->tsu))
3021 return -EPERM;
3022
3023 /* No filtering if vid = 0 */
3024 if (!vid)
3025 return 0;
3026
3027 mdp->vlan_num_ids--;
3028 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
3029
3030 return 0;
3031}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003032
3033/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00003034static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003035{
Sergei Shtylyova94cf2a2018-02-24 22:41:45 +03003036 if (!mdp->cd->dual_port) {
Simon Hormandb893472014-01-17 09:22:28 +09003037 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
Chris Brandte1487882016-09-07 14:57:09 -04003038 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL,
3039 TSU_FWSLC); /* Enable POST registers */
Simon Hormandb893472014-01-17 09:22:28 +09003040 return;
3041 }
3042
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00003043 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
3044 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
3045 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
3046 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
3047 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
3048 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
3049 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
3050 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
3051 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
3052 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Sergei Shtylyov4869a142018-02-24 20:28:16 +03003053 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
3054 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00003055 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
3056 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
3057 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
3058 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
3059 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
3060 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
3061 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003062}
3063
3064/* MDIO bus release function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01003065static int sh_mdio_release(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003066{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003067 /* unregister mdio bus */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01003068 mdiobus_unregister(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003069
3070 /* free bitbang info */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01003071 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003072
3073 return 0;
3074}
3075
3076/* MDIO bus init function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01003077static int sh_mdio_init(struct sh_eth_private *mdp,
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00003078 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003079{
Andrew Lunne7f4dc32016-01-06 20:11:15 +01003080 int ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003081 struct bb_info *bitbang;
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01003082 struct platform_device *pdev = mdp->pdev;
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01003083 struct device *dev = &mdp->pdev->dev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003084
3085 /* create bit control struct for PHY */
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01003086 bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL);
Laurent Pinchartf738a132014-03-20 15:00:35 +01003087 if (!bitbang)
3088 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003089
3090 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00003091 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00003092 bitbang->set_gate = pd->set_mdio_gate;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003093 bitbang->ctrl.ops = &bb_ops;
3094
Stefan Weilc2e07b32010-08-03 19:44:52 +02003095 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003096 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
Laurent Pinchartf738a132014-03-20 15:00:35 +01003097 if (!mdp->mii_bus)
3098 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003099
3100 /* Hook up MII support for ethtool */
3101 mdp->mii_bus->name = "sh_mii";
Laurent Pincharta5bd60602014-03-20 15:00:32 +01003102 mdp->mii_bus->parent = dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00003103 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01003104 pdev->name, pdev->id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003105
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01003106 /* register MDIO bus */
Florian Fainelli00e798c2018-05-15 16:56:19 -07003107 if (pd->phy_irq > 0)
3108 mdp->mii_bus->irq[pd->phy] = pd->phy_irq;
Ben Dooks702eca02014-03-12 17:47:40 +00003109
Florian Fainelli00e798c2018-05-15 16:56:19 -07003110 ret = of_mdiobus_register(mdp->mii_bus, dev->of_node);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003111 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00003112 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003113
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003114 return 0;
3115
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003116out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07003117 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003118 return ret;
3119}
3120
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00003121static const u16 *sh_eth_get_register_offset(int register_type)
3122{
3123 const u16 *reg_offset = NULL;
3124
3125 switch (register_type) {
3126 case SH_ETH_REG_GIGABIT:
3127 reg_offset = sh_eth_offset_gigabit;
3128 break;
Simon Hormandb893472014-01-17 09:22:28 +09003129 case SH_ETH_REG_FAST_RZ:
3130 reg_offset = sh_eth_offset_fast_rz;
3131 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00003132 case SH_ETH_REG_FAST_RCAR:
3133 reg_offset = sh_eth_offset_fast_rcar;
3134 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00003135 case SH_ETH_REG_FAST_SH4:
3136 reg_offset = sh_eth_offset_fast_sh4;
3137 break;
3138 case SH_ETH_REG_FAST_SH3_SH2:
3139 reg_offset = sh_eth_offset_fast_sh3_sh2;
3140 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00003141 }
3142
3143 return reg_offset;
3144}
3145
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003146static const struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00003147 .ndo_open = sh_eth_open,
3148 .ndo_stop = sh_eth_close,
3149 .ndo_start_xmit = sh_eth_start_xmit,
3150 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00003151 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00003152 .ndo_tx_timeout = sh_eth_tx_timeout,
3153 .ndo_do_ioctl = sh_eth_do_ioctl,
Niklas Söderlund78d61022017-06-12 10:39:03 +02003154 .ndo_change_mtu = sh_eth_change_mtu,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00003155 .ndo_validate_addr = eth_validate_addr,
3156 .ndo_set_mac_address = eth_mac_addr,
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03003157 .ndo_set_features = sh_eth_set_features,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00003158};
3159
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003160static const struct net_device_ops sh_eth_netdev_ops_tsu = {
3161 .ndo_open = sh_eth_open,
3162 .ndo_stop = sh_eth_close,
3163 .ndo_start_xmit = sh_eth_start_xmit,
3164 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00003165 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003166 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
3167 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
3168 .ndo_tx_timeout = sh_eth_tx_timeout,
3169 .ndo_do_ioctl = sh_eth_do_ioctl,
Niklas Söderlund78d61022017-06-12 10:39:03 +02003170 .ndo_change_mtu = sh_eth_change_mtu,
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003171 .ndo_validate_addr = eth_validate_addr,
3172 .ndo_set_mac_address = eth_mac_addr,
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03003173 .ndo_set_features = sh_eth_set_features,
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003174};
3175
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003176#ifdef CONFIG_OF
3177static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
3178{
3179 struct device_node *np = dev->of_node;
3180 struct sh_eth_plat_data *pdata;
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003181 const char *mac_addr;
3182
3183 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
3184 if (!pdata)
3185 return NULL;
3186
3187 pdata->phy_interface = of_get_phy_mode(np);
3188
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003189 mac_addr = of_get_mac_address(np);
3190 if (mac_addr)
3191 memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
3192
3193 pdata->no_ether_link =
3194 of_property_read_bool(np, "renesas,no-ether-link");
3195 pdata->ether_link_active_low =
3196 of_property_read_bool(np, "renesas,ether-link-active-low");
3197
3198 return pdata;
3199}
3200
3201static const struct of_device_id sh_eth_match_table[] = {
3202 { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data },
Simon Horman6c4b2f72017-10-18 09:21:27 +02003203 { .compatible = "renesas,ether-r8a7743", .data = &rcar_gen2_data },
3204 { .compatible = "renesas,ether-r8a7745", .data = &rcar_gen2_data },
3205 { .compatible = "renesas,ether-r8a7778", .data = &rcar_gen1_data },
3206 { .compatible = "renesas,ether-r8a7779", .data = &rcar_gen1_data },
3207 { .compatible = "renesas,ether-r8a7790", .data = &rcar_gen2_data },
3208 { .compatible = "renesas,ether-r8a7791", .data = &rcar_gen2_data },
3209 { .compatible = "renesas,ether-r8a7793", .data = &rcar_gen2_data },
3210 { .compatible = "renesas,ether-r8a7794", .data = &rcar_gen2_data },
Sergei Shtylyov3eb9c2a2018-05-18 21:32:46 +03003211 { .compatible = "renesas,gether-r8a77980", .data = &r8a77980_data },
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003212 { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data },
Chris Brandt6e0bb042018-08-27 12:42:02 -05003213 { .compatible = "renesas,ether-r7s9210", .data = &r7s9210_data },
Simon Hormanb4804e02017-10-18 09:21:28 +02003214 { .compatible = "renesas,rcar-gen1-ether", .data = &rcar_gen1_data },
3215 { .compatible = "renesas,rcar-gen2-ether", .data = &rcar_gen2_data },
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003216 { }
3217};
3218MODULE_DEVICE_TABLE(of, sh_eth_match_table);
3219#else
3220static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
3221{
3222 return NULL;
3223}
3224#endif
3225
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003226static int sh_eth_drv_probe(struct platform_device *pdev)
3227{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003228 struct resource *res;
Jingoo Han0b76b862013-08-30 14:00:11 +09003229 struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev);
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003230 const struct platform_device_id *id = platform_get_device_id(pdev);
Sergei Shtylyov4fa8c3c2016-03-13 01:29:45 +03003231 struct sh_eth_private *mdp;
3232 struct net_device *ndev;
Sergei Shtylyov9662ec12018-01-14 20:47:44 +03003233 int ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003234
3235 /* get base addr */
3236 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003237
3238 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
Laurent Pinchartf738a132014-03-20 15:00:35 +01003239 if (!ndev)
3240 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003241
Ben Dooksb5893a02014-03-21 12:09:14 +01003242 pm_runtime_enable(&pdev->dev);
3243 pm_runtime_get_sync(&pdev->dev);
3244
roel kluincc3c0802008-09-10 19:22:44 +02003245 ret = platform_get_irq(pdev, 0);
Sergei Shtylyov7a468ac2015-08-28 16:56:01 +03003246 if (ret < 0)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003247 goto out_release;
roel kluincc3c0802008-09-10 19:22:44 +02003248 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003249
3250 SET_NETDEV_DEV(ndev, &pdev->dev);
3251
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003252 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00003253 mdp->num_tx_ring = TX_RING_SIZE;
3254 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00003255 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
3256 if (IS_ERR(mdp->addr)) {
3257 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00003258 goto out_release;
3259 }
3260
Varka Bhadramc9608042014-10-24 07:42:09 +05303261 ndev->base_addr = res->start;
3262
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003263 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00003264 mdp->pdev = pdev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003265
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003266 if (pdev->dev.of_node)
3267 pd = sh_eth_parse_dt(&pdev->dev);
Sergei Shtylyov3b4c5cb2013-10-30 23:30:19 +03003268 if (!pd) {
3269 dev_err(&pdev->dev, "no platform data\n");
3270 ret = -EINVAL;
3271 goto out_release;
3272 }
3273
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003274 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04003275 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00003276 mdp->phy_interface = pd->phy_interface;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00003277 mdp->no_ether_link = pd->no_ether_link;
3278 mdp->ether_link_active_low = pd->ether_link_active_low;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003279
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003280 /* set cpu data */
Wolfram Sang42a67c92016-03-01 17:37:59 +01003281 if (id)
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003282 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
Wolfram Sang42a67c92016-03-01 17:37:59 +01003283 else
3284 mdp->cd = (struct sh_eth_cpu_data *)of_device_get_match_data(&pdev->dev);
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003285
Sergei Shtylyova3153d82013-08-18 03:11:28 +04003286 mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type);
Sergei Shtylyov264be2f2014-03-15 03:11:24 +03003287 if (!mdp->reg_offset) {
3288 dev_err(&pdev->dev, "Unknown register type (%d)\n",
3289 mdp->cd->register_type);
3290 ret = -EINVAL;
3291 goto out_release;
3292 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003293 sh_eth_set_default_cpu_data(mdp->cd);
3294
Niklas Söderlund78d61022017-06-12 10:39:03 +02003295 /* User's manual states max MTU should be 2048 but due to the
3296 * alignment calculations in sh_eth_ring_init() the practical
3297 * MTU is a bit less. Maybe this can be optimized some more.
3298 */
3299 ndev->max_mtu = 2000 - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
3300 ndev->min_mtu = ETH_MIN_MTU;
3301
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03003302 if (mdp->cd->rx_csum) {
3303 ndev->features = NETIF_F_RXCSUM;
3304 ndev->hw_features = NETIF_F_RXCSUM;
3305 }
3306
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003307 /* set function */
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003308 if (mdp->cd->tsu)
3309 ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
3310 else
3311 ndev->netdev_ops = &sh_eth_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003312 ndev->ethtool_ops = &sh_eth_ethtool_ops;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003313 ndev->watchdog_timeo = TX_TIMEOUT;
3314
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00003315 /* debug message level */
3316 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003317
3318 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00003319 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00003320 if (!is_valid_ether_addr(ndev->dev_addr)) {
3321 dev_warn(&pdev->dev,
3322 "no valid MAC address supplied, using a random one.\n");
3323 eth_hw_addr_random(ndev);
3324 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003325
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003326 if (mdp->cd->tsu) {
Sergei Shtylyov9662ec12018-01-14 20:47:44 +03003327 int port = pdev->id < 0 ? 0 : pdev->id % 2;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003328 struct resource *rtsu;
Sergei Shtylyovdfe82662018-01-03 20:09:49 +03003329
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003330 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovdfe82662018-01-03 20:09:49 +03003331 if (!rtsu) {
3332 dev_err(&pdev->dev, "no TSU resource\n");
3333 ret = -ENODEV;
3334 goto out_release;
3335 }
3336 /* We can only request the TSU region for the first port
3337 * of the two sharing this TSU for the probe to succeed...
3338 */
Sergei Shtylyov9662ec12018-01-14 20:47:44 +03003339 if (port == 0 &&
Sergei Shtylyovdfe82662018-01-03 20:09:49 +03003340 !devm_request_mem_region(&pdev->dev, rtsu->start,
3341 resource_size(rtsu),
3342 dev_name(&pdev->dev))) {
3343 dev_err(&pdev->dev, "can't request TSU resource.\n");
3344 ret = -EBUSY;
3345 goto out_release;
3346 }
Sergei Shtylyov3e14c962018-01-14 20:47:43 +03003347 /* ioremap the TSU registers */
Sergei Shtylyovdfe82662018-01-03 20:09:49 +03003348 mdp->tsu_addr = devm_ioremap(&pdev->dev, rtsu->start,
3349 resource_size(rtsu));
3350 if (!mdp->tsu_addr) {
3351 dev_err(&pdev->dev, "TSU region ioremap() failed.\n");
3352 ret = -ENOMEM;
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00003353 goto out_release;
3354 }
Sergei Shtylyov9662ec12018-01-14 20:47:44 +03003355 mdp->port = port;
Sergei Shtylyovf8e022d2019-02-04 21:06:52 +03003356 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003357
Sergei Shtylyov3e14c962018-01-14 20:47:43 +03003358 /* Need to init only the first port of the two sharing a TSU */
Sergei Shtylyov9662ec12018-01-14 20:47:44 +03003359 if (port == 0) {
Sergei Shtylyov3e14c962018-01-14 20:47:43 +03003360 if (mdp->cd->chip_reset)
3361 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003362
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00003363 /* TSU init (Init only)*/
3364 sh_eth_tsu_init(mdp);
3365 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003366 }
3367
Hisashi Nakamura966d6db2014-11-13 15:54:05 +09003368 if (mdp->cd->rmiimode)
3369 sh_eth_write(ndev, 0x1, RMIIMODE);
3370
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003371 /* MDIO bus init */
3372 ret = sh_mdio_init(mdp, pd);
3373 if (ret) {
Geert Uytterhoevenb7ce5202017-05-18 15:01:35 +02003374 if (ret != -EPROBE_DEFER)
3375 dev_err(&pdev->dev, "MDIO init failed: %d\n", ret);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003376 goto out_release;
3377 }
3378
Sergei Shtylyov37191092013-06-19 23:30:23 +04003379 netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
3380
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003381 /* network device register */
3382 ret = register_netdev(ndev);
3383 if (ret)
Sergei Shtylyov37191092013-06-19 23:30:23 +04003384 goto out_napi_del;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003385
Geert Uytterhoevenb4580c92018-02-12 14:42:36 +01003386 if (mdp->cd->magic)
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003387 device_set_wakeup_capable(&pdev->dev, 1);
3388
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003389 /* print device information */
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +03003390 netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n",
3391 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003392
Ben Dooksb5893a02014-03-21 12:09:14 +01003393 pm_runtime_put(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003394 platform_set_drvdata(pdev, ndev);
3395
3396 return ret;
3397
Sergei Shtylyov37191092013-06-19 23:30:23 +04003398out_napi_del:
3399 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003400 sh_mdio_release(mdp);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003401
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003402out_release:
3403 /* net_dev free */
Sergei Shtylyov4282fc42017-12-31 21:41:36 +03003404 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003405
Ben Dooksb5893a02014-03-21 12:09:14 +01003406 pm_runtime_put(&pdev->dev);
3407 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003408 return ret;
3409}
3410
3411static int sh_eth_drv_remove(struct platform_device *pdev)
3412{
3413 struct net_device *ndev = platform_get_drvdata(pdev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003414 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003415
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003416 unregister_netdev(ndev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003417 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003418 sh_mdio_release(mdp);
Magnus Dammbcd51492009-10-09 00:20:04 +00003419 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003420 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003421
3422 return 0;
3423}
3424
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003425#ifdef CONFIG_PM
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003426#ifdef CONFIG_PM_SLEEP
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003427static int sh_eth_wol_setup(struct net_device *ndev)
3428{
3429 struct sh_eth_private *mdp = netdev_priv(ndev);
3430
3431 /* Only allow ECI interrupts */
3432 synchronize_irq(ndev->irq);
3433 napi_disable(&mdp->napi);
Sergei Shtylyov1a0bee62017-01-29 15:07:34 +03003434 sh_eth_write(ndev, EESIPR_ECIIP, EESIPR);
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003435
3436 /* Enable MagicPacket */
Niklas Söderlund5e2ed132017-02-01 15:41:54 +01003437 sh_eth_modify(ndev, ECMR, ECMR_MPDE, ECMR_MPDE);
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003438
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003439 return enable_irq_wake(ndev->irq);
3440}
3441
3442static int sh_eth_wol_restore(struct net_device *ndev)
3443{
3444 struct sh_eth_private *mdp = netdev_priv(ndev);
3445 int ret;
3446
3447 napi_enable(&mdp->napi);
3448
3449 /* Disable MagicPacket */
3450 sh_eth_modify(ndev, ECMR, ECMR_MPDE, 0);
3451
3452 /* The device needs to be reset to restore MagicPacket logic
3453 * for next wakeup. If we close and open the device it will
3454 * both be reset and all registers restored. This is what
3455 * happens during suspend and resume without WoL enabled.
3456 */
3457 ret = sh_eth_close(ndev);
3458 if (ret < 0)
3459 return ret;
3460 ret = sh_eth_open(ndev);
3461 if (ret < 0)
3462 return ret;
3463
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003464 return disable_irq_wake(ndev->irq);
3465}
3466
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003467static int sh_eth_suspend(struct device *dev)
3468{
3469 struct net_device *ndev = dev_get_drvdata(dev);
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003470 struct sh_eth_private *mdp = netdev_priv(ndev);
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003471 int ret = 0;
3472
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003473 if (!netif_running(ndev))
3474 return 0;
3475
3476 netif_device_detach(ndev);
3477
3478 if (mdp->wol_enabled)
3479 ret = sh_eth_wol_setup(ndev);
3480 else
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003481 ret = sh_eth_close(ndev);
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003482
3483 return ret;
3484}
3485
3486static int sh_eth_resume(struct device *dev)
3487{
3488 struct net_device *ndev = dev_get_drvdata(dev);
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003489 struct sh_eth_private *mdp = netdev_priv(ndev);
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003490 int ret = 0;
3491
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003492 if (!netif_running(ndev))
3493 return 0;
3494
3495 if (mdp->wol_enabled)
3496 ret = sh_eth_wol_restore(ndev);
3497 else
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003498 ret = sh_eth_open(ndev);
Niklas Söderlundd8981d02017-01-09 16:34:05 +01003499
3500 if (ret < 0)
3501 return ret;
3502
3503 netif_device_attach(ndev);
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003504
3505 return ret;
3506}
3507#endif
3508
Magnus Dammbcd51492009-10-09 00:20:04 +00003509static int sh_eth_runtime_nop(struct device *dev)
3510{
Sergei Shtylyov128296f2014-01-03 15:52:22 +03003511 /* Runtime PM callback shared between ->runtime_suspend()
Magnus Dammbcd51492009-10-09 00:20:04 +00003512 * and ->runtime_resume(). Simply returns success.
3513 *
3514 * This driver re-initializes all registers after
3515 * pm_runtime_get_sync() anyway so there is no need
3516 * to save and restore registers here.
3517 */
3518 return 0;
3519}
3520
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003521static const struct dev_pm_ops sh_eth_dev_pm_ops = {
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003522 SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend, sh_eth_resume)
Mikhail Ulyanove7d7e892015-01-22 01:18:44 +03003523 SET_RUNTIME_PM_OPS(sh_eth_runtime_nop, sh_eth_runtime_nop, NULL)
Magnus Dammbcd51492009-10-09 00:20:04 +00003524};
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003525#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3526#else
3527#define SH_ETH_PM_OPS NULL
3528#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00003529
Arvind Yadavef00df82017-08-13 16:42:42 +05303530static const struct platform_device_id sh_eth_id_table[] = {
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00003531 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00003532 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +00003533 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003534 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
Sergei Shtylyov24549e22013-06-07 13:59:21 +00003535 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
3536 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003537 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003538 { }
3539};
3540MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
3541
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003542static struct platform_driver sh_eth_driver = {
3543 .probe = sh_eth_drv_probe,
3544 .remove = sh_eth_drv_remove,
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003545 .id_table = sh_eth_id_table,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003546 .driver = {
3547 .name = CARDNAME,
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003548 .pm = SH_ETH_PM_OPS,
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003549 .of_match_table = of_match_ptr(sh_eth_match_table),
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003550 },
3551};
3552
Axel Lindb62f682011-11-27 16:44:17 +00003553module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003554
3555MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3556MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3557MODULE_LICENSE("GPL v2");