blob: a5e3f9fb94488764a64117e080e7a183d42c76bd [file] [log] [blame]
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001/* SuperH Ethernet device driver
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002 *
Hisashi Nakamura966d6db2014-11-13 15:54:05 +09003 * Copyright (C) 2014 Renesas Electronics Corporation
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +00004 * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
Sergei Shtylyovb356e972014-02-18 03:12:43 +03005 * Copyright (C) 2008-2014 Renesas Solutions Corp.
6 * Copyright (C) 2013-2014 Cogent Embedded, Inc.
Ben Dooks702eca02014-03-12 17:47:40 +00007 * Copyright (C) 2014 Codethink Limited
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070017 *
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
20 */
21
Yoshihiro Shimoda06540112011-09-29 17:16:57 +000022#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/spinlock.h>
David S. Miller823dcd22011-08-20 10:39:12 -070025#include <linux/interrupt.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070026#include <linux/dma-mapping.h>
27#include <linux/etherdevice.h>
28#include <linux/delay.h>
29#include <linux/platform_device.h>
30#include <linux/mdio-bitbang.h>
31#include <linux/netdevice.h>
Sergei Shtylyovb356e972014-02-18 03:12:43 +030032#include <linux/of.h>
33#include <linux/of_device.h>
34#include <linux/of_irq.h>
35#include <linux/of_net.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070036#include <linux/phy.h>
37#include <linux/cache.h>
38#include <linux/io.h>
Magnus Dammbcd51492009-10-09 00:20:04 +000039#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000041#include <linux/ethtool.h>
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +000042#include <linux/if_vlan.h>
Nobuhiro Iwamatsuf0e81fe2012-03-25 18:59:51 +000043#include <linux/clk.h>
Yoshihiro Shimodad4fa0e32011-09-27 21:49:12 +000044#include <linux/sh_eth.h>
Ben Dooks702eca02014-03-12 17:47:40 +000045#include <linux/of_mdio.h>
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -070046
47#include "sh_eth.h"
48
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +000049#define SH_ETH_DEF_MSG_ENABLE \
50 (NETIF_MSG_LINK | \
51 NETIF_MSG_TIMER | \
52 NETIF_MSG_RX_ERR| \
53 NETIF_MSG_TX_ERR)
54
Sergei Shtylyov2274d372015-12-13 01:44:50 +030055#define SH_ETH_OFFSET_INVALID ((u16)~0)
56
Ben Hutchings33657112015-02-26 20:34:14 +000057#define SH_ETH_OFFSET_DEFAULTS \
58 [0 ... SH_ETH_MAX_REGISTER_OFFSET - 1] = SH_ETH_OFFSET_INVALID
59
Sergei Shtylyovc0013f62013-03-28 11:48:26 +000060static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +000061 SH_ETH_OFFSET_DEFAULTS,
62
Sergei Shtylyovc0013f62013-03-28 11:48:26 +000063 [EDSR] = 0x0000,
64 [EDMR] = 0x0400,
65 [EDTRR] = 0x0408,
66 [EDRRR] = 0x0410,
67 [EESR] = 0x0428,
68 [EESIPR] = 0x0430,
69 [TDLAR] = 0x0010,
70 [TDFAR] = 0x0014,
71 [TDFXR] = 0x0018,
72 [TDFFR] = 0x001c,
73 [RDLAR] = 0x0030,
74 [RDFAR] = 0x0034,
75 [RDFXR] = 0x0038,
76 [RDFFR] = 0x003c,
77 [TRSCER] = 0x0438,
78 [RMFCR] = 0x0440,
79 [TFTR] = 0x0448,
80 [FDR] = 0x0450,
81 [RMCR] = 0x0458,
82 [RPADIR] = 0x0460,
83 [FCFTR] = 0x0468,
84 [CSMR] = 0x04E4,
85
86 [ECMR] = 0x0500,
87 [ECSR] = 0x0510,
88 [ECSIPR] = 0x0518,
89 [PIR] = 0x0520,
90 [PSR] = 0x0528,
91 [PIPR] = 0x052c,
92 [RFLR] = 0x0508,
93 [APR] = 0x0554,
94 [MPR] = 0x0558,
95 [PFTCR] = 0x055c,
96 [PFRCR] = 0x0560,
97 [TPAUSER] = 0x0564,
98 [GECMR] = 0x05b0,
99 [BCULR] = 0x05b4,
100 [MAHR] = 0x05c0,
101 [MALR] = 0x05c8,
102 [TROCR] = 0x0700,
103 [CDCR] = 0x0708,
104 [LCCR] = 0x0710,
105 [CEFCR] = 0x0740,
106 [FRECR] = 0x0748,
107 [TSFRCR] = 0x0750,
108 [TLFRCR] = 0x0758,
109 [RFCR] = 0x0760,
110 [CERCR] = 0x0768,
111 [CEECR] = 0x0770,
112 [MAFCR] = 0x0778,
113 [RMII_MII] = 0x0790,
114
115 [ARSTR] = 0x0000,
116 [TSU_CTRST] = 0x0004,
117 [TSU_FWEN0] = 0x0010,
118 [TSU_FWEN1] = 0x0014,
119 [TSU_FCM] = 0x0018,
120 [TSU_BSYSL0] = 0x0020,
121 [TSU_BSYSL1] = 0x0024,
122 [TSU_PRISL0] = 0x0028,
123 [TSU_PRISL1] = 0x002c,
124 [TSU_FWSL0] = 0x0030,
125 [TSU_FWSL1] = 0x0034,
126 [TSU_FWSLC] = 0x0038,
127 [TSU_QTAG0] = 0x0040,
128 [TSU_QTAG1] = 0x0044,
129 [TSU_FWSR] = 0x0050,
130 [TSU_FWINMK] = 0x0054,
131 [TSU_ADQT0] = 0x0048,
132 [TSU_ADQT1] = 0x004c,
133 [TSU_VTAG0] = 0x0058,
134 [TSU_VTAG1] = 0x005c,
135 [TSU_ADSBSY] = 0x0060,
136 [TSU_TEN] = 0x0064,
137 [TSU_POST1] = 0x0070,
138 [TSU_POST2] = 0x0074,
139 [TSU_POST3] = 0x0078,
140 [TSU_POST4] = 0x007c,
141 [TSU_ADRH0] = 0x0100,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000142
143 [TXNLCR0] = 0x0080,
144 [TXALCR0] = 0x0084,
145 [RXNLCR0] = 0x0088,
146 [RXALCR0] = 0x008c,
147 [FWNLCR0] = 0x0090,
148 [FWALCR0] = 0x0094,
149 [TXNLCR1] = 0x00a0,
150 [TXALCR1] = 0x00a0,
151 [RXNLCR1] = 0x00a8,
152 [RXALCR1] = 0x00ac,
153 [FWNLCR1] = 0x00b0,
154 [FWALCR1] = 0x00b4,
155};
156
Simon Hormandb893472014-01-17 09:22:28 +0900157static const u16 sh_eth_offset_fast_rz[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000158 SH_ETH_OFFSET_DEFAULTS,
159
Simon Hormandb893472014-01-17 09:22:28 +0900160 [EDSR] = 0x0000,
161 [EDMR] = 0x0400,
162 [EDTRR] = 0x0408,
163 [EDRRR] = 0x0410,
164 [EESR] = 0x0428,
165 [EESIPR] = 0x0430,
166 [TDLAR] = 0x0010,
167 [TDFAR] = 0x0014,
168 [TDFXR] = 0x0018,
169 [TDFFR] = 0x001c,
170 [RDLAR] = 0x0030,
171 [RDFAR] = 0x0034,
172 [RDFXR] = 0x0038,
173 [RDFFR] = 0x003c,
174 [TRSCER] = 0x0438,
175 [RMFCR] = 0x0440,
176 [TFTR] = 0x0448,
177 [FDR] = 0x0450,
178 [RMCR] = 0x0458,
179 [RPADIR] = 0x0460,
180 [FCFTR] = 0x0468,
181 [CSMR] = 0x04E4,
182
183 [ECMR] = 0x0500,
184 [RFLR] = 0x0508,
185 [ECSR] = 0x0510,
186 [ECSIPR] = 0x0518,
187 [PIR] = 0x0520,
188 [APR] = 0x0554,
189 [MPR] = 0x0558,
190 [PFTCR] = 0x055c,
191 [PFRCR] = 0x0560,
192 [TPAUSER] = 0x0564,
193 [MAHR] = 0x05c0,
194 [MALR] = 0x05c8,
195 [CEFCR] = 0x0740,
196 [FRECR] = 0x0748,
197 [TSFRCR] = 0x0750,
198 [TLFRCR] = 0x0758,
199 [RFCR] = 0x0760,
200 [MAFCR] = 0x0778,
201
202 [ARSTR] = 0x0000,
203 [TSU_CTRST] = 0x0004,
204 [TSU_VTAG0] = 0x0058,
205 [TSU_ADSBSY] = 0x0060,
206 [TSU_TEN] = 0x0064,
207 [TSU_ADRH0] = 0x0100,
Simon Hormandb893472014-01-17 09:22:28 +0900208
209 [TXNLCR0] = 0x0080,
210 [TXALCR0] = 0x0084,
211 [RXNLCR0] = 0x0088,
212 [RXALCR0] = 0x008C,
213};
214
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000215static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000216 SH_ETH_OFFSET_DEFAULTS,
217
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000218 [ECMR] = 0x0300,
219 [RFLR] = 0x0308,
220 [ECSR] = 0x0310,
221 [ECSIPR] = 0x0318,
222 [PIR] = 0x0320,
223 [PSR] = 0x0328,
224 [RDMLR] = 0x0340,
225 [IPGR] = 0x0350,
226 [APR] = 0x0354,
227 [MPR] = 0x0358,
228 [RFCF] = 0x0360,
229 [TPAUSER] = 0x0364,
230 [TPAUSECR] = 0x0368,
231 [MAHR] = 0x03c0,
232 [MALR] = 0x03c8,
233 [TROCR] = 0x03d0,
234 [CDCR] = 0x03d4,
235 [LCCR] = 0x03d8,
236 [CNDCR] = 0x03dc,
237 [CEFCR] = 0x03e4,
238 [FRECR] = 0x03e8,
239 [TSFRCR] = 0x03ec,
240 [TLFRCR] = 0x03f0,
241 [RFCR] = 0x03f4,
242 [MAFCR] = 0x03f8,
243
244 [EDMR] = 0x0200,
245 [EDTRR] = 0x0208,
246 [EDRRR] = 0x0210,
247 [TDLAR] = 0x0218,
248 [RDLAR] = 0x0220,
249 [EESR] = 0x0228,
250 [EESIPR] = 0x0230,
251 [TRSCER] = 0x0238,
252 [RMFCR] = 0x0240,
253 [TFTR] = 0x0248,
254 [FDR] = 0x0250,
255 [RMCR] = 0x0258,
256 [TFUCR] = 0x0264,
257 [RFOCR] = 0x0268,
Simon Horman55754f12013-07-23 10:18:04 +0900258 [RMIIMODE] = 0x026c,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000259 [FCFTR] = 0x0270,
260 [TRIMD] = 0x027c,
261};
262
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000263static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000264 SH_ETH_OFFSET_DEFAULTS,
265
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000266 [ECMR] = 0x0100,
267 [RFLR] = 0x0108,
268 [ECSR] = 0x0110,
269 [ECSIPR] = 0x0118,
270 [PIR] = 0x0120,
271 [PSR] = 0x0128,
272 [RDMLR] = 0x0140,
273 [IPGR] = 0x0150,
274 [APR] = 0x0154,
275 [MPR] = 0x0158,
276 [TPAUSER] = 0x0164,
277 [RFCF] = 0x0160,
278 [TPAUSECR] = 0x0168,
279 [BCFRR] = 0x016c,
280 [MAHR] = 0x01c0,
281 [MALR] = 0x01c8,
282 [TROCR] = 0x01d0,
283 [CDCR] = 0x01d4,
284 [LCCR] = 0x01d8,
285 [CNDCR] = 0x01dc,
286 [CEFCR] = 0x01e4,
287 [FRECR] = 0x01e8,
288 [TSFRCR] = 0x01ec,
289 [TLFRCR] = 0x01f0,
290 [RFCR] = 0x01f4,
291 [MAFCR] = 0x01f8,
292 [RTRATE] = 0x01fc,
293
294 [EDMR] = 0x0000,
295 [EDTRR] = 0x0008,
296 [EDRRR] = 0x0010,
297 [TDLAR] = 0x0018,
298 [RDLAR] = 0x0020,
299 [EESR] = 0x0028,
300 [EESIPR] = 0x0030,
301 [TRSCER] = 0x0038,
302 [RMFCR] = 0x0040,
303 [TFTR] = 0x0048,
304 [FDR] = 0x0050,
305 [RMCR] = 0x0058,
306 [TFUCR] = 0x0064,
307 [RFOCR] = 0x0068,
308 [FCFTR] = 0x0070,
309 [RPADIR] = 0x0078,
310 [TRIMD] = 0x007c,
311 [RBWAR] = 0x00c8,
312 [RDFAR] = 0x00cc,
313 [TBRAR] = 0x00d4,
314 [TDFAR] = 0x00d8,
315};
316
317static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
Ben Hutchings33657112015-02-26 20:34:14 +0000318 SH_ETH_OFFSET_DEFAULTS,
319
Sergei Shtylyovd8b04262014-06-03 23:42:26 +0400320 [EDMR] = 0x0000,
321 [EDTRR] = 0x0004,
322 [EDRRR] = 0x0008,
323 [TDLAR] = 0x000c,
324 [RDLAR] = 0x0010,
325 [EESR] = 0x0014,
326 [EESIPR] = 0x0018,
327 [TRSCER] = 0x001c,
328 [RMFCR] = 0x0020,
329 [TFTR] = 0x0024,
330 [FDR] = 0x0028,
331 [RMCR] = 0x002c,
332 [EDOCR] = 0x0030,
333 [FCFTR] = 0x0034,
334 [RPADIR] = 0x0038,
335 [TRIMD] = 0x003c,
336 [RBWAR] = 0x0040,
337 [RDFAR] = 0x0044,
338 [TBRAR] = 0x004c,
339 [TDFAR] = 0x0050,
340
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000341 [ECMR] = 0x0160,
342 [ECSR] = 0x0164,
343 [ECSIPR] = 0x0168,
344 [PIR] = 0x016c,
345 [MAHR] = 0x0170,
346 [MALR] = 0x0174,
347 [RFLR] = 0x0178,
348 [PSR] = 0x017c,
349 [TROCR] = 0x0180,
350 [CDCR] = 0x0184,
351 [LCCR] = 0x0188,
352 [CNDCR] = 0x018c,
353 [CEFCR] = 0x0194,
354 [FRECR] = 0x0198,
355 [TSFRCR] = 0x019c,
356 [TLFRCR] = 0x01a0,
357 [RFCR] = 0x01a4,
358 [MAFCR] = 0x01a8,
359 [IPGR] = 0x01b4,
360 [APR] = 0x01b8,
361 [MPR] = 0x01bc,
362 [TPAUSER] = 0x01c4,
363 [BCFR] = 0x01cc,
364
365 [ARSTR] = 0x0000,
366 [TSU_CTRST] = 0x0004,
367 [TSU_FWEN0] = 0x0010,
368 [TSU_FWEN1] = 0x0014,
369 [TSU_FCM] = 0x0018,
370 [TSU_BSYSL0] = 0x0020,
371 [TSU_BSYSL1] = 0x0024,
372 [TSU_PRISL0] = 0x0028,
373 [TSU_PRISL1] = 0x002c,
374 [TSU_FWSL0] = 0x0030,
375 [TSU_FWSL1] = 0x0034,
376 [TSU_FWSLC] = 0x0038,
377 [TSU_QTAGM0] = 0x0040,
378 [TSU_QTAGM1] = 0x0044,
379 [TSU_ADQT0] = 0x0048,
380 [TSU_ADQT1] = 0x004c,
381 [TSU_FWSR] = 0x0050,
382 [TSU_FWINMK] = 0x0054,
383 [TSU_ADSBSY] = 0x0060,
384 [TSU_TEN] = 0x0064,
385 [TSU_POST1] = 0x0070,
386 [TSU_POST2] = 0x0074,
387 [TSU_POST3] = 0x0078,
388 [TSU_POST4] = 0x007c,
389
390 [TXNLCR0] = 0x0080,
391 [TXALCR0] = 0x0084,
392 [RXNLCR0] = 0x0088,
393 [RXALCR0] = 0x008c,
394 [FWNLCR0] = 0x0090,
395 [FWALCR0] = 0x0094,
396 [TXNLCR1] = 0x00a0,
397 [TXALCR1] = 0x00a0,
398 [RXNLCR1] = 0x00a8,
399 [RXALCR1] = 0x00ac,
400 [FWNLCR1] = 0x00b0,
401 [FWALCR1] = 0x00b4,
402
403 [TSU_ADRH0] = 0x0100,
Sergei Shtylyovc0013f62013-03-28 11:48:26 +0000404};
405
Ben Hutchings740c7f32015-01-27 00:49:32 +0000406static void sh_eth_rcv_snd_disable(struct net_device *ndev);
407static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev);
408
Sergei Shtylyov2274d372015-12-13 01:44:50 +0300409static void sh_eth_write(struct net_device *ndev, u32 data, int enum_index)
410{
411 struct sh_eth_private *mdp = netdev_priv(ndev);
412 u16 offset = mdp->reg_offset[enum_index];
413
414 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
415 return;
416
417 iowrite32(data, mdp->addr + offset);
418}
419
420static u32 sh_eth_read(struct net_device *ndev, int enum_index)
421{
422 struct sh_eth_private *mdp = netdev_priv(ndev);
423 u16 offset = mdp->reg_offset[enum_index];
424
425 if (WARN_ON(offset == SH_ETH_OFFSET_INVALID))
426 return ~0U;
427
428 return ioread32(mdp->addr + offset);
429}
430
Simon Horman504c8ca2014-01-17 09:22:27 +0900431static bool sh_eth_is_gether(struct sh_eth_private *mdp)
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000432{
Simon Horman504c8ca2014-01-17 09:22:27 +0900433 return mdp->reg_offset == sh_eth_offset_gigabit;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000434}
435
Simon Hormandb893472014-01-17 09:22:28 +0900436static bool sh_eth_is_rz_fast_ether(struct sh_eth_private *mdp)
437{
438 return mdp->reg_offset == sh_eth_offset_fast_rz;
439}
440
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400441static void sh_eth_select_mii(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000442{
443 u32 value = 0x0;
444 struct sh_eth_private *mdp = netdev_priv(ndev);
445
446 switch (mdp->phy_interface) {
447 case PHY_INTERFACE_MODE_GMII:
448 value = 0x2;
449 break;
450 case PHY_INTERFACE_MODE_MII:
451 value = 0x1;
452 break;
453 case PHY_INTERFACE_MODE_RMII:
454 value = 0x0;
455 break;
456 default:
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300457 netdev_warn(ndev,
458 "PHY interface mode was not setup. Set to MII.\n");
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000459 value = 0x1;
460 break;
461 }
462
463 sh_eth_write(ndev, value, RMII_MII);
464}
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000465
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400466static void sh_eth_set_duplex(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000467{
468 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000469
470 if (mdp->duplex) /* Full */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000471 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000472 else /* Half */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000473 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000474}
475
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000476/* There is CPU dependent code */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000477static void sh_eth_set_rate_r8a777x(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000478{
479 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000480
481 switch (mdp->speed) {
482 case 10: /* 10BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000483 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000484 break;
485 case 100:/* 100BASE */
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000486 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
487 break;
488 default:
489 break;
490 }
491}
492
Sergei Shtylyov674853b2013-04-27 10:44:24 +0000493/* R8A7778/9 */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000494static struct sh_eth_cpu_data r8a777x_data = {
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000495 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000496 .set_rate = sh_eth_set_rate_r8a777x,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000497
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400498 .register_type = SH_ETH_REG_FAST_RCAR,
499
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000500 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
501 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
502 .eesipr_value = 0x01ff009f,
503
504 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400505 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
506 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
507 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900508 .fdr_value = 0x00000f0f,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000509
510 .apr = 1,
511 .mpr = 1,
512 .tpauser = 1,
513 .hw_swap = 1,
514};
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000515
Sergei Shtylyov94a12b12013-12-08 02:59:18 +0300516/* R8A7790/1 */
517static struct sh_eth_cpu_data r8a779x_data = {
Simon Hormane18dbf72013-07-23 10:18:05 +0900518 .set_duplex = sh_eth_set_duplex,
519 .set_rate = sh_eth_set_rate_r8a777x,
520
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400521 .register_type = SH_ETH_REG_FAST_RCAR,
522
Simon Hormane18dbf72013-07-23 10:18:05 +0900523 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
524 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
525 .eesipr_value = 0x01ff009f,
526
527 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Laurent Pinchartba361cb2013-07-31 16:42:11 +0900528 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
529 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
530 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900531 .fdr_value = 0x00000f0f,
Simon Hormane18dbf72013-07-23 10:18:05 +0900532
Geert Uytterhoeven01fbd3f2015-01-15 11:52:19 +0100533 .trscer_err_mask = DESC_I_RINT8,
534
Simon Hormane18dbf72013-07-23 10:18:05 +0900535 .apr = 1,
536 .mpr = 1,
537 .tpauser = 1,
538 .hw_swap = 1,
539 .rmiimode = 1,
540};
541
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000542static void sh_eth_set_rate_sh7724(struct net_device *ndev)
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000543{
544 struct sh_eth_private *mdp = netdev_priv(ndev);
545
546 switch (mdp->speed) {
547 case 10: /* 10BASE */
548 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
549 break;
550 case 100:/* 100BASE */
551 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000552 break;
553 default:
554 break;
555 }
556}
557
558/* SH7724 */
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000559static struct sh_eth_cpu_data sh7724_data = {
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000560 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000561 .set_rate = sh_eth_set_rate_sh7724,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000562
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400563 .register_type = SH_ETH_REG_FAST_SH4,
564
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000565 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
566 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
Sergei Shtylyova80c3de2013-06-20 02:24:54 +0400567 .eesipr_value = 0x01ff009f,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000568
569 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400570 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
571 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
572 EESR_ECI,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000573
574 .apr = 1,
575 .mpr = 1,
576 .tpauser = 1,
577 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800578 .rpadir = 1,
579 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000580};
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000581
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000582static void sh_eth_set_rate_sh7757(struct net_device *ndev)
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000583{
584 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000585
586 switch (mdp->speed) {
587 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000588 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000589 break;
590 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000591 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000592 break;
593 default:
594 break;
595 }
596}
597
598/* SH7757 */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000599static struct sh_eth_cpu_data sh7757_data = {
600 .set_duplex = sh_eth_set_duplex,
601 .set_rate = sh_eth_set_rate_sh7757,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000602
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400603 .register_type = SH_ETH_REG_FAST_SH4,
604
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000605 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000606
607 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400608 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
609 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
610 EESR_ECI,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000611
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000612 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000613 .apr = 1,
614 .mpr = 1,
615 .tpauser = 1,
616 .hw_swap = 1,
617 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000618 .rpadir = 1,
619 .rpadir_value = 2 << 16,
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +0000620 .rtrate = 1,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000621};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000622
David S. Millere403d292013-06-07 23:40:41 -0700623#define SH_GIGA_ETH_BASE 0xfee00000UL
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000624#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
625#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
626static void sh_eth_chip_reset_giga(struct net_device *ndev)
627{
628 int i;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +0100629 u32 mahr[2], malr[2];
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000630
631 /* save MAHR and MALR */
632 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000633 malr[i] = ioread32((void *)GIGA_MALR(i));
634 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000635 }
636
637 /* reset device */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000638 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000639 mdelay(1);
640
641 /* restore MAHR and MALR */
642 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000643 iowrite32(malr[i], (void *)GIGA_MALR(i));
644 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000645 }
646}
647
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000648static void sh_eth_set_rate_giga(struct net_device *ndev)
649{
650 struct sh_eth_private *mdp = netdev_priv(ndev);
651
652 switch (mdp->speed) {
653 case 10: /* 10BASE */
654 sh_eth_write(ndev, 0x00000000, GECMR);
655 break;
656 case 100:/* 100BASE */
657 sh_eth_write(ndev, 0x00000010, GECMR);
658 break;
659 case 1000: /* 1000BASE */
660 sh_eth_write(ndev, 0x00000020, GECMR);
661 break;
662 default:
663 break;
664 }
665}
666
667/* SH7757(GETHERC) */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000668static struct sh_eth_cpu_data sh7757_data_giga = {
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000669 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000670 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000671 .set_rate = sh_eth_set_rate_giga,
672
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400673 .register_type = SH_ETH_REG_GIGABIT,
674
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000675 .ecsr_value = ECSR_ICD | ECSR_MPD,
676 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
677 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
678
679 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400680 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
681 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
682 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000683 .fdr_value = 0x0000072f,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000684
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000685 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000686 .apr = 1,
687 .mpr = 1,
688 .tpauser = 1,
689 .bculr = 1,
690 .hw_swap = 1,
691 .rpadir = 1,
692 .rpadir_value = 2 << 16,
693 .no_trimd = 1,
694 .no_ade = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000695 .tsu = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000696};
697
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000698static void sh_eth_chip_reset(struct net_device *ndev)
699{
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000700 struct sh_eth_private *mdp = netdev_priv(ndev);
701
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000702 /* reset device */
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000703 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000704 mdelay(1);
705}
706
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000707static void sh_eth_set_rate_gether(struct net_device *ndev)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000708{
709 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000710
711 switch (mdp->speed) {
712 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000713 sh_eth_write(ndev, GECMR_10, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000714 break;
715 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000716 sh_eth_write(ndev, GECMR_100, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000717 break;
718 case 1000: /* 1000BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000719 sh_eth_write(ndev, GECMR_1000, GECMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000720 break;
721 default:
722 break;
723 }
724}
725
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000726/* SH7734 */
727static struct sh_eth_cpu_data sh7734_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000728 .chip_reset = sh_eth_chip_reset,
729 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000730 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000731
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400732 .register_type = SH_ETH_REG_GIGABIT,
733
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000734 .ecsr_value = ECSR_ICD | ECSR_MPD,
735 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
736 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
737
738 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400739 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
740 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
741 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000742
743 .apr = 1,
744 .mpr = 1,
745 .tpauser = 1,
746 .bculr = 1,
747 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000748 .no_trimd = 1,
749 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000750 .tsu = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000751 .hw_crc = 1,
752 .select_mii = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000753};
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000754
755/* SH7763 */
756static struct sh_eth_cpu_data sh7763_data = {
757 .chip_reset = sh_eth_chip_reset,
758 .set_duplex = sh_eth_set_duplex,
759 .set_rate = sh_eth_set_rate_gether,
760
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400761 .register_type = SH_ETH_REG_GIGABIT,
762
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000763 .ecsr_value = ECSR_ICD | ECSR_MPD,
764 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
765 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
766
767 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300768 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
769 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000770 EESR_ECI,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000771
772 .apr = 1,
773 .mpr = 1,
774 .tpauser = 1,
775 .bculr = 1,
776 .hw_swap = 1,
777 .no_trimd = 1,
778 .no_ade = 1,
779 .tsu = 1,
780 .irq_flags = IRQF_SHARED,
781};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000782
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000783static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000784{
785 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000786
787 /* reset device */
788 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
789 mdelay(1);
790
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000791 sh_eth_select_mii(ndev);
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000792}
793
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000794/* R8A7740 */
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000795static struct sh_eth_cpu_data r8a7740_data = {
796 .chip_reset = sh_eth_chip_reset_r8a7740,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000797 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +0000798 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000799
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400800 .register_type = SH_ETH_REG_GIGABIT,
801
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000802 .ecsr_value = ECSR_ICD | ECSR_MPD,
803 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
804 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
805
806 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400807 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
808 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
809 EESR_TDE | EESR_ECI,
Simon Hormancc235282013-10-10 14:51:16 +0900810 .fdr_value = 0x0000070f,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000811
812 .apr = 1,
813 .mpr = 1,
814 .tpauser = 1,
815 .bculr = 1,
816 .hw_swap = 1,
Simon Hormancc235282013-10-10 14:51:16 +0900817 .rpadir = 1,
818 .rpadir_value = 2 << 16,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000819 .no_trimd = 1,
820 .no_ade = 1,
821 .tsu = 1,
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000822 .select_mii = 1,
Sergei Shtylyovac8025a2013-06-13 22:12:45 +0400823 .shift_rd0 = 1,
Yoshihiro Shimoda73a0d902012-04-04 18:37:10 +0000824};
825
Simon Hormandb893472014-01-17 09:22:28 +0900826/* R7S72100 */
827static struct sh_eth_cpu_data r7s72100_data = {
828 .chip_reset = sh_eth_chip_reset,
829 .set_duplex = sh_eth_set_duplex,
830
831 .register_type = SH_ETH_REG_FAST_RZ,
832
833 .ecsr_value = ECSR_ICD,
834 .ecsipr_value = ECSIPR_ICDIP,
835 .eesipr_value = 0xff7f009f,
836
837 .tx_check = EESR_TC1 | EESR_FTC,
838 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
839 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
840 EESR_TDE | EESR_ECI,
841 .fdr_value = 0x0000070f,
Simon Hormandb893472014-01-17 09:22:28 +0900842
843 .no_psr = 1,
844 .apr = 1,
845 .mpr = 1,
846 .tpauser = 1,
847 .hw_swap = 1,
848 .rpadir = 1,
849 .rpadir_value = 2 << 16,
850 .no_trimd = 1,
851 .no_ade = 1,
852 .hw_crc = 1,
853 .tsu = 1,
854 .shift_rd0 = 1,
855};
856
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +0000857static struct sh_eth_cpu_data sh7619_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400858 .register_type = SH_ETH_REG_FAST_SH3_SH2,
859
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000860 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
861
862 .apr = 1,
863 .mpr = 1,
864 .tpauser = 1,
865 .hw_swap = 1,
866};
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +0000867
868static struct sh_eth_cpu_data sh771x_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400869 .register_type = SH_ETH_REG_FAST_SH3_SH2,
870
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000871 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000872 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000873};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000874
875static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
876{
877 if (!cd->ecsr_value)
878 cd->ecsr_value = DEFAULT_ECSR_INIT;
879
880 if (!cd->ecsipr_value)
881 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
882
883 if (!cd->fcftr_value)
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300884 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000885 DEFAULT_FIFO_F_D_RFD;
886
887 if (!cd->fdr_value)
888 cd->fdr_value = DEFAULT_FDR_INIT;
889
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000890 if (!cd->tx_check)
891 cd->tx_check = DEFAULT_TX_CHECK;
892
893 if (!cd->eesr_err_check)
894 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +0900895
896 if (!cd->trscer_err_mask)
897 cd->trscer_err_mask = DEFAULT_TRSCER_ERR_MASK;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000898}
899
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000900static int sh_eth_check_reset(struct net_device *ndev)
901{
902 int ret = 0;
903 int cnt = 100;
904
905 while (cnt > 0) {
906 if (!(sh_eth_read(ndev, EDMR) & 0x3))
907 break;
908 mdelay(1);
909 cnt--;
910 }
Sergei Shtylyov9f8c4262013-06-05 23:54:01 +0400911 if (cnt <= 0) {
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300912 netdev_err(ndev, "Device reset failed\n");
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000913 ret = -ETIMEDOUT;
914 }
915 return ret;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000916}
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000917
918static int sh_eth_reset(struct net_device *ndev)
919{
920 struct sh_eth_private *mdp = netdev_priv(ndev);
921 int ret = 0;
922
Simon Hormandb893472014-01-17 09:22:28 +0900923 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp)) {
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000924 sh_eth_write(ndev, EDSR_ENALL, EDSR);
925 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
926 EDMR);
927
928 ret = sh_eth_check_reset(ndev);
929 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +0100930 return ret;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000931
932 /* Table Init */
933 sh_eth_write(ndev, 0x0, TDLAR);
934 sh_eth_write(ndev, 0x0, TDFAR);
935 sh_eth_write(ndev, 0x0, TDFXR);
936 sh_eth_write(ndev, 0x0, TDFFR);
937 sh_eth_write(ndev, 0x0, RDLAR);
938 sh_eth_write(ndev, 0x0, RDFAR);
939 sh_eth_write(ndev, 0x0, RDFXR);
940 sh_eth_write(ndev, 0x0, RDFFR);
941
942 /* Reset HW CRC register */
943 if (mdp->cd->hw_crc)
944 sh_eth_write(ndev, 0x0, CSMR);
945
946 /* Select MII mode */
947 if (mdp->cd->select_mii)
948 sh_eth_select_mii(ndev);
949 } else {
950 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
951 EDMR);
952 mdelay(3);
953 sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
954 EDMR);
955 }
956
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000957 return ret;
958}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000959
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000960static void sh_eth_set_receive_align(struct sk_buff *skb)
961{
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900962 uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000963
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000964 if (reserve)
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900965 skb_reserve(skb, SH_ETH_RX_ALIGN - reserve);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000966}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000967
968
Yoshinori Sato71557a32008-08-06 19:49:00 -0400969/* CPU <-> EDMAC endian convert */
970static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
971{
972 switch (mdp->edmac_endian) {
973 case EDMAC_LITTLE_ENDIAN:
974 return cpu_to_le32(x);
975 case EDMAC_BIG_ENDIAN:
976 return cpu_to_be32(x);
977 }
978 return x;
979}
980
981static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
982{
983 switch (mdp->edmac_endian) {
984 case EDMAC_LITTLE_ENDIAN:
985 return le32_to_cpu(x);
986 case EDMAC_BIG_ENDIAN:
987 return be32_to_cpu(x);
988 }
989 return x;
990}
991
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300992/* Program the hardware MAC address from dev->dev_addr. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700993static void update_mac_address(struct net_device *ndev)
994{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000995 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300996 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
997 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000998 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300999 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001000}
1001
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001002/* Get MAC address from SuperH MAC address register
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001003 *
1004 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
1005 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
1006 * When you want use this device, you must set MAC address in bootloader.
1007 *
1008 */
Magnus Damm748031f2009-10-09 00:17:14 +00001009static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001010{
Magnus Damm748031f2009-10-09 00:17:14 +00001011 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
Joe Perchesd458cdf2013-10-01 19:04:40 -07001012 memcpy(ndev->dev_addr, mac, ETH_ALEN);
Magnus Damm748031f2009-10-09 00:17:14 +00001013 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001014 ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
1015 ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
1016 ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
1017 ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
1018 ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
1019 ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
Magnus Damm748031f2009-10-09 00:17:14 +00001020 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001021}
1022
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001023static u32 sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001024{
Simon Hormandb893472014-01-17 09:22:28 +09001025 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001026 return EDTRR_TRNS_GETHER;
1027 else
1028 return EDTRR_TRNS_ETHER;
1029}
1030
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001031struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001032 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001033 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001034 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001035 u32 mmd_msk;/* MMD */
1036 u32 mdo_msk;
1037 u32 mdi_msk;
1038 u32 mdc_msk;
1039};
1040
1041/* PHY bit set */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001042static void bb_set(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001043{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001044 iowrite32(ioread32(addr) | msk, addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001045}
1046
1047/* PHY bit clear */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001048static void bb_clr(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001049{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001050 iowrite32((ioread32(addr) & ~msk), addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001051}
1052
1053/* PHY bit read */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001054static int bb_read(void *addr, u32 msk)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001055{
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001056 return (ioread32(addr) & msk) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001057}
1058
1059/* Data I/O pin control */
1060static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1061{
1062 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001063
1064 if (bitbang->set_gate)
1065 bitbang->set_gate(bitbang->addr);
1066
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001067 if (bit)
1068 bb_set(bitbang->addr, bitbang->mmd_msk);
1069 else
1070 bb_clr(bitbang->addr, bitbang->mmd_msk);
1071}
1072
1073/* Set bit data*/
1074static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
1075{
1076 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
1077
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001078 if (bitbang->set_gate)
1079 bitbang->set_gate(bitbang->addr);
1080
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001081 if (bit)
1082 bb_set(bitbang->addr, bitbang->mdo_msk);
1083 else
1084 bb_clr(bitbang->addr, bitbang->mdo_msk);
1085}
1086
1087/* Get bit data*/
1088static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
1089{
1090 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001091
1092 if (bitbang->set_gate)
1093 bitbang->set_gate(bitbang->addr);
1094
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001095 return bb_read(bitbang->addr, bitbang->mdi_msk);
1096}
1097
1098/* MDC pin control */
1099static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1100{
1101 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
1102
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001103 if (bitbang->set_gate)
1104 bitbang->set_gate(bitbang->addr);
1105
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001106 if (bit)
1107 bb_set(bitbang->addr, bitbang->mdc_msk);
1108 else
1109 bb_clr(bitbang->addr, bitbang->mdc_msk);
1110}
1111
1112/* mdio bus control struct */
1113static struct mdiobb_ops bb_ops = {
1114 .owner = THIS_MODULE,
1115 .set_mdc = sh_mdc_ctrl,
1116 .set_mdio_dir = sh_mmd_ctrl,
1117 .set_mdio_data = sh_set_mdio,
1118 .get_mdio_data = sh_get_mdio,
1119};
1120
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001121/* free skb and descriptor buffer */
1122static void sh_eth_ring_free(struct net_device *ndev)
1123{
1124 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001125 int ringsize, i;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001126
1127 /* Free Rx skb ringbuffer */
1128 if (mdp->rx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001129 for (i = 0; i < mdp->num_rx_ring; i++)
1130 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001131 }
1132 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001133 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001134
1135 /* Free Tx skb ringbuffer */
1136 if (mdp->tx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001137 for (i = 0; i < mdp->num_tx_ring; i++)
1138 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001139 }
1140 kfree(mdp->tx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001141 mdp->tx_skbuff = NULL;
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001142
1143 if (mdp->rx_ring) {
1144 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
1145 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1146 mdp->rx_desc_dma);
1147 mdp->rx_ring = NULL;
1148 }
1149
1150 if (mdp->tx_ring) {
1151 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
1152 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1153 mdp->tx_desc_dma);
1154 mdp->tx_ring = NULL;
1155 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001156}
1157
1158/* format skb and descriptor buffer */
1159static void sh_eth_ring_format(struct net_device *ndev)
1160{
1161 struct sh_eth_private *mdp = netdev_priv(ndev);
1162 int i;
1163 struct sk_buff *skb;
1164 struct sh_eth_rxdesc *rxdesc = NULL;
1165 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001166 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
1167 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Sergei Shtylyovcb368592015-10-24 00:46:40 +03001168 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001169 dma_addr_t dma_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001170
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001171 mdp->cur_rx = 0;
1172 mdp->cur_tx = 0;
1173 mdp->dirty_rx = 0;
1174 mdp->dirty_tx = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001175
1176 memset(mdp->rx_ring, 0, rx_ringsize);
1177
1178 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001179 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001180 /* skb */
1181 mdp->rx_skbuff[i] = NULL;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001182 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001183 if (skb == NULL)
1184 break;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001185 sh_eth_set_receive_align(skb);
1186
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001187 /* RX descriptor */
1188 rxdesc = &mdp->rx_ring[i];
Sergei Shtylyovab857912015-10-24 00:46:03 +03001189 /* The size of the buffer is a multiple of 32 bytes. */
1190 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 32);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001191 dma_addr = dma_map_single(&ndev->dev, skb->data,
1192 rxdesc->buffer_length,
1193 DMA_FROM_DEVICE);
1194 if (dma_mapping_error(&ndev->dev, dma_addr)) {
1195 kfree_skb(skb);
1196 break;
1197 }
1198 mdp->rx_skbuff[i] = skb;
1199 rxdesc->addr = dma_addr;
Yoshinori Sato71557a32008-08-06 19:49:00 -04001200 rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001201
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001202 /* Rx descriptor address set */
1203 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001204 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001205 if (sh_eth_is_gether(mdp) ||
1206 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001207 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001208 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001209 }
1210
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001211 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001212
1213 /* Mark the last entry as wrapping the ring. */
Sergei Shtylyovc2380412015-11-03 01:28:07 +03001214 rxdesc->status |= cpu_to_edmac(mdp, RD_RDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001215
1216 memset(mdp->tx_ring, 0, tx_ringsize);
1217
1218 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001219 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001220 mdp->tx_skbuff[i] = NULL;
1221 txdesc = &mdp->tx_ring[i];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001222 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001223 txdesc->buffer_length = 0;
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001224 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001225 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001226 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001227 if (sh_eth_is_gether(mdp) ||
1228 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001229 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001230 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001231 }
1232
Yoshinori Sato71557a32008-08-06 19:49:00 -04001233 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001234}
1235
1236/* Get skb and descriptor buffer */
1237static int sh_eth_ring_init(struct net_device *ndev)
1238{
1239 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001240 int rx_ringsize, tx_ringsize;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001241
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001242 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001243 * card needs room to do 8 byte alignment, +2 so we can reserve
1244 * the first 2 bytes, and +16 gets room for the status word from the
1245 * card.
1246 */
1247 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1248 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001249 if (mdp->cd->rpadir)
1250 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001251
1252 /* Allocate RX and TX skb rings */
Sergei Shtylyov2c94e852015-10-31 02:05:56 +03001253 mdp->rx_skbuff = kcalloc(mdp->num_rx_ring, sizeof(*mdp->rx_skbuff),
1254 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001255 if (!mdp->rx_skbuff)
1256 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001257
Sergei Shtylyov2c94e852015-10-31 02:05:56 +03001258 mdp->tx_skbuff = kcalloc(mdp->num_tx_ring, sizeof(*mdp->tx_skbuff),
1259 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001260 if (!mdp->tx_skbuff)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001261 goto ring_free;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001262
1263 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001264 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001265 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001266 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001267 if (!mdp->rx_ring)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001268 goto ring_free;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001269
1270 mdp->dirty_rx = 0;
1271
1272 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001273 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001274 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001275 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001276 if (!mdp->tx_ring)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001277 goto ring_free;
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001278 return 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001279
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001280ring_free:
1281 /* Free Rx and Tx skb ring buffer and DMA buffer */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001282 sh_eth_ring_free(ndev);
1283
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001284 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001285}
1286
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001287static int sh_eth_dev_init(struct net_device *ndev, bool start)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001288{
1289 int ret = 0;
1290 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001291 u32 val;
1292
1293 /* Soft Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001294 ret = sh_eth_reset(ndev);
1295 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +01001296 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001297
Simon Horman55754f12013-07-23 10:18:04 +09001298 if (mdp->cd->rmiimode)
1299 sh_eth_write(ndev, 0x1, RMIIMODE);
1300
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001301 /* Descriptor format */
1302 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001303 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001304 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001305
1306 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001307 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001308
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001309#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001310 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001311 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001312 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001313#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001314 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001315
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001316 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001317 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1318 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001319
Ben Dooks530aa2d2014-06-03 12:21:13 +01001320 /* Frame recv control (enable multiple-packets per rx irq) */
1321 sh_eth_write(ndev, RMCR_RNC, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001322
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +09001323 sh_eth_write(ndev, mdp->cd->trscer_err_mask, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001324
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001325 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001326 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001327
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001328 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001329
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001330 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001331 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001332
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001333 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001334 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1335 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001336
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001337 sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00001338 if (start) {
1339 mdp->irq_enabled = true;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001340 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00001341 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001342
1343 /* PAUSE Prohibition */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001344 val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001345 ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
1346
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001347 sh_eth_write(ndev, val, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001348
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001349 if (mdp->cd->set_rate)
1350 mdp->cd->set_rate(ndev);
1351
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001352 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001353 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001354
1355 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001356 if (start)
1357 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001358
1359 /* Set MAC address */
1360 update_mac_address(ndev);
1361
1362 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001363 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001364 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001365 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001366 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001367 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001368 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001369
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001370 if (start) {
1371 /* Setting the Rx mode will start the Rx process. */
1372 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001373
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001374 netif_start_queue(ndev);
1375 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001376
1377 return ret;
1378}
1379
Ben Hutchings740c7f32015-01-27 00:49:32 +00001380static void sh_eth_dev_exit(struct net_device *ndev)
1381{
1382 struct sh_eth_private *mdp = netdev_priv(ndev);
1383 int i;
1384
1385 /* Deactivate all TX descriptors, so DMA should stop at next
1386 * packet boundary if it's currently running
1387 */
1388 for (i = 0; i < mdp->num_tx_ring; i++)
1389 mdp->tx_ring[i].status &= ~cpu_to_edmac(mdp, TD_TACT);
1390
1391 /* Disable TX FIFO egress to MAC */
1392 sh_eth_rcv_snd_disable(ndev);
1393
1394 /* Stop RX DMA at next packet boundary */
1395 sh_eth_write(ndev, 0, EDRRR);
1396
1397 /* Aside from TX DMA, we can't tell when the hardware is
1398 * really stopped, so we need to reset to make sure.
1399 * Before doing that, wait for long enough to *probably*
1400 * finish transmitting the last packet and poll stats.
1401 */
1402 msleep(2); /* max frame time at 10 Mbps < 1250 us */
1403 sh_eth_get_stats(ndev);
1404 sh_eth_reset(ndev);
Geert Uytterhoevena14c7d12015-02-27 17:16:26 +01001405
1406 /* Set MAC address again */
1407 update_mac_address(ndev);
Ben Hutchings740c7f32015-01-27 00:49:32 +00001408}
1409
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001410/* free Tx skb function */
1411static int sh_eth_txfree(struct net_device *ndev)
1412{
1413 struct sh_eth_private *mdp = netdev_priv(ndev);
1414 struct sh_eth_txdesc *txdesc;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001415 int free_num = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001416 int entry = 0;
1417
1418 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001419 entry = mdp->dirty_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001420 txdesc = &mdp->tx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001421 if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001422 break;
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001423 /* TACT bit must be checked before all the following reads */
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001424 dma_rmb();
Ben Hutchingse5fd13f2015-02-26 20:34:46 +00001425 netif_info(mdp, tx_done, ndev,
1426 "tx entry %d status 0x%08x\n",
1427 entry, edmac_to_cpu(mdp, txdesc->status));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001428 /* Free the original skb. */
1429 if (mdp->tx_skbuff[entry]) {
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00001430 dma_unmap_single(&ndev->dev, txdesc->addr,
1431 txdesc->buffer_length, DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001432 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1433 mdp->tx_skbuff[entry] = NULL;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001434 free_num++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001435 }
Yoshinori Sato71557a32008-08-06 19:49:00 -04001436 txdesc->status = cpu_to_edmac(mdp, TD_TFP);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001437 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04001438 txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001439
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001440 ndev->stats.tx_packets++;
1441 ndev->stats.tx_bytes += txdesc->buffer_length;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001442 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001443 return free_num;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001444}
1445
1446/* Packet receive function */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001447static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001448{
1449 struct sh_eth_private *mdp = netdev_priv(ndev);
1450 struct sh_eth_rxdesc *rxdesc;
1451
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001452 int entry = mdp->cur_rx % mdp->num_rx_ring;
1453 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001454 int limit;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001455 struct sk_buff *skb;
1456 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001457 u32 desc_status;
Sergei Shtylyovcb368592015-10-24 00:46:40 +03001458 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001459 dma_addr_t dma_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001460
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001461 boguscnt = min(boguscnt, *quota);
1462 limit = boguscnt;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001463 rxdesc = &mdp->rx_ring[entry];
Yoshinori Sato71557a32008-08-06 19:49:00 -04001464 while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001465 /* RACT bit must be checked before all the following reads */
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001466 dma_rmb();
Yoshinori Sato71557a32008-08-06 19:49:00 -04001467 desc_status = edmac_to_cpu(mdp, rxdesc->status);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001468 pkt_len = rxdesc->frame_length;
1469
1470 if (--boguscnt < 0)
1471 break;
1472
Ben Hutchingse5fd13f2015-02-26 20:34:46 +00001473 netif_info(mdp, rx_status, ndev,
1474 "rx entry %d status 0x%08x len %d\n",
1475 entry, desc_status, pkt_len);
1476
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001477 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001478 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001479
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001480 /* In case of almost all GETHER/ETHERs, the Receive Frame State
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001481 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
Ben Hutchings9b4a6362015-03-03 00:52:39 +00001482 * bit 0. However, in case of the R8A7740 and R7S72100
1483 * the RFS bits are from bit 25 to bit 16. So, the
Simon Hormandb893472014-01-17 09:22:28 +09001484 * driver needs right shifting by 16.
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001485 */
Sergei Shtylyovac8025a2013-06-13 22:12:45 +04001486 if (mdp->cd->shift_rd0)
1487 desc_status >>= 16;
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001488
Sergei Shtylyov248be832015-12-04 01:45:40 +03001489 skb = mdp->rx_skbuff[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001490 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1491 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001492 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001493 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001494 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001495 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001496 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001497 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001498 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001499 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001500 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001501 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001502 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001503 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001504 ndev->stats.rx_over_errors++;
Sergei Shtylyov248be832015-12-04 01:45:40 +03001505 } else if (skb) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001506 if (!mdp->cd->hw_swap)
1507 sh_eth_soft_swap(
1508 phys_to_virt(ALIGN(rxdesc->addr, 4)),
1509 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001510 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001511 if (mdp->cd->rpadir)
1512 skb_reserve(skb, NET_IP_ALIGN);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001513 dma_unmap_single(&ndev->dev, rxdesc->addr,
Sergei Shtylyovab857912015-10-24 00:46:03 +03001514 ALIGN(mdp->rx_buf_sz, 32),
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001515 DMA_FROM_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001516 skb_put(skb, pkt_len);
1517 skb->protocol = eth_type_trans(skb, ndev);
Sergei Shtylyova8e9fd02013-09-03 03:03:10 +04001518 netif_receive_skb(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001519 ndev->stats.rx_packets++;
1520 ndev->stats.rx_bytes += pkt_len;
Ben Hutchings25b77ad2015-02-26 20:33:30 +00001521 if (desc_status & RD_RFS8)
1522 ndev->stats.multicast++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001523 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001524 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001525 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001526 }
1527
1528 /* Refill the Rx ring buffers. */
1529 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001530 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001531 rxdesc = &mdp->rx_ring[entry];
Sergei Shtylyovab857912015-10-24 00:46:03 +03001532 /* The size of the buffer is 32 byte boundary. */
1533 rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 32);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001534
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001535 if (mdp->rx_skbuff[entry] == NULL) {
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001536 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001537 if (skb == NULL)
1538 break; /* Better luck next round. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001539 sh_eth_set_receive_align(skb);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001540 dma_addr = dma_map_single(&ndev->dev, skb->data,
1541 rxdesc->buffer_length,
1542 DMA_FROM_DEVICE);
1543 if (dma_mapping_error(&ndev->dev, dma_addr)) {
1544 kfree_skb(skb);
1545 break;
1546 }
1547 mdp->rx_skbuff[entry] = skb;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001548
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001549 skb_checksum_none_assert(skb);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001550 rxdesc->addr = dma_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001551 }
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001552 dma_wmb(); /* RACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001553 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001554 rxdesc->status |=
Sergei Shtylyovc2380412015-11-03 01:28:07 +03001555 cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001556 else
1557 rxdesc->status |=
Yoshinori Sato71557a32008-08-06 19:49:00 -04001558 cpu_to_edmac(mdp, RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001559 }
1560
1561 /* Restart Rx engine if stopped. */
1562 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001563 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001564 /* fix the values for the next receiving if RDE is set */
Ben Hutchings33657112015-02-26 20:34:14 +00001565 if (intr_status & EESR_RDE &&
1566 mdp->reg_offset[RDFAR] != SH_ETH_OFFSET_INVALID) {
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001567 u32 count = (sh_eth_read(ndev, RDFAR) -
1568 sh_eth_read(ndev, RDLAR)) >> 4;
1569
1570 mdp->cur_rx = count;
1571 mdp->dirty_rx = count;
1572 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001573 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001574 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001575
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001576 *quota -= limit - boguscnt - 1;
1577
Yoshihiro Shimoda4f809ce2014-06-10 09:40:14 +09001578 return *quota <= 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001579}
1580
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001581static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001582{
1583 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001584 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
1585 ~(ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001586}
1587
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001588static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001589{
1590 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001591 sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
1592 (ECMR_RE | ECMR_TE), ECMR);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001593}
1594
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001595/* error control function */
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001596static void sh_eth_error(struct net_device *ndev, u32 intr_status)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001597{
1598 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001599 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001600 u32 link_stat;
1601 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001602
1603 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001604 felic_stat = sh_eth_read(ndev, ECSR);
1605 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001606 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001607 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001608 if (felic_stat & ECSR_LCHNG) {
1609 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001610 if (mdp->cd->no_psr || mdp->no_ether_link) {
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001611 goto ignore_link;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001612 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001613 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001614 if (mdp->ether_link_active_low)
1615 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001616 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001617 if (!(link_stat & PHY_ST_LINK)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001618 sh_eth_rcv_snd_disable(ndev);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001619 } else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001620 /* Link Up */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001621 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001622 ~DMAC_M_ECI, EESIPR);
1623 /* clear int */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001624 sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001625 ECSR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001626 sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001627 DMAC_M_ECI, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001628 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001629 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001630 }
1631 }
1632 }
1633
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001634ignore_link:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001635 if (intr_status & EESR_TWB) {
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001636 /* Unused write back interrupt */
1637 if (intr_status & EESR_TABT) { /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001638 ndev->stats.tx_aborted_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001639 netif_err(mdp, tx_err, ndev, "Transmit Abort\n");
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001640 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001641 }
1642
1643 if (intr_status & EESR_RABT) {
1644 /* Receive Abort int */
1645 if (intr_status & EESR_RFRMER) {
1646 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001647 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001648 }
1649 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001650
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001651 if (intr_status & EESR_TDE) {
1652 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001653 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001654 netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n");
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001655 }
1656
1657 if (intr_status & EESR_TFE) {
1658 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001659 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001660 netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001661 }
1662
1663 if (intr_status & EESR_RDE) {
1664 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001665 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001666 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001667
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001668 if (intr_status & EESR_RFE) {
1669 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001670 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001671 }
1672
1673 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1674 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001675 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001676 netif_err(mdp, tx_err, ndev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001677 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001678
1679 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1680 if (mdp->cd->no_ade)
1681 mask &= ~EESR_ADE;
1682 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001683 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001684 u32 edtrr = sh_eth_read(ndev, EDTRR);
Sergei Shtylyov090d5602014-01-11 02:41:49 +03001685
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001686 /* dmesg */
Sergei Shtylyovda246852014-03-15 03:29:14 +03001687 netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1688 intr_status, mdp->cur_tx, mdp->dirty_tx,
1689 (u32)ndev->state, edtrr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001690 /* dirty buffer free */
1691 sh_eth_txfree(ndev);
1692
1693 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001694 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001695 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001696 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001697 }
1698 /* wakeup */
1699 netif_wake_queue(ndev);
1700 }
1701}
1702
1703static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1704{
1705 struct net_device *ndev = netdev;
1706 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001707 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001708 irqreturn_t ret = IRQ_NONE;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001709 u32 intr_status, intr_enable;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001710
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001711 spin_lock(&mdp->lock);
1712
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001713 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001714 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001715 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1716 * enabled since it's the one that comes thru regardless of the mask,
1717 * and we need to fully handle it in sh_eth_error() in order to quench
1718 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1719 */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001720 intr_enable = sh_eth_read(ndev, EESIPR);
1721 intr_status &= intr_enable | DMAC_M_ECI;
1722 if (intr_status & (EESR_RX_CHECK | cd->tx_check | cd->eesr_err_check))
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001723 ret = IRQ_HANDLED;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001724 else
Ben Hutchings283e38d2015-01-22 12:44:08 +00001725 goto out;
1726
1727 if (!likely(mdp->irq_enabled)) {
1728 sh_eth_write(ndev, 0, EESIPR);
1729 goto out;
1730 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001731
Sergei Shtylyov37191092013-06-19 23:30:23 +04001732 if (intr_status & EESR_RX_CHECK) {
1733 if (napi_schedule_prep(&mdp->napi)) {
1734 /* Mask Rx interrupts */
1735 sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
1736 EESIPR);
1737 __napi_schedule(&mdp->napi);
1738 } else {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001739 netdev_warn(ndev,
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001740 "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
Sergei Shtylyovda246852014-03-15 03:29:14 +03001741 intr_status, intr_enable);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001742 }
1743 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001744
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001745 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001746 if (intr_status & cd->tx_check) {
Sergei Shtylyov37191092013-06-19 23:30:23 +04001747 /* Clear Tx interrupts */
1748 sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
1749
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001750 sh_eth_txfree(ndev);
1751 netif_wake_queue(ndev);
1752 }
1753
Sergei Shtylyov37191092013-06-19 23:30:23 +04001754 if (intr_status & cd->eesr_err_check) {
1755 /* Clear error interrupts */
1756 sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
1757
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001758 sh_eth_error(ndev, intr_status);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001759 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001760
Ben Hutchings283e38d2015-01-22 12:44:08 +00001761out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001762 spin_unlock(&mdp->lock);
1763
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001764 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001765}
1766
Sergei Shtylyov37191092013-06-19 23:30:23 +04001767static int sh_eth_poll(struct napi_struct *napi, int budget)
1768{
1769 struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
1770 napi);
1771 struct net_device *ndev = napi->dev;
1772 int quota = budget;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001773 u32 intr_status;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001774
1775 for (;;) {
1776 intr_status = sh_eth_read(ndev, EESR);
1777 if (!(intr_status & EESR_RX_CHECK))
1778 break;
1779 /* Clear Rx interrupts */
1780 sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
1781
1782 if (sh_eth_rx(ndev, intr_status, &quota))
1783 goto out;
1784 }
1785
1786 napi_complete(napi);
1787
1788 /* Reenable Rx interrupts */
Ben Hutchings283e38d2015-01-22 12:44:08 +00001789 if (mdp->irq_enabled)
1790 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001791out:
1792 return budget - quota;
1793}
1794
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001795/* PHY state control function */
1796static void sh_eth_adjust_link(struct net_device *ndev)
1797{
1798 struct sh_eth_private *mdp = netdev_priv(ndev);
1799 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001800 int new_state = 0;
1801
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001802 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001803 if (phydev->duplex != mdp->duplex) {
1804 new_state = 1;
1805 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001806 if (mdp->cd->set_duplex)
1807 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001808 }
1809
1810 if (phydev->speed != mdp->speed) {
1811 new_state = 1;
1812 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001813 if (mdp->cd->set_rate)
1814 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001815 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001816 if (!mdp->link) {
Yoshihiro Shimoda91a56152011-07-05 20:33:51 +00001817 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001818 sh_eth_read(ndev, ECMR) & ~ECMR_TXF,
1819 ECMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001820 new_state = 1;
1821 mdp->link = phydev->link;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001822 if (mdp->cd->no_psr || mdp->no_ether_link)
1823 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001824 }
1825 } else if (mdp->link) {
1826 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001827 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001828 mdp->speed = 0;
1829 mdp->duplex = -1;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001830 if (mdp->cd->no_psr || mdp->no_ether_link)
1831 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001832 }
1833
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001834 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001835 phy_print_status(phydev);
1836}
1837
1838/* PHY init function */
1839static int sh_eth_phy_init(struct net_device *ndev)
1840{
Ben Dooks702eca02014-03-12 17:47:40 +00001841 struct device_node *np = ndev->dev.parent->of_node;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001842 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001843 struct phy_device *phydev = NULL;
1844
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001845 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001846 mdp->speed = 0;
1847 mdp->duplex = -1;
1848
1849 /* Try connect to PHY */
Ben Dooks702eca02014-03-12 17:47:40 +00001850 if (np) {
1851 struct device_node *pn;
1852
1853 pn = of_parse_phandle(np, "phy-handle", 0);
1854 phydev = of_phy_connect(ndev, pn,
1855 sh_eth_adjust_link, 0,
1856 mdp->phy_interface);
1857
1858 if (!phydev)
1859 phydev = ERR_PTR(-ENOENT);
1860 } else {
1861 char phy_id[MII_BUS_ID_SIZE + 3];
1862
1863 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
1864 mdp->mii_bus->id, mdp->phy_id);
1865
1866 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
1867 mdp->phy_interface);
1868 }
1869
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001870 if (IS_ERR(phydev)) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001871 netdev_err(ndev, "failed to connect PHY\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001872 return PTR_ERR(phydev);
1873 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001874
Sergei Shtylyovda246852014-03-15 03:29:14 +03001875 netdev_info(ndev, "attached PHY %d (IRQ %d) to driver %s\n",
1876 phydev->addr, phydev->irq, phydev->drv->name);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001877
1878 mdp->phydev = phydev;
1879
1880 return 0;
1881}
1882
1883/* PHY control start function */
1884static int sh_eth_phy_start(struct net_device *ndev)
1885{
1886 struct sh_eth_private *mdp = netdev_priv(ndev);
1887 int ret;
1888
1889 ret = sh_eth_phy_init(ndev);
1890 if (ret)
1891 return ret;
1892
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001893 phy_start(mdp->phydev);
1894
1895 return 0;
1896}
1897
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001898static int sh_eth_get_settings(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001899 struct ethtool_cmd *ecmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001900{
1901 struct sh_eth_private *mdp = netdev_priv(ndev);
1902 unsigned long flags;
1903 int ret;
1904
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001905 if (!mdp->phydev)
1906 return -ENODEV;
1907
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001908 spin_lock_irqsave(&mdp->lock, flags);
1909 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1910 spin_unlock_irqrestore(&mdp->lock, flags);
1911
1912 return ret;
1913}
1914
1915static int sh_eth_set_settings(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001916 struct ethtool_cmd *ecmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001917{
1918 struct sh_eth_private *mdp = netdev_priv(ndev);
1919 unsigned long flags;
1920 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001921
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001922 if (!mdp->phydev)
1923 return -ENODEV;
1924
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001925 spin_lock_irqsave(&mdp->lock, flags);
1926
1927 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001928 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001929
1930 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1931 if (ret)
1932 goto error_exit;
1933
1934 if (ecmd->duplex == DUPLEX_FULL)
1935 mdp->duplex = 1;
1936 else
1937 mdp->duplex = 0;
1938
1939 if (mdp->cd->set_duplex)
1940 mdp->cd->set_duplex(ndev);
1941
1942error_exit:
1943 mdelay(1);
1944
1945 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001946 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001947
1948 spin_unlock_irqrestore(&mdp->lock, flags);
1949
1950 return ret;
1951}
1952
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00001953/* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
1954 * version must be bumped as well. Just adding registers up to that
1955 * limit is fine, as long as the existing register indices don't
1956 * change.
1957 */
1958#define SH_ETH_REG_DUMP_VERSION 1
1959#define SH_ETH_REG_DUMP_MAX_REGS 256
1960
1961static size_t __sh_eth_get_regs(struct net_device *ndev, u32 *buf)
1962{
1963 struct sh_eth_private *mdp = netdev_priv(ndev);
1964 struct sh_eth_cpu_data *cd = mdp->cd;
1965 u32 *valid_map;
1966 size_t len;
1967
1968 BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET > SH_ETH_REG_DUMP_MAX_REGS);
1969
1970 /* Dump starts with a bitmap that tells ethtool which
1971 * registers are defined for this chip.
1972 */
1973 len = DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS, 32);
1974 if (buf) {
1975 valid_map = buf;
1976 buf += len;
1977 } else {
1978 valid_map = NULL;
1979 }
1980
1981 /* Add a register to the dump, if it has a defined offset.
1982 * This automatically skips most undefined registers, but for
1983 * some it is also necessary to check a capability flag in
1984 * struct sh_eth_cpu_data.
1985 */
1986#define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
1987#define add_reg_from(reg, read_expr) do { \
1988 if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
1989 if (buf) { \
1990 mark_reg_valid(reg); \
1991 *buf++ = read_expr; \
1992 } \
1993 ++len; \
1994 } \
1995 } while (0)
1996#define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
1997#define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
1998
1999 add_reg(EDSR);
2000 add_reg(EDMR);
2001 add_reg(EDTRR);
2002 add_reg(EDRRR);
2003 add_reg(EESR);
2004 add_reg(EESIPR);
2005 add_reg(TDLAR);
2006 add_reg(TDFAR);
2007 add_reg(TDFXR);
2008 add_reg(TDFFR);
2009 add_reg(RDLAR);
2010 add_reg(RDFAR);
2011 add_reg(RDFXR);
2012 add_reg(RDFFR);
2013 add_reg(TRSCER);
2014 add_reg(RMFCR);
2015 add_reg(TFTR);
2016 add_reg(FDR);
2017 add_reg(RMCR);
2018 add_reg(TFUCR);
2019 add_reg(RFOCR);
2020 if (cd->rmiimode)
2021 add_reg(RMIIMODE);
2022 add_reg(FCFTR);
2023 if (cd->rpadir)
2024 add_reg(RPADIR);
2025 if (!cd->no_trimd)
2026 add_reg(TRIMD);
2027 add_reg(ECMR);
2028 add_reg(ECSR);
2029 add_reg(ECSIPR);
2030 add_reg(PIR);
2031 if (!cd->no_psr)
2032 add_reg(PSR);
2033 add_reg(RDMLR);
2034 add_reg(RFLR);
2035 add_reg(IPGR);
2036 if (cd->apr)
2037 add_reg(APR);
2038 if (cd->mpr)
2039 add_reg(MPR);
2040 add_reg(RFCR);
2041 add_reg(RFCF);
2042 if (cd->tpauser)
2043 add_reg(TPAUSER);
2044 add_reg(TPAUSECR);
2045 add_reg(GECMR);
2046 if (cd->bculr)
2047 add_reg(BCULR);
2048 add_reg(MAHR);
2049 add_reg(MALR);
2050 add_reg(TROCR);
2051 add_reg(CDCR);
2052 add_reg(LCCR);
2053 add_reg(CNDCR);
2054 add_reg(CEFCR);
2055 add_reg(FRECR);
2056 add_reg(TSFRCR);
2057 add_reg(TLFRCR);
2058 add_reg(CERCR);
2059 add_reg(CEECR);
2060 add_reg(MAFCR);
2061 if (cd->rtrate)
2062 add_reg(RTRATE);
2063 if (cd->hw_crc)
2064 add_reg(CSMR);
2065 if (cd->select_mii)
2066 add_reg(RMII_MII);
2067 add_reg(ARSTR);
2068 if (cd->tsu) {
2069 add_tsu_reg(TSU_CTRST);
2070 add_tsu_reg(TSU_FWEN0);
2071 add_tsu_reg(TSU_FWEN1);
2072 add_tsu_reg(TSU_FCM);
2073 add_tsu_reg(TSU_BSYSL0);
2074 add_tsu_reg(TSU_BSYSL1);
2075 add_tsu_reg(TSU_PRISL0);
2076 add_tsu_reg(TSU_PRISL1);
2077 add_tsu_reg(TSU_FWSL0);
2078 add_tsu_reg(TSU_FWSL1);
2079 add_tsu_reg(TSU_FWSLC);
2080 add_tsu_reg(TSU_QTAG0);
2081 add_tsu_reg(TSU_QTAG1);
2082 add_tsu_reg(TSU_QTAGM0);
2083 add_tsu_reg(TSU_QTAGM1);
2084 add_tsu_reg(TSU_FWSR);
2085 add_tsu_reg(TSU_FWINMK);
2086 add_tsu_reg(TSU_ADQT0);
2087 add_tsu_reg(TSU_ADQT1);
2088 add_tsu_reg(TSU_VTAG0);
2089 add_tsu_reg(TSU_VTAG1);
2090 add_tsu_reg(TSU_ADSBSY);
2091 add_tsu_reg(TSU_TEN);
2092 add_tsu_reg(TSU_POST1);
2093 add_tsu_reg(TSU_POST2);
2094 add_tsu_reg(TSU_POST3);
2095 add_tsu_reg(TSU_POST4);
2096 if (mdp->reg_offset[TSU_ADRH0] != SH_ETH_OFFSET_INVALID) {
2097 /* This is the start of a table, not just a single
2098 * register.
2099 */
2100 if (buf) {
2101 unsigned int i;
2102
2103 mark_reg_valid(TSU_ADRH0);
2104 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES * 2; i++)
2105 *buf++ = ioread32(
2106 mdp->tsu_addr +
2107 mdp->reg_offset[TSU_ADRH0] +
2108 i * 4);
2109 }
2110 len += SH_ETH_TSU_CAM_ENTRIES * 2;
2111 }
2112 }
2113
2114#undef mark_reg_valid
2115#undef add_reg_from
2116#undef add_reg
2117#undef add_tsu_reg
2118
2119 return len * 4;
2120}
2121
2122static int sh_eth_get_regs_len(struct net_device *ndev)
2123{
2124 return __sh_eth_get_regs(ndev, NULL);
2125}
2126
2127static void sh_eth_get_regs(struct net_device *ndev, struct ethtool_regs *regs,
2128 void *buf)
2129{
2130 struct sh_eth_private *mdp = netdev_priv(ndev);
2131
2132 regs->version = SH_ETH_REG_DUMP_VERSION;
2133
2134 pm_runtime_get_sync(&mdp->pdev->dev);
2135 __sh_eth_get_regs(ndev, buf);
2136 pm_runtime_put_sync(&mdp->pdev->dev);
2137}
2138
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002139static int sh_eth_nway_reset(struct net_device *ndev)
2140{
2141 struct sh_eth_private *mdp = netdev_priv(ndev);
2142 unsigned long flags;
2143 int ret;
2144
Ben Hutchings4f9dce232015-01-16 17:51:25 +00002145 if (!mdp->phydev)
2146 return -ENODEV;
2147
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002148 spin_lock_irqsave(&mdp->lock, flags);
2149 ret = phy_start_aneg(mdp->phydev);
2150 spin_unlock_irqrestore(&mdp->lock, flags);
2151
2152 return ret;
2153}
2154
2155static u32 sh_eth_get_msglevel(struct net_device *ndev)
2156{
2157 struct sh_eth_private *mdp = netdev_priv(ndev);
2158 return mdp->msg_enable;
2159}
2160
2161static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
2162{
2163 struct sh_eth_private *mdp = netdev_priv(ndev);
2164 mdp->msg_enable = value;
2165}
2166
2167static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
2168 "rx_current", "tx_current",
2169 "rx_dirty", "tx_dirty",
2170};
2171#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
2172
2173static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
2174{
2175 switch (sset) {
2176 case ETH_SS_STATS:
2177 return SH_ETH_STATS_LEN;
2178 default:
2179 return -EOPNOTSUPP;
2180 }
2181}
2182
2183static void sh_eth_get_ethtool_stats(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002184 struct ethtool_stats *stats, u64 *data)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002185{
2186 struct sh_eth_private *mdp = netdev_priv(ndev);
2187 int i = 0;
2188
2189 /* device-specific stats */
2190 data[i++] = mdp->cur_rx;
2191 data[i++] = mdp->cur_tx;
2192 data[i++] = mdp->dirty_rx;
2193 data[i++] = mdp->dirty_tx;
2194}
2195
2196static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
2197{
2198 switch (stringset) {
2199 case ETH_SS_STATS:
2200 memcpy(data, *sh_eth_gstrings_stats,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002201 sizeof(sh_eth_gstrings_stats));
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002202 break;
2203 }
2204}
2205
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002206static void sh_eth_get_ringparam(struct net_device *ndev,
2207 struct ethtool_ringparam *ring)
2208{
2209 struct sh_eth_private *mdp = netdev_priv(ndev);
2210
2211 ring->rx_max_pending = RX_RING_MAX;
2212 ring->tx_max_pending = TX_RING_MAX;
2213 ring->rx_pending = mdp->num_rx_ring;
2214 ring->tx_pending = mdp->num_tx_ring;
2215}
2216
2217static int sh_eth_set_ringparam(struct net_device *ndev,
2218 struct ethtool_ringparam *ring)
2219{
2220 struct sh_eth_private *mdp = netdev_priv(ndev);
2221 int ret;
2222
2223 if (ring->tx_pending > TX_RING_MAX ||
2224 ring->rx_pending > RX_RING_MAX ||
2225 ring->tx_pending < TX_RING_MIN ||
2226 ring->rx_pending < RX_RING_MIN)
2227 return -EINVAL;
2228 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2229 return -EINVAL;
2230
2231 if (netif_running(ndev)) {
Ben Hutchingsbd888912015-01-22 12:40:25 +00002232 netif_device_detach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002233 netif_tx_disable(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002234
Ben Hutchings283e38d2015-01-22 12:44:08 +00002235 /* Serialise with the interrupt handler and NAPI, then
2236 * disable interrupts. We have to clear the
2237 * irq_enabled flag first to ensure that interrupts
2238 * won't be re-enabled.
2239 */
2240 mdp->irq_enabled = false;
2241 synchronize_irq(ndev->irq);
2242 napi_synchronize(&mdp->napi);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002243 sh_eth_write(ndev, 0x0000, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00002244
Ben Hutchings740c7f32015-01-27 00:49:32 +00002245 sh_eth_dev_exit(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002246
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03002247 /* Free all the skbuffs in the Rx queue and the DMA buffers. */
Ben Hutchings084236d2015-01-22 12:41:34 +00002248 sh_eth_ring_free(ndev);
Ben Hutchings084236d2015-01-22 12:41:34 +00002249 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002250
2251 /* Set new parameters */
2252 mdp->num_rx_ring = ring->rx_pending;
2253 mdp->num_tx_ring = ring->tx_pending;
2254
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002255 if (netif_running(ndev)) {
Ben Hutchings084236d2015-01-22 12:41:34 +00002256 ret = sh_eth_ring_init(ndev);
2257 if (ret < 0) {
2258 netdev_err(ndev, "%s: sh_eth_ring_init failed.\n",
2259 __func__);
2260 return ret;
2261 }
2262 ret = sh_eth_dev_init(ndev, false);
2263 if (ret < 0) {
2264 netdev_err(ndev, "%s: sh_eth_dev_init failed.\n",
2265 __func__);
2266 return ret;
2267 }
2268
Ben Hutchings283e38d2015-01-22 12:44:08 +00002269 mdp->irq_enabled = true;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002270 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
2271 /* Setting the Rx mode will start the Rx process. */
2272 sh_eth_write(ndev, EDRRR_R, EDRRR);
Ben Hutchingsbd888912015-01-22 12:40:25 +00002273 netif_device_attach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002274 }
2275
2276 return 0;
2277}
2278
stephen hemminger9b07be42012-01-04 12:59:49 +00002279static const struct ethtool_ops sh_eth_ethtool_ops = {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002280 .get_settings = sh_eth_get_settings,
2281 .set_settings = sh_eth_set_settings,
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002282 .get_regs_len = sh_eth_get_regs_len,
2283 .get_regs = sh_eth_get_regs,
stephen hemminger9b07be42012-01-04 12:59:49 +00002284 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002285 .get_msglevel = sh_eth_get_msglevel,
2286 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00002287 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002288 .get_strings = sh_eth_get_strings,
2289 .get_ethtool_stats = sh_eth_get_ethtool_stats,
2290 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002291 .get_ringparam = sh_eth_get_ringparam,
2292 .set_ringparam = sh_eth_set_ringparam,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002293};
2294
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002295/* network device open function */
2296static int sh_eth_open(struct net_device *ndev)
2297{
2298 int ret = 0;
2299 struct sh_eth_private *mdp = netdev_priv(ndev);
2300
Magnus Dammbcd51492009-10-09 00:20:04 +00002301 pm_runtime_get_sync(&mdp->pdev->dev);
2302
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002303 napi_enable(&mdp->napi);
2304
Joe Perchesa0607fd2009-11-18 23:29:17 -08002305 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00002306 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002307 if (ret) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002308 netdev_err(ndev, "Can not assign IRQ number\n");
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002309 goto out_napi_off;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002310 }
2311
2312 /* Descriptor set */
2313 ret = sh_eth_ring_init(ndev);
2314 if (ret)
2315 goto out_free_irq;
2316
2317 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002318 ret = sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002319 if (ret)
2320 goto out_free_irq;
2321
2322 /* PHY control start*/
2323 ret = sh_eth_phy_start(ndev);
2324 if (ret)
2325 goto out_free_irq;
2326
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002327 mdp->is_opened = 1;
2328
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002329 return ret;
2330
2331out_free_irq:
2332 free_irq(ndev->irq, ndev);
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002333out_napi_off:
2334 napi_disable(&mdp->napi);
Magnus Dammbcd51492009-10-09 00:20:04 +00002335 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002336 return ret;
2337}
2338
2339/* Timeout function */
2340static void sh_eth_tx_timeout(struct net_device *ndev)
2341{
2342 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002343 struct sh_eth_rxdesc *rxdesc;
2344 int i;
2345
2346 netif_stop_queue(ndev);
2347
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002348 netif_err(mdp, timer, ndev,
2349 "transmit timed out, status %8.8x, resetting...\n",
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01002350 sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002351
2352 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002353 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002354
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002355 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002356 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002357 rxdesc = &mdp->rx_ring[i];
2358 rxdesc->status = 0;
2359 rxdesc->addr = 0xBADF00D0;
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002360 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002361 mdp->rx_skbuff[i] = NULL;
2362 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002363 for (i = 0; i < mdp->num_tx_ring; i++) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002364 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002365 mdp->tx_skbuff[i] = NULL;
2366 }
2367
2368 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002369 sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002370}
2371
2372/* Packet transmit function */
2373static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
2374{
2375 struct sh_eth_private *mdp = netdev_priv(ndev);
2376 struct sh_eth_txdesc *txdesc;
2377 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00002378 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002379
2380 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002381 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002382 if (!sh_eth_txfree(ndev)) {
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002383 netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002384 netif_stop_queue(ndev);
2385 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00002386 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002387 }
2388 }
2389 spin_unlock_irqrestore(&mdp->lock, flags);
2390
Ben Hutchingsdacc73e2015-03-03 00:53:08 +00002391 if (skb_put_padto(skb, ETH_ZLEN))
Ben Hutchingseebfb642015-01-22 12:40:13 +00002392 return NETDEV_TX_OK;
2393
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002394 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002395 mdp->tx_skbuff[entry] = skb;
2396 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002397 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002398 if (!mdp->cd->hw_swap)
2399 sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
2400 skb->len + 2);
Yoshihiro Shimoda31fcb992011-06-30 22:52:13 +00002401 txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
2402 DMA_TO_DEVICE);
Ben Hutchingsaa3933b2015-01-27 00:49:47 +00002403 if (dma_mapping_error(&ndev->dev, txdesc->addr)) {
2404 kfree_skb(skb);
2405 return NETDEV_TX_OK;
2406 }
Ben Hutchingseebfb642015-01-22 12:40:13 +00002407 txdesc->buffer_length = skb->len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002408
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03002409 dma_wmb(); /* TACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002410 if (entry >= mdp->num_tx_ring - 1)
Yoshinori Sato71557a32008-08-06 19:49:00 -04002411 txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002412 else
Yoshinori Sato71557a32008-08-06 19:49:00 -04002413 txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002414
2415 mdp->cur_tx++;
2416
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002417 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
2418 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09002419
Patrick McHardy6ed10652009-06-23 06:03:08 +00002420 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002421}
2422
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002423/* The statistics registers have write-clear behaviour, which means we
2424 * will lose any increment between the read and write. We mitigate
2425 * this by only clearing when we read a non-zero value, so we will
2426 * never falsely report a total of zero.
2427 */
2428static void
2429sh_eth_update_stat(struct net_device *ndev, unsigned long *stat, int reg)
2430{
2431 u32 delta = sh_eth_read(ndev, reg);
2432
2433 if (delta) {
2434 *stat += delta;
2435 sh_eth_write(ndev, 0, reg);
2436 }
2437}
2438
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002439static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
2440{
2441 struct sh_eth_private *mdp = netdev_priv(ndev);
2442
2443 if (sh_eth_is_rz_fast_ether(mdp))
2444 return &ndev->stats;
2445
2446 if (!mdp->is_opened)
2447 return &ndev->stats;
2448
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002449 sh_eth_update_stat(ndev, &ndev->stats.tx_dropped, TROCR);
2450 sh_eth_update_stat(ndev, &ndev->stats.collisions, CDCR);
2451 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, LCCR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002452
2453 if (sh_eth_is_gether(mdp)) {
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002454 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2455 CERCR);
2456 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2457 CEECR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002458 } else {
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002459 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2460 CNDCR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002461 }
2462
2463 return &ndev->stats;
2464}
2465
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002466/* device close function */
2467static int sh_eth_close(struct net_device *ndev)
2468{
2469 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002470
2471 netif_stop_queue(ndev);
2472
Ben Hutchings283e38d2015-01-22 12:44:08 +00002473 /* Serialise with the interrupt handler and NAPI, then disable
2474 * interrupts. We have to clear the irq_enabled flag first to
2475 * ensure that interrupts won't be re-enabled.
2476 */
2477 mdp->irq_enabled = false;
2478 synchronize_irq(ndev->irq);
2479 napi_disable(&mdp->napi);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002480 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002481
Ben Hutchings740c7f32015-01-27 00:49:32 +00002482 sh_eth_dev_exit(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002483
2484 /* PHY Disconnect */
2485 if (mdp->phydev) {
2486 phy_stop(mdp->phydev);
2487 phy_disconnect(mdp->phydev);
Ben Hutchings4f9dce232015-01-16 17:51:25 +00002488 mdp->phydev = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002489 }
2490
2491 free_irq(ndev->irq, ndev);
2492
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03002493 /* Free all the skbuffs in the Rx queue and the DMA buffer. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002494 sh_eth_ring_free(ndev);
2495
Magnus Dammbcd51492009-10-09 00:20:04 +00002496 pm_runtime_put_sync(&mdp->pdev->dev);
2497
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002498 mdp->is_opened = 0;
2499
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002500 return 0;
2501}
2502
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002503/* ioctl to device function */
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002504static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002505{
2506 struct sh_eth_private *mdp = netdev_priv(ndev);
2507 struct phy_device *phydev = mdp->phydev;
2508
2509 if (!netif_running(ndev))
2510 return -EINVAL;
2511
2512 if (!phydev)
2513 return -ENODEV;
2514
Richard Cochran28b04112010-07-17 08:48:55 +00002515 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002516}
2517
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002518/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2519static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2520 int entry)
2521{
2522 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2523}
2524
2525static u32 sh_eth_tsu_get_post_mask(int entry)
2526{
2527 return 0x0f << (28 - ((entry % 8) * 4));
2528}
2529
2530static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2531{
2532 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2533}
2534
2535static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2536 int entry)
2537{
2538 struct sh_eth_private *mdp = netdev_priv(ndev);
2539 u32 tmp;
2540 void *reg_offset;
2541
2542 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2543 tmp = ioread32(reg_offset);
2544 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2545}
2546
2547static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2548 int entry)
2549{
2550 struct sh_eth_private *mdp = netdev_priv(ndev);
2551 u32 post_mask, ref_mask, tmp;
2552 void *reg_offset;
2553
2554 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2555 post_mask = sh_eth_tsu_get_post_mask(entry);
2556 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2557
2558 tmp = ioread32(reg_offset);
2559 iowrite32(tmp & ~post_mask, reg_offset);
2560
2561 /* If other port enables, the function returns "true" */
2562 return tmp & ref_mask;
2563}
2564
2565static int sh_eth_tsu_busy(struct net_device *ndev)
2566{
2567 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2568 struct sh_eth_private *mdp = netdev_priv(ndev);
2569
2570 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2571 udelay(10);
2572 timeout--;
2573 if (timeout <= 0) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002574 netdev_err(ndev, "%s: timeout\n", __func__);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002575 return -ETIMEDOUT;
2576 }
2577 }
2578
2579 return 0;
2580}
2581
2582static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2583 const u8 *addr)
2584{
2585 u32 val;
2586
2587 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2588 iowrite32(val, reg);
2589 if (sh_eth_tsu_busy(ndev) < 0)
2590 return -EBUSY;
2591
2592 val = addr[4] << 8 | addr[5];
2593 iowrite32(val, reg + 4);
2594 if (sh_eth_tsu_busy(ndev) < 0)
2595 return -EBUSY;
2596
2597 return 0;
2598}
2599
2600static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2601{
2602 u32 val;
2603
2604 val = ioread32(reg);
2605 addr[0] = (val >> 24) & 0xff;
2606 addr[1] = (val >> 16) & 0xff;
2607 addr[2] = (val >> 8) & 0xff;
2608 addr[3] = val & 0xff;
2609 val = ioread32(reg + 4);
2610 addr[4] = (val >> 8) & 0xff;
2611 addr[5] = val & 0xff;
2612}
2613
2614
2615static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2616{
2617 struct sh_eth_private *mdp = netdev_priv(ndev);
2618 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2619 int i;
2620 u8 c_addr[ETH_ALEN];
2621
2622 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2623 sh_eth_tsu_read_entry(reg_offset, c_addr);
dingtianhongc4bde292013-12-30 15:41:17 +08002624 if (ether_addr_equal(addr, c_addr))
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002625 return i;
2626 }
2627
2628 return -ENOENT;
2629}
2630
2631static int sh_eth_tsu_find_empty(struct net_device *ndev)
2632{
2633 u8 blank[ETH_ALEN];
2634 int entry;
2635
2636 memset(blank, 0, sizeof(blank));
2637 entry = sh_eth_tsu_find_entry(ndev, blank);
2638 return (entry < 0) ? -ENOMEM : entry;
2639}
2640
2641static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2642 int entry)
2643{
2644 struct sh_eth_private *mdp = netdev_priv(ndev);
2645 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2646 int ret;
2647 u8 blank[ETH_ALEN];
2648
2649 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2650 ~(1 << (31 - entry)), TSU_TEN);
2651
2652 memset(blank, 0, sizeof(blank));
2653 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2654 if (ret < 0)
2655 return ret;
2656 return 0;
2657}
2658
2659static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2660{
2661 struct sh_eth_private *mdp = netdev_priv(ndev);
2662 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2663 int i, ret;
2664
2665 if (!mdp->cd->tsu)
2666 return 0;
2667
2668 i = sh_eth_tsu_find_entry(ndev, addr);
2669 if (i < 0) {
2670 /* No entry found, create one */
2671 i = sh_eth_tsu_find_empty(ndev);
2672 if (i < 0)
2673 return -ENOMEM;
2674 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2675 if (ret < 0)
2676 return ret;
2677
2678 /* Enable the entry */
2679 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2680 (1 << (31 - i)), TSU_TEN);
2681 }
2682
2683 /* Entry found or created, enable POST */
2684 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2685
2686 return 0;
2687}
2688
2689static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2690{
2691 struct sh_eth_private *mdp = netdev_priv(ndev);
2692 int i, ret;
2693
2694 if (!mdp->cd->tsu)
2695 return 0;
2696
2697 i = sh_eth_tsu_find_entry(ndev, addr);
2698 if (i) {
2699 /* Entry found */
2700 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2701 goto done;
2702
2703 /* Disable the entry if both ports was disabled */
2704 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2705 if (ret < 0)
2706 return ret;
2707 }
2708done:
2709 return 0;
2710}
2711
2712static int sh_eth_tsu_purge_all(struct net_device *ndev)
2713{
2714 struct sh_eth_private *mdp = netdev_priv(ndev);
2715 int i, ret;
2716
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002717 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002718 return 0;
2719
2720 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2721 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2722 continue;
2723
2724 /* Disable the entry if both ports was disabled */
2725 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2726 if (ret < 0)
2727 return ret;
2728 }
2729
2730 return 0;
2731}
2732
2733static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2734{
2735 struct sh_eth_private *mdp = netdev_priv(ndev);
2736 u8 addr[ETH_ALEN];
2737 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2738 int i;
2739
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002740 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002741 return;
2742
2743 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2744 sh_eth_tsu_read_entry(reg_offset, addr);
2745 if (is_multicast_ether_addr(addr))
2746 sh_eth_tsu_del_entry(ndev, addr);
2747 }
2748}
2749
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002750/* Update promiscuous flag and multicast filter */
2751static void sh_eth_set_rx_mode(struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002752{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002753 struct sh_eth_private *mdp = netdev_priv(ndev);
2754 u32 ecmr_bits;
2755 int mcast_all = 0;
2756 unsigned long flags;
2757
2758 spin_lock_irqsave(&mdp->lock, flags);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002759 /* Initial condition is MCT = 1, PRM = 0.
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002760 * Depending on ndev->flags, set PRM or clear MCT
2761 */
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002762 ecmr_bits = sh_eth_read(ndev, ECMR) & ~ECMR_PRM;
2763 if (mdp->cd->tsu)
2764 ecmr_bits |= ECMR_MCT;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002765
2766 if (!(ndev->flags & IFF_MULTICAST)) {
2767 sh_eth_tsu_purge_mcast(ndev);
2768 mcast_all = 1;
2769 }
2770 if (ndev->flags & IFF_ALLMULTI) {
2771 sh_eth_tsu_purge_mcast(ndev);
2772 ecmr_bits &= ~ECMR_MCT;
2773 mcast_all = 1;
2774 }
2775
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002776 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002777 sh_eth_tsu_purge_all(ndev);
2778 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2779 } else if (mdp->cd->tsu) {
2780 struct netdev_hw_addr *ha;
2781 netdev_for_each_mc_addr(ha, ndev) {
2782 if (mcast_all && is_multicast_ether_addr(ha->addr))
2783 continue;
2784
2785 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2786 if (!mcast_all) {
2787 sh_eth_tsu_purge_mcast(ndev);
2788 ecmr_bits &= ~ECMR_MCT;
2789 mcast_all = 1;
2790 }
2791 }
2792 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002793 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002794
2795 /* update the ethernet mode */
2796 sh_eth_write(ndev, ecmr_bits, ECMR);
2797
2798 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002799}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002800
2801static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2802{
2803 if (!mdp->port)
2804 return TSU_VTAG0;
2805 else
2806 return TSU_VTAG1;
2807}
2808
Patrick McHardy80d5c362013-04-19 02:04:28 +00002809static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2810 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002811{
2812 struct sh_eth_private *mdp = netdev_priv(ndev);
2813 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2814
2815 if (unlikely(!mdp->cd->tsu))
2816 return -EPERM;
2817
2818 /* No filtering if vid = 0 */
2819 if (!vid)
2820 return 0;
2821
2822 mdp->vlan_num_ids++;
2823
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002824 /* The controller has one VLAN tag HW filter. So, if the filter is
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002825 * already enabled, the driver disables it and the filte
2826 */
2827 if (mdp->vlan_num_ids > 1) {
2828 /* disable VLAN filter */
2829 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2830 return 0;
2831 }
2832
2833 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2834 vtag_reg_index);
2835
2836 return 0;
2837}
2838
Patrick McHardy80d5c362013-04-19 02:04:28 +00002839static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2840 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002841{
2842 struct sh_eth_private *mdp = netdev_priv(ndev);
2843 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2844
2845 if (unlikely(!mdp->cd->tsu))
2846 return -EPERM;
2847
2848 /* No filtering if vid = 0 */
2849 if (!vid)
2850 return 0;
2851
2852 mdp->vlan_num_ids--;
2853 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2854
2855 return 0;
2856}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002857
2858/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002859static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002860{
Simon Hormandb893472014-01-17 09:22:28 +09002861 if (sh_eth_is_rz_fast_ether(mdp)) {
2862 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2863 return;
2864 }
2865
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002866 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2867 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2868 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2869 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2870 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2871 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2872 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2873 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2874 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2875 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002876 if (sh_eth_is_gether(mdp)) {
2877 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2878 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2879 } else {
2880 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2881 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2882 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002883 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2884 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2885 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2886 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2887 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2888 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2889 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002890}
2891
2892/* MDIO bus release function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002893static int sh_mdio_release(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002894{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002895 /* unregister mdio bus */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002896 mdiobus_unregister(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002897
2898 /* free bitbang info */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002899 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002900
2901 return 0;
2902}
2903
2904/* MDIO bus init function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002905static int sh_mdio_init(struct sh_eth_private *mdp,
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002906 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002907{
2908 int ret, i;
2909 struct bb_info *bitbang;
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002910 struct platform_device *pdev = mdp->pdev;
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002911 struct device *dev = &mdp->pdev->dev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002912
2913 /* create bit control struct for PHY */
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002914 bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002915 if (!bitbang)
2916 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002917
2918 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002919 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002920 bitbang->set_gate = pd->set_mdio_gate;
Sergei Shtylyovdfed5e72013-03-21 10:37:54 +00002921 bitbang->mdi_msk = PIR_MDI;
2922 bitbang->mdo_msk = PIR_MDO;
2923 bitbang->mmd_msk = PIR_MMD;
2924 bitbang->mdc_msk = PIR_MDC;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002925 bitbang->ctrl.ops = &bb_ops;
2926
Stefan Weilc2e07b32010-08-03 19:44:52 +02002927 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002928 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002929 if (!mdp->mii_bus)
2930 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002931
2932 /* Hook up MII support for ethtool */
2933 mdp->mii_bus->name = "sh_mii";
Laurent Pincharta5bd60602014-03-20 15:00:32 +01002934 mdp->mii_bus->parent = dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00002935 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002936 pdev->name, pdev->id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002937
2938 /* PHY IRQ */
Sergei Shtylyov86b5d252014-05-13 02:30:14 +04002939 mdp->mii_bus->irq = devm_kmalloc_array(dev, PHY_MAX_ADDR, sizeof(int),
2940 GFP_KERNEL);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002941 if (!mdp->mii_bus->irq) {
2942 ret = -ENOMEM;
2943 goto out_free_bus;
2944 }
2945
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002946 /* register MDIO bus */
2947 if (dev->of_node) {
2948 ret = of_mdiobus_register(mdp->mii_bus, dev->of_node);
Ben Dooks702eca02014-03-12 17:47:40 +00002949 } else {
2950 for (i = 0; i < PHY_MAX_ADDR; i++)
2951 mdp->mii_bus->irq[i] = PHY_POLL;
2952 if (pd->phy_irq > 0)
2953 mdp->mii_bus->irq[pd->phy] = pd->phy_irq;
2954
2955 ret = mdiobus_register(mdp->mii_bus);
2956 }
2957
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002958 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002959 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002960
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002961 return 0;
2962
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002963out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002964 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002965 return ret;
2966}
2967
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002968static const u16 *sh_eth_get_register_offset(int register_type)
2969{
2970 const u16 *reg_offset = NULL;
2971
2972 switch (register_type) {
2973 case SH_ETH_REG_GIGABIT:
2974 reg_offset = sh_eth_offset_gigabit;
2975 break;
Simon Hormandb893472014-01-17 09:22:28 +09002976 case SH_ETH_REG_FAST_RZ:
2977 reg_offset = sh_eth_offset_fast_rz;
2978 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00002979 case SH_ETH_REG_FAST_RCAR:
2980 reg_offset = sh_eth_offset_fast_rcar;
2981 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002982 case SH_ETH_REG_FAST_SH4:
2983 reg_offset = sh_eth_offset_fast_sh4;
2984 break;
2985 case SH_ETH_REG_FAST_SH3_SH2:
2986 reg_offset = sh_eth_offset_fast_sh3_sh2;
2987 break;
2988 default:
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002989 break;
2990 }
2991
2992 return reg_offset;
2993}
2994
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002995static const struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002996 .ndo_open = sh_eth_open,
2997 .ndo_stop = sh_eth_close,
2998 .ndo_start_xmit = sh_eth_start_xmit,
2999 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00003000 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00003001 .ndo_tx_timeout = sh_eth_tx_timeout,
3002 .ndo_do_ioctl = sh_eth_do_ioctl,
3003 .ndo_validate_addr = eth_validate_addr,
3004 .ndo_set_mac_address = eth_mac_addr,
3005 .ndo_change_mtu = eth_change_mtu,
3006};
3007
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003008static const struct net_device_ops sh_eth_netdev_ops_tsu = {
3009 .ndo_open = sh_eth_open,
3010 .ndo_stop = sh_eth_close,
3011 .ndo_start_xmit = sh_eth_start_xmit,
3012 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00003013 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003014 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
3015 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
3016 .ndo_tx_timeout = sh_eth_tx_timeout,
3017 .ndo_do_ioctl = sh_eth_do_ioctl,
3018 .ndo_validate_addr = eth_validate_addr,
3019 .ndo_set_mac_address = eth_mac_addr,
3020 .ndo_change_mtu = eth_change_mtu,
3021};
3022
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003023#ifdef CONFIG_OF
3024static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
3025{
3026 struct device_node *np = dev->of_node;
3027 struct sh_eth_plat_data *pdata;
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003028 const char *mac_addr;
3029
3030 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
3031 if (!pdata)
3032 return NULL;
3033
3034 pdata->phy_interface = of_get_phy_mode(np);
3035
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003036 mac_addr = of_get_mac_address(np);
3037 if (mac_addr)
3038 memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
3039
3040 pdata->no_ether_link =
3041 of_property_read_bool(np, "renesas,no-ether-link");
3042 pdata->ether_link_active_low =
3043 of_property_read_bool(np, "renesas,ether-link-active-low");
3044
3045 return pdata;
3046}
3047
3048static const struct of_device_id sh_eth_match_table[] = {
3049 { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data },
3050 { .compatible = "renesas,ether-r8a7778", .data = &r8a777x_data },
3051 { .compatible = "renesas,ether-r8a7779", .data = &r8a777x_data },
3052 { .compatible = "renesas,ether-r8a7790", .data = &r8a779x_data },
3053 { .compatible = "renesas,ether-r8a7791", .data = &r8a779x_data },
Hisashi Nakamura9488e1e2014-11-13 15:59:07 +09003054 { .compatible = "renesas,ether-r8a7793", .data = &r8a779x_data },
Hisashi Nakamura0f76b9d2014-08-01 17:03:00 +02003055 { .compatible = "renesas,ether-r8a7794", .data = &r8a779x_data },
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003056 { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data },
3057 { }
3058};
3059MODULE_DEVICE_TABLE(of, sh_eth_match_table);
3060#else
3061static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
3062{
3063 return NULL;
3064}
3065#endif
3066
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003067static int sh_eth_drv_probe(struct platform_device *pdev)
3068{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07003069 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003070 struct resource *res;
3071 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00003072 struct sh_eth_private *mdp = NULL;
Jingoo Han0b76b862013-08-30 14:00:11 +09003073 struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev);
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003074 const struct platform_device_id *id = platform_get_device_id(pdev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003075
3076 /* get base addr */
3077 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003078
3079 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
Laurent Pinchartf738a132014-03-20 15:00:35 +01003080 if (!ndev)
3081 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003082
Ben Dooksb5893a02014-03-21 12:09:14 +01003083 pm_runtime_enable(&pdev->dev);
3084 pm_runtime_get_sync(&pdev->dev);
3085
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003086 devno = pdev->id;
3087 if (devno < 0)
3088 devno = 0;
3089
3090 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02003091 ret = platform_get_irq(pdev, 0);
Sergei Shtylyov7a468ac2015-08-28 16:56:01 +03003092 if (ret < 0)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003093 goto out_release;
roel kluincc3c0802008-09-10 19:22:44 +02003094 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003095
3096 SET_NETDEV_DEV(ndev, &pdev->dev);
3097
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003098 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00003099 mdp->num_tx_ring = TX_RING_SIZE;
3100 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00003101 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
3102 if (IS_ERR(mdp->addr)) {
3103 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00003104 goto out_release;
3105 }
3106
Varka Bhadramc9608042014-10-24 07:42:09 +05303107 ndev->base_addr = res->start;
3108
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003109 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00003110 mdp->pdev = pdev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003111
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003112 if (pdev->dev.of_node)
3113 pd = sh_eth_parse_dt(&pdev->dev);
Sergei Shtylyov3b4c5cb2013-10-30 23:30:19 +03003114 if (!pd) {
3115 dev_err(&pdev->dev, "no platform data\n");
3116 ret = -EINVAL;
3117 goto out_release;
3118 }
3119
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003120 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04003121 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00003122 mdp->phy_interface = pd->phy_interface;
Yoshinori Sato71557a32008-08-06 19:49:00 -04003123 /* EDMAC endian */
3124 mdp->edmac_endian = pd->edmac_endian;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00003125 mdp->no_ether_link = pd->no_ether_link;
3126 mdp->ether_link_active_low = pd->ether_link_active_low;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003127
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003128 /* set cpu data */
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003129 if (id) {
3130 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
3131 } else {
3132 const struct of_device_id *match;
3133
3134 match = of_match_device(of_match_ptr(sh_eth_match_table),
3135 &pdev->dev);
3136 mdp->cd = (struct sh_eth_cpu_data *)match->data;
3137 }
Sergei Shtylyova3153d82013-08-18 03:11:28 +04003138 mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type);
Sergei Shtylyov264be2f2014-03-15 03:11:24 +03003139 if (!mdp->reg_offset) {
3140 dev_err(&pdev->dev, "Unknown register type (%d)\n",
3141 mdp->cd->register_type);
3142 ret = -EINVAL;
3143 goto out_release;
3144 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003145 sh_eth_set_default_cpu_data(mdp->cd);
3146
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003147 /* set function */
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003148 if (mdp->cd->tsu)
3149 ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
3150 else
3151 ndev->netdev_ops = &sh_eth_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003152 ndev->ethtool_ops = &sh_eth_ethtool_ops;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003153 ndev->watchdog_timeo = TX_TIMEOUT;
3154
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00003155 /* debug message level */
3156 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003157
3158 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00003159 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00003160 if (!is_valid_ether_addr(ndev->dev_addr)) {
3161 dev_warn(&pdev->dev,
3162 "no valid MAC address supplied, using a random one.\n");
3163 eth_hw_addr_random(ndev);
3164 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003165
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003166 /* ioremap the TSU registers */
3167 if (mdp->cd->tsu) {
3168 struct resource *rtsu;
3169 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00003170 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
3171 if (IS_ERR(mdp->tsu_addr)) {
3172 ret = PTR_ERR(mdp->tsu_addr);
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00003173 goto out_release;
3174 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00003175 mdp->port = devno % 2;
Patrick McHardyf6469682013-04-19 02:04:27 +00003176 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003177 }
3178
Yoshihiro Shimoda150647f2012-02-15 17:54:56 +00003179 /* initialize first or needed device */
3180 if (!devno || pd->needs_init) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003181 if (mdp->cd->chip_reset)
3182 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003183
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00003184 if (mdp->cd->tsu) {
3185 /* TSU init (Init only)*/
3186 sh_eth_tsu_init(mdp);
3187 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003188 }
3189
Hisashi Nakamura966d6db2014-11-13 15:54:05 +09003190 if (mdp->cd->rmiimode)
3191 sh_eth_write(ndev, 0x1, RMIIMODE);
3192
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003193 /* MDIO bus init */
3194 ret = sh_mdio_init(mdp, pd);
3195 if (ret) {
3196 dev_err(&ndev->dev, "failed to initialise MDIO\n");
3197 goto out_release;
3198 }
3199
Sergei Shtylyov37191092013-06-19 23:30:23 +04003200 netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
3201
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003202 /* network device register */
3203 ret = register_netdev(ndev);
3204 if (ret)
Sergei Shtylyov37191092013-06-19 23:30:23 +04003205 goto out_napi_del;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003206
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003207 /* print device information */
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +03003208 netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n",
3209 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003210
Ben Dooksb5893a02014-03-21 12:09:14 +01003211 pm_runtime_put(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003212 platform_set_drvdata(pdev, ndev);
3213
3214 return ret;
3215
Sergei Shtylyov37191092013-06-19 23:30:23 +04003216out_napi_del:
3217 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003218 sh_mdio_release(mdp);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003219
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003220out_release:
3221 /* net_dev free */
3222 if (ndev)
3223 free_netdev(ndev);
3224
Ben Dooksb5893a02014-03-21 12:09:14 +01003225 pm_runtime_put(&pdev->dev);
3226 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003227 return ret;
3228}
3229
3230static int sh_eth_drv_remove(struct platform_device *pdev)
3231{
3232 struct net_device *ndev = platform_get_drvdata(pdev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003233 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003234
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003235 unregister_netdev(ndev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003236 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003237 sh_mdio_release(mdp);
Magnus Dammbcd51492009-10-09 00:20:04 +00003238 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003239 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003240
3241 return 0;
3242}
3243
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003244#ifdef CONFIG_PM
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003245#ifdef CONFIG_PM_SLEEP
3246static int sh_eth_suspend(struct device *dev)
3247{
3248 struct net_device *ndev = dev_get_drvdata(dev);
3249 int ret = 0;
3250
3251 if (netif_running(ndev)) {
3252 netif_device_detach(ndev);
3253 ret = sh_eth_close(ndev);
3254 }
3255
3256 return ret;
3257}
3258
3259static int sh_eth_resume(struct device *dev)
3260{
3261 struct net_device *ndev = dev_get_drvdata(dev);
3262 int ret = 0;
3263
3264 if (netif_running(ndev)) {
3265 ret = sh_eth_open(ndev);
3266 if (ret < 0)
3267 return ret;
3268 netif_device_attach(ndev);
3269 }
3270
3271 return ret;
3272}
3273#endif
3274
Magnus Dammbcd51492009-10-09 00:20:04 +00003275static int sh_eth_runtime_nop(struct device *dev)
3276{
Sergei Shtylyov128296f2014-01-03 15:52:22 +03003277 /* Runtime PM callback shared between ->runtime_suspend()
Magnus Dammbcd51492009-10-09 00:20:04 +00003278 * and ->runtime_resume(). Simply returns success.
3279 *
3280 * This driver re-initializes all registers after
3281 * pm_runtime_get_sync() anyway so there is no need
3282 * to save and restore registers here.
3283 */
3284 return 0;
3285}
3286
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003287static const struct dev_pm_ops sh_eth_dev_pm_ops = {
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003288 SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend, sh_eth_resume)
Mikhail Ulyanove7d7e892015-01-22 01:18:44 +03003289 SET_RUNTIME_PM_OPS(sh_eth_runtime_nop, sh_eth_runtime_nop, NULL)
Magnus Dammbcd51492009-10-09 00:20:04 +00003290};
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003291#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3292#else
3293#define SH_ETH_PM_OPS NULL
3294#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00003295
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003296static struct platform_device_id sh_eth_id_table[] = {
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00003297 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00003298 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +00003299 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003300 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
Sergei Shtylyov24549e22013-06-07 13:59:21 +00003301 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
3302 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003303 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
Simon Hormandb893472014-01-17 09:22:28 +09003304 { "r7s72100-ether", (kernel_ulong_t)&r7s72100_data },
Sergei Shtylyove5c9b4c2013-06-07 13:57:12 +00003305 { "r8a7740-gether", (kernel_ulong_t)&r8a7740_data },
Sergei Shtylyov589ebde2013-06-07 14:05:59 +00003306 { "r8a777x-ether", (kernel_ulong_t)&r8a777x_data },
Sergei Shtylyov94a12b12013-12-08 02:59:18 +03003307 { "r8a7790-ether", (kernel_ulong_t)&r8a779x_data },
3308 { "r8a7791-ether", (kernel_ulong_t)&r8a779x_data },
Hisashi Nakamura9488e1e2014-11-13 15:59:07 +09003309 { "r8a7793-ether", (kernel_ulong_t)&r8a779x_data },
Hisashi Nakamura0f76b9d2014-08-01 17:03:00 +02003310 { "r8a7794-ether", (kernel_ulong_t)&r8a779x_data },
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003311 { }
3312};
3313MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
3314
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003315static struct platform_driver sh_eth_driver = {
3316 .probe = sh_eth_drv_probe,
3317 .remove = sh_eth_drv_remove,
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003318 .id_table = sh_eth_id_table,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003319 .driver = {
3320 .name = CARDNAME,
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003321 .pm = SH_ETH_PM_OPS,
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003322 .of_match_table = of_match_ptr(sh_eth_match_table),
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003323 },
3324};
3325
Axel Lindb62f682011-11-27 16:44:17 +00003326module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003327
3328MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3329MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3330MODULE_LICENSE("GPL v2");