blob: 6fb75a1d68b9cfc1550fbae9528ac50a154dbf4d [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.
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03006 * Copyright (C) 2013-2016 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
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300431static void sh_eth_modify(struct net_device *ndev, int enum_index, u32 clear,
432 u32 set)
433{
434 sh_eth_write(ndev, (sh_eth_read(ndev, enum_index) & ~clear) | set,
435 enum_index);
436}
437
Simon Horman504c8ca2014-01-17 09:22:27 +0900438static bool sh_eth_is_gether(struct sh_eth_private *mdp)
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000439{
Simon Horman504c8ca2014-01-17 09:22:27 +0900440 return mdp->reg_offset == sh_eth_offset_gigabit;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000441}
442
Simon Hormandb893472014-01-17 09:22:28 +0900443static bool sh_eth_is_rz_fast_ether(struct sh_eth_private *mdp)
444{
445 return mdp->reg_offset == sh_eth_offset_fast_rz;
446}
447
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400448static void sh_eth_select_mii(struct net_device *ndev)
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000449{
450 u32 value = 0x0;
451 struct sh_eth_private *mdp = netdev_priv(ndev);
452
453 switch (mdp->phy_interface) {
454 case PHY_INTERFACE_MODE_GMII:
455 value = 0x2;
456 break;
457 case PHY_INTERFACE_MODE_MII:
458 value = 0x1;
459 break;
460 case PHY_INTERFACE_MODE_RMII:
461 value = 0x0;
462 break;
463 default:
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300464 netdev_warn(ndev,
465 "PHY interface mode was not setup. Set to MII.\n");
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000466 value = 0x1;
467 break;
468 }
469
470 sh_eth_write(ndev, value, RMII_MII);
471}
Nobuhiro Iwamatsu5e7a76b2012-06-25 17:34:14 +0000472
Sergei Shtylyov8e994402013-06-12 03:07:29 +0400473static void sh_eth_set_duplex(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000474{
475 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000476
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300477 sh_eth_modify(ndev, ECMR, ECMR_DM, mdp->duplex ? ECMR_DM : 0);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000478}
479
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100480static void sh_eth_chip_reset(struct net_device *ndev)
481{
482 struct sh_eth_private *mdp = netdev_priv(ndev);
483
484 /* reset device */
485 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
486 mdelay(1);
487}
488
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100489static void sh_eth_set_rate_gether(struct net_device *ndev)
490{
491 struct sh_eth_private *mdp = netdev_priv(ndev);
492
493 switch (mdp->speed) {
494 case 10: /* 10BASE */
495 sh_eth_write(ndev, GECMR_10, GECMR);
496 break;
497 case 100:/* 100BASE */
498 sh_eth_write(ndev, GECMR_100, GECMR);
499 break;
500 case 1000: /* 1000BASE */
501 sh_eth_write(ndev, GECMR_1000, GECMR);
502 break;
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100503 }
504}
505
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100506#ifdef CONFIG_OF
507/* R7S72100 */
508static struct sh_eth_cpu_data r7s72100_data = {
509 .chip_reset = sh_eth_chip_reset,
510 .set_duplex = sh_eth_set_duplex,
511
512 .register_type = SH_ETH_REG_FAST_RZ,
513
514 .ecsr_value = ECSR_ICD,
515 .ecsipr_value = ECSIPR_ICDIP,
516 .eesipr_value = 0xff7f009f,
517
518 .tx_check = EESR_TC1 | EESR_FTC,
519 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
520 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
521 EESR_TDE | EESR_ECI,
522 .fdr_value = 0x0000070f,
523
524 .no_psr = 1,
525 .apr = 1,
526 .mpr = 1,
527 .tpauser = 1,
528 .hw_swap = 1,
529 .rpadir = 1,
530 .rpadir_value = 2 << 16,
531 .no_trimd = 1,
532 .no_ade = 1,
533 .hw_crc = 1,
534 .tsu = 1,
535 .shift_rd0 = 1,
536};
Geert Uytterhoevena0f48be2015-11-24 15:40:59 +0100537
538static void sh_eth_chip_reset_r8a7740(struct net_device *ndev)
539{
540 struct sh_eth_private *mdp = netdev_priv(ndev);
541
542 /* reset device */
543 sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
544 mdelay(1);
545
546 sh_eth_select_mii(ndev);
547}
548
549/* R8A7740 */
550static struct sh_eth_cpu_data r8a7740_data = {
551 .chip_reset = sh_eth_chip_reset_r8a7740,
552 .set_duplex = sh_eth_set_duplex,
553 .set_rate = sh_eth_set_rate_gether,
554
555 .register_type = SH_ETH_REG_GIGABIT,
556
557 .ecsr_value = ECSR_ICD | ECSR_MPD,
558 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
559 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
560
561 .tx_check = EESR_TC1 | EESR_FTC,
562 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
563 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
564 EESR_TDE | EESR_ECI,
565 .fdr_value = 0x0000070f,
566
567 .apr = 1,
568 .mpr = 1,
569 .tpauser = 1,
570 .bculr = 1,
571 .hw_swap = 1,
572 .rpadir = 1,
573 .rpadir_value = 2 << 16,
574 .no_trimd = 1,
575 .no_ade = 1,
576 .tsu = 1,
577 .select_mii = 1,
578 .shift_rd0 = 1,
579};
Geert Uytterhoeven99f84be62015-11-24 15:40:57 +0100580
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000581/* There is CPU dependent code */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000582static void sh_eth_set_rate_r8a777x(struct net_device *ndev)
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000583{
584 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000585
586 switch (mdp->speed) {
587 case 10: /* 10BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300588 sh_eth_modify(ndev, ECMR, ECMR_ELB, 0);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000589 break;
590 case 100:/* 100BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300591 sh_eth_modify(ndev, ECMR, ECMR_ELB, ECMR_ELB);
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000592 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000593 }
594}
595
Sergei Shtylyov674853b2013-04-27 10:44:24 +0000596/* R8A7778/9 */
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000597static struct sh_eth_cpu_data r8a777x_data = {
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000598 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov589ebde2013-06-07 14:05:59 +0000599 .set_rate = sh_eth_set_rate_r8a777x,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000600
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400601 .register_type = SH_ETH_REG_FAST_RCAR,
602
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000603 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
604 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
605 .eesipr_value = 0x01ff009f,
606
607 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400608 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
609 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
610 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900611 .fdr_value = 0x00000f0f,
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000612
613 .apr = 1,
614 .mpr = 1,
615 .tpauser = 1,
616 .hw_swap = 1,
617};
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000618
Sergei Shtylyov94a12b12013-12-08 02:59:18 +0300619/* R8A7790/1 */
620static struct sh_eth_cpu_data r8a779x_data = {
Simon Hormane18dbf72013-07-23 10:18:05 +0900621 .set_duplex = sh_eth_set_duplex,
622 .set_rate = sh_eth_set_rate_r8a777x,
623
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400624 .register_type = SH_ETH_REG_FAST_RCAR,
625
Simon Hormane18dbf72013-07-23 10:18:05 +0900626 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
627 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
628 .eesipr_value = 0x01ff009f,
629
630 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Laurent Pinchartba361cb2013-07-31 16:42:11 +0900631 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
632 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
633 EESR_ECI,
Nobuhiro Iwamatsud407bc02015-01-07 14:40:15 +0900634 .fdr_value = 0x00000f0f,
Simon Hormane18dbf72013-07-23 10:18:05 +0900635
Geert Uytterhoeven01fbd3f2015-01-15 11:52:19 +0100636 .trscer_err_mask = DESC_I_RINT8,
637
Simon Hormane18dbf72013-07-23 10:18:05 +0900638 .apr = 1,
639 .mpr = 1,
640 .tpauser = 1,
641 .hw_swap = 1,
642 .rmiimode = 1,
643};
Geert Uytterhoevenc74a2242015-11-24 15:40:58 +0100644#endif /* CONFIG_OF */
Simon Hormane18dbf72013-07-23 10:18:05 +0900645
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000646static void sh_eth_set_rate_sh7724(struct net_device *ndev)
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000647{
648 struct sh_eth_private *mdp = netdev_priv(ndev);
649
650 switch (mdp->speed) {
651 case 10: /* 10BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300652 sh_eth_modify(ndev, ECMR, ECMR_RTM, 0);
Sergei Shtylyova3f109b2013-03-28 11:51:31 +0000653 break;
654 case 100:/* 100BASE */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300655 sh_eth_modify(ndev, ECMR, ECMR_RTM, ECMR_RTM);
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000656 break;
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000657 }
658}
659
660/* SH7724 */
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000661static struct sh_eth_cpu_data sh7724_data = {
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000662 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +0000663 .set_rate = sh_eth_set_rate_sh7724,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000664
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400665 .register_type = SH_ETH_REG_FAST_SH4,
666
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000667 .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
668 .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
Sergei Shtylyova80c3de2013-06-20 02:24:54 +0400669 .eesipr_value = 0x01ff009f,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000670
671 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400672 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
673 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
674 EESR_ECI,
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000675
676 .apr = 1,
677 .mpr = 1,
678 .tpauser = 1,
679 .hw_swap = 1,
Magnus Damm503914c2009-12-15 21:16:55 -0800680 .rpadir = 1,
681 .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000682};
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000683
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000684static void sh_eth_set_rate_sh7757(struct net_device *ndev)
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000685{
686 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000687
688 switch (mdp->speed) {
689 case 10: /* 10BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000690 sh_eth_write(ndev, 0, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000691 break;
692 case 100:/* 100BASE */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000693 sh_eth_write(ndev, 1, RTRATE);
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000694 break;
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000695 }
696}
697
698/* SH7757 */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000699static struct sh_eth_cpu_data sh7757_data = {
700 .set_duplex = sh_eth_set_duplex,
701 .set_rate = sh_eth_set_rate_sh7757,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000702
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400703 .register_type = SH_ETH_REG_FAST_SH4,
704
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000705 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000706
707 .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400708 .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RFE |
709 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
710 EESR_ECI,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000711
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000712 .irq_flags = IRQF_SHARED,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000713 .apr = 1,
714 .mpr = 1,
715 .tpauser = 1,
716 .hw_swap = 1,
717 .no_ade = 1,
Yoshihiro Shimoda2e98e792011-07-05 20:33:57 +0000718 .rpadir = 1,
719 .rpadir_value = 2 << 16,
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +0000720 .rtrate = 1,
Yoshihiro Shimodaf29a3d02010-07-05 18:32:50 +0000721};
Yoshihiro Shimoda65ac8852009-05-24 23:54:30 +0000722
David S. Millere403d292013-06-07 23:40:41 -0700723#define SH_GIGA_ETH_BASE 0xfee00000UL
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000724#define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
725#define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
726static void sh_eth_chip_reset_giga(struct net_device *ndev)
727{
728 int i;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +0100729 u32 mahr[2], malr[2];
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000730
731 /* save MAHR and MALR */
732 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000733 malr[i] = ioread32((void *)GIGA_MALR(i));
734 mahr[i] = ioread32((void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000735 }
736
737 /* reset device */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000738 iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000739 mdelay(1);
740
741 /* restore MAHR and MALR */
742 for (i = 0; i < 2; i++) {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +0000743 iowrite32(malr[i], (void *)GIGA_MALR(i));
744 iowrite32(mahr[i], (void *)GIGA_MAHR(i));
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000745 }
746}
747
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000748static void sh_eth_set_rate_giga(struct net_device *ndev)
749{
750 struct sh_eth_private *mdp = netdev_priv(ndev);
751
752 switch (mdp->speed) {
753 case 10: /* 10BASE */
754 sh_eth_write(ndev, 0x00000000, GECMR);
755 break;
756 case 100:/* 100BASE */
757 sh_eth_write(ndev, 0x00000010, GECMR);
758 break;
759 case 1000: /* 1000BASE */
760 sh_eth_write(ndev, 0x00000020, GECMR);
761 break;
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000762 }
763}
764
765/* SH7757(GETHERC) */
Sergei Shtylyov24549e22013-06-07 13:59:21 +0000766static struct sh_eth_cpu_data sh7757_data_giga = {
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000767 .chip_reset = sh_eth_chip_reset_giga,
Nobuhiro Iwamatsu04b0ed22013-06-06 09:45:25 +0000768 .set_duplex = sh_eth_set_duplex,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000769 .set_rate = sh_eth_set_rate_giga,
770
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400771 .register_type = SH_ETH_REG_GIGABIT,
772
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000773 .ecsr_value = ECSR_ICD | ECSR_MPD,
774 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
775 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
776
777 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400778 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
779 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
780 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000781 .fdr_value = 0x0000072f,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000782
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +0000783 .irq_flags = IRQF_SHARED,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000784 .apr = 1,
785 .mpr = 1,
786 .tpauser = 1,
787 .bculr = 1,
788 .hw_swap = 1,
789 .rpadir = 1,
790 .rpadir_value = 2 << 16,
791 .no_trimd = 1,
792 .no_ade = 1,
Yoshihiro Shimoda3acbc972012-02-15 17:54:51 +0000793 .tsu = 1,
Yoshihiro Shimoda8fcd4962011-03-07 21:59:49 +0000794};
795
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000796/* SH7734 */
797static struct sh_eth_cpu_data sh7734_data = {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000798 .chip_reset = sh_eth_chip_reset,
799 .set_duplex = sh_eth_set_duplex,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000800 .set_rate = sh_eth_set_rate_gether,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000801
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400802 .register_type = SH_ETH_REG_GIGABIT,
803
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000804 .ecsr_value = ECSR_ICD | ECSR_MPD,
805 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
806 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
807
808 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyovca8c3582013-06-21 01:12:21 +0400809 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
810 EESR_RFE | EESR_RDE | EESR_RFRMER | EESR_TFE |
811 EESR_TDE | EESR_ECI,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000812
813 .apr = 1,
814 .mpr = 1,
815 .tpauser = 1,
816 .bculr = 1,
817 .hw_swap = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000818 .no_trimd = 1,
819 .no_ade = 1,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000820 .tsu = 1,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000821 .hw_crc = 1,
822 .select_mii = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000823};
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000824
825/* SH7763 */
826static struct sh_eth_cpu_data sh7763_data = {
827 .chip_reset = sh_eth_chip_reset,
828 .set_duplex = sh_eth_set_duplex,
829 .set_rate = sh_eth_set_rate_gether,
830
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400831 .register_type = SH_ETH_REG_GIGABIT,
832
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000833 .ecsr_value = ECSR_ICD | ECSR_MPD,
834 .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
835 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
836
837 .tx_check = EESR_TC1 | EESR_FTC,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300838 .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT |
839 EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE |
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000840 EESR_ECI,
Sergei Shtylyovf5d12762013-06-07 13:58:18 +0000841
842 .apr = 1,
843 .mpr = 1,
844 .tpauser = 1,
845 .bculr = 1,
846 .hw_swap = 1,
847 .no_trimd = 1,
848 .no_ade = 1,
849 .tsu = 1,
850 .irq_flags = IRQF_SHARED,
851};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000852
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +0000853static struct sh_eth_cpu_data sh7619_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400854 .register_type = SH_ETH_REG_FAST_SH3_SH2,
855
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000856 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
857
858 .apr = 1,
859 .mpr = 1,
860 .tpauser = 1,
861 .hw_swap = 1,
862};
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +0000863
864static struct sh_eth_cpu_data sh771x_data = {
Sergei Shtylyova3153d82013-08-18 03:11:28 +0400865 .register_type = SH_ETH_REG_FAST_SH3_SH2,
866
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000867 .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +0000868 .tsu = 1,
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000869};
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000870
871static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
872{
873 if (!cd->ecsr_value)
874 cd->ecsr_value = DEFAULT_ECSR_INIT;
875
876 if (!cd->ecsipr_value)
877 cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
878
879 if (!cd->fcftr_value)
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300880 cd->fcftr_value = DEFAULT_FIFO_F_D_RFF |
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000881 DEFAULT_FIFO_F_D_RFD;
882
883 if (!cd->fdr_value)
884 cd->fdr_value = DEFAULT_FDR_INIT;
885
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000886 if (!cd->tx_check)
887 cd->tx_check = DEFAULT_TX_CHECK;
888
889 if (!cd->eesr_err_check)
890 cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +0900891
892 if (!cd->trscer_err_mask)
893 cd->trscer_err_mask = DEFAULT_TRSCER_ERR_MASK;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000894}
895
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000896static int sh_eth_check_reset(struct net_device *ndev)
897{
898 int ret = 0;
899 int cnt = 100;
900
901 while (cnt > 0) {
902 if (!(sh_eth_read(ndev, EDMR) & 0x3))
903 break;
904 mdelay(1);
905 cnt--;
906 }
Sergei Shtylyov9f8c4262013-06-05 23:54:01 +0400907 if (cnt <= 0) {
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +0300908 netdev_err(ndev, "Device reset failed\n");
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +0000909 ret = -ETIMEDOUT;
910 }
911 return ret;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000912}
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000913
914static int sh_eth_reset(struct net_device *ndev)
915{
916 struct sh_eth_private *mdp = netdev_priv(ndev);
917 int ret = 0;
918
Simon Hormandb893472014-01-17 09:22:28 +0900919 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp)) {
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000920 sh_eth_write(ndev, EDSR_ENALL, EDSR);
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300921 sh_eth_modify(ndev, EDMR, EDMR_SRST_GETHER, EDMR_SRST_GETHER);
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000922
923 ret = sh_eth_check_reset(ndev);
924 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +0100925 return ret;
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000926
927 /* Table Init */
928 sh_eth_write(ndev, 0x0, TDLAR);
929 sh_eth_write(ndev, 0x0, TDFAR);
930 sh_eth_write(ndev, 0x0, TDFXR);
931 sh_eth_write(ndev, 0x0, TDFFR);
932 sh_eth_write(ndev, 0x0, RDLAR);
933 sh_eth_write(ndev, 0x0, RDFAR);
934 sh_eth_write(ndev, 0x0, RDFXR);
935 sh_eth_write(ndev, 0x0, RDFFR);
936
937 /* Reset HW CRC register */
938 if (mdp->cd->hw_crc)
939 sh_eth_write(ndev, 0x0, CSMR);
940
941 /* Select MII mode */
942 if (mdp->cd->select_mii)
943 sh_eth_select_mii(ndev);
944 } else {
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300945 sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, EDMR_SRST_ETHER);
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000946 mdelay(3);
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +0300947 sh_eth_modify(ndev, EDMR, EDMR_SRST_ETHER, 0);
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000948 }
949
Nobuhiro Iwamatsudabdde92013-06-06 09:51:39 +0000950 return ret;
951}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000952
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000953static void sh_eth_set_receive_align(struct sk_buff *skb)
954{
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900955 uintptr_t reserve = (uintptr_t)skb->data & (SH_ETH_RX_ALIGN - 1);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000956
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000957 if (reserve)
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +0900958 skb_reserve(skb, SH_ETH_RX_ALIGN - reserve);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000959}
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +0000960
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300961/* Program the hardware MAC address from dev->dev_addr. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700962static void update_mac_address(struct net_device *ndev)
963{
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000964 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300965 (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
966 (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +0000967 sh_eth_write(ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300968 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700969}
970
Sergei Shtylyov128296f2014-01-03 15:52:22 +0300971/* Get MAC address from SuperH MAC address register
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700972 *
973 * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
974 * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
975 * When you want use this device, you must set MAC address in bootloader.
976 *
977 */
Magnus Damm748031f2009-10-09 00:17:14 +0000978static void read_mac_address(struct net_device *ndev, unsigned char *mac)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700979{
Magnus Damm748031f2009-10-09 00:17:14 +0000980 if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
Joe Perchesd458cdf2013-10-01 19:04:40 -0700981 memcpy(ndev->dev_addr, mac, ETH_ALEN);
Magnus Damm748031f2009-10-09 00:17:14 +0000982 } else {
Sergei Shtylyov37742f02015-12-05 00:58:57 +0300983 u32 mahr = sh_eth_read(ndev, MAHR);
984 u32 malr = sh_eth_read(ndev, MALR);
985
986 ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
987 ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
988 ndev->dev_addr[2] = (mahr >> 8) & 0xFF;
989 ndev->dev_addr[3] = (mahr >> 0) & 0xFF;
990 ndev->dev_addr[4] = (malr >> 8) & 0xFF;
991 ndev->dev_addr[5] = (malr >> 0) & 0xFF;
Magnus Damm748031f2009-10-09 00:17:14 +0000992 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -0700993}
994
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +0100995static u32 sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000996{
Simon Hormandb893472014-01-17 09:22:28 +0900997 if (sh_eth_is_gether(mdp) || sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +0000998 return EDTRR_TRNS_GETHER;
999 else
1000 return EDTRR_TRNS_ETHER;
1001}
1002
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001003struct bb_info {
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001004 void (*set_gate)(void *addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001005 struct mdiobb_ctrl ctrl;
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00001006 void *addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001007};
1008
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001009static void sh_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001010{
1011 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001012 u32 pir;
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001013
1014 if (bitbang->set_gate)
1015 bitbang->set_gate(bitbang->addr);
1016
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001017 pir = ioread32(bitbang->addr);
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001018 if (set)
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001019 pir |= mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001020 else
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001021 pir &= ~mask;
1022 iowrite32(pir, bitbang->addr);
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001023}
1024
1025/* Data I/O pin control */
1026static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1027{
1028 sh_mdio_ctrl(ctrl, PIR_MMD, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001029}
1030
1031/* Set bit data*/
1032static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
1033{
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001034 sh_mdio_ctrl(ctrl, PIR_MDO, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001035}
1036
1037/* Get bit data*/
1038static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
1039{
1040 struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00001041
1042 if (bitbang->set_gate)
1043 bitbang->set_gate(bitbang->addr);
1044
Sergei Shtylyov78fa3c52015-12-08 00:41:43 +03001045 return (ioread32(bitbang->addr) & PIR_MDI) != 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001046}
1047
1048/* MDC pin control */
1049static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
1050{
Sergei Shtylyov39b4b062015-12-08 00:40:57 +03001051 sh_mdio_ctrl(ctrl, PIR_MDC, bit);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001052}
1053
1054/* mdio bus control struct */
1055static struct mdiobb_ops bb_ops = {
1056 .owner = THIS_MODULE,
1057 .set_mdc = sh_mdc_ctrl,
1058 .set_mdio_dir = sh_mmd_ctrl,
1059 .set_mdio_data = sh_set_mdio,
1060 .get_mdio_data = sh_get_mdio,
1061};
1062
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001063/* free skb and descriptor buffer */
1064static void sh_eth_ring_free(struct net_device *ndev)
1065{
1066 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001067 int ringsize, i;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001068
1069 /* Free Rx skb ringbuffer */
1070 if (mdp->rx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001071 for (i = 0; i < mdp->num_rx_ring; i++)
1072 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001073 }
1074 kfree(mdp->rx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001075 mdp->rx_skbuff = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001076
1077 /* Free Tx skb ringbuffer */
1078 if (mdp->tx_skbuff) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04001079 for (i = 0; i < mdp->num_tx_ring; i++)
1080 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001081 }
1082 kfree(mdp->tx_skbuff);
Yoshihiro Shimoda91c77552012-06-26 20:00:01 +00001083 mdp->tx_skbuff = NULL;
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001084
1085 if (mdp->rx_ring) {
1086 ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
1087 dma_free_coherent(NULL, ringsize, mdp->rx_ring,
1088 mdp->rx_desc_dma);
1089 mdp->rx_ring = NULL;
1090 }
1091
1092 if (mdp->tx_ring) {
1093 ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
1094 dma_free_coherent(NULL, ringsize, mdp->tx_ring,
1095 mdp->tx_desc_dma);
1096 mdp->tx_ring = NULL;
1097 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001098}
1099
1100/* format skb and descriptor buffer */
1101static void sh_eth_ring_format(struct net_device *ndev)
1102{
1103 struct sh_eth_private *mdp = netdev_priv(ndev);
1104 int i;
1105 struct sk_buff *skb;
1106 struct sh_eth_rxdesc *rxdesc = NULL;
1107 struct sh_eth_txdesc *txdesc = NULL;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001108 int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
1109 int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
Sergei Shtylyovcb368592015-10-24 00:46:40 +03001110 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001111 dma_addr_t dma_addr;
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001112 u32 buf_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001113
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001114 mdp->cur_rx = 0;
1115 mdp->cur_tx = 0;
1116 mdp->dirty_rx = 0;
1117 mdp->dirty_tx = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001118
1119 memset(mdp->rx_ring, 0, rx_ringsize);
1120
1121 /* build Rx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001122 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001123 /* skb */
1124 mdp->rx_skbuff[i] = NULL;
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001125 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001126 if (skb == NULL)
1127 break;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001128 sh_eth_set_receive_align(skb);
1129
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001130 /* RX descriptor */
1131 rxdesc = &mdp->rx_ring[i];
Sergei Shtylyovab857912015-10-24 00:46:03 +03001132 /* The size of the buffer is a multiple of 32 bytes. */
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001133 buf_len = ALIGN(mdp->rx_buf_sz, 32);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001134 rxdesc->len = cpu_to_le32(buf_len << 16);
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001135 dma_addr = dma_map_single(&ndev->dev, skb->data, buf_len,
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001136 DMA_FROM_DEVICE);
1137 if (dma_mapping_error(&ndev->dev, dma_addr)) {
1138 kfree_skb(skb);
1139 break;
1140 }
1141 mdp->rx_skbuff[i] = skb;
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001142 rxdesc->addr = cpu_to_le32(dma_addr);
1143 rxdesc->status = cpu_to_le32(RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001144
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001145 /* Rx descriptor address set */
1146 if (i == 0) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001147 sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001148 if (sh_eth_is_gether(mdp) ||
1149 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001150 sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001151 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001152 }
1153
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001154 mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001155
1156 /* Mark the last entry as wrapping the ring. */
Sergei Shtylyovc1b7fca2016-03-08 01:36:28 +03001157 if (rxdesc)
1158 rxdesc->status |= cpu_to_le32(RD_RDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001159
1160 memset(mdp->tx_ring, 0, tx_ringsize);
1161
1162 /* build Tx ring buffer */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001163 for (i = 0; i < mdp->num_tx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001164 mdp->tx_skbuff[i] = NULL;
1165 txdesc = &mdp->tx_ring[i];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001166 txdesc->status = cpu_to_le32(TD_TFP);
1167 txdesc->len = cpu_to_le32(0);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001168 if (i == 0) {
Yoshinori Sato71557a32008-08-06 19:49:00 -04001169 /* Tx descriptor address set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001170 sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
Simon Hormandb893472014-01-17 09:22:28 +09001171 if (sh_eth_is_gether(mdp) ||
1172 sh_eth_is_rz_fast_ether(mdp))
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001173 sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001174 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001175 }
1176
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001177 txdesc->status |= cpu_to_le32(TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001178}
1179
1180/* Get skb and descriptor buffer */
1181static int sh_eth_ring_init(struct net_device *ndev)
1182{
1183 struct sh_eth_private *mdp = netdev_priv(ndev);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001184 int rx_ringsize, tx_ringsize;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001185
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001186 /* +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001187 * card needs room to do 8 byte alignment, +2 so we can reserve
1188 * the first 2 bytes, and +16 gets room for the status word from the
1189 * card.
1190 */
1191 mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
1192 (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
Magnus Damm503914c2009-12-15 21:16:55 -08001193 if (mdp->cd->rpadir)
1194 mdp->rx_buf_sz += NET_IP_ALIGN;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001195
1196 /* Allocate RX and TX skb rings */
Sergei Shtylyov2c94e852015-10-31 02:05:56 +03001197 mdp->rx_skbuff = kcalloc(mdp->num_rx_ring, sizeof(*mdp->rx_skbuff),
1198 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001199 if (!mdp->rx_skbuff)
1200 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001201
Sergei Shtylyov2c94e852015-10-31 02:05:56 +03001202 mdp->tx_skbuff = kcalloc(mdp->num_tx_ring, sizeof(*mdp->tx_skbuff),
1203 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001204 if (!mdp->tx_skbuff)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001205 goto ring_free;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001206
1207 /* Allocate all Rx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001208 rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001209 mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001210 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001211 if (!mdp->rx_ring)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001212 goto ring_free;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001213
1214 mdp->dirty_rx = 0;
1215
1216 /* Allocate all Tx descriptors. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001217 tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001218 mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
Joe Perchesd0320f72013-03-14 13:07:21 +00001219 GFP_KERNEL);
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001220 if (!mdp->tx_ring)
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001221 goto ring_free;
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001222 return 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001223
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03001224ring_free:
1225 /* Free Rx and Tx skb ring buffer and DMA buffer */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001226 sh_eth_ring_free(ndev);
1227
Sergei Shtylyov91d80682015-11-04 00:17:08 +03001228 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001229}
1230
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001231static int sh_eth_dev_init(struct net_device *ndev, bool start)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001232{
1233 int ret = 0;
1234 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001235
1236 /* Soft Reset */
Nobuhiro Iwamatsu5cee1d32012-06-25 17:35:12 +00001237 ret = sh_eth_reset(ndev);
1238 if (ret)
Laurent Pinchartf738a132014-03-20 15:00:35 +01001239 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001240
Simon Horman55754f12013-07-23 10:18:04 +09001241 if (mdp->cd->rmiimode)
1242 sh_eth_write(ndev, 0x1, RMIIMODE);
1243
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001244 /* Descriptor format */
1245 sh_eth_ring_format(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001246 if (mdp->cd->rpadir)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001247 sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001248
1249 /* all sh_eth int mask */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001250 sh_eth_write(ndev, 0, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001251
Yoshihiro Shimoda10b91942012-03-29 19:32:08 +00001252#if defined(__LITTLE_ENDIAN)
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001253 if (mdp->cd->hw_swap)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001254 sh_eth_write(ndev, EDMR_EL, EDMR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001255 else
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001256#endif
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001257 sh_eth_write(ndev, 0, EDMR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001258
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001259 /* FIFO size set */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001260 sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
1261 sh_eth_write(ndev, 0, TFTR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001262
Ben Dooks530aa2d2014-06-03 12:21:13 +01001263 /* Frame recv control (enable multiple-packets per rx irq) */
1264 sh_eth_write(ndev, RMCR_RNC, RMCR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001265
Nobuhiro Iwamatsub284fbe2015-01-08 15:25:07 +09001266 sh_eth_write(ndev, mdp->cd->trscer_err_mask, TRSCER);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001267
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001268 if (mdp->cd->bculr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001269 sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001270
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001271 sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001272
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001273 if (!mdp->cd->no_trimd)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001274 sh_eth_write(ndev, 0, TRIMD);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001275
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001276 /* Recv frame limit set register */
Yoshihiro Shimodafdb37a72012-02-06 23:55:15 +00001277 sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
1278 RFLR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001279
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001280 sh_eth_modify(ndev, EESR, 0, 0);
Ben Hutchings283e38d2015-01-22 12:44:08 +00001281 if (start) {
1282 mdp->irq_enabled = true;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001283 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00001284 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001285
1286 /* PAUSE Prohibition */
Sergei Shtylyovbffa7312016-01-11 00:28:14 +03001287 sh_eth_write(ndev, ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) |
1288 ECMR_TE | ECMR_RE, ECMR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001289
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001290 if (mdp->cd->set_rate)
1291 mdp->cd->set_rate(ndev);
1292
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001293 /* E-MAC Status Register clear */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001294 sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001295
1296 /* E-MAC Interrupt Enable register */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001297 if (start)
1298 sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001299
1300 /* Set MAC address */
1301 update_mac_address(ndev);
1302
1303 /* mask reset */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001304 if (mdp->cd->apr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001305 sh_eth_write(ndev, APR_AP, APR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001306 if (mdp->cd->mpr)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001307 sh_eth_write(ndev, MPR_MP, MPR);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001308 if (mdp->cd->tpauser)
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001309 sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001310
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001311 if (start) {
1312 /* Setting the Rx mode will start the Rx process. */
1313 sh_eth_write(ndev, EDRRR_R, EDRRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001314
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001315 netif_start_queue(ndev);
1316 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001317
1318 return ret;
1319}
1320
Ben Hutchings740c7f32015-01-27 00:49:32 +00001321static void sh_eth_dev_exit(struct net_device *ndev)
1322{
1323 struct sh_eth_private *mdp = netdev_priv(ndev);
1324 int i;
1325
1326 /* Deactivate all TX descriptors, so DMA should stop at next
1327 * packet boundary if it's currently running
1328 */
1329 for (i = 0; i < mdp->num_tx_ring; i++)
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001330 mdp->tx_ring[i].status &= ~cpu_to_le32(TD_TACT);
Ben Hutchings740c7f32015-01-27 00:49:32 +00001331
1332 /* Disable TX FIFO egress to MAC */
1333 sh_eth_rcv_snd_disable(ndev);
1334
1335 /* Stop RX DMA at next packet boundary */
1336 sh_eth_write(ndev, 0, EDRRR);
1337
1338 /* Aside from TX DMA, we can't tell when the hardware is
1339 * really stopped, so we need to reset to make sure.
1340 * Before doing that, wait for long enough to *probably*
1341 * finish transmitting the last packet and poll stats.
1342 */
1343 msleep(2); /* max frame time at 10 Mbps < 1250 us */
1344 sh_eth_get_stats(ndev);
1345 sh_eth_reset(ndev);
Geert Uytterhoevena14c7d12015-02-27 17:16:26 +01001346
1347 /* Set MAC address again */
1348 update_mac_address(ndev);
Ben Hutchings740c7f32015-01-27 00:49:32 +00001349}
1350
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001351/* free Tx skb function */
1352static int sh_eth_txfree(struct net_device *ndev)
1353{
1354 struct sh_eth_private *mdp = netdev_priv(ndev);
1355 struct sh_eth_txdesc *txdesc;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001356 int free_num = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001357 int entry = 0;
1358
1359 for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001360 entry = mdp->dirty_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001361 txdesc = &mdp->tx_ring[entry];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001362 if (txdesc->status & cpu_to_le32(TD_TACT))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001363 break;
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001364 /* TACT bit must be checked before all the following reads */
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001365 dma_rmb();
Ben Hutchingse5fd13f2015-02-26 20:34:46 +00001366 netif_info(mdp, tx_done, ndev,
1367 "tx entry %d status 0x%08x\n",
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001368 entry, le32_to_cpu(txdesc->status));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001369 /* Free the original skb. */
1370 if (mdp->tx_skbuff[entry]) {
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001371 dma_unmap_single(&ndev->dev, le32_to_cpu(txdesc->addr),
1372 le32_to_cpu(txdesc->len) >> 16,
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001373 DMA_TO_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001374 dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
1375 mdp->tx_skbuff[entry] = NULL;
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001376 free_num++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001377 }
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001378 txdesc->status = cpu_to_le32(TD_TFP);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001379 if (entry >= mdp->num_tx_ring - 1)
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001380 txdesc->status |= cpu_to_le32(TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001381
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001382 ndev->stats.tx_packets++;
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001383 ndev->stats.tx_bytes += le32_to_cpu(txdesc->len) >> 16;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001384 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001385 return free_num;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001386}
1387
1388/* Packet receive function */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001389static int sh_eth_rx(struct net_device *ndev, u32 intr_status, int *quota)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001390{
1391 struct sh_eth_private *mdp = netdev_priv(ndev);
1392 struct sh_eth_rxdesc *rxdesc;
1393
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001394 int entry = mdp->cur_rx % mdp->num_rx_ring;
1395 int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001396 int limit;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001397 struct sk_buff *skb;
1398 u16 pkt_len = 0;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001399 u32 desc_status;
Sergei Shtylyovcb368592015-10-24 00:46:40 +03001400 int skbuff_size = mdp->rx_buf_sz + SH_ETH_RX_ALIGN + 32 - 1;
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001401 dma_addr_t dma_addr;
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001402 u32 buf_len;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001403
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001404 boguscnt = min(boguscnt, *quota);
1405 limit = boguscnt;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001406 rxdesc = &mdp->rx_ring[entry];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001407 while (!(rxdesc->status & cpu_to_le32(RD_RACT))) {
Ben Hutchings7d7355f2015-03-03 00:52:00 +00001408 /* RACT bit must be checked before all the following reads */
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001409 dma_rmb();
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001410 desc_status = le32_to_cpu(rxdesc->status);
1411 pkt_len = le32_to_cpu(rxdesc->len) & RD_RFL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001412
1413 if (--boguscnt < 0)
1414 break;
1415
Ben Hutchingse5fd13f2015-02-26 20:34:46 +00001416 netif_info(mdp, rx_status, ndev,
1417 "rx entry %d status 0x%08x len %d\n",
1418 entry, desc_status, pkt_len);
1419
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001420 if (!(desc_status & RDFEND))
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001421 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001422
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001423 /* In case of almost all GETHER/ETHERs, the Receive Frame State
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001424 * (RFS) bits in the Receive Descriptor 0 are from bit 9 to
Ben Hutchings9b4a6362015-03-03 00:52:39 +00001425 * bit 0. However, in case of the R8A7740 and R7S72100
1426 * the RFS bits are from bit 25 to bit 16. So, the
Simon Hormandb893472014-01-17 09:22:28 +09001427 * driver needs right shifting by 16.
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001428 */
Sergei Shtylyovac8025a2013-06-13 22:12:45 +04001429 if (mdp->cd->shift_rd0)
1430 desc_status >>= 16;
Yoshihiro Shimodadd019892013-06-13 10:15:45 +09001431
Sergei Shtylyov248be832015-12-04 01:45:40 +03001432 skb = mdp->rx_skbuff[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001433 if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
1434 RD_RFS5 | RD_RFS6 | RD_RFS10)) {
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001435 ndev->stats.rx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001436 if (desc_status & RD_RFS1)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001437 ndev->stats.rx_crc_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001438 if (desc_status & RD_RFS2)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001439 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001440 if (desc_status & RD_RFS3)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001441 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001442 if (desc_status & RD_RFS4)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001443 ndev->stats.rx_length_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001444 if (desc_status & RD_RFS6)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001445 ndev->stats.rx_missed_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001446 if (desc_status & RD_RFS10)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001447 ndev->stats.rx_over_errors++;
Sergei Shtylyov248be832015-12-04 01:45:40 +03001448 } else if (skb) {
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001449 dma_addr = le32_to_cpu(rxdesc->addr);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001450 if (!mdp->cd->hw_swap)
1451 sh_eth_soft_swap(
Sergei Shtylyov12996532015-12-13 23:05:07 +03001452 phys_to_virt(ALIGN(dma_addr, 4)),
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001453 pkt_len + 2);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001454 mdp->rx_skbuff[entry] = NULL;
Magnus Damm503914c2009-12-15 21:16:55 -08001455 if (mdp->cd->rpadir)
1456 skb_reserve(skb, NET_IP_ALIGN);
Sergei Shtylyov12996532015-12-13 23:05:07 +03001457 dma_unmap_single(&ndev->dev, dma_addr,
Sergei Shtylyovab857912015-10-24 00:46:03 +03001458 ALIGN(mdp->rx_buf_sz, 32),
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001459 DMA_FROM_DEVICE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001460 skb_put(skb, pkt_len);
1461 skb->protocol = eth_type_trans(skb, ndev);
Sergei Shtylyova8e9fd02013-09-03 03:03:10 +04001462 netif_receive_skb(skb);
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001463 ndev->stats.rx_packets++;
1464 ndev->stats.rx_bytes += pkt_len;
Ben Hutchings25b77ad2015-02-26 20:33:30 +00001465 if (desc_status & RD_RFS8)
1466 ndev->stats.multicast++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001467 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001468 entry = (++mdp->cur_rx) % mdp->num_rx_ring;
Yoshihiro Shimoda862df492009-05-24 23:53:40 +00001469 rxdesc = &mdp->rx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001470 }
1471
1472 /* Refill the Rx ring buffers. */
1473 for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001474 entry = mdp->dirty_rx % mdp->num_rx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001475 rxdesc = &mdp->rx_ring[entry];
Sergei Shtylyovab857912015-10-24 00:46:03 +03001476 /* The size of the buffer is 32 byte boundary. */
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001477 buf_len = ALIGN(mdp->rx_buf_sz, 32);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001478 rxdesc->len = cpu_to_le32(buf_len << 16);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001479
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001480 if (mdp->rx_skbuff[entry] == NULL) {
Mitsuhiro Kimura4d6a9492014-11-27 20:34:00 +09001481 skb = netdev_alloc_skb(ndev, skbuff_size);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001482 if (skb == NULL)
1483 break; /* Better luck next round. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001484 sh_eth_set_receive_align(skb);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001485 dma_addr = dma_map_single(&ndev->dev, skb->data,
Sergei Shtylyov5cbf20c2015-12-20 01:48:04 +03001486 buf_len, DMA_FROM_DEVICE);
Ben Hutchings52b9fa32015-01-27 00:50:24 +00001487 if (dma_mapping_error(&ndev->dev, dma_addr)) {
1488 kfree_skb(skb);
1489 break;
1490 }
1491 mdp->rx_skbuff[entry] = skb;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001492
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001493 skb_checksum_none_assert(skb);
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001494 rxdesc->addr = cpu_to_le32(dma_addr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001495 }
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03001496 dma_wmb(); /* RACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00001497 if (entry >= mdp->num_rx_ring - 1)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001498 rxdesc->status |=
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001499 cpu_to_le32(RD_RACT | RD_RFP | RD_RDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001500 else
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03001501 rxdesc->status |= cpu_to_le32(RD_RACT | RD_RFP);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001502 }
1503
1504 /* Restart Rx engine if stopped. */
1505 /* If we don't need to check status, don't. -KDU */
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001506 if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
Yoshihiro Shimodaa18e08b2012-06-20 15:26:34 +00001507 /* fix the values for the next receiving if RDE is set */
Ben Hutchings33657112015-02-26 20:34:14 +00001508 if (intr_status & EESR_RDE &&
1509 mdp->reg_offset[RDFAR] != SH_ETH_OFFSET_INVALID) {
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001510 u32 count = (sh_eth_read(ndev, RDFAR) -
1511 sh_eth_read(ndev, RDLAR)) >> 4;
1512
1513 mdp->cur_rx = count;
1514 mdp->dirty_rx = count;
1515 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001516 sh_eth_write(ndev, EDRRR_R, EDRRR);
Yoshihiro Shimoda79fba9f2012-05-28 23:07:55 +00001517 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001518
Mitsuhiro Kimura319cd522014-12-09 21:23:42 +09001519 *quota -= limit - boguscnt - 1;
1520
Yoshihiro Shimoda4f809ce2014-06-10 09:40:14 +09001521 return *quota <= 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001522}
1523
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001524static void sh_eth_rcv_snd_disable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001525{
1526 /* disable tx and rx */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001527 sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001528}
1529
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001530static void sh_eth_rcv_snd_enable(struct net_device *ndev)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001531{
1532 /* enable tx and rx */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001533 sh_eth_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001534}
1535
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001536/* error control function */
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001537static void sh_eth_error(struct net_device *ndev, u32 intr_status)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001538{
1539 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001540 u32 felic_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001541 u32 link_stat;
1542 u32 mask;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001543
1544 if (intr_status & EESR_ECI) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001545 felic_stat = sh_eth_read(ndev, ECSR);
1546 sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001547 if (felic_stat & ECSR_ICD)
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001548 ndev->stats.tx_carrier_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001549 if (felic_stat & ECSR_LCHNG) {
1550 /* Link Changed */
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001551 if (mdp->cd->no_psr || mdp->no_ether_link) {
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001552 goto ignore_link;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001553 } else {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001554 link_stat = (sh_eth_read(ndev, PSR));
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00001555 if (mdp->ether_link_active_low)
1556 link_stat = ~link_stat;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001557 }
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001558 if (!(link_stat & PHY_ST_LINK)) {
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001559 sh_eth_rcv_snd_disable(ndev);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001560 } else {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001561 /* Link Up */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001562 sh_eth_modify(ndev, EESIPR, DMAC_M_ECI, 0);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001563 /* clear int */
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001564 sh_eth_modify(ndev, ECSR, 0, 0);
1565 sh_eth_modify(ndev, EESIPR, DMAC_M_ECI,
1566 DMAC_M_ECI);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001567 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001568 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001569 }
1570 }
1571 }
1572
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001573ignore_link:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001574 if (intr_status & EESR_TWB) {
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001575 /* Unused write back interrupt */
1576 if (intr_status & EESR_TABT) { /* Transmit Abort int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001577 ndev->stats.tx_aborted_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001578 netif_err(mdp, tx_err, ndev, "Transmit Abort\n");
Sergei Shtylyov4eb313a2013-06-21 01:13:42 +04001579 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001580 }
1581
1582 if (intr_status & EESR_RABT) {
1583 /* Receive Abort int */
1584 if (intr_status & EESR_RFRMER) {
1585 /* Receive Frame Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001586 ndev->stats.rx_frame_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001587 }
1588 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001589
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001590 if (intr_status & EESR_TDE) {
1591 /* Transmit Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001592 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001593 netif_err(mdp, tx_err, ndev, "Transmit Descriptor Empty\n");
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001594 }
1595
1596 if (intr_status & EESR_TFE) {
1597 /* FIFO under flow */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001598 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001599 netif_err(mdp, tx_err, ndev, "Transmit FIFO Under flow\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001600 }
1601
1602 if (intr_status & EESR_RDE) {
1603 /* Receive Descriptor Empty int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001604 ndev->stats.rx_over_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001605 }
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001606
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001607 if (intr_status & EESR_RFE) {
1608 /* Receive FIFO Overflow int */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001609 ndev->stats.rx_fifo_errors++;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001610 }
1611
1612 if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
1613 /* Address Error */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00001614 ndev->stats.tx_fifo_errors++;
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03001615 netif_err(mdp, tx_err, ndev, "Address Error\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001616 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001617
1618 mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
1619 if (mdp->cd->no_ade)
1620 mask &= ~EESR_ADE;
1621 if (intr_status & mask) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001622 /* Tx error */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001623 u32 edtrr = sh_eth_read(ndev, EDTRR);
Sergei Shtylyov090d5602014-01-11 02:41:49 +03001624
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001625 /* dmesg */
Sergei Shtylyovda246852014-03-15 03:29:14 +03001626 netdev_err(ndev, "TX error. status=%8.8x cur_tx=%8.8x dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
1627 intr_status, mdp->cur_tx, mdp->dirty_tx,
1628 (u32)ndev->state, edtrr);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001629 /* dirty buffer free */
1630 sh_eth_txfree(ndev);
1631
1632 /* SH7712 BUG */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001633 if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001634 /* tx dma start */
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00001635 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001636 }
1637 /* wakeup */
1638 netif_wake_queue(ndev);
1639 }
1640}
1641
1642static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
1643{
1644 struct net_device *ndev = netdev;
1645 struct sh_eth_private *mdp = netdev_priv(ndev);
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001646 struct sh_eth_cpu_data *cd = mdp->cd;
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001647 irqreturn_t ret = IRQ_NONE;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001648 u32 intr_status, intr_enable;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001649
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001650 spin_lock(&mdp->lock);
1651
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001652 /* Get interrupt status */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001653 intr_status = sh_eth_read(ndev, EESR);
Sergei Shtylyov3893b273452013-03-31 09:54:20 +00001654 /* Mask it with the interrupt mask, forcing ECI interrupt to be always
1655 * enabled since it's the one that comes thru regardless of the mask,
1656 * and we need to fully handle it in sh_eth_error() in order to quench
1657 * it as it doesn't get cleared by just writing 1 to the ECI bit...
1658 */
Sergei Shtylyov37191092013-06-19 23:30:23 +04001659 intr_enable = sh_eth_read(ndev, EESIPR);
1660 intr_status &= intr_enable | DMAC_M_ECI;
1661 if (intr_status & (EESR_RX_CHECK | cd->tx_check | cd->eesr_err_check))
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001662 ret = IRQ_HANDLED;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001663 else
Ben Hutchings283e38d2015-01-22 12:44:08 +00001664 goto out;
1665
1666 if (!likely(mdp->irq_enabled)) {
1667 sh_eth_write(ndev, 0, EESIPR);
1668 goto out;
1669 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001670
Sergei Shtylyov37191092013-06-19 23:30:23 +04001671 if (intr_status & EESR_RX_CHECK) {
1672 if (napi_schedule_prep(&mdp->napi)) {
1673 /* Mask Rx interrupts */
1674 sh_eth_write(ndev, intr_enable & ~EESR_RX_CHECK,
1675 EESIPR);
1676 __napi_schedule(&mdp->napi);
1677 } else {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001678 netdev_warn(ndev,
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001679 "ignoring interrupt, status 0x%08x, mask 0x%08x.\n",
Sergei Shtylyovda246852014-03-15 03:29:14 +03001680 intr_status, intr_enable);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001681 }
1682 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001683
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09001684 /* Tx Check */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001685 if (intr_status & cd->tx_check) {
Sergei Shtylyov37191092013-06-19 23:30:23 +04001686 /* Clear Tx interrupts */
1687 sh_eth_write(ndev, intr_status & cd->tx_check, EESR);
1688
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001689 sh_eth_txfree(ndev);
1690 netif_wake_queue(ndev);
1691 }
1692
Sergei Shtylyov37191092013-06-19 23:30:23 +04001693 if (intr_status & cd->eesr_err_check) {
1694 /* Clear error interrupts */
1695 sh_eth_write(ndev, intr_status & cd->eesr_err_check, EESR);
1696
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001697 sh_eth_error(ndev, intr_status);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001698 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001699
Ben Hutchings283e38d2015-01-22 12:44:08 +00001700out:
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001701 spin_unlock(&mdp->lock);
1702
Nobuhiro Iwamatsu0e0fde32009-03-16 19:50:57 +00001703 return ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001704}
1705
Sergei Shtylyov37191092013-06-19 23:30:23 +04001706static int sh_eth_poll(struct napi_struct *napi, int budget)
1707{
1708 struct sh_eth_private *mdp = container_of(napi, struct sh_eth_private,
1709 napi);
1710 struct net_device *ndev = napi->dev;
1711 int quota = budget;
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01001712 u32 intr_status;
Sergei Shtylyov37191092013-06-19 23:30:23 +04001713
1714 for (;;) {
1715 intr_status = sh_eth_read(ndev, EESR);
1716 if (!(intr_status & EESR_RX_CHECK))
1717 break;
1718 /* Clear Rx interrupts */
1719 sh_eth_write(ndev, intr_status & EESR_RX_CHECK, EESR);
1720
1721 if (sh_eth_rx(ndev, intr_status, &quota))
1722 goto out;
1723 }
1724
1725 napi_complete(napi);
1726
1727 /* Reenable Rx interrupts */
Ben Hutchings283e38d2015-01-22 12:44:08 +00001728 if (mdp->irq_enabled)
1729 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
Sergei Shtylyov37191092013-06-19 23:30:23 +04001730out:
1731 return budget - quota;
1732}
1733
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001734/* PHY state control function */
1735static void sh_eth_adjust_link(struct net_device *ndev)
1736{
1737 struct sh_eth_private *mdp = netdev_priv(ndev);
1738 struct phy_device *phydev = mdp->phydev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001739 int new_state = 0;
1740
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001741 if (phydev->link) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001742 if (phydev->duplex != mdp->duplex) {
1743 new_state = 1;
1744 mdp->duplex = phydev->duplex;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001745 if (mdp->cd->set_duplex)
1746 mdp->cd->set_duplex(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001747 }
1748
1749 if (phydev->speed != mdp->speed) {
1750 new_state = 1;
1751 mdp->speed = phydev->speed;
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001752 if (mdp->cd->set_rate)
1753 mdp->cd->set_rate(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001754 }
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001755 if (!mdp->link) {
Sergei Shtylyovb2b14d22016-02-10 01:38:28 +03001756 sh_eth_modify(ndev, ECMR, ECMR_TXF, 0);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001757 new_state = 1;
1758 mdp->link = phydev->link;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001759 if (mdp->cd->no_psr || mdp->no_ether_link)
1760 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001761 }
1762 } else if (mdp->link) {
1763 new_state = 1;
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001764 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001765 mdp->speed = 0;
1766 mdp->duplex = -1;
Sergei Shtylyov1e1b8122013-03-31 09:50:07 +00001767 if (mdp->cd->no_psr || mdp->no_ether_link)
1768 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001769 }
1770
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001771 if (new_state && netif_msg_link(mdp))
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001772 phy_print_status(phydev);
1773}
1774
1775/* PHY init function */
1776static int sh_eth_phy_init(struct net_device *ndev)
1777{
Ben Dooks702eca02014-03-12 17:47:40 +00001778 struct device_node *np = ndev->dev.parent->of_node;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001779 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001780 struct phy_device *phydev = NULL;
1781
Sergei Shtylyov3340d2a2013-03-31 10:11:04 +00001782 mdp->link = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001783 mdp->speed = 0;
1784 mdp->duplex = -1;
1785
1786 /* Try connect to PHY */
Ben Dooks702eca02014-03-12 17:47:40 +00001787 if (np) {
1788 struct device_node *pn;
1789
1790 pn = of_parse_phandle(np, "phy-handle", 0);
1791 phydev = of_phy_connect(ndev, pn,
1792 sh_eth_adjust_link, 0,
1793 mdp->phy_interface);
1794
1795 if (!phydev)
1796 phydev = ERR_PTR(-ENOENT);
1797 } else {
1798 char phy_id[MII_BUS_ID_SIZE + 3];
1799
1800 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
1801 mdp->mii_bus->id, mdp->phy_id);
1802
1803 phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
1804 mdp->phy_interface);
1805 }
1806
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001807 if (IS_ERR(phydev)) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03001808 netdev_err(ndev, "failed to connect PHY\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001809 return PTR_ERR(phydev);
1810 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00001811
Andrew Lunn22209432016-01-06 20:11:13 +01001812 phy_attached_info(phydev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001813
1814 mdp->phydev = phydev;
1815
1816 return 0;
1817}
1818
1819/* PHY control start function */
1820static int sh_eth_phy_start(struct net_device *ndev)
1821{
1822 struct sh_eth_private *mdp = netdev_priv(ndev);
1823 int ret;
1824
1825 ret = sh_eth_phy_init(ndev);
1826 if (ret)
1827 return ret;
1828
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07001829 phy_start(mdp->phydev);
1830
1831 return 0;
1832}
1833
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001834static int sh_eth_get_settings(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001835 struct ethtool_cmd *ecmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001836{
1837 struct sh_eth_private *mdp = netdev_priv(ndev);
1838 unsigned long flags;
1839 int ret;
1840
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001841 if (!mdp->phydev)
1842 return -ENODEV;
1843
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001844 spin_lock_irqsave(&mdp->lock, flags);
1845 ret = phy_ethtool_gset(mdp->phydev, ecmd);
1846 spin_unlock_irqrestore(&mdp->lock, flags);
1847
1848 return ret;
1849}
1850
1851static int sh_eth_set_settings(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03001852 struct ethtool_cmd *ecmd)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001853{
1854 struct sh_eth_private *mdp = netdev_priv(ndev);
1855 unsigned long flags;
1856 int ret;
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001857
Ben Hutchings4f9dce232015-01-16 17:51:25 +00001858 if (!mdp->phydev)
1859 return -ENODEV;
1860
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001861 spin_lock_irqsave(&mdp->lock, flags);
1862
1863 /* disable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001864 sh_eth_rcv_snd_disable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001865
1866 ret = phy_ethtool_sset(mdp->phydev, ecmd);
1867 if (ret)
1868 goto error_exit;
1869
1870 if (ecmd->duplex == DUPLEX_FULL)
1871 mdp->duplex = 1;
1872 else
1873 mdp->duplex = 0;
1874
1875 if (mdp->cd->set_duplex)
1876 mdp->cd->set_duplex(ndev);
1877
1878error_exit:
1879 mdelay(1);
1880
1881 /* enable tx and rx */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00001882 sh_eth_rcv_snd_enable(ndev);
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00001883
1884 spin_unlock_irqrestore(&mdp->lock, flags);
1885
1886 return ret;
1887}
1888
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00001889/* If it is ever necessary to increase SH_ETH_REG_DUMP_MAX_REGS, the
1890 * version must be bumped as well. Just adding registers up to that
1891 * limit is fine, as long as the existing register indices don't
1892 * change.
1893 */
1894#define SH_ETH_REG_DUMP_VERSION 1
1895#define SH_ETH_REG_DUMP_MAX_REGS 256
1896
1897static size_t __sh_eth_get_regs(struct net_device *ndev, u32 *buf)
1898{
1899 struct sh_eth_private *mdp = netdev_priv(ndev);
1900 struct sh_eth_cpu_data *cd = mdp->cd;
1901 u32 *valid_map;
1902 size_t len;
1903
1904 BUILD_BUG_ON(SH_ETH_MAX_REGISTER_OFFSET > SH_ETH_REG_DUMP_MAX_REGS);
1905
1906 /* Dump starts with a bitmap that tells ethtool which
1907 * registers are defined for this chip.
1908 */
1909 len = DIV_ROUND_UP(SH_ETH_REG_DUMP_MAX_REGS, 32);
1910 if (buf) {
1911 valid_map = buf;
1912 buf += len;
1913 } else {
1914 valid_map = NULL;
1915 }
1916
1917 /* Add a register to the dump, if it has a defined offset.
1918 * This automatically skips most undefined registers, but for
1919 * some it is also necessary to check a capability flag in
1920 * struct sh_eth_cpu_data.
1921 */
1922#define mark_reg_valid(reg) valid_map[reg / 32] |= 1U << (reg % 32)
1923#define add_reg_from(reg, read_expr) do { \
1924 if (mdp->reg_offset[reg] != SH_ETH_OFFSET_INVALID) { \
1925 if (buf) { \
1926 mark_reg_valid(reg); \
1927 *buf++ = read_expr; \
1928 } \
1929 ++len; \
1930 } \
1931 } while (0)
1932#define add_reg(reg) add_reg_from(reg, sh_eth_read(ndev, reg))
1933#define add_tsu_reg(reg) add_reg_from(reg, sh_eth_tsu_read(mdp, reg))
1934
1935 add_reg(EDSR);
1936 add_reg(EDMR);
1937 add_reg(EDTRR);
1938 add_reg(EDRRR);
1939 add_reg(EESR);
1940 add_reg(EESIPR);
1941 add_reg(TDLAR);
1942 add_reg(TDFAR);
1943 add_reg(TDFXR);
1944 add_reg(TDFFR);
1945 add_reg(RDLAR);
1946 add_reg(RDFAR);
1947 add_reg(RDFXR);
1948 add_reg(RDFFR);
1949 add_reg(TRSCER);
1950 add_reg(RMFCR);
1951 add_reg(TFTR);
1952 add_reg(FDR);
1953 add_reg(RMCR);
1954 add_reg(TFUCR);
1955 add_reg(RFOCR);
1956 if (cd->rmiimode)
1957 add_reg(RMIIMODE);
1958 add_reg(FCFTR);
1959 if (cd->rpadir)
1960 add_reg(RPADIR);
1961 if (!cd->no_trimd)
1962 add_reg(TRIMD);
1963 add_reg(ECMR);
1964 add_reg(ECSR);
1965 add_reg(ECSIPR);
1966 add_reg(PIR);
1967 if (!cd->no_psr)
1968 add_reg(PSR);
1969 add_reg(RDMLR);
1970 add_reg(RFLR);
1971 add_reg(IPGR);
1972 if (cd->apr)
1973 add_reg(APR);
1974 if (cd->mpr)
1975 add_reg(MPR);
1976 add_reg(RFCR);
1977 add_reg(RFCF);
1978 if (cd->tpauser)
1979 add_reg(TPAUSER);
1980 add_reg(TPAUSECR);
1981 add_reg(GECMR);
1982 if (cd->bculr)
1983 add_reg(BCULR);
1984 add_reg(MAHR);
1985 add_reg(MALR);
1986 add_reg(TROCR);
1987 add_reg(CDCR);
1988 add_reg(LCCR);
1989 add_reg(CNDCR);
1990 add_reg(CEFCR);
1991 add_reg(FRECR);
1992 add_reg(TSFRCR);
1993 add_reg(TLFRCR);
1994 add_reg(CERCR);
1995 add_reg(CEECR);
1996 add_reg(MAFCR);
1997 if (cd->rtrate)
1998 add_reg(RTRATE);
1999 if (cd->hw_crc)
2000 add_reg(CSMR);
2001 if (cd->select_mii)
2002 add_reg(RMII_MII);
2003 add_reg(ARSTR);
2004 if (cd->tsu) {
2005 add_tsu_reg(TSU_CTRST);
2006 add_tsu_reg(TSU_FWEN0);
2007 add_tsu_reg(TSU_FWEN1);
2008 add_tsu_reg(TSU_FCM);
2009 add_tsu_reg(TSU_BSYSL0);
2010 add_tsu_reg(TSU_BSYSL1);
2011 add_tsu_reg(TSU_PRISL0);
2012 add_tsu_reg(TSU_PRISL1);
2013 add_tsu_reg(TSU_FWSL0);
2014 add_tsu_reg(TSU_FWSL1);
2015 add_tsu_reg(TSU_FWSLC);
2016 add_tsu_reg(TSU_QTAG0);
2017 add_tsu_reg(TSU_QTAG1);
2018 add_tsu_reg(TSU_QTAGM0);
2019 add_tsu_reg(TSU_QTAGM1);
2020 add_tsu_reg(TSU_FWSR);
2021 add_tsu_reg(TSU_FWINMK);
2022 add_tsu_reg(TSU_ADQT0);
2023 add_tsu_reg(TSU_ADQT1);
2024 add_tsu_reg(TSU_VTAG0);
2025 add_tsu_reg(TSU_VTAG1);
2026 add_tsu_reg(TSU_ADSBSY);
2027 add_tsu_reg(TSU_TEN);
2028 add_tsu_reg(TSU_POST1);
2029 add_tsu_reg(TSU_POST2);
2030 add_tsu_reg(TSU_POST3);
2031 add_tsu_reg(TSU_POST4);
2032 if (mdp->reg_offset[TSU_ADRH0] != SH_ETH_OFFSET_INVALID) {
2033 /* This is the start of a table, not just a single
2034 * register.
2035 */
2036 if (buf) {
2037 unsigned int i;
2038
2039 mark_reg_valid(TSU_ADRH0);
2040 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES * 2; i++)
2041 *buf++ = ioread32(
2042 mdp->tsu_addr +
2043 mdp->reg_offset[TSU_ADRH0] +
2044 i * 4);
2045 }
2046 len += SH_ETH_TSU_CAM_ENTRIES * 2;
2047 }
2048 }
2049
2050#undef mark_reg_valid
2051#undef add_reg_from
2052#undef add_reg
2053#undef add_tsu_reg
2054
2055 return len * 4;
2056}
2057
2058static int sh_eth_get_regs_len(struct net_device *ndev)
2059{
2060 return __sh_eth_get_regs(ndev, NULL);
2061}
2062
2063static void sh_eth_get_regs(struct net_device *ndev, struct ethtool_regs *regs,
2064 void *buf)
2065{
2066 struct sh_eth_private *mdp = netdev_priv(ndev);
2067
2068 regs->version = SH_ETH_REG_DUMP_VERSION;
2069
2070 pm_runtime_get_sync(&mdp->pdev->dev);
2071 __sh_eth_get_regs(ndev, buf);
2072 pm_runtime_put_sync(&mdp->pdev->dev);
2073}
2074
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002075static int sh_eth_nway_reset(struct net_device *ndev)
2076{
2077 struct sh_eth_private *mdp = netdev_priv(ndev);
2078 unsigned long flags;
2079 int ret;
2080
Ben Hutchings4f9dce232015-01-16 17:51:25 +00002081 if (!mdp->phydev)
2082 return -ENODEV;
2083
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002084 spin_lock_irqsave(&mdp->lock, flags);
2085 ret = phy_start_aneg(mdp->phydev);
2086 spin_unlock_irqrestore(&mdp->lock, flags);
2087
2088 return ret;
2089}
2090
2091static u32 sh_eth_get_msglevel(struct net_device *ndev)
2092{
2093 struct sh_eth_private *mdp = netdev_priv(ndev);
2094 return mdp->msg_enable;
2095}
2096
2097static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
2098{
2099 struct sh_eth_private *mdp = netdev_priv(ndev);
2100 mdp->msg_enable = value;
2101}
2102
2103static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
2104 "rx_current", "tx_current",
2105 "rx_dirty", "tx_dirty",
2106};
2107#define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
2108
2109static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
2110{
2111 switch (sset) {
2112 case ETH_SS_STATS:
2113 return SH_ETH_STATS_LEN;
2114 default:
2115 return -EOPNOTSUPP;
2116 }
2117}
2118
2119static void sh_eth_get_ethtool_stats(struct net_device *ndev,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002120 struct ethtool_stats *stats, u64 *data)
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002121{
2122 struct sh_eth_private *mdp = netdev_priv(ndev);
2123 int i = 0;
2124
2125 /* device-specific stats */
2126 data[i++] = mdp->cur_rx;
2127 data[i++] = mdp->cur_tx;
2128 data[i++] = mdp->dirty_rx;
2129 data[i++] = mdp->dirty_tx;
2130}
2131
2132static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
2133{
2134 switch (stringset) {
2135 case ETH_SS_STATS:
2136 memcpy(data, *sh_eth_gstrings_stats,
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002137 sizeof(sh_eth_gstrings_stats));
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002138 break;
2139 }
2140}
2141
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002142static void sh_eth_get_ringparam(struct net_device *ndev,
2143 struct ethtool_ringparam *ring)
2144{
2145 struct sh_eth_private *mdp = netdev_priv(ndev);
2146
2147 ring->rx_max_pending = RX_RING_MAX;
2148 ring->tx_max_pending = TX_RING_MAX;
2149 ring->rx_pending = mdp->num_rx_ring;
2150 ring->tx_pending = mdp->num_tx_ring;
2151}
2152
2153static int sh_eth_set_ringparam(struct net_device *ndev,
2154 struct ethtool_ringparam *ring)
2155{
2156 struct sh_eth_private *mdp = netdev_priv(ndev);
2157 int ret;
2158
2159 if (ring->tx_pending > TX_RING_MAX ||
2160 ring->rx_pending > RX_RING_MAX ||
2161 ring->tx_pending < TX_RING_MIN ||
2162 ring->rx_pending < RX_RING_MIN)
2163 return -EINVAL;
2164 if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2165 return -EINVAL;
2166
2167 if (netif_running(ndev)) {
Ben Hutchingsbd888912015-01-22 12:40:25 +00002168 netif_device_detach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002169 netif_tx_disable(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002170
Ben Hutchings283e38d2015-01-22 12:44:08 +00002171 /* Serialise with the interrupt handler and NAPI, then
2172 * disable interrupts. We have to clear the
2173 * irq_enabled flag first to ensure that interrupts
2174 * won't be re-enabled.
2175 */
2176 mdp->irq_enabled = false;
2177 synchronize_irq(ndev->irq);
2178 napi_synchronize(&mdp->napi);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002179 sh_eth_write(ndev, 0x0000, EESIPR);
Ben Hutchings283e38d2015-01-22 12:44:08 +00002180
Ben Hutchings740c7f32015-01-27 00:49:32 +00002181 sh_eth_dev_exit(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002182
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03002183 /* Free all the skbuffs in the Rx queue and the DMA buffers. */
Ben Hutchings084236d2015-01-22 12:41:34 +00002184 sh_eth_ring_free(ndev);
Ben Hutchings084236d2015-01-22 12:41:34 +00002185 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002186
2187 /* Set new parameters */
2188 mdp->num_rx_ring = ring->rx_pending;
2189 mdp->num_tx_ring = ring->tx_pending;
2190
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002191 if (netif_running(ndev)) {
Ben Hutchings084236d2015-01-22 12:41:34 +00002192 ret = sh_eth_ring_init(ndev);
2193 if (ret < 0) {
2194 netdev_err(ndev, "%s: sh_eth_ring_init failed.\n",
2195 __func__);
2196 return ret;
2197 }
2198 ret = sh_eth_dev_init(ndev, false);
2199 if (ret < 0) {
2200 netdev_err(ndev, "%s: sh_eth_dev_init failed.\n",
2201 __func__);
2202 return ret;
2203 }
2204
Ben Hutchings283e38d2015-01-22 12:44:08 +00002205 mdp->irq_enabled = true;
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002206 sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
2207 /* Setting the Rx mode will start the Rx process. */
2208 sh_eth_write(ndev, EDRRR_R, EDRRR);
Ben Hutchingsbd888912015-01-22 12:40:25 +00002209 netif_device_attach(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002210 }
2211
2212 return 0;
2213}
2214
stephen hemminger9b07be42012-01-04 12:59:49 +00002215static const struct ethtool_ops sh_eth_ethtool_ops = {
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002216 .get_settings = sh_eth_get_settings,
2217 .set_settings = sh_eth_set_settings,
Ben Hutchings6b4b4fe2015-02-26 20:34:35 +00002218 .get_regs_len = sh_eth_get_regs_len,
2219 .get_regs = sh_eth_get_regs,
stephen hemminger9b07be42012-01-04 12:59:49 +00002220 .nway_reset = sh_eth_nway_reset,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002221 .get_msglevel = sh_eth_get_msglevel,
2222 .set_msglevel = sh_eth_set_msglevel,
stephen hemminger9b07be42012-01-04 12:59:49 +00002223 .get_link = ethtool_op_get_link,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002224 .get_strings = sh_eth_get_strings,
2225 .get_ethtool_stats = sh_eth_get_ethtool_stats,
2226 .get_sset_count = sh_eth_get_sset_count,
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002227 .get_ringparam = sh_eth_get_ringparam,
2228 .set_ringparam = sh_eth_set_ringparam,
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00002229};
2230
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002231/* network device open function */
2232static int sh_eth_open(struct net_device *ndev)
2233{
2234 int ret = 0;
2235 struct sh_eth_private *mdp = netdev_priv(ndev);
2236
Magnus Dammbcd51492009-10-09 00:20:04 +00002237 pm_runtime_get_sync(&mdp->pdev->dev);
2238
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002239 napi_enable(&mdp->napi);
2240
Joe Perchesa0607fd2009-11-18 23:29:17 -08002241 ret = request_irq(ndev->irq, sh_eth_interrupt,
Nobuhiro Iwamatsu5b3dfd12013-06-06 09:49:30 +00002242 mdp->cd->irq_flags, ndev->name, ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002243 if (ret) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002244 netdev_err(ndev, "Can not assign IRQ number\n");
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002245 goto out_napi_off;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002246 }
2247
2248 /* Descriptor set */
2249 ret = sh_eth_ring_init(ndev);
2250 if (ret)
2251 goto out_free_irq;
2252
2253 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002254 ret = sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002255 if (ret)
2256 goto out_free_irq;
2257
2258 /* PHY control start*/
2259 ret = sh_eth_phy_start(ndev);
2260 if (ret)
2261 goto out_free_irq;
2262
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002263 mdp->is_opened = 1;
2264
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002265 return ret;
2266
2267out_free_irq:
2268 free_irq(ndev->irq, ndev);
Sergei Shtylyovd2779e92013-09-04 02:41:27 +04002269out_napi_off:
2270 napi_disable(&mdp->napi);
Magnus Dammbcd51492009-10-09 00:20:04 +00002271 pm_runtime_put_sync(&mdp->pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002272 return ret;
2273}
2274
2275/* Timeout function */
2276static void sh_eth_tx_timeout(struct net_device *ndev)
2277{
2278 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002279 struct sh_eth_rxdesc *rxdesc;
2280 int i;
2281
2282 netif_stop_queue(ndev);
2283
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002284 netif_err(mdp, timer, ndev,
2285 "transmit timed out, status %8.8x, resetting...\n",
Geert Uytterhoeven0799c2d2015-01-15 11:54:28 +01002286 sh_eth_read(ndev, EESR));
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002287
2288 /* tx_errors count up */
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002289 ndev->stats.tx_errors++;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002290
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002291 /* Free all the skbuffs in the Rx queue. */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002292 for (i = 0; i < mdp->num_rx_ring; i++) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002293 rxdesc = &mdp->rx_ring[i];
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002294 rxdesc->status = cpu_to_le32(0);
2295 rxdesc->addr = cpu_to_le32(0xBADF00D0);
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002296 dev_kfree_skb(mdp->rx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002297 mdp->rx_skbuff[i] = NULL;
2298 }
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002299 for (i = 0; i < mdp->num_tx_ring; i++) {
Sergei Shtylyov179d80a2014-06-28 04:10:00 +04002300 dev_kfree_skb(mdp->tx_skbuff[i]);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002301 mdp->tx_skbuff[i] = NULL;
2302 }
2303
2304 /* device init */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002305 sh_eth_dev_init(ndev, true);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002306}
2307
2308/* Packet transmit function */
2309static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
2310{
2311 struct sh_eth_private *mdp = netdev_priv(ndev);
2312 struct sh_eth_txdesc *txdesc;
Sergei Shtylyov12996532015-12-13 23:05:07 +03002313 dma_addr_t dma_addr;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002314 u32 entry;
Nobuhiro Iwamatsufb5e2f92008-11-17 20:29:58 +00002315 unsigned long flags;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002316
2317 spin_lock_irqsave(&mdp->lock, flags);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002318 if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002319 if (!sh_eth_txfree(ndev)) {
Sergei Shtylyov8d5009f2014-03-15 03:30:59 +03002320 netif_warn(mdp, tx_queued, ndev, "TxFD exhausted.\n");
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002321 netif_stop_queue(ndev);
2322 spin_unlock_irqrestore(&mdp->lock, flags);
Patrick McHardy5b548142009-06-12 06:22:29 +00002323 return NETDEV_TX_BUSY;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002324 }
2325 }
2326 spin_unlock_irqrestore(&mdp->lock, flags);
2327
Ben Hutchingsdacc73e2015-03-03 00:53:08 +00002328 if (skb_put_padto(skb, ETH_ZLEN))
Ben Hutchingseebfb642015-01-22 12:40:13 +00002329 return NETDEV_TX_OK;
2330
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002331 entry = mdp->cur_tx % mdp->num_tx_ring;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002332 mdp->tx_skbuff[entry] = skb;
2333 txdesc = &mdp->tx_ring[entry];
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002334 /* soft swap. */
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00002335 if (!mdp->cd->hw_swap)
Sergei Shtylyov3e230992015-12-13 21:27:04 +03002336 sh_eth_soft_swap(PTR_ALIGN(skb->data, 4), skb->len + 2);
Sergei Shtylyov12996532015-12-13 23:05:07 +03002337 dma_addr = dma_map_single(&ndev->dev, skb->data, skb->len,
2338 DMA_TO_DEVICE);
2339 if (dma_mapping_error(&ndev->dev, dma_addr)) {
Ben Hutchingsaa3933b2015-01-27 00:49:47 +00002340 kfree_skb(skb);
2341 return NETDEV_TX_OK;
2342 }
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002343 txdesc->addr = cpu_to_le32(dma_addr);
2344 txdesc->len = cpu_to_le32(skb->len << 16);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002345
Sergei Shtylyovf32bfb92015-11-03 22:36:04 +03002346 dma_wmb(); /* TACT bit must be set after all the above writes */
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00002347 if (entry >= mdp->num_tx_ring - 1)
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002348 txdesc->status |= cpu_to_le32(TD_TACT | TD_TDLE);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002349 else
Sergei Shtylyov7cf72472015-12-28 02:10:47 +03002350 txdesc->status |= cpu_to_le32(TD_TACT);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002351
2352 mdp->cur_tx++;
2353
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002354 if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
2355 sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
Nobuhiro Iwamatsub0ca2a22008-06-30 11:08:17 +09002356
Patrick McHardy6ed10652009-06-23 06:03:08 +00002357 return NETDEV_TX_OK;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002358}
2359
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002360/* The statistics registers have write-clear behaviour, which means we
2361 * will lose any increment between the read and write. We mitigate
2362 * this by only clearing when we read a non-zero value, so we will
2363 * never falsely report a total of zero.
2364 */
2365static void
2366sh_eth_update_stat(struct net_device *ndev, unsigned long *stat, int reg)
2367{
2368 u32 delta = sh_eth_read(ndev, reg);
2369
2370 if (delta) {
2371 *stat += delta;
2372 sh_eth_write(ndev, 0, reg);
2373 }
2374}
2375
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002376static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
2377{
2378 struct sh_eth_private *mdp = netdev_priv(ndev);
2379
2380 if (sh_eth_is_rz_fast_ether(mdp))
2381 return &ndev->stats;
2382
2383 if (!mdp->is_opened)
2384 return &ndev->stats;
2385
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002386 sh_eth_update_stat(ndev, &ndev->stats.tx_dropped, TROCR);
2387 sh_eth_update_stat(ndev, &ndev->stats.collisions, CDCR);
2388 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors, LCCR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002389
2390 if (sh_eth_is_gether(mdp)) {
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002391 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2392 CERCR);
2393 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2394 CEECR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002395 } else {
Ben Hutchings4398f9c2015-02-26 20:35:05 +00002396 sh_eth_update_stat(ndev, &ndev->stats.tx_carrier_errors,
2397 CNDCR);
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002398 }
2399
2400 return &ndev->stats;
2401}
2402
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002403/* device close function */
2404static int sh_eth_close(struct net_device *ndev)
2405{
2406 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002407
2408 netif_stop_queue(ndev);
2409
Ben Hutchings283e38d2015-01-22 12:44:08 +00002410 /* Serialise with the interrupt handler and NAPI, then disable
2411 * interrupts. We have to clear the irq_enabled flag first to
2412 * ensure that interrupts won't be re-enabled.
2413 */
2414 mdp->irq_enabled = false;
2415 synchronize_irq(ndev->irq);
2416 napi_disable(&mdp->napi);
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002417 sh_eth_write(ndev, 0x0000, EESIPR);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002418
Ben Hutchings740c7f32015-01-27 00:49:32 +00002419 sh_eth_dev_exit(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002420
2421 /* PHY Disconnect */
2422 if (mdp->phydev) {
2423 phy_stop(mdp->phydev);
2424 phy_disconnect(mdp->phydev);
Ben Hutchings4f9dce232015-01-16 17:51:25 +00002425 mdp->phydev = NULL;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002426 }
2427
2428 free_irq(ndev->irq, ndev);
2429
Sergei Shtylyov8e03a5e2015-11-04 00:55:13 +03002430 /* Free all the skbuffs in the Rx queue and the DMA buffer. */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002431 sh_eth_ring_free(ndev);
2432
Magnus Dammbcd51492009-10-09 00:20:04 +00002433 pm_runtime_put_sync(&mdp->pdev->dev);
2434
Mitsuhiro Kimura7fa29552014-11-28 10:04:15 +09002435 mdp->is_opened = 0;
2436
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002437 return 0;
2438}
2439
Eric Dumazetbb7d92e2012-02-06 22:17:21 +00002440/* ioctl to device function */
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002441static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002442{
2443 struct sh_eth_private *mdp = netdev_priv(ndev);
2444 struct phy_device *phydev = mdp->phydev;
2445
2446 if (!netif_running(ndev))
2447 return -EINVAL;
2448
2449 if (!phydev)
2450 return -ENODEV;
2451
Richard Cochran28b04112010-07-17 08:48:55 +00002452 return phy_mii_ioctl(phydev, rq, cmd);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002453}
2454
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002455/* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
2456static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
2457 int entry)
2458{
2459 return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
2460}
2461
2462static u32 sh_eth_tsu_get_post_mask(int entry)
2463{
2464 return 0x0f << (28 - ((entry % 8) * 4));
2465}
2466
2467static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
2468{
2469 return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
2470}
2471
2472static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
2473 int entry)
2474{
2475 struct sh_eth_private *mdp = netdev_priv(ndev);
2476 u32 tmp;
2477 void *reg_offset;
2478
2479 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2480 tmp = ioread32(reg_offset);
2481 iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
2482}
2483
2484static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
2485 int entry)
2486{
2487 struct sh_eth_private *mdp = netdev_priv(ndev);
2488 u32 post_mask, ref_mask, tmp;
2489 void *reg_offset;
2490
2491 reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
2492 post_mask = sh_eth_tsu_get_post_mask(entry);
2493 ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
2494
2495 tmp = ioread32(reg_offset);
2496 iowrite32(tmp & ~post_mask, reg_offset);
2497
2498 /* If other port enables, the function returns "true" */
2499 return tmp & ref_mask;
2500}
2501
2502static int sh_eth_tsu_busy(struct net_device *ndev)
2503{
2504 int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
2505 struct sh_eth_private *mdp = netdev_priv(ndev);
2506
2507 while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
2508 udelay(10);
2509 timeout--;
2510 if (timeout <= 0) {
Sergei Shtylyovda246852014-03-15 03:29:14 +03002511 netdev_err(ndev, "%s: timeout\n", __func__);
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002512 return -ETIMEDOUT;
2513 }
2514 }
2515
2516 return 0;
2517}
2518
2519static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
2520 const u8 *addr)
2521{
2522 u32 val;
2523
2524 val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
2525 iowrite32(val, reg);
2526 if (sh_eth_tsu_busy(ndev) < 0)
2527 return -EBUSY;
2528
2529 val = addr[4] << 8 | addr[5];
2530 iowrite32(val, reg + 4);
2531 if (sh_eth_tsu_busy(ndev) < 0)
2532 return -EBUSY;
2533
2534 return 0;
2535}
2536
2537static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
2538{
2539 u32 val;
2540
2541 val = ioread32(reg);
2542 addr[0] = (val >> 24) & 0xff;
2543 addr[1] = (val >> 16) & 0xff;
2544 addr[2] = (val >> 8) & 0xff;
2545 addr[3] = val & 0xff;
2546 val = ioread32(reg + 4);
2547 addr[4] = (val >> 8) & 0xff;
2548 addr[5] = val & 0xff;
2549}
2550
2551
2552static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
2553{
2554 struct sh_eth_private *mdp = netdev_priv(ndev);
2555 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2556 int i;
2557 u8 c_addr[ETH_ALEN];
2558
2559 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2560 sh_eth_tsu_read_entry(reg_offset, c_addr);
dingtianhongc4bde292013-12-30 15:41:17 +08002561 if (ether_addr_equal(addr, c_addr))
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002562 return i;
2563 }
2564
2565 return -ENOENT;
2566}
2567
2568static int sh_eth_tsu_find_empty(struct net_device *ndev)
2569{
2570 u8 blank[ETH_ALEN];
2571 int entry;
2572
2573 memset(blank, 0, sizeof(blank));
2574 entry = sh_eth_tsu_find_entry(ndev, blank);
2575 return (entry < 0) ? -ENOMEM : entry;
2576}
2577
2578static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
2579 int entry)
2580{
2581 struct sh_eth_private *mdp = netdev_priv(ndev);
2582 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2583 int ret;
2584 u8 blank[ETH_ALEN];
2585
2586 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
2587 ~(1 << (31 - entry)), TSU_TEN);
2588
2589 memset(blank, 0, sizeof(blank));
2590 ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
2591 if (ret < 0)
2592 return ret;
2593 return 0;
2594}
2595
2596static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
2597{
2598 struct sh_eth_private *mdp = netdev_priv(ndev);
2599 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2600 int i, ret;
2601
2602 if (!mdp->cd->tsu)
2603 return 0;
2604
2605 i = sh_eth_tsu_find_entry(ndev, addr);
2606 if (i < 0) {
2607 /* No entry found, create one */
2608 i = sh_eth_tsu_find_empty(ndev);
2609 if (i < 0)
2610 return -ENOMEM;
2611 ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
2612 if (ret < 0)
2613 return ret;
2614
2615 /* Enable the entry */
2616 sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
2617 (1 << (31 - i)), TSU_TEN);
2618 }
2619
2620 /* Entry found or created, enable POST */
2621 sh_eth_tsu_enable_cam_entry_post(ndev, i);
2622
2623 return 0;
2624}
2625
2626static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
2627{
2628 struct sh_eth_private *mdp = netdev_priv(ndev);
2629 int i, ret;
2630
2631 if (!mdp->cd->tsu)
2632 return 0;
2633
2634 i = sh_eth_tsu_find_entry(ndev, addr);
2635 if (i) {
2636 /* Entry found */
2637 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2638 goto done;
2639
2640 /* Disable the entry if both ports was disabled */
2641 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2642 if (ret < 0)
2643 return ret;
2644 }
2645done:
2646 return 0;
2647}
2648
2649static int sh_eth_tsu_purge_all(struct net_device *ndev)
2650{
2651 struct sh_eth_private *mdp = netdev_priv(ndev);
2652 int i, ret;
2653
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002654 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002655 return 0;
2656
2657 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
2658 if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
2659 continue;
2660
2661 /* Disable the entry if both ports was disabled */
2662 ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
2663 if (ret < 0)
2664 return ret;
2665 }
2666
2667 return 0;
2668}
2669
2670static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
2671{
2672 struct sh_eth_private *mdp = netdev_priv(ndev);
2673 u8 addr[ETH_ALEN];
2674 void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
2675 int i;
2676
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002677 if (!mdp->cd->tsu)
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002678 return;
2679
2680 for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
2681 sh_eth_tsu_read_entry(reg_offset, addr);
2682 if (is_multicast_ether_addr(addr))
2683 sh_eth_tsu_del_entry(ndev, addr);
2684 }
2685}
2686
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002687/* Update promiscuous flag and multicast filter */
2688static void sh_eth_set_rx_mode(struct net_device *ndev)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002689{
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002690 struct sh_eth_private *mdp = netdev_priv(ndev);
2691 u32 ecmr_bits;
2692 int mcast_all = 0;
2693 unsigned long flags;
2694
2695 spin_lock_irqsave(&mdp->lock, flags);
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002696 /* Initial condition is MCT = 1, PRM = 0.
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002697 * Depending on ndev->flags, set PRM or clear MCT
2698 */
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002699 ecmr_bits = sh_eth_read(ndev, ECMR) & ~ECMR_PRM;
2700 if (mdp->cd->tsu)
2701 ecmr_bits |= ECMR_MCT;
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002702
2703 if (!(ndev->flags & IFF_MULTICAST)) {
2704 sh_eth_tsu_purge_mcast(ndev);
2705 mcast_all = 1;
2706 }
2707 if (ndev->flags & IFF_ALLMULTI) {
2708 sh_eth_tsu_purge_mcast(ndev);
2709 ecmr_bits &= ~ECMR_MCT;
2710 mcast_all = 1;
2711 }
2712
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002713 if (ndev->flags & IFF_PROMISC) {
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002714 sh_eth_tsu_purge_all(ndev);
2715 ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
2716 } else if (mdp->cd->tsu) {
2717 struct netdev_hw_addr *ha;
2718 netdev_for_each_mc_addr(ha, ndev) {
2719 if (mcast_all && is_multicast_ether_addr(ha->addr))
2720 continue;
2721
2722 if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
2723 if (!mcast_all) {
2724 sh_eth_tsu_purge_mcast(ndev);
2725 ecmr_bits &= ~ECMR_MCT;
2726 mcast_all = 1;
2727 }
2728 }
2729 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002730 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00002731
2732 /* update the ethernet mode */
2733 sh_eth_write(ndev, ecmr_bits, ECMR);
2734
2735 spin_unlock_irqrestore(&mdp->lock, flags);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002736}
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002737
2738static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
2739{
2740 if (!mdp->port)
2741 return TSU_VTAG0;
2742 else
2743 return TSU_VTAG1;
2744}
2745
Patrick McHardy80d5c362013-04-19 02:04:28 +00002746static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
2747 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002748{
2749 struct sh_eth_private *mdp = netdev_priv(ndev);
2750 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2751
2752 if (unlikely(!mdp->cd->tsu))
2753 return -EPERM;
2754
2755 /* No filtering if vid = 0 */
2756 if (!vid)
2757 return 0;
2758
2759 mdp->vlan_num_ids++;
2760
Sergei Shtylyov128296f2014-01-03 15:52:22 +03002761 /* The controller has one VLAN tag HW filter. So, if the filter is
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002762 * already enabled, the driver disables it and the filte
2763 */
2764 if (mdp->vlan_num_ids > 1) {
2765 /* disable VLAN filter */
2766 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2767 return 0;
2768 }
2769
2770 sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
2771 vtag_reg_index);
2772
2773 return 0;
2774}
2775
Patrick McHardy80d5c362013-04-19 02:04:28 +00002776static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
2777 __be16 proto, u16 vid)
Yoshihiro Shimoda71cc7c32012-02-15 17:55:06 +00002778{
2779 struct sh_eth_private *mdp = netdev_priv(ndev);
2780 int vtag_reg_index = sh_eth_get_vtag_index(mdp);
2781
2782 if (unlikely(!mdp->cd->tsu))
2783 return -EPERM;
2784
2785 /* No filtering if vid = 0 */
2786 if (!vid)
2787 return 0;
2788
2789 mdp->vlan_num_ids--;
2790 sh_eth_tsu_write(mdp, 0, vtag_reg_index);
2791
2792 return 0;
2793}
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002794
2795/* SuperH's TSU register init function */
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002796static void sh_eth_tsu_init(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002797{
Simon Hormandb893472014-01-17 09:22:28 +09002798 if (sh_eth_is_rz_fast_ether(mdp)) {
2799 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2800 return;
2801 }
2802
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002803 sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
2804 sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
2805 sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
2806 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
2807 sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
2808 sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
2809 sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
2810 sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
2811 sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
2812 sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
Yoshihiro Shimodac5ed5362011-03-07 21:59:38 +00002813 if (sh_eth_is_gether(mdp)) {
2814 sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
2815 sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
2816 } else {
2817 sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
2818 sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
2819 }
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002820 sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
2821 sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
2822 sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
2823 sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
2824 sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
2825 sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
2826 sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002827}
2828
2829/* MDIO bus release function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002830static int sh_mdio_release(struct sh_eth_private *mdp)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002831{
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002832 /* unregister mdio bus */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002833 mdiobus_unregister(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002834
2835 /* free bitbang info */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002836 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002837
2838 return 0;
2839}
2840
2841/* MDIO bus init function */
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002842static int sh_mdio_init(struct sh_eth_private *mdp,
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002843 struct sh_eth_plat_data *pd)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002844{
Andrew Lunne7f4dc32016-01-06 20:11:15 +01002845 int ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002846 struct bb_info *bitbang;
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002847 struct platform_device *pdev = mdp->pdev;
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002848 struct device *dev = &mdp->pdev->dev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002849
2850 /* create bit control struct for PHY */
Laurent Pinchartaa8d4222014-03-20 15:00:31 +01002851 bitbang = devm_kzalloc(dev, sizeof(struct bb_info), GFP_KERNEL);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002852 if (!bitbang)
2853 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002854
2855 /* bitbang init */
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00002856 bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
Yoshihiro Shimodab3017e62011-03-07 21:59:55 +00002857 bitbang->set_gate = pd->set_mdio_gate;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002858 bitbang->ctrl.ops = &bb_ops;
2859
Stefan Weilc2e07b32010-08-03 19:44:52 +02002860 /* MII controller setting */
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002861 mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
Laurent Pinchartf738a132014-03-20 15:00:35 +01002862 if (!mdp->mii_bus)
2863 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002864
2865 /* Hook up MII support for ethtool */
2866 mdp->mii_bus->name = "sh_mii";
Laurent Pincharta5bd60602014-03-20 15:00:32 +01002867 mdp->mii_bus->parent = dev;
Florian Fainelli5278fb52012-01-09 23:59:17 +00002868 snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002869 pdev->name, pdev->id);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002870
Laurent Pinchartbd920ff2014-03-20 15:00:33 +01002871 /* register MDIO bus */
2872 if (dev->of_node) {
2873 ret = of_mdiobus_register(mdp->mii_bus, dev->of_node);
Ben Dooks702eca02014-03-12 17:47:40 +00002874 } else {
Ben Dooks702eca02014-03-12 17:47:40 +00002875 if (pd->phy_irq > 0)
2876 mdp->mii_bus->irq[pd->phy] = pd->phy_irq;
2877
2878 ret = mdiobus_register(mdp->mii_bus);
2879 }
2880
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002881 if (ret)
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00002882 goto out_free_bus;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002883
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002884 return 0;
2885
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002886out_free_bus:
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002887 free_mdio_bitbang(mdp->mii_bus);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002888 return ret;
2889}
2890
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002891static const u16 *sh_eth_get_register_offset(int register_type)
2892{
2893 const u16 *reg_offset = NULL;
2894
2895 switch (register_type) {
2896 case SH_ETH_REG_GIGABIT:
2897 reg_offset = sh_eth_offset_gigabit;
2898 break;
Simon Hormandb893472014-01-17 09:22:28 +09002899 case SH_ETH_REG_FAST_RZ:
2900 reg_offset = sh_eth_offset_fast_rz;
2901 break;
Sergei Shtylyova3f109b2013-03-28 11:51:31 +00002902 case SH_ETH_REG_FAST_RCAR:
2903 reg_offset = sh_eth_offset_fast_rcar;
2904 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002905 case SH_ETH_REG_FAST_SH4:
2906 reg_offset = sh_eth_offset_fast_sh4;
2907 break;
2908 case SH_ETH_REG_FAST_SH3_SH2:
2909 reg_offset = sh_eth_offset_fast_sh3_sh2;
2910 break;
Yoshihiro Shimoda4a555302011-03-07 21:59:26 +00002911 }
2912
2913 return reg_offset;
2914}
2915
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002916static const struct net_device_ops sh_eth_netdev_ops = {
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002917 .ndo_open = sh_eth_open,
2918 .ndo_stop = sh_eth_close,
2919 .ndo_start_xmit = sh_eth_start_xmit,
2920 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002921 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Alexander Beregalovebf84ea2009-04-11 07:40:49 +00002922 .ndo_tx_timeout = sh_eth_tx_timeout,
2923 .ndo_do_ioctl = sh_eth_do_ioctl,
2924 .ndo_validate_addr = eth_validate_addr,
2925 .ndo_set_mac_address = eth_mac_addr,
2926 .ndo_change_mtu = eth_change_mtu,
2927};
2928
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002929static const struct net_device_ops sh_eth_netdev_ops_tsu = {
2930 .ndo_open = sh_eth_open,
2931 .ndo_stop = sh_eth_close,
2932 .ndo_start_xmit = sh_eth_start_xmit,
2933 .ndo_get_stats = sh_eth_get_stats,
Ben Hutchingsb37feed2015-01-16 17:51:12 +00002934 .ndo_set_rx_mode = sh_eth_set_rx_mode,
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04002935 .ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid,
2936 .ndo_vlan_rx_kill_vid = sh_eth_vlan_rx_kill_vid,
2937 .ndo_tx_timeout = sh_eth_tx_timeout,
2938 .ndo_do_ioctl = sh_eth_do_ioctl,
2939 .ndo_validate_addr = eth_validate_addr,
2940 .ndo_set_mac_address = eth_mac_addr,
2941 .ndo_change_mtu = eth_change_mtu,
2942};
2943
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002944#ifdef CONFIG_OF
2945static struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
2946{
2947 struct device_node *np = dev->of_node;
2948 struct sh_eth_plat_data *pdata;
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002949 const char *mac_addr;
2950
2951 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2952 if (!pdata)
2953 return NULL;
2954
2955 pdata->phy_interface = of_get_phy_mode(np);
2956
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002957 mac_addr = of_get_mac_address(np);
2958 if (mac_addr)
2959 memcpy(pdata->mac_addr, mac_addr, ETH_ALEN);
2960
2961 pdata->no_ether_link =
2962 of_property_read_bool(np, "renesas,no-ether-link");
2963 pdata->ether_link_active_low =
2964 of_property_read_bool(np, "renesas,ether-link-active-low");
2965
2966 return pdata;
2967}
2968
2969static const struct of_device_id sh_eth_match_table[] = {
2970 { .compatible = "renesas,gether-r8a7740", .data = &r8a7740_data },
2971 { .compatible = "renesas,ether-r8a7778", .data = &r8a777x_data },
2972 { .compatible = "renesas,ether-r8a7779", .data = &r8a777x_data },
2973 { .compatible = "renesas,ether-r8a7790", .data = &r8a779x_data },
2974 { .compatible = "renesas,ether-r8a7791", .data = &r8a779x_data },
Hisashi Nakamura9488e1e2014-11-13 15:59:07 +09002975 { .compatible = "renesas,ether-r8a7793", .data = &r8a779x_data },
Hisashi Nakamura0f76b9d2014-08-01 17:03:00 +02002976 { .compatible = "renesas,ether-r8a7794", .data = &r8a779x_data },
Sergei Shtylyovb356e972014-02-18 03:12:43 +03002977 { .compatible = "renesas,ether-r7s72100", .data = &r7s72100_data },
2978 { }
2979};
2980MODULE_DEVICE_TABLE(of, sh_eth_match_table);
2981#else
2982static inline struct sh_eth_plat_data *sh_eth_parse_dt(struct device *dev)
2983{
2984 return NULL;
2985}
2986#endif
2987
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002988static int sh_eth_drv_probe(struct platform_device *pdev)
2989{
Kuninori Morimoto9c386572010-08-19 00:39:45 -07002990 int ret, devno = 0;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002991 struct resource *res;
2992 struct net_device *ndev = NULL;
Kuninori Morimotoec0d7552011-06-23 16:02:38 +00002993 struct sh_eth_private *mdp = NULL;
Jingoo Han0b76b862013-08-30 14:00:11 +09002994 struct sh_eth_plat_data *pd = dev_get_platdata(&pdev->dev);
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00002995 const struct platform_device_id *id = platform_get_device_id(pdev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002996
2997 /* get base addr */
2998 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07002999
3000 ndev = alloc_etherdev(sizeof(struct sh_eth_private));
Laurent Pinchartf738a132014-03-20 15:00:35 +01003001 if (!ndev)
3002 return -ENOMEM;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003003
Ben Dooksb5893a02014-03-21 12:09:14 +01003004 pm_runtime_enable(&pdev->dev);
3005 pm_runtime_get_sync(&pdev->dev);
3006
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003007 devno = pdev->id;
3008 if (devno < 0)
3009 devno = 0;
3010
3011 ndev->dma = -1;
roel kluincc3c0802008-09-10 19:22:44 +02003012 ret = platform_get_irq(pdev, 0);
Sergei Shtylyov7a468ac2015-08-28 16:56:01 +03003013 if (ret < 0)
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003014 goto out_release;
roel kluincc3c0802008-09-10 19:22:44 +02003015 ndev->irq = ret;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003016
3017 SET_NETDEV_DEV(ndev, &pdev->dev);
3018
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003019 mdp = netdev_priv(ndev);
Yoshihiro Shimoda525b8072012-06-26 20:00:03 +00003020 mdp->num_tx_ring = TX_RING_SIZE;
3021 mdp->num_rx_ring = RX_RING_SIZE;
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00003022 mdp->addr = devm_ioremap_resource(&pdev->dev, res);
3023 if (IS_ERR(mdp->addr)) {
3024 ret = PTR_ERR(mdp->addr);
Yoshihiro Shimodaae706442011-09-27 21:48:58 +00003025 goto out_release;
3026 }
3027
Varka Bhadramc9608042014-10-24 07:42:09 +05303028 ndev->base_addr = res->start;
3029
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003030 spin_lock_init(&mdp->lock);
Magnus Dammbcd51492009-10-09 00:20:04 +00003031 mdp->pdev = pdev;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003032
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003033 if (pdev->dev.of_node)
3034 pd = sh_eth_parse_dt(&pdev->dev);
Sergei Shtylyov3b4c5cb2013-10-30 23:30:19 +03003035 if (!pd) {
3036 dev_err(&pdev->dev, "no platform data\n");
3037 ret = -EINVAL;
3038 goto out_release;
3039 }
3040
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003041 /* get PHY ID */
Yoshinori Sato71557a32008-08-06 19:49:00 -04003042 mdp->phy_id = pd->phy;
Yoshihiro Shimodae47c9052011-03-07 21:59:45 +00003043 mdp->phy_interface = pd->phy_interface;
Yoshihiro Shimoda49235762009-08-27 23:25:03 +00003044 mdp->no_ether_link = pd->no_ether_link;
3045 mdp->ether_link_active_low = pd->ether_link_active_low;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003046
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003047 /* set cpu data */
Wolfram Sang42a67c92016-03-01 17:37:59 +01003048 if (id)
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003049 mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
Wolfram Sang42a67c92016-03-01 17:37:59 +01003050 else
3051 mdp->cd = (struct sh_eth_cpu_data *)of_device_get_match_data(&pdev->dev);
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003052
Sergei Shtylyova3153d82013-08-18 03:11:28 +04003053 mdp->reg_offset = sh_eth_get_register_offset(mdp->cd->register_type);
Sergei Shtylyov264be2f2014-03-15 03:11:24 +03003054 if (!mdp->reg_offset) {
3055 dev_err(&pdev->dev, "Unknown register type (%d)\n",
3056 mdp->cd->register_type);
3057 ret = -EINVAL;
3058 goto out_release;
3059 }
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003060 sh_eth_set_default_cpu_data(mdp->cd);
3061
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003062 /* set function */
Sergei Shtylyov8f728d72013-06-13 00:55:34 +04003063 if (mdp->cd->tsu)
3064 ndev->netdev_ops = &sh_eth_netdev_ops_tsu;
3065 else
3066 ndev->netdev_ops = &sh_eth_netdev_ops;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00003067 ndev->ethtool_ops = &sh_eth_ethtool_ops;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003068 ndev->watchdog_timeo = TX_TIMEOUT;
3069
Nobuhiro Iwamatsudc19e4e2011-02-15 21:17:32 +00003070 /* debug message level */
3071 mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003072
3073 /* read and set MAC address */
Magnus Damm748031f2009-10-09 00:17:14 +00003074 read_mac_address(ndev, pd->mac_addr);
Sergei Shtylyovff6e7222013-04-29 09:49:42 +00003075 if (!is_valid_ether_addr(ndev->dev_addr)) {
3076 dev_warn(&pdev->dev,
3077 "no valid MAC address supplied, using a random one.\n");
3078 eth_hw_addr_random(ndev);
3079 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003080
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003081 /* ioremap the TSU registers */
3082 if (mdp->cd->tsu) {
3083 struct resource *rtsu;
3084 rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Sergei Shtylyovd5e07e62013-03-21 10:41:11 +00003085 mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
3086 if (IS_ERR(mdp->tsu_addr)) {
3087 ret = PTR_ERR(mdp->tsu_addr);
Sergei Shtylyovfc0c0902013-03-19 13:41:32 +00003088 goto out_release;
3089 }
Yoshihiro Shimoda6743fe62012-02-15 17:55:03 +00003090 mdp->port = devno % 2;
Patrick McHardyf6469682013-04-19 02:04:27 +00003091 ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
Yoshihiro Shimoda6ba88022012-02-15 17:55:01 +00003092 }
3093
Yoshihiro Shimoda150647f2012-02-15 17:54:56 +00003094 /* initialize first or needed device */
3095 if (!devno || pd->needs_init) {
Yoshihiro Shimoda380af9e2009-05-24 23:54:21 +00003096 if (mdp->cd->chip_reset)
3097 mdp->cd->chip_reset(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003098
Yoshihiro Shimoda4986b992011-03-07 21:59:34 +00003099 if (mdp->cd->tsu) {
3100 /* TSU init (Init only)*/
3101 sh_eth_tsu_init(mdp);
3102 }
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003103 }
3104
Hisashi Nakamura966d6db2014-11-13 15:54:05 +09003105 if (mdp->cd->rmiimode)
3106 sh_eth_write(ndev, 0x1, RMIIMODE);
3107
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003108 /* MDIO bus init */
3109 ret = sh_mdio_init(mdp, pd);
3110 if (ret) {
3111 dev_err(&ndev->dev, "failed to initialise MDIO\n");
3112 goto out_release;
3113 }
3114
Sergei Shtylyov37191092013-06-19 23:30:23 +04003115 netif_napi_add(ndev, &mdp->napi, sh_eth_poll, 64);
3116
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003117 /* network device register */
3118 ret = register_netdev(ndev);
3119 if (ret)
Sergei Shtylyov37191092013-06-19 23:30:23 +04003120 goto out_napi_del;
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003121
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003122 /* print device information */
Sergei Shtylyovf75f14e2014-03-15 03:27:54 +03003123 netdev_info(ndev, "Base address at 0x%x, %pM, IRQ %d.\n",
3124 (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003125
Ben Dooksb5893a02014-03-21 12:09:14 +01003126 pm_runtime_put(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003127 platform_set_drvdata(pdev, ndev);
3128
3129 return ret;
3130
Sergei Shtylyov37191092013-06-19 23:30:23 +04003131out_napi_del:
3132 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003133 sh_mdio_release(mdp);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003134
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003135out_release:
3136 /* net_dev free */
3137 if (ndev)
3138 free_netdev(ndev);
3139
Ben Dooksb5893a02014-03-21 12:09:14 +01003140 pm_runtime_put(&pdev->dev);
3141 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003142 return ret;
3143}
3144
3145static int sh_eth_drv_remove(struct platform_device *pdev)
3146{
3147 struct net_device *ndev = platform_get_drvdata(pdev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003148 struct sh_eth_private *mdp = netdev_priv(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003149
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003150 unregister_netdev(ndev);
Sergei Shtylyov37191092013-06-19 23:30:23 +04003151 netif_napi_del(&mdp->napi);
Laurent Pinchartdaacf032014-03-20 15:00:34 +01003152 sh_mdio_release(mdp);
Magnus Dammbcd51492009-10-09 00:20:04 +00003153 pm_runtime_disable(&pdev->dev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003154 free_netdev(ndev);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003155
3156 return 0;
3157}
3158
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003159#ifdef CONFIG_PM
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003160#ifdef CONFIG_PM_SLEEP
3161static int sh_eth_suspend(struct device *dev)
3162{
3163 struct net_device *ndev = dev_get_drvdata(dev);
3164 int ret = 0;
3165
3166 if (netif_running(ndev)) {
3167 netif_device_detach(ndev);
3168 ret = sh_eth_close(ndev);
3169 }
3170
3171 return ret;
3172}
3173
3174static int sh_eth_resume(struct device *dev)
3175{
3176 struct net_device *ndev = dev_get_drvdata(dev);
3177 int ret = 0;
3178
3179 if (netif_running(ndev)) {
3180 ret = sh_eth_open(ndev);
3181 if (ret < 0)
3182 return ret;
3183 netif_device_attach(ndev);
3184 }
3185
3186 return ret;
3187}
3188#endif
3189
Magnus Dammbcd51492009-10-09 00:20:04 +00003190static int sh_eth_runtime_nop(struct device *dev)
3191{
Sergei Shtylyov128296f2014-01-03 15:52:22 +03003192 /* Runtime PM callback shared between ->runtime_suspend()
Magnus Dammbcd51492009-10-09 00:20:04 +00003193 * and ->runtime_resume(). Simply returns success.
3194 *
3195 * This driver re-initializes all registers after
3196 * pm_runtime_get_sync() anyway so there is no need
3197 * to save and restore registers here.
3198 */
3199 return 0;
3200}
3201
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003202static const struct dev_pm_ops sh_eth_dev_pm_ops = {
Mikhail Ulyanovb71af042015-01-22 01:19:48 +03003203 SET_SYSTEM_SLEEP_PM_OPS(sh_eth_suspend, sh_eth_resume)
Mikhail Ulyanove7d7e892015-01-22 01:18:44 +03003204 SET_RUNTIME_PM_OPS(sh_eth_runtime_nop, sh_eth_runtime_nop, NULL)
Magnus Dammbcd51492009-10-09 00:20:04 +00003205};
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003206#define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
3207#else
3208#define SH_ETH_PM_OPS NULL
3209#endif
Magnus Dammbcd51492009-10-09 00:20:04 +00003210
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003211static struct platform_device_id sh_eth_id_table[] = {
Sergei Shtylyovc18a79a2013-06-07 13:56:05 +00003212 { "sh7619-ether", (kernel_ulong_t)&sh7619_data },
Sergei Shtylyov7bbe1502013-06-07 13:55:08 +00003213 { "sh771x-ether", (kernel_ulong_t)&sh771x_data },
Sergei Shtylyov9c3beaa2013-06-07 14:03:37 +00003214 { "sh7724-ether", (kernel_ulong_t)&sh7724_data },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003215 { "sh7734-gether", (kernel_ulong_t)&sh7734_data },
Sergei Shtylyov24549e22013-06-07 13:59:21 +00003216 { "sh7757-ether", (kernel_ulong_t)&sh7757_data },
3217 { "sh7757-gether", (kernel_ulong_t)&sh7757_data_giga },
Sergei Shtylyovf5d12762013-06-07 13:58:18 +00003218 { "sh7763-gether", (kernel_ulong_t)&sh7763_data },
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003219 { }
3220};
3221MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
3222
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003223static struct platform_driver sh_eth_driver = {
3224 .probe = sh_eth_drv_probe,
3225 .remove = sh_eth_drv_remove,
Sergei Shtylyovafe391a2013-06-07 13:54:02 +00003226 .id_table = sh_eth_id_table,
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003227 .driver = {
3228 .name = CARDNAME,
Nobuhiro Iwamatsu540ad1b2013-06-06 09:52:37 +00003229 .pm = SH_ETH_PM_OPS,
Sergei Shtylyovb356e972014-02-18 03:12:43 +03003230 .of_match_table = of_match_ptr(sh_eth_match_table),
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003231 },
3232};
3233
Axel Lindb62f682011-11-27 16:44:17 +00003234module_platform_driver(sh_eth_driver);
Nobuhiro Iwamatsu86a74ff2008-06-09 16:33:56 -07003235
3236MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
3237MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
3238MODULE_LICENSE("GPL v2");